001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MainFrame.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.old;
009:
010: import bsh.EvalError;
011: import bsh.Interpreter;
012: import com.uwyn.rife.gui.Rife;
013: import com.uwyn.rife.swing.JDialogSystemError;
014: import com.uwyn.rife.tools.ExceptionUtils;
015: import com.uwyn.rife.tools.Localization;
016:
017: import javax.swing.*;
018: import java.awt.*;
019: import java.awt.event.*;
020: import java.io.BufferedReader;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.io.UnsupportedEncodingException;
024:
025: public class MainFrame extends JFrame implements WindowListener,
026: ActionListener {
027: private StructureEditor mStructureEditor = null;
028:
029: public MainFrame() {
030: super (Localization.getString("rife.application.title"));
031: addWindowListener(this );
032:
033: createMenuBar();
034: mStructureEditor = new StructureEditor();
035: addKeyListener(mStructureEditor.getStructurePanel());
036:
037: getContentPane().setLayout(new BorderLayout());
038: getContentPane().add(mStructureEditor, BorderLayout.CENTER);
039: pack();
040: setSize(800, 600);
041: }
042:
043: public StructureEditor getStructureEditor() {
044: return mStructureEditor;
045: }
046:
047: public void updateMenuBar() {
048: JMenuBar menu_bar = getJMenuBar();
049: menu_bar.removeAll();
050: populateMenuBar(menu_bar);
051: menu_bar.revalidate();
052: menu_bar.repaint();
053: }
054:
055: private void createMenuBar() {
056: JMenuBar menu_bar = new JMenuBar();
057: populateMenuBar(menu_bar);
058: setJMenuBar(menu_bar);
059: }
060:
061: private void populateMenuBar(JMenuBar menuBar) {
062: DynamicMenuBuilder menu_builder = new DynamicMenuBuilder();
063:
064: JMenu menu_file = menu_builder.addMenu(menuBar, Localization
065: .getString("rife.menu.file"), Localization
066: .getChar("rife.menu.file.mnemonic"));
067: menu_builder.addMenuItem(menu_file, Localization
068: .getString("rife.menu.file.exit"), new Exit(),
069: Localization.getChar("rife.menu.file.exit.mnemonic"),
070: KeyStroke.getKeyStroke(KeyEvent.VK_Q,
071: InputEvent.CTRL_MASK, false));
072:
073: JMenu menu_view = menu_builder.addMenu(menuBar, Localization
074: .getString("rife.menu.view"), Localization
075: .getChar("rife.menu.view.mnemonic"));
076: JMenu menu_lookandfeel = menu_builder
077: .addMenu(
078: menu_view,
079: Localization
080: .getString("rife.menu.view.lookandfeel"),
081: Localization
082: .getChar("rife.menu.view.lookandfeel.mnemonic"));
083: DynamicMenuAction lookandfeel_action = new ChangeLookAndFeel();
084: ButtonGroup lookandfeel_group = new ButtonGroup();
085: LookAndFeel current_lookandfeel = UIManager.getLookAndFeel();
086: UIManager.LookAndFeelInfo[] look_and_feels = UIManager
087: .getInstalledLookAndFeels();
088: JRadioButtonMenuItem menu_item = null;
089: String name = null;
090: String classname = null;
091: Class<LookAndFeel> testclass = null;
092: LookAndFeel lookandfeel = null;
093: for (int counter = 0; counter < look_and_feels.length; counter++) {
094: name = look_and_feels[counter].getName();
095: classname = look_and_feels[counter].getClassName();
096: try {
097: testclass = (Class<LookAndFeel>) Class
098: .forName(classname);
099: lookandfeel = testclass.newInstance();
100: if (lookandfeel.isSupportedLookAndFeel()) {
101: menu_item = menu_builder.addRadioButtonMenuItem(
102: menu_lookandfeel, name, lookandfeel_action);
103: menu_item.putClientProperty(
104: "LOOKANDFEEL_CLASSNAME", classname);
105: if (current_lookandfeel.getName().equals(name)) {
106: menu_item.setSelected(true);
107: }
108: lookandfeel_group.add(menu_item);
109: }
110: } catch (Throwable e) {
111: (new JDialogSystemError(MainFrame.this ,
112: "MainFrame.populateMenuBar() : Error while determining the look & feel to '"
113: + classname
114: + "' : "
115: + ExceptionUtils
116: .getExceptionStackTrace(e)))
117: .setVisible(true);
118: }
119: }
120:
121: JMenu menu_tools = menu_builder.addMenu(menuBar, Localization
122: .getString("rife.menu.tools"), Localization
123: .getChar("rife.menu.tools.mnemonic"));
124: JMenu menu_beanshell = menu_builder.addMenu(menu_tools,
125: Localization.getString("rife.menu.tools.beanshell"),
126: Localization
127: .getChar("rife.menu.tools.beanshell.mnemonic"));
128: menu_builder
129: .addMenuItem(
130: menu_beanshell,
131: Localization
132: .getString("rife.menu.tools.beanshell.opendesktop"),
133: new OpenBeanshellDesktop(),
134: Localization
135: .getChar("rife.menu.tools.beanshell.opendesktop.mnemonic"),
136: KeyStroke.getKeyStroke(KeyEvent.VK_B,
137: InputEvent.SHIFT_MASK
138: | InputEvent.CTRL_MASK, false));
139:
140: menuBar.add(Box.createHorizontalGlue());
141:
142: JMenu menu_help = menu_builder.addMenu(menuBar, Localization
143: .getString("rife.menu.help"), Localization
144: .getChar("rife.menu.help.mnemonic"));
145: menu_builder
146: .addMenuItem(
147: menu_help,
148: Localization
149: .getString("rife.menu.help.context"),
150: null,
151: Localization
152: .getChar("rife.menu.help.context.mnemonic"),
153: KeyStroke
154: .getKeyStroke(KeyEvent.VK_F1, 0, false));
155: menu_builder.addMenuItem(menu_help, Localization
156: .getString("rife.menu.help.about"), null, Localization
157: .getChar("rife.menu.help.about.mnemonic"));
158: }
159:
160: public void windowActivated(WindowEvent e) {
161: }
162:
163: public void windowDeactivated(WindowEvent e) {
164: }
165:
166: public void windowOpened(WindowEvent e) {
167: }
168:
169: public void windowClosing(WindowEvent e) {
170: e.getWindow().dispose();
171: }
172:
173: public void windowClosed(WindowEvent e) {
174: Rife.quit();
175: }
176:
177: public void windowIconified(WindowEvent e) {
178: }
179:
180: public void windowDeiconified(WindowEvent e) {
181: }
182:
183: public void actionPerformed(ActionEvent e) {
184: }
185:
186: protected class Exit implements DynamicMenuAction {
187: public void execute(JMenuItem menuItem) {
188: Rife.quit();
189: }
190: }
191:
192: protected class ChangeLookAndFeel implements DynamicMenuAction {
193: public void execute(JMenuItem menuItem) {
194: String classname = (String) menuItem
195: .getClientProperty("LOOKANDFEEL_CLASSNAME");
196: Rife.setLookAndFeel(classname);
197: }
198: }
199:
200: protected class OpenBeanshellDesktop implements DynamicMenuAction {
201: public void execute(JMenuItem menuItem) {
202: (new RealExecute()).start();
203: }
204:
205: private class RealExecute extends Thread {
206: public void run() {
207: String beanshell_desktop_path = "/scripts/rife_desktop.bsh";
208: InputStream input_stream = null;
209: InputStreamReader input_stream_reader = null;
210: BufferedReader buffered_reader = null;
211:
212: input_stream = this .getClass().getResourceAsStream(
213: beanshell_desktop_path);
214:
215: if (input_stream != null) {
216: try {
217: input_stream_reader = new InputStreamReader(
218: input_stream, "ISO8859_1");
219:
220: if (input_stream_reader != null) {
221: buffered_reader = new BufferedReader(
222: input_stream_reader);
223:
224: if (buffered_reader != null) {
225: try {
226: new Interpreter()
227: .eval(buffered_reader);
228: return;
229: } catch (EvalError e) {
230: (new JDialogSystemError(
231: MainFrame.this ,
232: "MainFrame.OpenBeanshellDesktop.RealExecute.run() : Error while evaluating the beanshell desktop script : "
233: + ExceptionUtils
234: .getExceptionStackTrace(e)))
235: .setVisible(true);
236: return;
237: }
238: }
239: } else {
240: (new JDialogSystemError(
241: MainFrame.this ,
242: "MainFrame.OpenBeanshellDesktop.RealExecute.run() : Couldn't create the buffered reader for the beanshell desktop script resource at '"
243: + beanshell_desktop_path
244: + "'.")).setVisible(true);
245: return;
246: }
247: } catch (UnsupportedEncodingException e) {
248: (new JDialogSystemError(
249: MainFrame.this ,
250: "MainFrame.OpenBeanshellDesktop.RealExecute.run() : Error while creating the inputstream reader for the beanshell desktop script resource at '"
251: + beanshell_desktop_path
252: + "' : "
253: + ExceptionUtils
254: .getExceptionStackTrace(e)))
255: .setVisible(true);
256: return;
257: }
258: } else {
259: (new JDialogSystemError(
260: MainFrame.this ,
261: "MainFrame.OpenBeanshellDesktop.RealExecute.run() : Couldn't open the beanshell desktop script resource at '"
262: + beanshell_desktop_path + "'."))
263: .setVisible(true);
264: return;
265: }
266: (new JDialogSystemError(
267: MainFrame.this ,
268: "MainFrame.OpenBeanshellDesktop.RealExecute.run() : Couldn't open the beanshell desktop."))
269: .setVisible(true);
270: return;
271: }
272: }
273: }
274: }
|