1 package demo.yext.graphml;
2
3 import java.awt.event.ActionEvent;
4 import java.io.IOException;
5 import java.io.File;
6 import java.net.URL;
7 import javax.swing.AbstractAction;
8 import javax.swing.Action;
9 import javax.swing.JFileChooser;
10 import javax.swing.JMenu;
11 import javax.swing.JMenuBar;
12 import javax.swing.filechooser.FileFilter;
13
14 import y.io.GMLIOHandler;
15 import y.io.IOHandler;
16 import y.io.YGFIOHandler;
17 import y.util.D;
18 import y.view.EditMode;
19 import y.view.Graph2D;
20 import y.view.hierarchy.HierarchyEditMode;
21 import y.view.hierarchy.HierarchyManager;
22 import yext.graphml.graph2D.GraphMLIOHandler;
23
24
29 public class GraphMLDemo extends ViewActionDemo
30 {
31 HierarchyManager hierarchy;
32 private GraphMLIOHandler ioHandler;
33
34 private JFileChooser chooser;
35
36 private File currentDir;
37
38
41 public GraphMLDemo()
42 {
43 hierarchy = new HierarchyManager(view.getGraph2D());
45
46 ioHandler = createGraphMLIOHandler();
47 createChooser();
48 }
49
50
51
52 protected EditMode createEditMode()
53 {
54 return new HierarchyEditMode();
56 }
57
58 protected JMenuBar createMenuBar()
59 {
60 JMenuBar jtb = super.createMenuBar();
61 JMenu sampleGraphMenu = createSampleGraphMenu();
62 if(sampleGraphMenu != null) {
63 jtb.add(sampleGraphMenu);
64 }
65 return jtb;
66 }
67
68 protected JMenu createSampleGraphMenu() {
69 String[] resources = {
70 "resources/ygraph/visual_features.graphml",
71 "resources/ygraph/problemsolving.graphml",
72 "resources/ygraph/funky.graphml",
73 "resources/ygraph/simple.graphml",
74 "resources/ygraph/grouping.graphml",
75 };
76 return createSampleGraphMenu(resources);
77 }
78
79 JMenu createSampleGraphMenu( final String[] resources ) {
80 JMenu menu = new JMenu("Sample Graphs");
81 int resolvedResourceCount = 0;
82 for (int i = 0; i < resources.length; ++i) {
83 final URL resource = getClass().getResource(resources[i]);
84 if (resource != null) {
85 ++resolvedResourceCount;
86 menu.add(new LoadResourceAction(resource));
87 } else {
88 System.err.println("Could not resolve sample resource " + resources[i]);
89 }
90 }
91 if (resolvedResourceCount > 0) {
92 return menu;
93 } else {
94 return null;
95 }
96 }
97
98 private void createChooser() {
99 if (chooser == null) {
100 chooser = new JFileChooser();
101
102 chooser.addChoosableFileFilter(new FileFilter() {
103 public boolean accept(final File f) {
104 String s = f.getName().toLowerCase();
105 return f.isDirectory() || s.endsWith(ioHandler.getFileNameExtension());
106 }
107
108 public String getDescription() {
109 return ioHandler.getFileFormatString();
110 }
111 });
112 }
113 }
114
115 protected GraphMLIOHandler createGraphMLIOHandler()
116 {
117 return new GraphMLIOHandler();
118 }
119
120 public GraphMLIOHandler getGraphMLIOHandler()
121 {
122 return ioHandler;
123 }
124
125 protected Action createLoadAction()
126 {
127 return new LoadAction();
128 }
129
130 protected Action createSaveAction()
131 {
132 return new SaveAction();
133 }
134
135
138 class SaveAction extends AbstractAction
139 {
140 SaveAction()
141 {
142 super("Save...");
143 }
144
145 public void actionPerformed(ActionEvent e)
146 {
147 if(currentDir != null) {
148 chooser.setCurrentDirectory(currentDir);
149 }
150
151 if(chooser.showSaveDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION)
152 {
153 String name = chooser.getSelectedFile().toString();
154 currentDir = chooser.getCurrentDirectory();
155 if (!name.endsWith(".graphml")) {
156 name = name + ".graphml";
157 }
158 IOHandler ioh = getGraphMLIOHandler();
159 try
160 {
161 ioh.write(view.getGraph2D(),name);
162 }
163 catch (IOException ioe)
164 {
165 D.show(ioe);
166 }
167 }
168 }
169 }
170
171
174 class LoadResourceAction extends AbstractAction
175 {
176 URL resource;
177 LoadResourceAction(URL resource)
178 {
179 putValue(AbstractAction.NAME, urlToName(resource));
180 this.resource = resource;
181 }
182
183 String urlToName(URL url)
184 {
185 String file = url.getFile();
186 return file.substring(file.lastIndexOf('/')+1);
187 }
188
189 public void actionPerformed(ActionEvent ev)
190 {
191 try
192 {
193 Graph2D graph = view.getGraph2D();
194 graph.clear();
195 IOHandler ioh = getGraphMLIOHandler();
196 ioh.read(graph, resource);
197 view.fitContent();
198 graph.updateViews();
199 }
200 catch (IOException ioe)
201 {
202 D.show(ioe);
203 }
204 }
205 }
206
207
210 class LoadAction extends AbstractAction
211 {
212 LoadAction()
213 {
214 super("Load...");
215 }
216
217 public void actionPerformed(ActionEvent e)
218 {
219 if(currentDir != null) {
220 chooser.setCurrentDirectory(currentDir);
221 }
222 if(chooser.showOpenDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION)
223 {
224 String name = chooser.getSelectedFile().toString();
225 currentDir = chooser.getCurrentDirectory();
226 IOHandler ioh = null;
227 if (name.endsWith(".gml")) {
228 ioh = new GMLIOHandler();
229 }
230 else if(name.endsWith(".ygf"))
231 {
232 ioh = new YGFIOHandler();
233 }
234 else {
236 if (!name.endsWith(".graphml")) {
237 name += ".graphml";
238 }
239 ioh = getGraphMLIOHandler();
240 }
241
242 try
243 {
244 Graph2D graph = view.getGraph2D();
245 graph.clear();
246 ioh.read(graph, name);
247 view.fitContent();
248 graph.updateViews();
249 }
250 catch (IOException ioe)
251 {
252 D.show(ioe);
253 }
254 }
255 }
256 }
257
258
261 public static void main(String[] args)
262 {
263 initLnF();
264 final GraphMLDemo demo = new GraphMLDemo();
265 demo.start();
266 }
267 }
268