|
1 /*************************************************************************** |
|
2 Title: GraphBrowser/GraphBrowserFrame.java |
|
3 ID: $Id$ |
|
4 Author: Stefan Berghofer, TU Muenchen |
|
5 Copyright 1997 TU Muenchen |
|
6 |
|
7 This class is the frame for the stand-alone application. It contains |
|
8 methods for handling menubar events. |
|
9 ***************************************************************************/ |
|
10 |
|
11 package GraphBrowser; |
|
12 |
|
13 import java.awt.*; |
|
14 import java.io.*; |
|
15 import awtUtilities.*; |
|
16 |
|
17 public class GraphBrowserFrame extends Frame { |
|
18 GraphBrowser gb; |
|
19 MenuItem i1,i2; |
|
20 String graphDir,psDir; |
|
21 |
|
22 public void checkMenuItems() { |
|
23 if (gb.isEmpty()) { |
|
24 i1.disable(); |
|
25 i2.disable(); |
|
26 } else { |
|
27 i1.enable(); |
|
28 i2.enable(); |
|
29 } |
|
30 } |
|
31 |
|
32 public boolean action(Event evt, Object arg) { |
|
33 if (evt.target instanceof MenuItem) { |
|
34 String label=(String)arg; |
|
35 if (label.equals("Quit")) |
|
36 System.exit(0); |
|
37 else if (label.equals("Export to PostScript")) { |
|
38 PS(true,label); |
|
39 return true; |
|
40 } else if (label.equals("Export to EPS")) { |
|
41 PS(false,label); |
|
42 return true; |
|
43 } else if (label.equals("Open ...")) { |
|
44 FileDialog fd=new FileDialog(this,"Open graph file",FileDialog.LOAD); |
|
45 if (graphDir!=null) |
|
46 fd.setDirectory(graphDir); |
|
47 fd.show(); |
|
48 if (fd.getFile()==null) return true; |
|
49 graphDir=fd.getDirectory(); |
|
50 String fname=graphDir+fd.getFile(); |
|
51 GraphBrowser gb2=new GraphBrowser(fname); |
|
52 try { |
|
53 InputStream is=new FileInputStream(fname); |
|
54 gb2.initBrowser(is); |
|
55 is.close(); |
|
56 } catch (IOException exn) { |
|
57 String button[]={"OK"}; |
|
58 MessageDialog md=new MessageDialog(this,"Error","Can't open file "+fname+".",button); |
|
59 md.resize(350,200); |
|
60 md.show(); |
|
61 return true; |
|
62 } |
|
63 remove(gb); |
|
64 add("Center",gb2); |
|
65 gb=gb2; |
|
66 checkMenuItems(); |
|
67 validate(); |
|
68 return true; |
|
69 } |
|
70 } |
|
71 return false; |
|
72 } |
|
73 |
|
74 public void PS(boolean printable,String label) { |
|
75 FileDialog fd=new FileDialog(this,label,FileDialog.SAVE); |
|
76 if (psDir!=null) |
|
77 fd.setDirectory(psDir); |
|
78 fd.show(); |
|
79 if (fd.getFile()==null) return; |
|
80 psDir=fd.getDirectory(); |
|
81 String fname=psDir+fd.getFile(); |
|
82 |
|
83 if ((new File(fname)).exists()) { |
|
84 String buttons[]={"Overwrite","Cancel"}; |
|
85 MessageDialog md=new MessageDialog(this,"Warning", |
|
86 "Warning: File "+fname+" already exists. Overwrite?", |
|
87 buttons); |
|
88 md.resize(350,200); |
|
89 md.show(); |
|
90 if (md.getText().equals("Cancel")) return; |
|
91 } |
|
92 |
|
93 try { |
|
94 gb.PS(fname,printable); |
|
95 } catch (IOException exn) { |
|
96 String button[]={"OK"}; |
|
97 MessageDialog md=new MessageDialog(this,"Error", |
|
98 "Unable to write file "+fname+".",button); |
|
99 md.resize(350,200); |
|
100 md.show(); |
|
101 } |
|
102 } |
|
103 |
|
104 public GraphBrowserFrame(GraphBrowser br) { |
|
105 super("GraphBrowser"); |
|
106 gb=br; |
|
107 MenuBar mb=new MenuBar(); |
|
108 Menu m1=new Menu("File"); |
|
109 m1.add(new MenuItem("Open ...")); |
|
110 m1.add(i1=new MenuItem("Export to PostScript")); |
|
111 m1.add(i2=new MenuItem("Export to EPS")); |
|
112 m1.add(new MenuItem("Quit")); |
|
113 checkMenuItems(); |
|
114 mb.add(m1); |
|
115 setMenuBar(mb); |
|
116 add("Center",br); |
|
117 } |
|
118 } |