001: /*
002: * Project: AMODA - Abstract Modeled Application
003: * Class: de.gulden.framework.amoda.environment.gui.component.CommandLineWrapperFrame
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the AMODA 0.2 development branch,
009: * it is not released as a seperate version.
010: * For AMODA, see http://amoda.berlios.de/.
011: *
012: * This is licensed under the GNU Lesser General Public License (LGPL)
013: * and comes with NO WARRANTY.
014: *
015: * Author: Jens Gulden
016: * Email: amoda@jensgulden.de
017: */
018:
019: package de.gulden.framework.amoda.environment.gui.component;
020:
021: import de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment;
022: import de.gulden.framework.amoda.model.core.*;
023: import de.gulden.framework.amoda.model.interaction.*;
024: import de.gulden.framework.amoda.model.behaviour.event.*;
025: import de.gulden.framework.amoda.model.option.Option;
026: import de.gulden.framework.amoda.generic.core.*;
027: import de.gulden.framework.amoda.generic.interaction.GenericDialog;
028: import de.gulden.framework.amoda.generic.option.GenericOptions;
029: import java.awt.*;
030: import java.awt.event.*;
031: import javax.swing.*;
032: import java.util.*;
033:
034: /**
035: * Class CommandLineWrapperFrame.
036: *
037: * @author Jens Gulden
038: * @version snapshot-beautyj-1.1
039: */
040: public class CommandLineWrapperFrame extends GUIFrameAbstract implements
041: ApplicationListener, MessagePerformer, ActionListener {
042:
043: // ------------------------------------------------------------------------
044: // --- fields ---
045: // ------------------------------------------------------------------------
046:
047: protected JComponent extendedPanel;
048:
049: protected boolean extendedOpen;
050:
051: protected JButton extendedButton;
052:
053: protected String extendedLabelOpen;
054:
055: protected String extendedLabelClosed;
056:
057: protected boolean guiMessages;
058:
059: private JPanel basePanel;
060:
061: private JPanel buttonExtendPanel;
062:
063: private JPanel buttonPanel;
064:
065: private JPanel buttonsStartStopCloseOkPanel;
066:
067: private JMenu fileMenu;
068:
069: private JMenuBar menuBar;
070:
071: private JPanel menuPanel;
072:
073: private JEditorPane outputEditorPane;
074:
075: private JPanel outputPanel;
076:
077: private JScrollPane outputScrollPane;
078:
079: private JPanel padPanel;
080:
081: // ------------------------------------------------------------------------
082: // --- constructor ---
083: // ------------------------------------------------------------------------
084:
085: public CommandLineWrapperFrame(GUIApplicationEnvironment environment) {
086: super (environment);
087: environment.setFrame(this );
088: GenericApplication application = environment
089: .getGenericApplication();
090: initComponents();
091: application.addApplicationListener(this );
092:
093: FeatureGroup fg = application
094: .getFeatureGroup("commandline-wrapper");
095: Feature extended = fg.getFeature("extend");
096: Feature options = fg.getFeature("options");
097: Feature about = fg.getFeature("about");
098: Feature help = fg.getFeature("help");
099: Feature start = fg.getFeature("start");
100: Feature stop = fg.getFeature("stop");
101: Feature close = fg.getFeature("close");
102: Feature ok = fg.getFeature("ok");
103:
104: this .extendedOpen = application.getOptions().getBoolean(
105: "extended-on-open");
106: this .guiMessages = application.getOptions().getBoolean(
107: "gui-messages");
108:
109: if (extended.isEnabled()) {
110: this .extendedLabelOpen = extended.getOptions().getString(
111: "label-open");
112: this .extendedLabelClosed = extended.getOptions().getString(
113: "label-closed");
114: this .extendedButton = environment
115: .createButtonFromFeature(extended);
116: updateExtendedButtonLabel();
117: buttonExtendPanel.add(this .extendedButton);
118: } else if (options.isEnabled()) {
119: buttonExtendPanel.add(environment
120: .createButtonFromFeature(options));
121: }
122:
123: Dimension s = buttonExtendPanel.getPreferredSize();
124: padPanel.setPreferredSize(s);
125:
126: // menu bar
127:
128: fileMenu.setFont(menuBar.getFont());
129:
130: if (options.isEnabled()) {
131: JMenu optionsMenu = new JMenu("Edit");
132: optionsMenu.setFont(menuBar.getFont());
133: optionsMenu.add(this .environment
134: .createMenuItemFromFeature(options));
135: menuBar.add(optionsMenu);
136: }
137:
138: if (about.isEnabled() || help.isEnabled()) {
139: JMenu helpMenu = new JMenu("Help");
140: helpMenu.setFont(menuBar.getFont());
141: if (about.isEnabled()) {
142: helpMenu.add(this .environment
143: .createMenuItemFromFeature(about));
144: }
145: if (help.isEnabled()) {
146: helpMenu.add(this .environment
147: .createMenuItemFromFeature(help));
148: }
149: menuBar.add(helpMenu);
150: }
151:
152: /*if (about.isEnabled()) {
153: buttonsAboutHelpPanel.add(environment.createButtonFromFeature(about));
154: }
155: if (help.isEnabled()) {
156: buttonsAboutHelpPanel.add(environment.createButtonFromFeature(help));
157: }*/
158:
159: for (Iterator it = application.getFeatures().iterator(); it
160: .hasNext();) {
161: GenericFeature f = (GenericFeature) it.next();
162: if ((!(f instanceof FeatureGroup))
163: && (!(f instanceof InteractionMember))
164: && (!f.isSystem()) && f.isEnabled()) {
165: JButton button = environment.createButtonFromFeature(f);
166: JMenuItem menuitem = environment
167: .createMenuItemFromFeature(f);
168: addUpdaterInFront(button);
169: addUpdaterInFront(menuitem);
170: buttonsStartStopCloseOkPanel.add(button);
171: if (f.getId().equals("exit")) {
172: fileMenu.addSeparator();
173: } else if (f.getId().equals("default")) {
174: button.requestFocus();
175: getRootPane().setDefaultButton(button);
176: }
177: fileMenu.add(menuitem);
178: }
179: }
180:
181: /*
182: if (start.isEnabled()) {
183: buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(start));
184: }
185: if (stop.isEnabled()) {
186: buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(stop));
187: }
188: if (close.isEnabled()) {
189: buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(close));
190: } else if (ok.isEnabled()) {
191: buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(ok));
192: }*/
193:
194: updateExtendedPanel();
195: }
196:
197: // ------------------------------------------------------------------------
198: // --- methods ---
199: // ------------------------------------------------------------------------
200:
201: public void setSize(int width, int height) {
202: if (isExtended()) {
203: height += extendedPanel.getPreferredSize().height;
204: }
205: super .setSize(width, height);
206: }
207:
208: public void setExtended(boolean open) {
209: this .extendedOpen = open;
210: updateExtendedPanel();
211: }
212:
213: public boolean isExtended() {
214: return this .extendedOpen;
215: }
216:
217: public void doMessage(Message message) {
218: if ((!guiMessages) || (message instanceof LogMessage)
219: || (((GenericFeature) message).isSystem())) {
220: try {
221: int offset = outputEditorPane.getDocument()
222: .getEndPosition().getOffset() - 1;
223: String txt = message.getText();
224: if (!txt.endsWith("\n")) {
225: txt += "\n";
226: }
227: if (offset == 0) {
228: txt = txt + "\n"; // (double line-break after first message, assuming first msg is welcome msg)
229: }
230: outputEditorPane.getDocument().insertString(offset,
231: txt, null);
232: int endOffset = outputEditorPane.getDocument()
233: .getEndPosition().getOffset();
234: outputEditorPane.getCaret().setDot(endOffset);
235: outputScrollPane.updateUI();
236: } catch (Exception e) {
237: this .environment.getGenericApplication().error(e);
238: }
239: } else { // normal GUIApplicationEnvironent-style
240: this .environment.internalDoMessage(message);
241: }
242: }
243:
244: public void moduleLoaded(ApplicationEvent event) {
245: updateExtendedPanel(); // rebuild option panel
246: }
247:
248: public void pluginInstalled(ApplicationEvent event) {
249: // nop
250: }
251:
252: public void pluginUninstalled(ApplicationEvent event) {
253: // nop
254: }
255:
256: /**
257: *
258: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
259: */
260: public void actionPerformed(ActionEvent e) {
261: ((GenericOptions) environment.getGenericApplication()
262: .getOptions()).shiftAllValues(Option.STATE_EDIT,
263: Option.STATE_CURRENT);
264: }
265:
266: protected void updateExtendedPanel() {
267: updateExtendedButtonLabel();
268: Dimension currentSize = this .getSize();
269: int heightWithoutExtended = currentSize.height;
270: if (this .extendedPanel != null) {
271: Dimension size = extendedPanel.getSize();
272: heightWithoutExtended -= size.height;
273: this .menuPanel.remove(this .extendedPanel);
274: }
275: if (this .extendedOpen) { // open
276: this .extendedPanel = this .environment
277: .createOptionsEditor((GenericOptions) this .environment
278: .getGenericApplication().getOptions());
279: GridBagConstraints gc = new GridBagConstraints();
280: gc.gridwidth = GridBagConstraints.REMAINDER;
281: gc.weightx = 1.0;
282: gc.fill = GridBagConstraints.BOTH;
283: this .menuPanel.add(this .extendedPanel, gc);
284: Dimension preferredSize = extendedPanel.getPreferredSize();
285: super .setSize(currentSize.width, heightWithoutExtended
286: + preferredSize.height);
287: } else { // close
288: this .extendedPanel = null;
289: super .setSize(currentSize.width, heightWithoutExtended);
290: }
291: this .validate();
292: }
293:
294: protected void updateExtendedButtonLabel() {
295: if (this .extendedButton != null) {
296: this .extendedButton
297: .setText(this .extendedOpen ? this .extendedLabelOpen
298: : this .extendedLabelClosed);
299: this .extendedButton.validate();
300: }
301: }
302:
303: private void addUpdaterInFront(AbstractButton b) {
304: /*
305: ActionListener[] l = b.getActionListeners();
306: // remove all
307: for (int i=0; i<l.length; i++) {
308: b.removeActionListener(l[i]);
309: }
310: // add special as first
311: b.addActionListener(this);
312: // add previous ones again
313: for (int i=0; i<l.length; i++) {
314: b.addActionListener(l[i]);
315: }
316: */
317: // adding at end will cause execution as first
318: b.addActionListener(this );
319: }
320:
321: private void initComponents() {
322: //GEN-BEGIN:initComponents
323: java.awt.GridBagConstraints gridBagConstraints;
324:
325: basePanel = new javax.swing.JPanel();
326: outputPanel = new javax.swing.JPanel();
327: outputScrollPane = new javax.swing.JScrollPane();
328: outputEditorPane = new javax.swing.JEditorPane();
329: menuPanel = new javax.swing.JPanel();
330: buttonPanel = new javax.swing.JPanel();
331: padPanel = new javax.swing.JPanel();
332: buttonsStartStopCloseOkPanel = new javax.swing.JPanel();
333: buttonExtendPanel = new javax.swing.JPanel();
334: menuBar = new javax.swing.JMenuBar();
335: fileMenu = new javax.swing.JMenu();
336:
337: addWindowListener(new java.awt.event.WindowAdapter() {
338: public void windowClosing(java.awt.event.WindowEvent evt) {
339: exitForm(evt);
340: }
341: });
342:
343: basePanel.setLayout(new java.awt.BorderLayout(0, 3));
344:
345: outputPanel.setLayout(new java.awt.BorderLayout());
346:
347: outputEditorPane.setBackground(javax.swing.UIManager
348: .getDefaults().getColor("Button.background"));
349: outputEditorPane
350: .setFont(new java.awt.Font("DialogInput", 0, 12));
351: outputScrollPane.setViewportView(outputEditorPane);
352:
353: outputPanel.add(outputScrollPane, java.awt.BorderLayout.CENTER);
354:
355: basePanel.add(outputPanel, java.awt.BorderLayout.CENTER);
356:
357: menuPanel.setLayout(new java.awt.GridBagLayout());
358:
359: buttonPanel.setLayout(new java.awt.GridBagLayout());
360:
361: padPanel.setLayout(null);
362:
363: buttonPanel.add(padPanel, new java.awt.GridBagConstraints());
364:
365: gridBagConstraints = new java.awt.GridBagConstraints();
366: gridBagConstraints.weightx = 1.0;
367: buttonPanel.add(buttonsStartStopCloseOkPanel,
368: gridBagConstraints);
369:
370: buttonPanel.add(buttonExtendPanel,
371: new java.awt.GridBagConstraints());
372:
373: gridBagConstraints = new java.awt.GridBagConstraints();
374: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
375: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
376: gridBagConstraints.weightx = 1.0;
377: menuPanel.add(buttonPanel, gridBagConstraints);
378:
379: basePanel.add(menuPanel, java.awt.BorderLayout.SOUTH);
380:
381: getContentPane().add(basePanel, java.awt.BorderLayout.CENTER);
382:
383: fileMenu.setText("File");
384: menuBar.add(fileMenu);
385:
386: setJMenuBar(menuBar);
387:
388: pack();
389: }
390:
391: private void exitForm(WindowEvent evt) {
392: //GEN-FIRST:event_exitForm
393: environment.getExitCommand().perform();
394: }
395:
396: } // end CommandLineWrapperFrame
|