KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > environment > gui > component > CommandLineWrapperFrame


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.environment.gui.component.CommandLineWrapperFrame
4  * Version: snapshot-beautyj-1.1
5  *
6  * Date: 2004-09-29
7  *
8  * This is a snapshot version of the AMODA 0.2 development branch,
9  * it is not released as a seperate version.
10  * For AMODA, see http://amoda.berlios.de/.
11  *
12  * This is licensed under the GNU Lesser General Public License (LGPL)
13  * and comes with NO WARRANTY.
14  *
15  * Author: Jens Gulden
16  * Email: amoda@jensgulden.de
17  */

18
19 package de.gulden.framework.amoda.environment.gui.component;
20
21 import de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment;
22 import de.gulden.framework.amoda.model.core.*;
23 import de.gulden.framework.amoda.model.interaction.*;
24 import de.gulden.framework.amoda.model.behaviour.event.*;
25 import de.gulden.framework.amoda.model.option.Option;
26 import de.gulden.framework.amoda.generic.core.*;
27 import de.gulden.framework.amoda.generic.interaction.GenericDialog;
28 import de.gulden.framework.amoda.generic.option.GenericOptions;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.*;
32 import java.util.*;
33
34 /**
35  * Class CommandLineWrapperFrame.
36  *
37  * @author Jens Gulden
38  * @version snapshot-beautyj-1.1
39  */

