1/***************************************************************************
2  Title:      GraphBrowser/GraphBrowserFrame.java
3  Author:     Stefan Berghofer, TU Muenchen
4  Options:    :tabSize=2:
5
6  This class is the frame for the stand-alone application. It contains
7  methods for handling menubar events.
8***************************************************************************/
9
10package GraphBrowser;
11
12import java.awt.*;
13import java.awt.event.*;
14import java.io.*;
15import awtUtilities.*;
16
17public class GraphBrowserFrame extends Frame implements ActionListener {
18	GraphBrowser gb;
19	MenuItem i1, i2;
20	String graphDir, psDir;
21
22	public void checkMenuItems() {
23		if (gb.isEmpty()) {
24			i1.setEnabled(false);
25			i2.setEnabled(false);
26		} else {
27			i1.setEnabled(true);
28			i2.setEnabled(true);
29		}
30	}
31
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);
53				gb2.initBrowser(is, false);
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;
62			}
63			remove(gb);
64			add("Center", gb2);
65			gb = gb2;
66			checkMenuItems();
67			validate();
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);
75		fd.setVisible(true);
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);
85			md.setSize(350,200);
86			md.setVisible(true);
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);
96			md.setSize(350,200);
97			md.setVisible(true);
98		}
99	}
100
101	public GraphBrowserFrame(GraphBrowser br) {
102		super("GraphBrowser");
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);
115		checkMenuItems();
116		mb.add(m1);
117		setMenuBar(mb);
118		add("Center", br);
119    addWindowListener( new WindowAdapter() {
120      public void windowClosing(WindowEvent e) {
121        System.exit(0);
122      }
123    });
124	}
125}
126