author | wenzelm |
Tue, 22 Feb 2011 19:44:15 +0100 | |
changeset 41819 | 2d84831dc1a0 |
parent 33686 | 8e33ca8832b1 |
child 50473 | ca4088bf8365 |
permissions | -rw-r--r-- |
3599 | 1 |
/*************************************************************************** |
2 |
Title: GraphBrowser/GraphBrowserFrame.java |
|
3 |
Author: Stefan Berghofer, TU Muenchen |
|
4 |
||
5 |
This class is the frame for the stand-alone application. It contains |
|
6 |
methods for handling menubar events. |
|
7 |
***************************************************************************/ |
|
8 |
||
9 |
package GraphBrowser; |
|
10 |
||
11 |
import java.awt.*; |
|
6541 | 12 |
import java.awt.event.*; |
3599 | 13 |
import java.io.*; |
14 |
import awtUtilities.*; |
|
15 |
||
6541 | 16 |
public class GraphBrowserFrame extends Frame implements ActionListener { |
3599 | 17 |
GraphBrowser gb; |
6541 | 18 |
MenuItem i1, i2; |
19 |
String graphDir, psDir; |
|
3599 | 20 |
|
21 |
public void checkMenuItems() { |
|
22 |
if (gb.isEmpty()) { |
|
6541 | 23 |
i1.setEnabled(false); |
24 |
i2.setEnabled(false); |
|
3599 | 25 |
} else { |
6541 | 26 |
i1.setEnabled(true); |
27 |
i2.setEnabled(true); |
|
3599 | 28 |
} |
29 |
} |
|
30 |
||
6541 | 31 |
public void actionPerformed(ActionEvent evt) { |
32 |
String label = evt.getActionCommand(); |
|
33 |
if (label.equals("Quit")) |
|
34 |
System.exit(0); |
|
35 |
else if (label.equals("Export to PostScript")) { |
|
36 |
PS(true, label); |
|
37 |
return; |
|
38 |
} else if (label.equals("Export to EPS")) { |
|
39 |
PS(false, label); |
|
40 |
return; |
|
41 |
} else if (label.equals("Open ...")) { |
|
42 |
FileDialog fd = new FileDialog(this, "Open graph file", FileDialog.LOAD); |
|
43 |
if (graphDir != null) |
|
44 |
fd.setDirectory(graphDir); |
|
45 |
fd.setVisible(true); |
|
46 |
if (fd.getFile() == null) return; |
|
47 |
graphDir = fd.getDirectory(); |
|
48 |
String fname = graphDir + fd.getFile(); |
|
49 |
GraphBrowser gb2 = new GraphBrowser(fname); |
|
50 |
try { |
|
51 |
InputStream is = new FileInputStream(fname); |
|
11875 | 52 |
gb2.initBrowser(is, false); |
6541 | 53 |
is.close(); |
54 |
} catch (IOException exn) { |
|
55 |
String button[] = {"OK"}; |
|
56 |
MessageDialog md = new MessageDialog(this, "Error", |
|
57 |
"Can't open file " + fname + ".", button); |
|
58 |
md.setSize(350, 200); |
|
59 |
md.setVisible(true); |
|
60 |
return; |
|
3599 | 61 |
} |
6541 | 62 |
remove(gb); |
63 |
add("Center", gb2); |
|
64 |
gb = gb2; |
|
65 |
checkMenuItems(); |
|
66 |
validate(); |
|
3599 | 67 |
} |
68 |
} |
|
69 |
||
70 |
public void PS(boolean printable,String label) { |
|
71 |
FileDialog fd=new FileDialog(this,label,FileDialog.SAVE); |
|
72 |
if (psDir!=null) |
|
73 |
fd.setDirectory(psDir); |
|
6541 | 74 |
fd.setVisible(true); |
3599 | 75 |
if (fd.getFile()==null) return; |
76 |
psDir=fd.getDirectory(); |
|
77 |
String fname=psDir+fd.getFile(); |
|
78 |
||
79 |
if ((new File(fname)).exists()) { |
|
80 |
String buttons[]={"Overwrite","Cancel"}; |
|
81 |
MessageDialog md=new MessageDialog(this,"Warning", |
|
82 |
"Warning: File "+fname+" already exists. Overwrite?", |
|
83 |
buttons); |
|
6541 | 84 |
md.setSize(350,200); |
85 |
md.setVisible(true); |
|
3599 | 86 |
if (md.getText().equals("Cancel")) return; |
87 |
} |
|
88 |
||
89 |
try { |
|
90 |
gb.PS(fname,printable); |
|
91 |
} catch (IOException exn) { |
|
92 |
String button[]={"OK"}; |
|
93 |
MessageDialog md=new MessageDialog(this,"Error", |
|
94 |
"Unable to write file "+fname+".",button); |
|
6541 | 95 |
md.setSize(350,200); |
96 |
md.setVisible(true); |
|
3599 | 97 |
} |
98 |
} |
|
99 |
||
100 |
public GraphBrowserFrame(GraphBrowser br) { |
|
101 |
super("GraphBrowser"); |
|
6541 | 102 |
MenuItem i3, i4; |
103 |
gb = br; |
|
104 |
MenuBar mb = new MenuBar(); |
|
105 |
Menu m1 = new Menu("File"); |
|
106 |
m1.add(i3 = new MenuItem("Open ...")); |
|
107 |
m1.add(i1 = new MenuItem("Export to PostScript")); |
|
108 |
m1.add(i2 = new MenuItem("Export to EPS")); |
|
109 |
m1.add(i4 = new MenuItem("Quit")); |
|
110 |
i1.addActionListener(this); |
|
111 |
i2.addActionListener(this); |
|
112 |
i3.addActionListener(this); |
|
113 |
i4.addActionListener(this); |
|
3599 | 114 |
checkMenuItems(); |
115 |
mb.add(m1); |
|
116 |
setMenuBar(mb); |
|
6541 | 117 |
add("Center", br); |
11931
a5d1c9b34900
added windowlistener (can now close the frame by window controls)
kleing
parents:
11875
diff
changeset
|
118 |
addWindowListener( new WindowAdapter() { |
a5d1c9b34900
added windowlistener (can now close the frame by window controls)
kleing
parents:
11875
diff
changeset
|
119 |
public void windowClosing(WindowEvent e) { |
a5d1c9b34900
added windowlistener (can now close the frame by window controls)
kleing
parents:
11875
diff
changeset
|
120 |
System.exit(0); |
a5d1c9b34900
added windowlistener (can now close the frame by window controls)
kleing
parents:
11875
diff
changeset
|
121 |
} |
a5d1c9b34900
added windowlistener (can now close the frame by window controls)
kleing
parents:
11875
diff
changeset
|
122 |
}); |
3599 | 123 |
} |
124 |
} |