1
2 package org.freehep.graphicsio.emf;
3
4 import org.freehep.swing.ExtensionFileFilter;
5
6 import javax.swing.JFrame;
7 import javax.swing.JFileChooser;
8 import javax.swing.JButton;
9 import javax.swing.JPanel;
10 import javax.swing.JScrollPane;
11 import java.awt.BorderLayout;
12 import java.awt.FlowLayout;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.BufferedInputStream;
18
19
20
21
22
23
24
25 public class EMFViewer extends JFrame {
26
27
28
29
30 private static String TITLE = "Freehep EMF Viewer";
31
32
33
34
35 private static String LOAD_BUTTON_TITLE = "Open EMF";
36
37
38
39
40 private static String ZOOMIN__BUTTON_TITLE = "Zoom in";
41
42
43
44
45 private static String ZOOMOUT_BUTTON_TITLE = "Zoom out";
46
47
48
49
50 private EMFPanel emfPanel = new EMFPanel();
51
52
53
54
55 private JFileChooser fileChooser = new JFileChooser();
56
57 public EMFViewer() {
58 initGUI();
59 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
60 setSize(500, 300);
61 }
62
63
64
65
66 private void initGUI() {
67 setTitle(TITLE);
68
69
70 getContentPane().setLayout(new BorderLayout(0, 0));
71 JPanel mainPanel = new JPanel();
72 getContentPane().add(mainPanel);
73
74
75 mainPanel.setLayout(new BorderLayout(3, 3));
76 mainPanel.add(
77 BorderLayout.CENTER,
78 new JScrollPane(emfPanel));
79
80
81 fileChooser.addChoosableFileFilter(
82 new ExtensionFileFilter("emf", "Encapsulated Metafile"));
83
84
85 JPanel buttonPanel = new JPanel(
86 new FlowLayout(FlowLayout.RIGHT, 3, 3));
87 mainPanel.add(
88 BorderLayout.SOUTH,
89 buttonPanel);
90
91
92
93 JButton zoomInButton = new JButton(ZOOMIN__BUTTON_TITLE);
94 zoomInButton.addActionListener(new ZoomInAction());
95 buttonPanel.add(zoomInButton);
96
97
98 JButton zoomOutButton = new JButton(ZOOMOUT_BUTTON_TITLE);
99 zoomOutButton.addActionListener(new ZoomOutAction());
100 buttonPanel.add(zoomOutButton);
101
102
103 JButton loadButton = new JButton(LOAD_BUTTON_TITLE);
104 loadButton.addActionListener(new OpenFileAction());
105 buttonPanel.add(loadButton);
106 }
107
108
109
110
111
112 public void show(File emfFile) {
113 try {
114 FileInputStream fis = new FileInputStream(emfFile);
115 EMFInputStream emf = new EMFInputStream(new BufferedInputStream(fis));
116
117 EMFRenderer renderer = new EMFRenderer(emf);
118 emfPanel.setRenderer(renderer);
119
120
121 setTitle(TITLE + " - " + emfFile.getName());
122 } catch (Exception e) {
123 e.printStackTrace();
124 }
125
126
127 if (!isVisible()) {
128 setVisible(true);
129 }
130 }
131
132
133
134
135
136 private class OpenFileAction implements ActionListener {
137 public void actionPerformed(ActionEvent e) {
138 int result = fileChooser.showOpenDialog(EMFViewer.this);
139 if (result == JFileChooser.APPROVE_OPTION) {
140 show(fileChooser.getSelectedFile());
141 }
142 }
143 }
144
145
146
147
148 private class ZoomInAction implements ActionListener {
149 public void actionPerformed(ActionEvent e) {
150 emfPanel.zoomIn();
151 }
152 }
153
154
155
156
157 private class ZoomOutAction implements ActionListener {
158 public void actionPerformed(ActionEvent e) {
159 emfPanel.zoomOut();
160 }
161 }
162 }