40 public class CommandLineWrapperFrame extends GUIFrameAbstract implements ApplicationListener, MessagePerformer, ActionListener {
41
42     // ------------------------------------------------------------------------
43
// --- fields ---
44
// ------------------------------------------------------------------------
45

46     protected JComponent extendedPanel;
47
48     protected boolean extendedOpen;
49
50     protected JButton extendedButton;
51
52     protected String JavaDoc extendedLabelOpen;
53
54     protected String JavaDoc extendedLabelClosed;
55
56     protected boolean guiMessages;
57
58     private JPanel basePanel;
59
60     private JPanel buttonExtendPanel;
61
62     private JPanel buttonPanel;
63
64     private JPanel buttonsStartStopCloseOkPanel;
65
66     private JMenu fileMenu;
67
68     private JMenuBar menuBar;
69
70     private JPanel menuPanel;
71
72     private JEditorPane outputEditorPane;
73
74     private JPanel outputPanel;
75
76     private JScrollPane outputScrollPane;
77
78     private JPanel padPanel;
79
80
81     // ------------------------------------------------------------------------
82
// --- constructor ---
83
// ------------------------------------------------------------------------
84

85     public CommandLineWrapperFrame(GUIApplicationEnvironment environment) {
86         super(environment);
87         environment.setFrame(this);
88         GenericApplication application = environment.getGenericApplication();
89         initComponents();
90         application.addApplicationListener(this);
91
92         FeatureGroup fg = application.getFeatureGroup("commandline-wrapper");
93         Feature extended = fg.getFeature("extend");
94         Feature options = fg.getFeature("options");
95         Feature about = fg.getFeature("about");
96         Feature help = fg.getFeature("help");
97         Feature start = fg.getFeature("start");
98         Feature stop = fg.getFeature("stop");
99         Feature close = fg.getFeature("close");
100         Feature ok = fg.getFeature("ok");
101
102         this.extendedOpen = application.getOptions().getBoolean("extended-on-open");
103         this.guiMessages = application.getOptions().getBoolean("gui-messages");
104
105         if (extended.isEnabled()) {
106             this.extendedLabelOpen = extended.getOptions().getString("label-open");
107             this.extendedLabelClosed = extended.getOptions().getString("label-closed");
108             this.extendedButton = environment.createButtonFromFeature(extended);
109             updateExtendedButtonLabel();
110             buttonExtendPanel.add(this.extendedButton);
111         } else if (options.isEnabled()) {
112             buttonExtendPanel.add(environment.createButtonFromFeature(options));
113         }
114
115         Dimension s = buttonExtendPanel.getPreferredSize();
116         padPanel.setPreferredSize(s);
117
118         // menu bar
119

120         fileMenu.setFont(menuBar.getFont());
121
122         if (options.isEnabled()) {
123             JMenu optionsMenu = new JMenu("Edit");
124             optionsMenu.setFont(menuBar.getFont());
125             optionsMenu.add(this.environment.createMenuItemFromFeature(options));
126             menuBar.add(optionsMenu);
127         }
128
129         if (about.isEnabled()||help.isEnabled()) {
130             JMenu helpMenu = new JMenu("Help");
131             helpMenu.setFont(menuBar.getFont());
132             if (about.isEnabled()) {
133                 helpMenu.add(this.environment.createMenuItemFromFeature(about));
134             }
135             if (help.isEnabled()) {
136                 helpMenu.add(this.environment.createMenuItemFromFeature(help));
137             }
138             menuBar.add(helpMenu);
139         }
140
141
142         /*if (about.isEnabled()) {
143             buttonsAboutHelpPanel.add(environment.createButtonFromFeature(about));
144         }
145         if (help.isEnabled()) {
146             buttonsAboutHelpPanel.add(environment.createButtonFromFeature(help));
147         }*/

148
149         for (Iterator it = application.getFeatures().iterator(); it.hasNext(); ) {
150             GenericFeature f = (GenericFeature)it.next();
151             if ((!(f instanceof FeatureGroup))&&(!(f instanceof InteractionMember))&&(!f.isSystem())&&f.isEnabled()) {
152                 JButton button = environment.createButtonFromFeature(f);
153                 JMenuItem menuitem = environment.createMenuItemFromFeature(f);
154                 addUpdaterInFront(button);
155                 addUpdaterInFront(menuitem);
156                 buttonsStartStopCloseOkPanel.add(button);
157                 if (f.getId().equals("exit")) {
158                     fileMenu.addSeparator();
159                 } else if (f.getId().equals("default")) {
160                             button.requestFocus();
161                             getRootPane().setDefaultButton(button);
162                 }
163                 fileMenu.add(menuitem);
164             }
165         }
166
167
168         /*
169         if (start.isEnabled()) {
170             buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(start));
171         }
172         if (stop.isEnabled()) {
173             buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(stop));
174         }
175         if (close.isEnabled()) {
176             buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(close));
177         } else if (ok.isEnabled()) {
178             buttonsStartStopCloseOkPanel.add(environment.createButtonFromFeature(ok));
179         }*/

180
181
182         updateExtendedPanel();
183     }
184
185
186     // ------------------------------------------------------------------------
187
// --- methods ---
188
// ------------------------------------------------------------------------
189

190     public void setSize(int width, int height) {
191         if (isExtended()) {
192             height += extendedPanel.getPreferredSize().height;
193         }
194         super.setSize(width, height);
195     }
196
197     public void setExtended(boolean open) {
198         this.extendedOpen = open;
199         updateExtendedPanel();
200     }
201
202     public boolean isExtended() {
203         return this.extendedOpen;
204     }
205
206     public void doMessage(Message message) {
207         if ( (!guiMessages) || (message instanceof LogMessage) || (((GenericFeature)message).isSystem()) ) {
208             try {
209             int offset = outputEditorPane.getDocument().getEndPosition().getOffset()-1;
210                 String JavaDoc txt = message.getText();
211         if (!txt.endsWith("\n")) {
212           txt += "\n";
213         }
214                 if (offset == 0) {
215           txt = txt + "\n"; // (double line-break after first message, assuming first msg is welcome msg)
216
}
217         outputEditorPane.getDocument().insertString(offset, txt, null);
218                 int endOffset = outputEditorPane.getDocument().getEndPosition().getOffset();
219                 outputEditorPane.getCaret().setDot(endOffset);
220                 outputScrollPane.updateUI();
221          } catch (Exception JavaDoc e) {
222         this.environment.getGenericApplication().error(e);
223          }
224         } else { // normal GUIApplicationEnvironent-style
225
this.environment.internalDoMessage(message);
226         }
227     }
228
229     public void moduleLoaded(ApplicationEvent event) {
230         updateExtendedPanel(); // rebuild option panel
231
}
232
233     public void pluginInstalled(ApplicationEvent event) {
234         // nop
235
}
236
237     public void pluginUninstalled(ApplicationEvent event) {
238         // nop
239
}
240
241     /**
242      *
243      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
244      */

245     public void actionPerformed(ActionEvent e) {
246         ((GenericOptions)environment.getGenericApplication().getOptions()).shiftAllValues(Option.STATE_EDIT, Option.STATE_CURRENT);
247     }
248
249     protected void updateExtendedPanel() {
250         updateExtendedButtonLabel();
251         Dimension currentSize = this.getSize();
252         int heightWithoutExtended = currentSize.height;
253         if (this.extendedPanel != null) {
254             Dimension size = extendedPanel.getSize();
255             heightWithoutExtended -= size.height;
256             this.menuPanel.remove(this.extendedPanel);
257         }
258         if (this.extendedOpen) { // open
259
this.extendedPanel = this.environment.createOptionsEditor((GenericOptions)this.environment.getGenericApplication().getOptions());
260             GridBagConstraints gc = new GridBagConstraints();
261             gc.gridwidth = GridBagConstraints.REMAINDER;
262             gc.weightx = 1.0;
263             gc.fill = GridBagConstraints.BOTH;
264             this.menuPanel.add(this.extendedPanel, gc);
265             Dimension preferredSize = extendedPanel.getPreferredSize();
266             super.setSize(currentSize.width, heightWithoutExtended + preferredSize.height);
267         } else { // close
268
this.extendedPanel = null;
269             super.setSize(currentSize.width, heightWithoutExtended);
270         }
271         this.validate();
272     }
273
274     protected void updateExtendedButtonLabel() {
275         if (this.extendedButton!=null) {
276             this.extendedButton.setText(this.extendedOpen ? this.extendedLabelOpen : this.extendedLabelClosed);
277             this.extendedButton.validate();
278         }
279     }
280
281     private void addUpdaterInFront(AbstractButton b) {
282         /*
283         ActionListener[] l = b.getActionListeners();
284         // remove all
285         for (int i=0; i<l.length; i++) {
286             b.removeActionListener(l[i]);
287         }
288         // add special as first
289         b.addActionListener(this);
290         // add previous ones again
291         for (int i=0; i<l.length; i++) {
292             b.addActionListener(l[i]);
293         }
294         */

295         // adding at end will cause execution as first
296
b.addActionListener(this);
297     }
298
299     private void initComponents() {
300         //GEN-BEGIN:initComponents
301
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
302
303                 basePanel = new javax.swing.JPanel JavaDoc();
304                 outputPanel = new javax.swing.JPanel JavaDoc();
305                 outputScrollPane = new javax.swing.JScrollPane JavaDoc();
306                 outputEditorPane = new javax.swing.JEditorPane JavaDoc();
307                 menuPanel = new javax.swing.JPanel JavaDoc();
308                 buttonPanel = new javax.swing.JPanel JavaDoc();
309                 padPanel = new javax.swing.JPanel JavaDoc();
310                 buttonsStartStopCloseOkPanel = new javax.swing.JPanel JavaDoc();
311                 buttonExtendPanel = new javax.swing.JPanel JavaDoc();
312                 menuBar = new javax.swing.JMenuBar JavaDoc();
313                 fileMenu = new javax.swing.JMenu JavaDoc();
314
315                 addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
316                     public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
317                         exitForm(evt);
318                     }
319                 });
320
321                 basePanel.setLayout(new java.awt.BorderLayout JavaDoc(0, 3));
322
323                 outputPanel.setLayout(new java.awt.BorderLayout JavaDoc());
324
325                 outputEditorPane.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
326                 outputEditorPane.setFont(new java.awt.Font JavaDoc("DialogInput", 0, 12));
327                 outputScrollPane.setViewportView(outputEditorPane);
328
329                 outputPanel.add(outputScrollPane, java.awt.BorderLayout.CENTER);
330
331                 basePanel.add(outputPanel, java.awt.BorderLayout.CENTER);
332
333                 menuPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
334
335                 buttonPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
336
337                 padPanel.setLayout(null);
338
339                 buttonPanel.add(padPanel, new java.awt.GridBagConstraints JavaDoc());
340
341                 gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
342                 gridBagConstraints.weightx = 1.0;
343                 buttonPanel.add(buttonsStartStopCloseOkPanel, gridBagConstraints);
344
345                 buttonPanel.add(buttonExtendPanel, new java.awt.GridBagConstraints JavaDoc());
346
347                 gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
348                 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
349                 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
350                 gridBagConstraints.weightx = 1.0;
351                 menuPanel.add(buttonPanel, gridBagConstraints);
352
353                 basePanel.add(menuPanel, java.awt.BorderLayout.SOUTH);
354
355                 getContentPane().add(basePanel, java.awt.BorderLayout.CENTER);
356
357                 fileMenu.setText("File");
358                 menuBar.add(fileMenu);
359
360                 setJMenuBar(menuBar);
361
362                 pack();
363     }
364
365     private void exitForm(WindowEvent evt) {
366         //GEN-FIRST:event_exitForm
367
environment.getExitCommand().perform();
368     }
369
370 } // end CommandLineWrapperFrame
371
Popular Tags