1
14
15 package demo.yext.export;
16
17 import y.io.GraphMLIOHandler;
18 import y.option.OptionHandler;
19 import y.util.D;
20 import y.view.EditMode;
21 import y.view.Graph2DPrinter;
22 import y.view.Graph2DView;
23 import y.view.Graph2DViewActions;
24 import y.view.Graph2DViewMouseWheelZoomListener;
25
26 import javax.swing.AbstractAction;
27 import javax.swing.Action;
28 import javax.swing.ImageIcon;
29 import javax.swing.InputMap;
30 import javax.swing.JComponent;
31 import javax.swing.JFileChooser;
32 import javax.swing.JFrame;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuBar;
35 import javax.swing.JPanel;
36 import javax.swing.JRootPane;
37 import javax.swing.JToolBar;
38 import javax.swing.UIManager;
39 import javax.swing.filechooser.FileFilter;
40
41 import java.awt.BorderLayout;
42 import java.awt.Rectangle;
43 import java.awt.EventQueue;
44 import java.awt.event.ActionEvent;
45 import java.awt.print.PageFormat;
46 import java.awt.print.PrinterException;
47 import java.awt.print.PrinterJob;
48 import java.io.File;
49 import java.io.IOException;
50 import java.net.URL;
51 import java.util.Locale;
52
53
74 public class ViewActionDemo extends JPanel {
75
76
79 protected Graph2DView view;
80
83 protected EditMode editMode;
84
85
86 public ViewActionDemo() {
87 setLayout(new BorderLayout());
88
89 view = new Graph2DView();
90 view.setAntialiasedPainting(true);
91 view.getCanvasComponent().addMouseWheelListener(new Graph2DViewMouseWheelZoomListener());
92
93 editMode = createEditMode();
94 if (editMode != null) {
95 view.addViewMode(editMode);
96 }
97
98 Graph2DViewActions actions = new Graph2DViewActions(view);
99 InputMap imap = actions.createDefaultInputMap();
100 view.getCanvasComponent().setInputMap(JComponent.WHEN_FOCUSED, imap);
101
102 add(view, BorderLayout.CENTER);
103 add(createToolBar(), BorderLayout.NORTH);
104 }
105
106 protected EditMode createEditMode() {
107 final EditMode editMode = new EditMode();
108 editMode.showNodeTips(true);
109 return editMode;
110 }
111
112
116 protected JToolBar createToolBar() {
117 JToolBar bar = new JToolBar();
118 bar.add(new DeleteSelection());
119 bar.add(new Zoom(1.2));
120 bar.add(new Zoom(0.8));
121 bar.add(new ResetZoom());
122 bar.add(new FitContent());
123
124 return bar;
125 }
126
127
131 protected JMenuBar createMenuBar() {
132 JMenuBar bar = new JMenuBar();
133 JMenu menu = new JMenu("File");
134 menu.add(createLoadAction());
135 menu.add(createSaveAction());
136 menu.addSeparator();
137 menu.add(new PrintAction());
138 menu.addSeparator();
139 menu.add(new ExitAction());
140 bar.add(menu);
141 return bar;
142 }
143
144 protected Action createLoadAction() {
145 return new LoadAction();
146 }
147
148 protected Action createSaveAction() {
149 return new SaveAction();
150 }
151
152
156 public void start() {
157 start(getClass().getName());
158 }
159
160
164 public void start(final String title) {
165 JFrame frame = new JFrame(title);
166 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
167 addContentTo(frame.getRootPane());
168 frame.pack();
169 frame.setLocationRelativeTo(null);
170 frame.setVisible(true);
171 }
172
173 public final void addContentTo(final JRootPane rootPane) {
174 rootPane.setJMenuBar(createMenuBar());
175 rootPane.setContentPane(this);
176 }
177
178
181 public static void initLnF() {
182 try {
183 if (!"com.sun.java.swing.plaf.motif.MotifLookAndFeel".equals(UIManager.getSystemLookAndFeelClassName())
184 && !"com.sun.java.swing.plaf.gtk.GTKLookAndFeel".equals(UIManager.getSystemLookAndFeelClassName())
185 && !UIManager.getSystemLookAndFeelClassName().equals(UIManager.getLookAndFeel().getClass().getName())
186 && !(System.getProperty("java.version").startsWith("1.4") && System.getProperty("os.name").startsWith(
187 "Windows") && "6.1".equals(System.getProperty("os.version")))) {
188 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
189 }
190 }
191 catch (Exception e) {
192 e.printStackTrace();
193 }
194 }
195
196
200 public static void main(final String[] args) {
201 EventQueue.invokeLater(new Runnable() {
202 public void run() {
203 Locale.setDefault(Locale.ENGLISH);
204 initLnF();
205 (new ViewActionDemo()).start();
206 }
207 });
208 }
209
210 protected GraphMLIOHandler createGraphMLIOHandler() {
211 return new GraphMLIOHandler();
212 }
213
214
215
218 protected class PrintAction extends AbstractAction {
219 PageFormat pageFormat;
220 OptionHandler printOptions;
221
222 public PrintAction() {
223 super("Print");
224 putValue(Action.SHORT_DESCRIPTION, "Print");
225
226 printOptions = new OptionHandler("Print Options");
228 printOptions.addInt("Poster Rows", 1);
229 printOptions.addInt("Poster Columns", 1);
230 printOptions.addBool("Add Poster Coords", false);
231 final String[] area = {"View", "Graph"};
232 printOptions.addEnum("Clip Area", area, 1);
233 }
234
235 public void actionPerformed(ActionEvent e) {
236 Graph2DPrinter gprinter = new Graph2DPrinter(view);
237
238 if (!printOptions.showEditor(view.getFrame())) {
240 return;
241 }
242 gprinter.setPosterRows(printOptions.getInt("Poster Rows"));
243 gprinter.setPosterColumns(printOptions.getInt("Poster Columns"));
244 gprinter.setPrintPosterCoords(
245 printOptions.getBool("Add Poster Coords"));
246 if ("Graph".equals(printOptions.get("Clip Area"))) {
247 gprinter.setClipType(Graph2DPrinter.CLIP_GRAPH);
248 } else {
249 gprinter.setClipType(Graph2DPrinter.CLIP_VIEW);
250 }
251
252 PrinterJob printJob = PrinterJob.getPrinterJob();
254 if (pageFormat == null) {
255 pageFormat = printJob.defaultPage();
256 }
257 PageFormat pf = printJob.pageDialog(pageFormat);
258 if (pf == pageFormat) {
259 return;
260 } else {
261 pageFormat = pf;
262 }
263
264 printJob.setPrintable(gprinter, pageFormat);
267
268 if (printJob.printDialog()) {
269 try {
270 printJob.print();
271 } catch (PrinterException ex) {
272 ex.printStackTrace();
273 }
274 }
275 }
276 }
277
278
281 protected static class ExitAction extends AbstractAction {
282 ExitAction() {
283 super("Exit");
284 putValue(Action.SHORT_DESCRIPTION, "Exit");
285 }
286
287 public void actionPerformed(ActionEvent e) {
288
289 System.exit(0);
290 }
291 }
292
293 JFileChooser createGraphMLFileChooser() {
294 JFileChooser chooser = new JFileChooser();
295 chooser.setAcceptAllFileFilterUsed(false);
296 chooser.addChoosableFileFilter(new FileFilter() {
297 public boolean accept(File f) {
298 return f.isDirectory() || f.getName().endsWith(".graphml");
299 }
300
301 public String getDescription() {
302 return "GraphML Format (.graphml)";
303 }
304 });
305
306 return chooser;
307 }
308
309
312 protected class SaveAction extends AbstractAction {
313 JFileChooser chooser;
314
315 public SaveAction() {
316 super("Save...");
317 putValue(Action.SHORT_DESCRIPTION, "Save...");
318 chooser = null;
319 }
320
321 public void actionPerformed(ActionEvent e) {
322 if (chooser == null) {
323 chooser = createGraphMLFileChooser();
324 }
325 if (chooser.showSaveDialog(ViewActionDemo.this) == JFileChooser.APPROVE_OPTION) {
326 String name = chooser.getSelectedFile().toString();
327 GraphMLIOHandler ioh = new GraphMLIOHandler();
328 try {
329 ioh.write(view.getGraph2D(), name);
330 } catch (IOException ioe) {
331 D.show(ioe);
332 }
333 }
334 }
335 }
336
337
340 protected class LoadAction extends AbstractAction {
341 JFileChooser chooser;
342
343 public LoadAction() {
344 super("Load...");
345 putValue(Action.SHORT_DESCRIPTION, "Load...");
346 chooser = null;
347 }
348
349 public void actionPerformed(ActionEvent e) {
350 if (chooser == null) {
351 chooser = createGraphMLFileChooser();
352 }
353 if (chooser.showOpenDialog(ViewActionDemo.this) == JFileChooser.APPROVE_OPTION) {
354 String name = chooser.getSelectedFile().toString();
355 GraphMLIOHandler ioh = createGraphMLIOHandler();
356 try {
357 view.getGraph2D().clear();
358 ioh.read(view.getGraph2D(), name);
359 } catch (IOException ioe) {
360 D.show(ioe);
361 }
362
363 view.fitContent();
365 view.getGraph2D().updateViews();
366 }
367 }
368 }
369
370
373 protected class DeleteSelection extends AbstractAction {
374 public DeleteSelection() {
375 super("Delete Selection");
376 putValue(Action.SHORT_DESCRIPTION, "Delete Selection");
377 URL imageURL = getClass().getResource("resource/delete.png");
378 if (imageURL != null) {
379 this.putValue(Action.SMALL_ICON, new ImageIcon(imageURL));
380 }
381 }
382
383 public void actionPerformed(ActionEvent e) {
384 view.getGraph2D().removeSelection();
385 view.getGraph2D().updateViews();
386 }
387 }
388
389
392 protected class Zoom extends AbstractAction {
393 double factor;
394
395 public Zoom(double factor) {
396 final String name = "Zoom " + (factor > 1.0 ? "In" : "Out");
397 putValue(Action.NAME, name);
398 putValue(Action.SHORT_DESCRIPTION, name);
399 URL imageURL;
400 if (factor > 1.0d) {
401 imageURL = getClass().getResource("resource/zoomIn.png");
402 } else {
403 imageURL = getClass().getResource("resource/zoomOut.png");
404 }
405 if (imageURL != null) {
406 this.putValue(Action.SMALL_ICON, new ImageIcon(imageURL));
407 }
408 this.factor = factor;
409 }
410
411 public void actionPerformed(ActionEvent e) {
412 view.setZoom(view.getZoom() * factor);
413 Rectangle box = view.getGraph2D().getBoundingBox();
418 view.setWorldRect(box.x - 20, box.y - 20, box.width + 40, box.height + 40);
419
420 view.updateView();
421 }
422 }
423
424
427 protected class ResetZoom extends AbstractAction {
428 public ResetZoom() {
429 super("Reset Zoom");
430 this.putValue(Action.SHORT_DESCRIPTION, "Reset Zoom");
431 final URL imageURL = getClass().getResource("resource/zoomOriginal.png");
432 if (imageURL != null) {
433 this.putValue(Action.SMALL_ICON, new ImageIcon(imageURL));
434 }
435 }
436
437 public void actionPerformed( final ActionEvent e ) {
438 view.setZoom(1);
439 Rectangle box = view.getGraph2D().getBoundingBox();
444 view.setWorldRect(box.x - 20, box.y - 20, box.width + 40, box.height + 40);
445
446 view.updateView();
447 }
448 }
449
450
453 protected class FitContent extends AbstractAction {
454 public FitContent() {
455 super("Fit Content");
456 putValue(Action.SHORT_DESCRIPTION, "Fit Content");
457 final URL imageURL = getClass().getResource("resource/zoomFit.png");
458 if (imageURL != null) {
459 this.putValue(Action.SMALL_ICON, new ImageIcon(imageURL));
460 }
461 }
462
463 public void actionPerformed(ActionEvent e) {
464 view.fitContent();
465 view.updateView();
466 }
467 }
468 }