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 3669 2007-02-26 13:51:23Z gbevin $
007: */
008: package com.uwyn.rife.gui.ui;
009:
010: import java.awt.BorderLayout;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import java.awt.event.WindowEvent;
014: import java.awt.event.WindowListener;
015:
016: import javax.swing.*;
017:
018: import com.uwyn.rife.gui.Rife;
019: import com.uwyn.rife.gui.model.ElementModel;
020: import com.uwyn.rife.gui.model.SiteModel;
021: import com.uwyn.rife.gui.model.exceptions.GuiModelException;
022: import com.uwyn.rife.gui.ui.actions.ExitAction;
023: import com.uwyn.rife.gui.ui.actions.OpenBeanshellAction;
024: import com.uwyn.rife.gui.ui.commands.ChangeLookAndFeel;
025: import com.uwyn.rife.swing.JAction;
026: import com.uwyn.rife.swing.JDialogSystemError;
027: import com.uwyn.rife.swing.JMenuBuilder;
028: import com.uwyn.rife.tools.ExceptionUtils;
029: import com.uwyn.rife.tools.Localization;
030:
031: public class MainFrame extends JFrame implements WindowListener,
032: ActionListener {
033: private EditorPane mEditorPane = null;
034:
035: public MainFrame() {
036: super (Localization.getString("rife.application.title"));
037: this .addWindowListener(this );
038:
039: this .createMenuBar();
040: this .getContentPane().setLayout(new BorderLayout());
041:
042: SiteModel site = new SiteModel();
043: ElementModel element = null;
044: for (int j = 0; j < 3; j++) {
045: try {
046: element = new ElementModel("element " + j);
047: site.addElement(element);
048: } catch (GuiModelException e) {
049: }
050:
051: int exits = (int) (Math.random() * 3);
052: int consumeds = (int) (Math.random() * 3);
053: int useds = (int) (Math.random() * 3);
054: try {
055: for (int i = 0; i < exits; i++) {
056: element.addExit("exit" + i);
057: }
058: for (int i = 0; i < consumeds; i++) {
059: element.addInput("input" + i);
060: }
061: for (int i = 0; i < useds; i++) {
062: element.addOutput("output" + i);
063: }
064: } catch (GuiModelException e) {
065: }
066: // Color body_color = new Color(155+(int)(Math.random()*100), 155+(int)(Math.random()*100), 155+(int)(Math.random()*100));
067: // element.setElementColor(body_color);
068: // element.addElementListener(this);
069: // mElements.add(element);
070:
071: // this.add(element);
072: // element.setBounds((int)(Math.random()*800), (int)(Math.random()*600), element.getWidth(), element.getHeight());
073:
074: // calculateDimension();
075: }
076:
077: System.out.println(site.toString());
078:
079: // addKeyListener(mStructureEditor.getStructurePanel());
080:
081: this .setEditorPane(new SiteEditorPane(site));
082: this .pack();
083: this .setSize(800, 600);
084: }
085:
086: public EditorPane getEditorPane() {
087: return this .mEditorPane;
088: }
089:
090: protected void setEditorPane(EditorPane pane) {
091: this .mEditorPane = pane;
092: this .mEditorPane.constructLayout();
093: this .getContentPane()
094: .add(this .mEditorPane, BorderLayout.CENTER);
095: }
096:
097: public void updateMenuBar() {
098: JMenuBar menu_bar = getJMenuBar();
099: menu_bar.removeAll();
100: this .populateMenuBar(menu_bar);
101: menu_bar.revalidate();
102: menu_bar.repaint();
103: }
104:
105: private void createMenuBar() {
106: JMenuBar menu_bar = new JMenuBar();
107: this .populateMenuBar(menu_bar);
108: this .setJMenuBar(menu_bar);
109: }
110:
111: private void populateMenuBar(JMenuBar menuBar) {
112: // build the file menu
113: JMenu menu_file = JMenuBuilder.addMenu(menuBar, Localization
114: .getString("rife.menu.file"), Localization
115: .getChar("rife.menu.file.mnemonic"));
116: JMenuBuilder.addMenuItem(menu_file, new ExitAction());
117:
118: // build the view menu
119: JMenu menu_view = JMenuBuilder.addMenu(menuBar, Localization
120: .getString("rife.menu.view"), Localization
121: .getChar("rife.menu.view.mnemonic"));
122: JMenu menu_lookandfeel = JMenuBuilder
123: .addMenu(
124: menu_view,
125: Localization
126: .getString("rife.menu.view.lookandfeel"),
127: Localization
128: .getChar("rife.menu.view.lookandfeel.mnemonic"));
129:
130: // build the look&feel menu
131: ButtonGroup lookandfeel_group = new ButtonGroup();
132: LookAndFeel current_lookandfeel = UIManager.getLookAndFeel();
133: UIManager.LookAndFeelInfo[] look_and_feels = UIManager
134: .getInstalledLookAndFeels();
135: JRadioButtonMenuItem menu_item = null;
136: String name = null;
137: String classname = null;
138: Class<LookAndFeel> testclass = null;
139: LookAndFeel lookandfeel = null;
140: for (int counter = 0; counter < look_and_feels.length; counter++) {
141: name = look_and_feels[counter].getName();
142: classname = look_and_feels[counter].getClassName();
143: try {
144: testclass = (Class<LookAndFeel>) Class
145: .forName(classname);
146: lookandfeel = testclass.newInstance();
147: if (lookandfeel.isSupportedLookAndFeel()) {
148: menu_item = JMenuBuilder.addRadioButtonMenuItem(
149: menu_lookandfeel, new JAction(
150: new ChangeLookAndFeel(classname),
151: name));
152: if (current_lookandfeel.getName().equals(name)) {
153: menu_item.setSelected(true);
154: }
155: lookandfeel_group.add(menu_item);
156: }
157: } catch (Throwable e) {
158: (new JDialogSystemError(MainFrame.this ,
159: "MainFrame.populateMenuBar() : Error while determining the look & feel to '"
160: + classname
161: + "' : "
162: + ExceptionUtils
163: .getExceptionStackTrace(e)))
164: .setVisible(true);
165: }
166: }
167:
168: // build the tools menu
169: JMenu menu_tools = JMenuBuilder.addMenu(menuBar, Localization
170: .getString("rife.menu.tools"), Localization
171: .getChar("rife.menu.tools.mnemonic"));
172: JMenu menu_beanshell = JMenuBuilder.addMenu(menu_tools,
173: Localization.getString("rife.menu.tools.beanshell"),
174: Localization
175: .getChar("rife.menu.tools.beanshell.mnemonic"));
176: JMenuBuilder.addMenuItem(menu_beanshell,
177: new OpenBeanshellAction());
178:
179: // insert a horizontal glue to push the following menu items to the right
180: menuBar.add(Box.createHorizontalGlue());
181:
182: // build the help menu
183: JMenuBuilder.addMenu(menuBar, Localization
184: .getString("rife.menu.help"), Localization
185: .getChar("rife.menu.help.mnemonic"));
186: /* JMenuBuilder.addMenuItem(menu_help, Localization.getString("rife.menu.help.context"),
187: null, Localization.getChar("rife.menu.help.context.mnemonic"),
188: KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false));
189: JMenuBuilder.addMenuItem(menu_help, Localization.getString("rife.menu.help.about"),
190: null, Localization.getChar("rife.menu.help.about.mnemonic"));*/
191: }
192:
193: public void windowActivated(WindowEvent e) {
194: }
195:
196: public void windowDeactivated(WindowEvent e) {
197: }
198:
199: public void windowOpened(WindowEvent e) {
200: }
201:
202: public void windowClosing(WindowEvent e) {
203: e.getWindow().dispose();
204: }
205:
206: public void windowClosed(WindowEvent e) {
207: Rife.quit();
208: }
209:
210: public void windowIconified(WindowEvent e) {
211: }
212:
213: public void windowDeiconified(WindowEvent e) {
214: }
215:
216: public void actionPerformed(ActionEvent e) {
217: }
218: }
|