Debian Codegraph @

freehep-graphicsio-emf/src/main/java/org/freehep/graphicsio/emf/EMFViewer.java

raw
1 // Copyright 2007 FreeHEP
2 package org.freehep.graphicsio.emf;
3
4 import org.freehep.graphicsbase.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 * Simple frame to display EMF images.
21 *
22 * @author Steffen Greiffenberg
23 * @version $ID$
24 */
25 public class EMFViewer extends JFrame {
26
27 /**
28 * Prefix for the TITLE
29 */
30 private static String TITLE = "Freehep EMF Viewer";
31
32 /**
33 * Title for the Button
34 */
35 private static String LOAD_BUTTON_TITLE = "Open EMF";
36
37 /**
38 * Title for the Button
39 */
40 private static String ZOOMIN__BUTTON_TITLE = "Zoom in";
41
42 /**
43 * Title for the Button
44 */
45 private static String ZOOMOUT_BUTTON_TITLE = "Zoom out";
46
47 /**
48 * simple panel for displaying an EMF
49 */
50 private EMFPanel emfPanel = new EMFPanel();
51
52 /**
53 * used to open EMF-Files
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 * creates the content of tha frame
65 */
66 private void initGUI() {
67 setTitle(TITLE);
68
69 // create a panel for the window content
70 getContentPane().setLayout(new BorderLayout(0, 0));
71 JPanel mainPanel = new JPanel();
72 getContentPane().add(mainPanel);
73
74 // layout the mainPanel
75 mainPanel.setLayout(new BorderLayout(3, 3));
76 mainPanel.add(
77 BorderLayout.CENTER,
78 new JScrollPane(emfPanel));
79
80 // prepare the filechooser
81 fileChooser.addChoosableFileFilter(
82 new ExtensionFileFilter("emf", "Encapsulated Metafile"));
83
84 // panel for buttons
85 JPanel buttonPanel = new JPanel(
86 new FlowLayout(FlowLayout.RIGHT, 3, 3));
87 mainPanel.add(
88 BorderLayout.SOUTH,
89 buttonPanel);
90
91
92 // button to zoom in
93 JButton zoomInButton = new JButton(ZOOMIN__BUTTON_TITLE);
94 zoomInButton.addActionListener(new ZoomInAction());
95 buttonPanel.add(zoomInButton);
96
97 // button to zoom out
98 JButton zoomOutButton = new JButton(ZOOMOUT_BUTTON_TITLE);
99 zoomOutButton.addActionListener(new ZoomOutAction());
100 buttonPanel.add(zoomOutButton);
101
102 // loadButton to open files
103 JButton loadButton = new JButton(LOAD_BUTTON_TITLE);
104 loadButton.addActionListener(new OpenFileAction());
105 buttonPanel.add(loadButton);
106 }
107
108 /**
109 * displays the file and
110 * @param emfFile File to show
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 // set the window title
121 setTitle(TITLE + " - " + emfFile.getName());
122 } catch (Exception e) {
123 e.printStackTrace();
124 }
125
126 // show the frame
127 if (!isVisible()) {
128 setVisible(true);
129 }
130 }
131
132 /**
133 * Uses the fileChooser to open a file.
134 * Uses the emfPanel to display the file.
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 * zoom the panel in
147 */
148 private class ZoomInAction implements ActionListener {
149 public void actionPerformed(ActionEvent e) {
150 emfPanel.zoomIn();
151 }
152 }
153
154 /**
155 * zoom the panel out
156 */
157 private class ZoomOutAction implements ActionListener {
158 public void actionPerformed(ActionEvent e) {
159 emfPanel.zoomOut();
160 }
161 }
162 }