001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.browser;
024:
025: import java.awt.*;
026: import java.awt.event.*;
027: import java.io.*;
028: import javax.swing.*;
029: import drjava.smyle.*;
030:
031: /*
032: TODO: check for in-use.flag
033: */
034:
035: public class Main extends JFrame {
036: static final String aOpen = "Open store...",
037: aClose = "Close store", aExit = "Exit";
038:
039: String storeDir;
040: Store store;
041: StoreView view;
042: JMenuItem iClose;
043:
044: void openStore(String dir) {
045: try {
046: if (store != null)
047: store.close();
048: } catch (Exception e) {
049: e.printStackTrace();
050: }
051: store = null;
052:
053: storeDir = dir;
054: if (storeDir != null) {
055: if (storeDir.endsWith("/"))
056: storeDir = storeDir.substring(0, storeDir.length() - 1);
057:
058: if (!Smyle.storeExists(storeDir)) {
059: JOptionPane.showMessageDialog(this ,
060: "No Smyle store found in " + storeDir, "Error",
061: JOptionPane.OK_OPTION);
062: } else
063: try {
064: if (Smyle.storeInUse(storeDir))
065: if (JOptionPane
066: .showConfirmDialog(
067: this ,
068: "The store in "
069: + storeDir
070: + " seems to be in use by another process.\n"
071: + "This can cause the browser to hang. Open anyway?",
072: "Warning",
073: JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
074: update();
075: return;
076: }
077: store = Smyle.openStoreReadOnly(storeDir);
078: } catch (Exception e) {
079: store = null;
080: JOptionPane.showMessageDialog(this , e.getMessage());
081: }
082: }
083:
084: update();
085: }
086:
087: static ButtonModel mnemonic(char c) {
088: DefaultButtonModel model = new DefaultButtonModel();
089: model.setMnemonic((int) c);
090: return model;
091: }
092:
093: void exit() {
094: if (store != null)
095: store.close();
096: System.exit(0);
097: }
098:
099: Main() {
100: updateTitle();
101: setSize(500, 400);
102: getContentPane().setLayout(new BorderLayout());
103:
104: addWindowListener(new WindowAdapter() {
105: public void windowClosing(WindowEvent e) {
106: exit();
107: }
108: });
109:
110: JMenu mFile = new JMenu("File");
111: mFile.setModel(mnemonic('f'));
112:
113: JMenuItem item = mFile.add(aOpen);
114: item.setModel(mnemonic('o'));
115: item.addActionListener(new ActionListener() {
116: public void actionPerformed(ActionEvent evt) {
117: FileDialog dialog = new FileDialog(Main.this ,
118: "Open store");
119: dialog.setFilenameFilter(new FilenameFilter() {
120: public boolean accept(File dir, String name) {
121: name = name.toLowerCase();
122: return name.startsWith("m")
123: && name.endsWith(".smy");
124: }
125: });
126: dialog.show();
127:
128: if (dialog.getDirectory() != null) {
129: openStore(dialog.getDirectory());
130: }
131: }
132: });
133:
134: item = iClose = mFile.add(aClose);
135: item.setModel(mnemonic('c'));
136: item.addActionListener(new ActionListener() {
137: public void actionPerformed(ActionEvent evt) {
138: openStore(null);
139: }
140: });
141:
142: mFile.addSeparator();
143:
144: item = mFile.add(aExit);
145: item.setModel(mnemonic('e'));
146: item.addActionListener(new ActionListener() {
147: public void actionPerformed(ActionEvent evt) {
148: exit();
149: }
150: });
151:
152: JMenuBar menubar = new JMenuBar();
153: menubar.add(mFile);
154: setJMenuBar(menubar);
155: }
156:
157: void updateTitle() {
158: setTitle("Smyle Browser " + Smyle.getVersion()
159: + (store == null ? "" : " [" + storeDir + "]"));
160: }
161:
162: void update() {
163: updateTitle();
164: if (view != null)
165: getContentPane().remove(view);
166: if (store == null) {
167: view = null;
168: getContentPane().repaint();
169: } else {
170: view = new StoreView(store);
171: getContentPane().add(BorderLayout.CENTER, view);
172: }
173: getContentPane().validate();
174: iClose.setEnabled(store != null);
175: }
176:
177: public static void main(String[] args) throws Exception {
178: Main main = new Main();
179: main.show();
180: if (args.length != 0)
181: main.openStore(args[0]);
182: Object o = new Object();
183: synchronized (o) {
184: o.wait();
185: }
186: }
187: }
|