| SimpleGMLDemo.java |
1 /****************************************************************************
2 **
3 ** This file is part of yFiles-2.6.
4 **
5 ** yWorks proprietary/confidential. Use is subject to license terms.
6 **
7 ** Redistribution of this file or of an unauthorized byte-code version
8 ** of this file is strictly forbidden.
9 **
10 ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28,
11 ** 72070 Tuebingen, Germany. All rights reserved.
12 **
13 ***************************************************************************/
14 package demo.io;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import java.io.*;
20 import java.net.*;
21
22 import y.view.Graph2DView;
23 import y.view.EditMode;
24 import y.io.GMLIOHandler;
25
26 import y.util.D;
27
28 /**
29 * Demonstrates simple usage of the Graph2DView.
30 * The initial graph will be read from a GML file,
31 * that can be specified on the command line.
32 *
33 * The edit mode won't allow node/edge creation
34 *
35 */
36 public class SimpleGMLDemo extends JPanel
37 {
38 Graph2DView view;
39
40 public SimpleGMLDemo()
41 {
42 setLayout(new BorderLayout());
43 view = new Graph2DView();
44 EditMode mode = new EditMode();
45
46 //editing not allowed anymore
47 mode.allowNodeCreation(false);
48 mode.allowEdgeCreation(false);
49 mode.allowBendCreation(false);
50
51 view.addViewMode(mode);
52 add(view);
53 }
54
55 /** read in a GML file and display it's contents.
56 * the view mode will not allow node and edge creation
57 * anymore.
58 */
59 void loadGML(URL url)
60 {
61 GMLIOHandler ioh = new GMLIOHandler();
62 //read file into view
63 try
64 {
65 ioh.read(view.getGraph2D(),url);
66 //fit the content nicely into the view
67 view.fitContent();
68 //update the view
69 view.updateView();
70 } catch (IOException ioe){
71 D.show(ioe);
72 }
73 }
74
75 public static void main(String args[])
76 {
77 JFrame frame = new JFrame("GML Demo");
78
79 frame.addWindowListener(new WindowAdapter() {
80 public void windowClosing(WindowEvent e) {
81 System.exit(0);
82 }
83 });
84
85 SimpleGMLDemo demo = new SimpleGMLDemo();
86 frame.setContentPane(demo);
87 frame.pack();
88
89 if(args.length == 0)
90 {
91 demo.loadGML(demo.getClass().getResource("resource/graph1.gml"));
92 }
93 else
94 {
95 File file = new File(args[0]);
96 if(file.canRead())
97 try {
98 System.out.println("file >>> " + file.toURL());
99 demo.loadGML(file.toURL());
100 }catch(MalformedURLException mex) {}
101 }
102
103 frame.setVisible(true);
104 }
105 }
106
107
108