AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
DotViewer.java
Go to the documentation of this file.
1package aud.util;
2
3import java.awt.*;
4import java.awt.event.*;
5import java.io.*;
6import java.nio.file.Files;
7import java.nio.file.Path;
8import java.nio.file.FileSystems;
9
10import javax.swing.*;
11
12import org.apache.batik.swing.JSVGCanvas;
13import org.apache.batik.swing.JSVGScrollPane;
14import org.apache.batik.util.XMLResourceDescriptor;
15//import org.apache.batik.dom.svg.SAXSVGDocumentFactory; // Batik 1.7
16import org.apache.batik.anim.dom.SAXSVGDocumentFactory; // Batik 1.8
17import org.apache.batik.dom.svg.*;
18import org.apache.batik.anim.dom.*;
19import org.w3c.dom.svg.SVGDocument;
20
48public class DotViewer {
49
50 protected File dotfile;
51 protected JFrame frame;
52 protected JSVGCanvas svgCanvas;
53 protected Graphviz graphviz;
54 protected JLabel label;
55
57 public DotViewer(JFrame parent) {
59 svgCanvas=new JSVGCanvas();
60 svgCanvas.setEnableRotateInteractor(false);//
61
62 label=new JLabel();
63 parent.getContentPane().add(createComponents());
64
65 try {
66 dotfile=File.createTempFile("aud-dotviewer-",".dot");
67 } catch (IOException e) {
68 System.err.println("ERROR: "+e.getMessage());
69 System.exit(-1);
70 }
71 //System.err.println(dotfile);
72 graphviz=new Graphviz();
73 dotfile.deleteOnExit();
74 }
75
77 public JFrame parent() { return frame; }
79 public JLabel statusbar() { return label; }
80
81 protected JComponent createComponents() {
82 final JPanel panel = new JPanel(new BorderLayout());
83 @SuppressWarnings("unused")
84 final JSVGScrollPane pane = new JSVGScrollPane(svgCanvas);
85
86 panel.add("Center", svgCanvas);
87 panel.add("South", label);
88 return panel;
89 }
90
92 public static void help() {
93 System.out.println
94 ( "Batik JSVGCanvas overview\n"+
95 "-------------------------\n"+
96 "Shift+Mouse Left\tpan\n"+
97 "Shift+Mouse Right\tzoom in/out (drag)\n"+
98 "Ctrl+Mouse Left\trectangle zoom\n"+
99 "Ctrl+Mouse Right\trotate [disabled!]\n"+
100 "Ctrl+I\tzoom in\n"+
101 "Ctrl+O\tzoom out\n"
102 );
103 }
104
108 public void display(String code) {
109 Sys.writeToFile(dotfile,code);
110 //System.err.println(dotfile);
111 File svgfile=graphviz.renderDotFileToFile(dotfile,"svg");
112 //svgfile.deleteOnExit();
113 //svgCanvas.setURI(svgfile.toURI().toString());
114
115 Path path=FileSystems.getDefault().getPath(svgfile.getPath());
116
117 try {
118 String data=new String(Files.readAllBytes(path));
119 data=data.replaceAll("stroke=\"transparent\"","stroke-opacity=\"0\"");
120
121 String parser=XMLResourceDescriptor.getXMLParserClassName();
122 SAXSVGDocumentFactory factory=new SAXSVGDocumentFactory(parser);
123 SVGDocument document=
124 factory.createSVGDocument("",new ByteArrayInputStream(data.getBytes("UTF-8")));
125 svgCanvas.setDocument(document);
126 } catch (IOException e) {
127 System.err.println(e);
128 System.exit(-1);
129 }
130
131 svgfile.delete();
132 }
133
135 public void display(Graphvizable object) {
136 display(object.toDot());
137 }
138
140 public static DotViewer displayWindow(Graphvizable object,String caption) {
141 return displayWindow(object.toDot(),caption);
142 }
144 public static DotViewer displayWindow(String code,String caption) {
145 JFrame f=new JFrame(caption!=null ? caption : "aud.util.DotViewer");
146 DotViewer dot=new DotViewer(f);
147 f.setSize(800, 600);
148 f.setVisible(true);
149 if (code!=null)
150 dot.display(code);
151 return dot;
152 }
153
155 public void setExitOnClose() {
156 parent().addWindowListener(new WindowAdapter() {
157 public void windowClosing(WindowEvent e) {
158 System.exit(0);
159 }
160 });
161 }
162
164 public static void main(String[] args) {
165 if (args.length==0) {
166 System.err.println("usage: java aud.util.DotViewer file.dot\n");
167 DotViewer.help();
168 }
169 else
170 for (String arg : args) {
171 String dotCode=Sys.readFile(new File(arg));
172 DotViewer v=DotViewer.displayWindow(dotCode,arg);
173 v.setExitOnClose();
174 }
175 }
176}
Simple viewer for Graphvizable.
Definition: DotViewer.java:48
JComponent createComponents()
Definition: DotViewer.java:81
static DotViewer displayWindow(String code, String caption)
create new DotViewer (toplevel window) and display code
Definition: DotViewer.java:144
void display(Graphvizable object)
display object
Definition: DotViewer.java:135
static void main(String[] args)
visualize given dot files (file names as command line arguments)
Definition: DotViewer.java:164
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Definition: DotViewer.java:140
void display(String code)
display dot code
Definition: DotViewer.java:108
JSVGCanvas svgCanvas
Definition: DotViewer.java:52
JLabel statusbar()
get status bar
Definition: DotViewer.java:79
JFrame parent()
get parent widget
Definition: DotViewer.java:77
void setExitOnClose()
exit application if viewer is closed
Definition: DotViewer.java:155
static void help()
print help (mouse/key bindings) to stdout
Definition: DotViewer.java:92
DotViewer(JFrame parent)
create new instance
Definition: DotViewer.java:57
Use GraphViz to render graph structures.
Definition: Graphviz.java:15
File renderDotFileToFile(File dotfile, String format)
Render dot file.
Definition: Graphviz.java:36
System related utilities.
Definition: Sys.java:58
static File writeToFile(File file, String text)
write text to file @endiliteral
Definition: Sys.java:192
static String readFile(File file)
read entire file and return contents as String
Definition: Sys.java:216
Interface for GraphViz rendering.