KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > gulden > framework > amoda > environment > gui > GUIApplicationEnvironment


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.environment.gui.GUIApplicationEnvironment
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;
20
21 import de.gulden.framework.amoda.environment.commandline.*;
22 import de.gulden.framework.amoda.environment.gui.component.DefaultButton;
23 import de.gulden.framework.amoda.generic.behaviour.*;
24 import de.gulden.framework.amoda.generic.core.*;
25 import de.gulden.framework.amoda.generic.interaction.*;
26 import de.gulden.framework.amoda.generic.option.*;
27 import de.gulden.framework.amoda.model.behaviour.*;
28 import de.gulden.framework.amoda.model.core.*;
29 import de.gulden.framework.amoda.model.core.WorkspaceProvider;
30 import de.gulden.framework.amoda.model.interaction.*;
31 import de.gulden.framework.amoda.model.interaction.Dialog;
32 import de.gulden.util.Toolbox;
33 import de.gulden.util.xml.XMLToolbox;
34 import java.awt.*;
35 import java.lang.*;
36 import java.util.*;
37 import javax.swing.*;
38
39 /**
40  * Class GUIApplicationEnvironment.
41  *
42  * @author Jens Gulden
43  * @version snapshot-beautyj-1.1
44  */

45 public class GUIApplicationEnvironment extends CommandLineApplicationEnvironment implements WorkspaceProvider, FontProvider {
46
47     // ------------------------------------------------------------------------
48
// --- fields ---
49
// ------------------------------------------------------------------------
50

51     public static String JavaDoc FONT_DEFAULT = "default";
52
53     public static String JavaDoc FONT_MENU = "menu";
54
55     public static String JavaDoc FONT_BUTTON = "button";
56
57     public static String JavaDoc FONT_LABEL = "label";
58
59     protected JFrame frame;
60
61     protected Hashtable fontCache = new Hashtable();
62
63     protected JMenu windowMenu = null;
64
65     protected JMenu fileMenu = null;
66
67     protected Command exitCommand;
68
69     protected Command openMostRecentFileCommand;
70
71     public static Dimension BUTTON_PREFERRED_SIZE = new Dimension(80,25);
72
73     public CommandLineApplicationEnvironment fallbackCommandLineApplicationEnvironment;
74
75
76     // ------------------------------------------------------------------------
77
// --- constructor ---
78
// ------------------------------------------------------------------------
79

80     public GUIApplicationEnvironment() {
81         // your code here
82
}
83
84
85     // ------------------------------------------------------------------------
86
// --- methods ---
87
// ------------------------------------------------------------------------
88

89     public CommandLineApplicationEnvironment getFallbackCommandLineApplicationEnvironment() {
90         return fallbackCommandLineApplicationEnvironment;
91     }
92
93     public void setFallbackCommandLineApplicationEnvironment(CommandLineApplicationEnvironment commandLineApplicationEnvironment) {
94         this.fallbackCommandLineApplicationEnvironment = commandLineApplicationEnvironment;
95     }
96
97     public void launch(Application application) {
98         if (!(application instanceof GUIApplication)) { // running a CommandLineApplication inside GUIApplicationEnvirnonment
99
de.gulden.framework.amoda.environment.commandline.CommandLineApplicationEnvironmentFactory f = new de.gulden.framework.amoda.environment.commandline.CommandLineApplicationEnvironmentFactory(getFactory().getArgs());
100             setFallbackCommandLineApplicationEnvironment((CommandLineApplicationEnvironment)f.createApplicationEnvironment());
101             // enable 'gui' option, so user can set
102
((GenericFeature)options.get("gui")).setSystem(false);
103             // hide main-gui-window related option-groups
104
((GenericFeature)options.get("environment")).setSystem(true);
105             ((GenericFeature)options.get("font")).setSystem(true);
106         } else { // normal GUIApplication
107
((GenericFeature)application.getFeature("default")).setSystem(true);
108         }
109         super.launch(application);
110     }
111
112     public void doMessage(Message message) {
113         GenericApplication application = getGenericApplication();
114         if ((application==null) || !application.isQuiet()) {
115             JFrame frame = getFrame();
116             if (frame instanceof de.gulden.framework.amoda.environment.gui.component.CommandLineWrapperFrame) {
117                 ((de.gulden.framework.amoda.environment.gui.component.CommandLineWrapperFrame)frame).doMessage(message);
118             } else {
119                 if ((frame!=null) && (!((de.gulden.framework.amoda.generic.core.GenericFeature)message).isSystem())) {
120                     if (message.getType()==Message.STATUS_MESSAGE) {
121                     String JavaDoc text=message.getText();
122                         if (!Toolbox.isEmpty(text)) {
123                             ((de.gulden.framework.amoda.environment.gui.component.GUIFrame)frame).setStatus(text);
124                         } else {
125                             ((de.gulden.framework.amoda.environment.gui.component.GUIFrame)frame).resetStatus();
126                         }
127                     } else {
128                         internalDoMessage(message);
129                     }
130                 } else { // marked as "system": fallback to command-line behaviour
131
getFallbackCommandLineApplicationEnvironment().doMessage(message);
132                 }
133             }
134
135         }
136     }
137
138     public void doQuestion(Question question) {
139         GenericQuestion q=(GenericQuestion)question;
140         JOptionPane p=internalCreateJOptionPane(q);
141         JDialog dialog=p.createDialog(getFrame(),q.getMetadata().get("title","Question"));
142         dialog.setVisible(true);
143         String JavaDoc answerString=(String JavaDoc)p.getValue();
144         if (answerString==null) {
145             q.setAnswer(Question.CANCEL); // option id "cancel"
146
return;
147         }
148         // find answer in answer options
149
for (Iterator it=q.getAnswerOptions().iterator();it.hasNext();) {
150             de.gulden.framework.amoda.model.option.Option o=(de.gulden.framework.amoda.model.option.Option)it.next();
151             if (answerString.equals(o.toString())) {
152                 q.setAnswer(o.getId());
153                 return;
154             }
155         }
156     }
157
158     public void doErrorMessage(ErrorMessage errorMessage) {
159         // pass to GUIFrame:
160
JFrame frame = getFrame();
161         if (frame instanceof de.gulden.framework.amoda.model.interaction.ErrorMessagePerformer) {
162         ((de.gulden.framework.amoda.model.interaction.ErrorMessagePerformer)frame).doErrorMessage(errorMessage); // GUIFrame implements ErrorMessagePerformer
163
} else { // fallback as ordinary message, might lead to console output if another fallback
164
doMessage(errorMessage);
165         }
166         if (errorMessage.exitApplication()) {
167         // System.exit(1); // **************** SAUBERER: Application.stop(), frame.destroy() ... ****!!!!
168
}
169     }
170
171     public void doDialog(Dialog dialog) {
172         GenericDialog d=(GenericDialog)dialog;
173         de.gulden.framework.amoda.generic.option.GenericOptions options=(de.gulden.framework.amoda.generic.option.GenericOptions)d.getOptions();
174         if (!d.isFileDialog()) {
175             JComponent optionsPanel=createOptionsEditor(options);
176             JDialog jdialog=internalCreateJDialog(d,optionsPanel,FlowLayout.CENTER);
177             jdialog.setVisible(true);
178             // ActionListeners on buttons will close dialog and set answer value
179
// now execute all <command>s attached to <dialog>
180
for (Iterator it=d.getAll(de.gulden.framework.amoda.model.behaviour.Command.class,false).values().iterator();it.hasNext();) {
181                 de.gulden.framework.amoda.generic.behaviour.GenericCommand c=(de.gulden.framework.amoda.generic.behaviour.GenericCommand)it.next();
182                 c.perform();
183             }
184         } else { // isFileDialog()==true
185
JFileChooser fc=new JFileChooser();
186                 /*if (isSave()) {
187                     title="Save";
188                 } else {
189                     title="Load";
190                 }*/

191             /*if (d.isDirectories()) {
192                 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
193             } else {
194                 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);//FILES_AND_DIRECTORIES);
195             }*/

196             fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
197
198             // set file filters
199
for (Iterator it=d.getOptions().getEntries().values().iterator();it.hasNext();) {
200                 de.gulden.framework.amoda.generic.option.GenericOptionEntry o=(de.gulden.framework.amoda.generic.option.GenericOptionEntry)it.next();
201                 Class JavaDoc optType=o.getType();
202                 if (o.getType()==String JavaDoc.class) {
203                     String JavaDoc suffix=o.getValue().getString();
204                     if (suffix.startsWith("*.")) {
205                         suffix=suffix.substring(2);
206                     }
207                     String JavaDoc description=o.findTitle();
208                     javax.swing.filechooser.FileFilter JavaDoc ff=new de.gulden.util.swing.SuffixFileFilter(suffix,description);
209                     fc.addChoosableFileFilter(ff);
210                 }
211             }
212
213             // find first file-typed option among options
214
de.gulden.framework.amoda.generic.option.GenericOptionEntry resultOption=null;
215             for (Iterator it=d.getOptions().getEntries().values().iterator();(resultOption==null)&&it.hasNext();) {
216                 de.gulden.framework.amoda.generic.option.GenericOptionEntry o=(de.gulden.framework.amoda.generic.option.GenericOptionEntry)it.next();
217                 Class JavaDoc optType=o.getType();
218                 if (optType==java.io.File JavaDoc.class) {
219                     resultOption=o;
220                     //// clear before new asking:
221
}
222             }
223
224             if (resultOption==null) { // create if doesn't exist yet
225
resultOption=new de.gulden.framework.amoda.generic.option.GenericOptionEntry();
226                 resultOption.setName("file");
227                 resultOption.setType(java.io.File JavaDoc.class);
228                 ((de.gulden.framework.amoda.generic.option.GenericOptions)d.getOptions()).add(resultOption);
229             }
230
231             java.io.File JavaDoc file=resultOption.getValue(de.gulden.framework.amoda.model.option.Option.STATE_CURRENT).getFile();
232             if (file!=null) {
233                 fc.setSelectedFile(file);
234             }
235
236             int answer;
237             if (d.isSave()) {
238                 answer=fc.showSaveDialog(getFrame());
239             } else {
240                 answer=fc.showOpenDialog(getFrame());
241             }
242
243             if (answer==JFileChooser.APPROVE_OPTION) {
244                 if (resultOption!=null) {
245                     java.io.File JavaDoc f=fc.getSelectedFile();
246                     if (!f.isDirectory()) { // if file, make sure the filename has the correct suffix
247
javax.swing.filechooser.FileFilter JavaDoc ff=fc.getFileFilter();
248                         if (ff instanceof de.gulden.util.swing.SuffixFileFilter) {
249                             de.gulden.util.swing.SuffixFileFilter sff=(de.gulden.util.swing.SuffixFileFilter)ff;
250                             String JavaDoc suffix=sff.getSuffix();
251                             if (!f.getName().endsWith("."+suffix)) {
252                                 f=new java.io.File JavaDoc(f.getAbsolutePath()+"."+suffix);
253                             }
254                         }
255                     }
256                     ((de.gulden.framework.amoda.generic.data.GenericValue)resultOption.getValue(de.gulden.framework.amoda.model.option.Option.STATE_CURRENT)).set(f);
257                 } else {
258                     //nop - wouldn't make sence to not give a file option to store the result, but do not throw error
259
}
260             } else {
261                 if (resultOption!=null) {
262                     ((de.gulden.framework.amoda.generic.data.GenericValue)resultOption.getValue(de.gulden.framework.amoda.model.option.Option.STATE_CURRENT)).set(null);
263                 } else {
264                     //nop - wouldn't make sence to not give a file option to store the result, but do not throw error
265
}
266             }
267         }
268     }
269
270     public void doWizard(Wizard wizard) {
271         // your code here
272
}
273
274     public JFrame getFrame() {
275         return frame;
276     }
277
278     public void setFrame(JFrame _frame) {
279         frame = _frame;
280     }
281
282     public JComponent createOptionsEditor(GenericOptions options) {
283         if (options.getParentMember()==null) options.setParentMember(getGenericApplication()); // WORKAROUND
284
if (options == getGenericApplication().getOptions()) {
285             ((GenericOptionEntry)options.getOptionEntry("gui")).setSystem(true); // might have been false for wrapped CommandLineApplication, make invisible here because we are surely running in a GUI here
286
}
287         de.gulden.framework.amoda.generic.behaviour.GenericCondition cond=new de.gulden.framework.amoda.generic.behaviour.GenericCondition() {
288             public boolean test() {
289                 de.gulden.framework.amoda.generic.core.GenericFeature f=((de.gulden.framework.amoda.generic.core.GenericFeature)getObject());
290                 return f.isEnabled()&&(!f.isSystem());
291             }
292         };
293         int depth=options.countMaxDepth(cond);
294         if (depth==0) {
295             JLabel label=new JLabel("-empty-",JLabel.CENTER);
296             label.setFont(getDefaultFont());
297             return label;
298         } else if (depth==1) { // no grouping at all
299
JPanel panel=new JPanel();
300             panel.setLayout(new GridBagLayout());
301             FeatureGroup fg = getGenericApplication().getFeatureGroup("commandline-wrapper");
302             Feature optionsFeature = fg.getFeature("options");
303             boolean showDescriptionLabels = optionsFeature.getOptions().getBoolean("show-descriptions");
304             for (Iterator it=options.getEntries().values().iterator();it.hasNext();) {
305                 GenericOptionEntry entry=(GenericOptionEntry)it.next();
306                 entry.setParent(options); // WORKAROUND
307
if (cond.test(entry)) {
308                     entry.createGUIEditorComponent(panel, this);
309                     if (showDescriptionLabels) {
310                         String JavaDoc descr = entry.getMetadata().get("description");
311                         JTextArea descrLabel = new JTextArea();
312                         descrLabel.setText(descr);
313                         descrLabel.setLineWrap(true);
314                         descrLabel.setWrapStyleWord(true);
315                         descrLabel.setEditable(false);
316                         descrLabel.setOpaque(false); // don't paint white background
317
descrLabel.setFont(getFont("menu"));
318                         descrLabel.setForeground(new Color(0xff9999cc)); // "Sun dark-blue", TODO: configurable
319
GridBagConstraints gc = new GridBagConstraints();
320                         gc.fill = GridBagConstraints.HORIZONTAL;
321                         gc.gridwidth = GridBagConstraints.REMAINDER;
322                         gc.gridx = 1;
323                         panel.add(descrLabel, gc);
324                         // vertical space
325
gc = new GridBagConstraints();
326                         gc.fill = GridBagConstraints.HORIZONTAL;
327                         gc.gridwidth = GridBagConstraints.REMAINDER;
328                         panel.add(new JPanel(), gc);
329                     }
330                 }
331             }
332             return panel;
333         } else if (depth==2) { // grouping by titledborders
334
JPanel panel=new JPanel();
335             panel.setLayout(new GridBagLayout());
336             GridBagConstraints gbc=new GridBagConstraints();
337             gbc.gridwidth=GridBagConstraints.REMAINDER;
338             gbc.fill=GridBagConstraints.HORIZONTAL;
339             for (Iterator it=options.getGroups().values().iterator();it.hasNext();) {
340                 GenericOptions g=(GenericOptions)it.next();
341                 if (cond.test(g)&&g.countMaxDepth(cond)>0) {
342                     JComponent gPanel=createOptionsEditor(g); // RECURSION
343
javax.swing.border.TitledBorder JavaDoc border=new javax.swing.border.TitledBorder JavaDoc(g.findTitle());
344                     border.setTitleFont(getFont(FONT_MENU));
345                     gPanel.setBorder(border);
346                     panel.add(gPanel,gbc);
347                 }
348             }
349             return panel;
350         } else { // depth>=3: grouping with tabbedpane and titledborders
351
JTabbedPane panel=new JTabbedPane();
352             panel.setFont(getDefaultFont());
353             // flat entries, which are attached to the application directly without group
354
GenericOptions flatEntries=new GenericOptions();
355             flatEntries.setParent(options.getParent());
356             Collection flats=options.getEntries().values();
357             flats = cond.filter(flats); // only enabled and non-system
358
if (flats.size()>0) {
359                 flatEntries.addAll(flats);
360                 flatEntries.setParent(null); // !!! make sure that only flat entries are found in next recursion step
361
// but to keep option entries valid, set parent directly on each
362
for (Iterator it=flats.iterator();it.hasNext();) {
363                     GenericOptionEntry entry=(GenericOptionEntry)it.next();
364                     entry.setParent(options.getParent());
365                 }
366                 //flatEntries.setParent(options.getParent()/*getGenericApplication()*/); // now set correct parent
367
JComponent gPanel=createOptionsEditor(flatEntries); // RECURSION
368
//flatEntries.setParent(getGenericApplication()); // now set correct parent
369
panel.addTab("General",gPanel);
370             }
371             // groups
372
for (Iterator it=options.getGroups().values().iterator();it.hasNext();) {
373                 GenericOptions g=(GenericOptions)it.next();
374                 if (cond.test(g)&&(g.countMaxDepth(cond)>0)) {
375                     g=g.flattenHierarchy(2,cond);
376                     JComponent gPanel=createOptionsEditor(g); // RECURSION
377
panel.addTab(g.findTitle(),gPanel);
378                 }
379             }
380             return panel;
381         }
382     }
383
384     public Font getDefaultFont() {
385         return getFont(FONT_DEFAULT);
386     }
387
388     public Font getFont(String JavaDoc id) {
389         Font f=(Font)fontCache.get(id);
390         if (f==null) {
391             String JavaDoc fontDescription=getGenericApplication().getOptions().getString("font."+id);
392             StringTokenizer st=new StringTokenizer(fontDescription,",",false);
393             String JavaDoc fontName=st.nextToken();
394             String JavaDoc fontStyleStr=st.nextToken(); // may also be "bold|italics"
395
String JavaDoc fontSizeStr=st.nextToken();
396             int fontStyle=Font.PLAIN;
397             if (fontStyleStr!=null) {
398                 fontStyleStr=fontStyleStr.toLowerCase();
399                 if (fontStyleStr.indexOf("bold")!=-1) {
400                     fontStyle|=Font.BOLD;
401                 }
402                 if (fontStyleStr.indexOf("italic")!=-1) {
403                     fontStyle|=Font.ITALIC;
404                 }
405             }
406             int fontSize=12;
407             if (fontStyleStr!=null) {
408                 try {
409                     fontSize=Integer.parseInt(fontStyleStr);
410                 } catch (NumberFormatException JavaDoc nfe) {
411                     //nop
412
}
413             }
414             f=new Font(fontName,fontStyle,fontSize);
415             fontCache.put(id,f);
416         }
417         return f;
418     }
419
420     public JMenu getWindowMenu() {
421         return windowMenu;
422     }
423
424     public Command getExitCommand() {
425         return exitCommand;
426     }
427
428     public void createPopupMenuFromFeatureGroup(FeatureGroup featureGroup, JPopupMenu menu) {
429         if (featureGroup.isEnabled()) {
430             if (menu.getSubElements().length>0) {
431                 menu.addSeparator();
432             }
433             Object JavaDoc previousFeature=null;
434             for (Iterator it=featureGroup.getFeatures().iterator();it.hasNext();) {
435                 Object JavaDoc f=it.next();
436                 if (f instanceof FeatureGroup) {
437                     createPopupMenuFromFeatureGroup((FeatureGroup)f,menu);
438                 } else {
439                     if (previousFeature instanceof FeatureGroup) {
440                         menu.addSeparator();
441                     }
442                     menu.add(createMenuItemFromFeature((Feature)f));
443                 }
444                 previousFeature=f;
445             }
446         }
447     }
448
449     public JMenu getFileMenu() {
450         return fileMenu;
451     }
452
453     public Command getOpenMostRecentFileCommand() {
454         return openMostRecentFileCommand;
455     }
456
457     public void setOpenMostRecentFileCommand(Command _openMostRecentFileCommand) {
458         openMostRecentFileCommand = _openMostRecentFileCommand;
459     }
460
461     public JButton createButtonFromFeature(Feature feature) {
462         JButton item = new DefaultButton();
463         initFromFeature(item, feature);
464         return item;
465     }
466
467     public JMenuItem createMenuItemFromFeature(Feature feature) {
468         JMenuItem item = new JMenuItem();
469         item.setFont(getFrame().getJMenuBar().getFont());
470         initFromFeature(item, feature);
471         return item;
472     }
473
474     public void doAbout() {
475         // this is called only when a non-GUIApp is run inside GUIEnv
476
if (getFrame().isVisible()) { // normal mode
477
GUIApplication a = new GUIApplication() {};
478             a.internalSetGenericApplicationEnvironment(this); // (set field directly to avoid auto-setting backward-reference)
479
a.setMetadata(getGenericApplication().getMetadata());
480             a.setOptions(getGenericApplication().getOptions());
481             a.about();
482         } else { // during init: fallback to text output
483
super.doAbout();
484         }
485     }
486
487     public void doHelp() {
488         // this is called only when a non-GUIApp is run inside GUIEnv
489
if (getFrame().isVisible()) { // normal mode
490
GUIApplication a = new GUIApplication() {};
491             a.internalSetGenericApplicationEnvironment(this); // (set field directly to avoid auto-setting backward-reference)
492
a.setMetadata(getGenericApplication().getMetadata());
493             a.setOptions(getGenericApplication().getOptions());
494             a.help();
495         } else { // during init: fallback to text output
496
super.doHelp();
497         }
498     }
499
500     public void internalDoMessage(Message message) {
501         JOptionPane.showMessageDialog(frame,message.getText(),message.getMetadata().get("title","Message"),message.getType());
502     }
503
504     protected void prepareApplicationWelcome() {
505         super.prepareApplicationWelcome();
506         de.gulden.framework.amoda.generic.core.GenericApplication application = getGenericApplication();
507         // set look and feel if specified by options
508
String JavaDoc lafName = application.getOptions().getString("swing-look-and-feel");
509         if (!Toolbox.empty(lafName)) {
510             try {
511                 javax.swing.UIManager.setLookAndFeel(lafName);
512             } catch (Throwable JavaDoc t) {
513                 System.out.println("Warning: Cannot set look-and-feel '"+lafName+"' - ignoring.");
514             }
515         }
516         initDefaultUIFonts();
517
518         JFrame frame;
519         if (application instanceof GUIApplication) { // no GUIFrame if CommandLineApplication
520
frame = createGUIFrame(); //new de.gulden.framework.amoda.environment.gui.component.GUIFrame(this);
521
} else {
522             frame = createCommandLineWrapperFrame();//new JFrame(); // invisible dummy
523
}
524         java.net.URL JavaDoc url=de.gulden.framework.amoda.generic.metadata.GenericMetadata.findIconResource(getGenericApplication());
525         if (url!=null) {
526             java.awt.Image JavaDoc icon=frame.getToolkit().getImage(url);
527             frame.setIconImage(icon);
528         }
529         setFrame(frame);
530     }
531
532     protected void prepareApplicationStart() {
533         super.prepareApplicationStart();
534         // this is called right before application.start() ist called
535
de.gulden.framework.amoda.generic.core.GenericApplication application = getGenericApplication();
536         if (application.getOptions().getBoolean("main-window-maximized")) {
537             // doesn't work on Linux with 1.4.1:
538
//f.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); // Jdk 1.4 required! ***
539
getFrame().setLocation(0,0);
540             java.awt.Dimension JavaDoc screenSize=getFrame().getToolkit().getScreenSize();
541             getFrame().setSize(screenSize.width,screenSize.height);
542         } else {
543             getFrame().setLocation(100,100);
544             getFrame().setSize(getGenericApplication().getOptions().getInt("main-window-width"),getGenericApplication().getOptions().getInt("main-window-height"));
545         }
546         getFrame().setTitle(de.gulden.framework.amoda.generic.metadata.GenericMetadata.findTitle(getGenericApplication()));
547
548         if (application instanceof GUIApplication) {
549             de.gulden.framework.amoda.environment.gui.component.GUIFrame f = (de.gulden.framework.amoda.environment.gui.component.GUIFrame)getFrame();
550             JMenuBar menubar;
551             JMenu menu;
552             JMenuItem item;
553             Feature feature;
554             menubar=f.getJMenuBar();
555             for (Iterator it=application.getFeatureGroups().iterator();it.hasNext();) {
556                 Object JavaDoc o=it.next();
557                 de.gulden.framework.amoda.generic.core.GenericFeatureGroup g=(de.gulden.framework.amoda.generic.core.GenericFeatureGroup)o;
558                 if (g.isEnabled()&&(!g.isSystem())) {
559                     menu=new JMenu(g.findTitle());
560                     menu.setFont(menubar.getFont());
561                     createMenuItemsFromFeatureGroup(g,menu);
562                     menubar.add(menu);
563                     // special:
564
if (g.getId().equals("file-features")) {
565                         this.fileMenu=menu;
566                     } else if (g.getId().equals("window-features")) {
567                         this.windowMenu=menu;
568                     }
569                 }
570             }
571             f.resetStatus();
572         } else { // e.g. a CommandLineApplication running
573
getFrame().show();
574         }
575     }
576
577     protected void initDefaultUIFonts() {
578         Font font;
579         font=getDefaultFont();
580         UIDefaults defaults=UIManager.getDefaults();
581         for (Enumeration e=defaults.keys();e.hasMoreElements();) {
582             Object JavaDoc o=e.nextElement();
583             if (o instanceof String JavaDoc) {
584                 String JavaDoc key=(String JavaDoc)o;
585                 if (key.endsWith(".font")) {
586                     UIManager.put(key,new javax.swing.plaf.FontUIResource JavaDoc(font));
587                 }
588             }
589         }
590     }
591
592     protected JFrame createGUIFrame() {
593         return new de.gulden.framework.amoda.environment.gui.component.GUIFrame(this);
594     }
595
596     protected JFrame createCommandLineWrapperFrame() {
597         return new de.gulden.framework.amoda.environment.gui.component.CommandLineWrapperFrame(this);
598     }
599
600     protected void launchAfterInit(GenericApplication genericApplication) {
601         if (!genericApplication.getOptions().getBoolean("gui")) {
602             // perform launch again on fallback environment (CommandLineApplications that run in GUIEnvionrments should be able to be initialized mutliple times without harm)
603
getFallbackCommandLineApplicationEnvironment().launch(genericApplication);
604         } else {
605             super.launchAfterInit(genericApplication); // normal GUI mode, or CommandLineApplication in GUI-wrapper
606
}
607     }
608
609     protected void initFromFeature(AbstractButton item, Feature feature) {
610         String JavaDoc title=de.gulden.framework.amoda.generic.metadata.GenericMetadata.findTitle(feature);
611         if (item instanceof JMenuItem) {
612             title = title.trim();
613         }
614         item.setText(title);
615         String JavaDoc shortcut=((de.gulden.framework.amoda.generic.core.GenericFeature)feature).getShortcut();
616         if (shortcut!=null) {
617             StringTokenizer st=new StringTokenizer(shortcut," +-,",false);
618             shortcut="ctrl";
619             while (st.hasMoreTokens()) {
620                 String JavaDoc tok=st.nextToken();
621                 if (st.hasMoreTokens()) { // not last one: force modifiers to lower case
622
tok=tok.toLowerCase();
623                 }
624                 shortcut+=" "+tok;
625             }
626             if (item instanceof JMenuItem) {
627                 javax.swing.KeyStroke JavaDoc ks=javax.swing.KeyStroke.getKeyStroke(shortcut); // parses string
628
if (ks!=null) {
629                     ((JMenuItem)item).setAccelerator(ks);
630                 }
631             }
632         }
633         int commandCount=0;
634         for (Iterator it=feature.getAll(GenericCommand.class,false).values().iterator();it.hasNext();) {
635             GenericCommand c=(GenericCommand)it.next();
636             c.setParent(getGenericApplication());
637             item.addActionListener((java.awt.event.ActionListener JavaDoc)c);
638             commandCount++;
639             // special:
640
if (((c instanceof de.gulden.framework.amoda.generic.behaviour.CommandApplicationExit)||((c instanceof de.gulden.framework.amoda.generic.behaviour.CommandWrapper) && (((de.gulden.framework.amoda.generic.behaviour.CommandWrapper)c).getWrapped() instanceof de.gulden.framework.amoda.generic.behaviour.CommandApplicationExit)))&&(exitCommand==null)) {
641                 exitCommand=c; // remember exit command to execute exactly the same when window-closing is activated
642
}
643         }
644         if (commandCount==0) {
645             item.setEnabled(false);
646         }
647     }
648
649     protected void createMenuItemsFromFeatureGroup(FeatureGroup featureGroup, JMenu menu) {
650         if (featureGroup.isEnabled()) {
651             java.awt.Component JavaDoc[] components=menu.getMenuComponents();
652             if ((components.length>0)&&(components[components.length-1] instanceof javax.swing.MenuElement JavaDoc)) { // last one not yet separator
653
menu.addSeparator();
654             }
655             Object JavaDoc previousFeature=null;
656             for (Iterator it=featureGroup.getFeatures().iterator();it.hasNext();) {
657                 Object JavaDoc f=it.next();
658                 if (f instanceof FeatureGroup) {
659                     createMenuItemsFromFeatureGroup((FeatureGroup)f,menu);
660                 } else {
661                     if (previousFeature instanceof FeatureGroup) {
662                         menu.addSeparator();
663                     }
664                     menu.add(createMenuItemFromFeature((Feature)f));
665                 }
666                 previousFeature=f;
667             }
668         }
669     }
670
671     protected void launchStart(GenericApplication application) {
672         //if (application instanceof GUIApplication) { // if CommandLineApp in CommandLineWrapperFrame, start via Start-Button
673
if ((!(getFrame() instanceof de.gulden.framework.amoda.environment.gui.component.CommandLineWrapperFrame)) || (!application.getFeatureGroup("commandline-wrapper").getFeature("start").isEnabled())) {
674             application.start();
675         }
676     }
677
678     private JOptionPane internalCreateJOptionPane(GenericQuestion q) {
679         Collection answerOptions=q.getAnswerOptions();
680         // convert each answer option to Strings for button labels
681
String JavaDoc[] answers=new String JavaDoc[answerOptions.size()];
682         int i=0;
683         for (Iterator it=answerOptions.iterator();it.hasNext();) {
684             answers[i++]=it.next().toString();
685         }
686         JOptionPane p=new JOptionPane();
687         p.setMessageType(q.getType());
688         p.setMessage(q.getText());
689         p.setOptions(answers);
690         p.setInitialValue(q.getDefaultAnswer());
691         return p;
692     }
693
694     private JButton internalCreateOptionButton(GenericOptionEntry option, GenericDialog parentDialog) {
695         JButton button=new DefaultButton(option.findTitle());
696         button.setFont(getDefaultFont());
697         if (option.getId().equals(parentDialog.getDefaultAnswer())) {
698             parentDialog.getJDialog().getRootPane().setDefaultButton(button);
699         }
700         for (Iterator it=option.getAll(de.gulden.framework.amoda.generic.behaviour.GenericCommand.class,false).values().iterator();it.hasNext();) {
701             de.gulden.framework.amoda.generic.behaviour.GenericCommand c=(de.gulden.framework.amoda.generic.behaviour.GenericCommand)it.next();
702             de.gulden.framework.amoda.generic.behaviour.GenericCommand cc=(de.gulden.framework.amoda.generic.behaviour.GenericCommand)c.clone();
703             cc.setParent(parentDialog);
704             button.addActionListener(cc); // let all <command>s specified inside <option> be executed when button is chosen
705
}
706         return button;
707     }
708
709     private JDialog internalCreateJDialog(GenericDialog d, JComponent mainComponent, int buttonOrientation) {
710         JDialog dialog=new JDialog(getFrame(),true);
711         d.setJDialog(dialog);
712         String JavaDoc title = d.getTitle();
713         if (title == null) {
714             title = d.getMetadata().get("title","Dialog");
715         }
716         dialog.setTitle(title);
717         dialog.getContentPane().setLayout(new BorderLayout());
718         dialog.getContentPane().add(mainComponent,BorderLayout.CENTER);
719         JPanel buttonsPanel=new JPanel();
720         buttonsPanel.setLayout(new FlowLayout(buttonOrientation));
721         Collection answerOptions=d.getAnswerOptions();
722         for (Iterator it=answerOptions.iterator();it.hasNext();) {
723             de.gulden.framework.amoda.generic.option.GenericOptionEntry answer=(de.gulden.framework.amoda.generic.option.GenericOptionEntry)it.next();
724             buttonsPanel.add(internalCreateOptionButton(answer,d));
725         }
726         dialog.getContentPane().add(buttonsPanel,BorderLayout.SOUTH);
727         dialog.pack();
728         Toolbox.centerComponent(dialog,getFrame());
729         return dialog;
730     }
731
732
733     // ------------------------------------------------------------------------
734
// --- static method ---
735
// ------------------------------------------------------------------------
736

737     public static void invoke(Class JavaDoc applicationClass, String JavaDoc[] args) throws Exception JavaDoc {
738         de.gulden.framework.amoda.generic.core.GenericApplicationEnvironment.invoke(applicationClass, args, GUIApplicationEnvironmentFactory.class);
739     }
740
741 } // end GUIApplicationEnvironment
742
Popular Tags