1
14
15 package demo.yext.svg;
16
17 import y.util.D;
18 import yext.graphml.graph2D.GraphMLIOHandler;
19 import yext.svg.io.SVGNodeRealizerSerializer;
20
21 import java.awt.event.ActionEvent;
22 import java.io.IOException;
23 import java.io.File;
24 import javax.swing.AbstractAction;
25 import javax.swing.Action;
26 import javax.swing.JFileChooser;
27 import javax.swing.filechooser.FileFilter;
28
29
30
35 public class GraphMLDemo extends SVGNodeRealizerDemo {
36
37
46 protected GraphMLIOHandler createIOHandler() {
47 final GraphMLIOHandler ioh = new GraphMLIOHandler();
48
49 ioh.getRealizerSerializerManager()
51 .addNodeRealizerSerializer(new SVGNodeRealizerSerializer());
52
53 return ioh;
54 }
55
56
59 protected Action createLoadAction() {
60 return new GraphMLLoadAction();
61 }
62
63
66 protected Action createSaveAction() {
67 return new GraphMLSaveAction();
68 }
69
70
71 public static void main( final String[] args ) {
72 initLnF();
73
74 (new GraphMLDemo()).start();
75 }
76
77
78
83
84
87 private class GraphMLLoadAction extends AbstractAction {
88 GraphMLIOHandler ioh;
89 JFileChooser chooser;
90
91 public GraphMLLoadAction() {
92 super("Load...");
93 ioh = null;
94 chooser = null;
95 }
96
97 public void actionPerformed(ActionEvent e) {
98 if (ioh == null) {
99 ioh = createIOHandler();
100 }
101 if (chooser == null) {
102 chooser = new JFileChooser();
103 chooser.addChoosableFileFilter(new FileFilter() {
104 public boolean accept( final File f ) {
105 return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
106 }
107
108 public String getDescription() {
109 return "GraphML (*.graphml)";
110 }
111 });
112 }
113 if (chooser.showOpenDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
114 String name = chooser.getSelectedFile().toString();
115 if (!name.endsWith(".graphml")){
116 name = name + ".graphml";
117 }
118 try {
119 view.getGraph2D().clear();
120 ioh.read(view.getGraph2D(), name);
121 } catch (IOException ioe) {
122 D.show(ioe);
123 }
124 view.fitContent();
126 view.getGraph2D().updateViews();
127 }
128 }
129 }
130
131
134 private class GraphMLSaveAction extends AbstractAction {
135 GraphMLIOHandler ioh;
136 JFileChooser chooser;
137
138 public GraphMLSaveAction() {
139 super("Save...");
140 ioh = null;
141 chooser = null;
142 }
143
144 public void actionPerformed( final ActionEvent e ) {
145 if (ioh == null) {
146 ioh = createIOHandler();
147 }
148 if (chooser == null) {
149 chooser = new JFileChooser();
150 chooser.addChoosableFileFilter(new FileFilter() {
151 public boolean accept( final File f ) {
152 return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
153 }
154
155 public String getDescription() {
156 return "GraphML (*.graphml)";
157 }
158 });
159 }
160 if (chooser.showSaveDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
161 String name = chooser.getSelectedFile().toString();
162 if (!name.endsWith(".graphml")) {
163 name = name + ".graphml";
164 }
165 try {
166 ioh.write(view.getGraph2D(), name);
167 } catch (IOException ioe) {
168 D.show(ioe);
169 }
170 }
171 }
172 }
173 }
174