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