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
47public class DotViewer {
48
49 protected File dotfile;
50 protected JFrame frame;
51 protected JSVGCanvas svgCanvas;
52 protected Graphviz graphviz;
53 protected JLabel label;
54
56 public DotViewer(JFrame parent) {
58 svgCanvas=new JSVGCanvas();
59 svgCanvas.setEnableRotateInteractor(false);//
60
61 label=new JLabel();
62 parent.getContentPane().add(createComponents());
63
64 try {
65 dotfile=File.createTempFile("aud-dotviewer-",".dot");
66 } catch (IOException e) {
67 System.err.println("ERROR: "+e.getMessage());
68 System.exit(-1);
69 }
70 //System.err.println(dotfile);
71 graphviz=new Graphviz();
72 dotfile.deleteOnExit();
73 }
74
76 public JFrame parent() { return frame; }
78 public JLabel statusbar() { return label; }
79
80 protected JComponent createComponents() {
81 final JPanel panel = new JPanel(new BorderLayout());
82 @SuppressWarnings("unused")
83 final JSVGScrollPane pane = new JSVGScrollPane(svgCanvas);
84
85 panel.add("Center", svgCanvas);
86 panel.add("South", label);
87 return panel;
88 }
89
91 public static void help() {
92 System.out.println
93 ( "Batik JSVGCanvas overview\n"+
94 "-------------------------\n"+
95 "Shift+Mouse Left\tpan\n"+
96 "Shift+Mouse Right\tzoom in/out (drag)\n"+
97 "Ctrl+Mouse Left\trectangle zoom\n"+
98 "Ctrl+Mouse Right\trotate [disabled!]\n"+
99 "Ctrl+I\tzoom in\n"+
100 "Ctrl+O\tzoom out\n"
101 );
102 }
103
107 public void display(String code) {
108 Sys.writeToFile(dotfile,code);
109 //System.err.println(dotfile);
110 File svgfile=graphviz.renderDotFileToFile(dotfile,"svg");
111 //svgfile.deleteOnExit();
112 //svgCanvas.setURI(svgfile.toURI().toString());
113
114 Path path=FileSystems.getDefault().getPath(svgfile.getPath());
115
116 try {
117 String data=new String(Files.readAllBytes(path));
118 data=data.replaceAll("stroke=\"transparent\"","stroke-opacity=\"0\"");
119
120 String parser=XMLResourceDescriptor.getXMLParserClassName();
121 SAXSVGDocumentFactory factory=new SAXSVGDocumentFactory(parser);
122 SVGDocument document=
123 factory.createSVGDocument("",new ByteArrayInputStream(data.getBytes("UTF-8")));
124 svgCanvas.setDocument(document);
125 } catch (IOException e) {
126 System.err.println(e);
127 System.exit(-1);
128 }
129
130 svgfile.delete();
131 }
132
134 public void display(Graphvizable object) {
135 display(object.toDot());
136 }
137
139 public static DotViewer displayWindow(Graphvizable object,String caption) {
140 return displayWindow(object.toDot(),caption);
141 }
143 public static DotViewer displayWindow(String code,String caption) {
144 JFrame f=new JFrame(caption!=null ? caption : "aud.util.DotViewer");
145 DotViewer dot=new DotViewer(f);
146 f.setSize(800, 600);
147 f.setVisible(true);
148 if (code!=null)
149 dot.display(code);
150 return dot;
151 }
152
154 public void setExitOnClose() {
155 parent().addWindowListener(new WindowAdapter() {
156 public void windowClosing(WindowEvent e) {
157 System.exit(0);
158 }
159 });
160 }
161
163 public static void main(String[] args) {
164 if (args.length==0) {
165 System.err.println("usage: java aud.util.DotViewer file.dot\n");
166 DotViewer.help();
167 }
168 else
169 for (String arg : args) {
170 String dotCode=Sys.readFile(new File(arg));
171 DotViewer v=DotViewer.displayWindow(dotCode,arg);
172 v.setExitOnClose();
173 }
174 }
175}
Simple viewer for Graphvizable.
Definition: DotViewer.java:47
JComponent createComponents()
Definition: DotViewer.java:80
static DotViewer displayWindow(String code, String caption)
create new DotViewer (toplevel window) and display code
Definition: DotViewer.java:143
void display(Graphvizable object)
display object
Definition: DotViewer.java:134
static void main(String[] args)
visualize given dot files (file names as command line arguments)
Definition: DotViewer.java:163
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Definition: DotViewer.java:139
void display(String code)
display dot code
Definition: DotViewer.java:107
JSVGCanvas svgCanvas
Definition: DotViewer.java:51
JLabel statusbar()
get status bar
Definition: DotViewer.java:78
JFrame parent()
get parent widget
Definition: DotViewer.java:76
void setExitOnClose()
exit application if viewer is closed
Definition: DotViewer.java:154
static void help()
print help (mouse/key bindings) to stdout
Definition: DotViewer.java:91
DotViewer(JFrame parent)
create new instance
Definition: DotViewer.java:56
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.