1
14 package demo.yext.svg;
15
16 import yext.svg.io.SVGNodeRealizerSerializer;
17
18 import y.io.GraphMLIOHandler;
19 import y.util.D;
20
21 import javax.swing.AbstractAction;
22 import javax.swing.Action;
23 import javax.swing.JFileChooser;
24 import javax.swing.filechooser.FileFilter;
25 import java.awt.EventQueue;
26 import java.awt.event.ActionEvent;
27 import java.io.File;
28 import java.io.IOException;
29
30
31
36 public class GraphMLDemo extends SVGNodeRealizerDemo {
37
38
47 protected GraphMLIOHandler createIOHandler() {
48 final GraphMLIOHandler ioh = new GraphMLIOHandler();
49
50 ioh.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
75 public static void main( String[] args ) {
76 EventQueue.invokeLater(new Runnable() {
77 public void run() {
78 initLnF();
79 (new GraphMLDemo()).start();
80 }
81 });
82 }
83
84
85
90
91
94 private class GraphMLLoadAction extends AbstractAction {
95 GraphMLIOHandler ioh;
96 JFileChooser chooser;
97
98 public GraphMLLoadAction() {
99 super("Load...");
100 ioh = null;
101 chooser = null;
102 }
103
104 public void actionPerformed(ActionEvent e) {
105 if (ioh == null) {
106 ioh = createIOHandler();
107 }
108 if (chooser == null) {
109 chooser = new JFileChooser();
110 chooser.addChoosableFileFilter(new FileFilter() {
111 public boolean accept(final File f) {
112 return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
113 }
114
115 public String getDescription() {
116 return "GraphML (*.graphml)";
117 }
118 });
119 }
120 if (chooser.showOpenDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
121 String name = chooser.getSelectedFile().toString();
122 if (!name.endsWith(".graphml")) {
123 name = name + ".graphml";
124 }
125 try {
126 view.getGraph2D().clear();
127 ioh.read(view.getGraph2D(), name);
128 } catch (IOException ioe) {
129 D.show(ioe);
130 }
131 view.fitContent();
133 view.getGraph2D().updateViews();
134 }
135 }
136 }
137
138
141 private class GraphMLSaveAction extends AbstractAction {
142 GraphMLIOHandler ioh;
143 JFileChooser chooser;
144
145 public GraphMLSaveAction() {
146 super("Save...");
147 ioh = null;
148 chooser = null;
149 }
150
151 public void actionPerformed(final ActionEvent e) {
152 if (ioh == null) {
153 ioh = createIOHandler();
154 }
155 if (chooser == null) {
156 chooser = new JFileChooser();
157 chooser.addChoosableFileFilter(new FileFilter() {
158 public boolean accept(final File f) {
159 return f.isDirectory() || f.getName().toLowerCase().endsWith(".graphml");
160 }
161
162 public String getDescription() {
163 return "GraphML (*.graphml)";
164 }
165 });
166 }
167 if (chooser.showSaveDialog(GraphMLDemo.this) == JFileChooser.APPROVE_OPTION) {
168 String name = chooser.getSelectedFile().toString();
169 if (!name.endsWith(".graphml")) {
170 name = name + ".graphml";
171 }
172 try {
173 ioh.write(view.getGraph2D(), name);
174 } catch (IOException ioe) {
175 D.show(ioe);
176 }
177 }
178 }
179 }
180 }
181