KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Project: AMODA - Abstract Modeled Application
3  * Class: de.gulden.framework.amoda.environment.gui.GUIApplication
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.generic.core.*;
23 import de.gulden.framework.amoda.model.core.*;
24 import de.gulden.framework.amoda.model.core.WorkspaceProvider;
25 import de.gulden.framework.amoda.model.document.*;
26 import de.gulden.framework.amoda.model.document.ClipboardHandler;
27 import de.gulden.framework.amoda.model.document.Document;
28 import de.gulden.framework.amoda.model.document.DocumentHandler;
29 import de.gulden.util.Toolbox;
30 import de.gulden.util.xml.XMLToolbox;
31 import java.awt.*;
32 import java.io.*;
33 import java.lang.*;
34 import java.net.*;
35 import java.util.*;
36 import javax.swing.*;
37 import org.w3c.dom.*;
38
39 /**
40  * Class GUIApplication.
41  *
42  * @author Jens Gulden
43  * @version snapshot-beautyj-1.1
44  */

45 public abstract class GUIApplication extends CommandLineApplication implements WorkspaceProvider, ClipboardHandler, DocumentHandler {
46
47     // ------------------------------------------------------------------------
48
// --- fields ---
49
// ------------------------------------------------------------------------
50

51     protected boolean modeMultiDocuments;
52
53     protected boolean modeMultiViews;
54
55     protected JDialog aboutDialog;
56
57     protected JWindow aboutSplashWindow;
58
59     protected JDialog helpDialog;
60
61
62     // ------------------------------------------------------------------------
63
// --- methods ---
64
// ------------------------------------------------------------------------
65

66     public Workspace getWorkspace() {
67         return ((GUIApplicationEnvironment)getEnvironment()).getWorkspace();
68     }
69
70     public JComponent createGUIComponent(DocumentView view) {
71         // to be overwritten by subclass if used
72
throw new AbstractMethodError JavaDoc("createDefaultView is not implemented by subclass");
73     }
74
75     public void cutSelection(DocumentSelection sel) {
76         // your code here
77
}
78
79     public void copySelection(DocumentSelection sel) {
80         // your code here
81
}
82
83     public void pasteSelection(DocumentSelection sel) {
84         // your code here
85
}
86
87     public DocumentHandler getDocumentHandler() {
88         return this; // default is this, may be overwritten
89
}
90
91     public ClipboardHandler getClipboardHandler() {
92         return this; // default is this, may be overwritten
93
}
94
95     public void destroy() {
96         ((GUIApplicationEnvironment)getGenericApplicationEnvironment()).getFrame().dispose();
97         System.exit(0);
98     }
99
100     public void start() {
101         // init recent files list
102
getRecentFilesList().init(this);
103         // open window
104
((GUIApplicationEnvironment)getEnvironment()).getFrame().setVisible(true);
105         // open initial documents or whatever else on the workspace
106
startWorkspace();
107         if (aboutSplashWindow!=null) {
108             aboutSplashWindow.dispose();
109         }
110         super.start();
111     }
112
113     public void about() {
114         if (((GUIApplicationEnvironment)getEnvironment()).getFrame().isVisible()) { // normal mode
115
if (aboutDialog==null) {
116                 aboutDialog=createAboutDialog();
117             }
118             de.gulden.util.Toolbox.centerOnScreen(aboutDialog);
119             aboutDialog.setVisible(true);
120         } else {
121             aboutSplash(); // special mode during init
122
}
123     }
124
125     public void aboutSplash() {
126         JWindow window=new JWindow(((GUIApplicationEnvironment)getEnvironment()).getFrame());
127         JComponent aboutComponent=createAboutComponent();
128         window.getContentPane().add(aboutComponent);
129         window.pack();
130         Dimension size=window.getSize();
131         size.width=500;
132         window.setSize(size);
133         de.gulden.util.Toolbox.centerOnScreen(window);
134         this.aboutSplashWindow=window;
135         window.setVisible(true);
136     }
137
138     public Document newDocument() {
139         // your code here
140
return null;
141     }
142
143     public Document openDocument(File file) {
144         // your code here
145
return null;
146     }
147
148     public void saveDocument(Document doc) {
149         // your code here
150
}
151
152     public Workspace createWorkspace() {
153         // may be overwritten by sublcasses
154
GUIWorkspace workspace=new GUIWorkspace();
155         workspace.setEnvironment((de.gulden.framework.amoda.generic.core.GenericApplicationEnvironment)getEnvironment());
156         return workspace;
157     }
158
159     public Document openDocument(URL url) {
160         String JavaDoc protocol=url.getProtocol();
161         if (protocol.equals("file")) {
162             return openDocument(new java.io.File JavaDoc(url.getPath()));
163         } else {
164             error("URLs other than file:// are not supported.");
165             return null;
166         }
167     }
168
169     public void saveDocumentAs(Document doc, File file) {
170         // your code here
171
}
172
173     public void closeDocument(Document doc) {
174         if (confirm("Really close this document and all its views?")) {
175             Collection views=getWorkspace().getViews(doc);
176             views=(Collection)views; // avoid concurrent modification exception
177
for (Iterator it=views.iterator();it.hasNext();) {
178                 DocumentView view=(DocumentView)it.next();
179                 getWorkspace().removeView(view);
180             }
181         }
182     }
183
184     public void loadDocument(File file) {
185         openDocumentOnWorkspace(openDocument(file));
186     }
187
188     public void loadDocument(URL url) {
189         openDocumentOnWorkspace(openDocument(url));
190     }
191
192     public void storeDocument(Document document, File file) {
193         // your code here
194
}
195
196     public void storeDocument(Document document, URL url) {
197         // your code here
198
}
199
200     public void help() {
201         if (helpDialog==null) {
202             helpDialog=createHelpDialog();
203         }
204         de.gulden.util.Toolbox.centerOnScreen(helpDialog);
205         helpDialog.setVisible(true);
206     }
207
208     protected JDialog createOptionsDialog() {
209         // your code here
210
return null;
211     }
212
213     protected JDialog createAboutDialog() {
214         de.gulden.framework.amoda.environment.gui.component.JDialogCloseable dialog=new de.gulden.framework.amoda.environment.gui.component.JDialogCloseable(((GUIApplicationEnvironment)getEnvironment()).getFrame(),"About",false);
215         JTabbedPane tabbedPane=new JTabbedPane();
216         tabbedPane.setFont(((GUIApplicationEnvironment)getEnvironment()).getFont(GUIApplicationEnvironment.FONT_MENU));
217
218         JComponent comp=createAboutComponent();
219         tabbedPane.add("About",comp);
220
221         // set size now (after filling text will be too late)
222
dialog.getContentPane().add(tabbedPane);
223
224         String JavaDoc licenseText=createLicenseText();
225         if (licenseText!=null) {
226             JTextArea textArea=new JTextArea(licenseText);
227             JScrollPane scrollPane=new JScrollPane(textArea);
228             comp=new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("SecurityModern13.png"),scrollPane);
229             tabbedPane.add("License",comp);
230         }
231
232         de.gulden.util.swing.MapTableModel tableModel=new de.gulden.util.swing.MapTableModel(System.getProperties());
233         JTable table=new JTable(tableModel);
234         JScrollPane scrollPane=new JScrollPane(table);
235         comp=new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("ManagementModern02.png"),scrollPane);
236         tabbedPane.add("System",comp);
237
238         dialog.pack(); // will lead to extremely over-wide dialog
239
Dimension size=dialog.getSize();
240         //size.height-=10; // ******************************************
241
size.height = 400;
242         dialog.setSize(size);
243         return dialog;
244     }
245
246     protected JComponent createAboutComponent() {
247         String JavaDoc aboutHTML=createAboutHTML();
248         return new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("SetupModern24.png"), "<html>"+aboutHTML+"</html>");
249     }
250
251     protected ApplicationEnvironment createApplicationEnvironment() {
252         // overwrites CommandlineApplication.createApplicationEnvironment
253
de.gulden.framework.amoda.model.core.ApplicationEnvironmentFactory factory=new GUIApplicationEnvironmentFactory(getArgs());//,getConfigurationResourceURL());
254
return factory.createApplicationEnvironment();
255     }
256
257     protected String JavaDoc createAboutHTML() {
258         StringBuffer JavaDoc html=new StringBuffer JavaDoc();
259         html.append("<h2>"+de.gulden.framework.amoda.generic.metadata.GenericMetadata.findTitle(this)+"</h2>");
260         String JavaDoc subtitle=getMetadata().get("subtitle");
261         if (!Toolbox.empty(subtitle)) {
262             html.append("<h3>"+subtitle+"</h3>");
263         }
264         String JavaDoc description=getMetadata().get("description");
265         if (!Toolbox.empty(description)) {
266             html.append("<h4>"+description+"</h4>");
267         }
268         String JavaDoc licenseMessage=getMetadata().get("license-message");
269         if (!Toolbox.empty(subtitle)) {
270             html.append("<h4>"+licenseMessage+"</h4>");
271         }
272         html.append("<table>");
273         for (Iterator it=getMetadata().getEntries().iterator();it.hasNext();) {
274             de.gulden.framework.amoda.model.metadata.MetadataEntry entry=(de.gulden.framework.amoda.model.metadata.MetadataEntry)it.next();
275             String JavaDoc name=entry.getName();
276             String JavaDoc value=entry.getString();
277             if (!(name.equals("license-message")
278                 ||name.equals("license-text")
279                 ||name.equals("description")
280                 ||name.equals("title"))) {
281                 html.append("<tr><td><i>"+Toolbox.capitalize(entry.getName())+"</i></td><td><b>"+entry.getString()+"</b></td></tr>");
282             }
283         }
284         html.append("</table>");
285         return html.toString();
286     }
287
288     protected String JavaDoc createLicenseText() {
289         String JavaDoc license=getMetadata().get("license-text");
290         if (!Toolbox.empty(license)) {
291             return license;
292         } else {
293             return null;
294         }
295     }
296
297     protected void startWorkspace() {
298         // default implementation, opens 1 new blank document
299
de.gulden.framework.amoda.model.document.Document doc=getInitialDocument();
300         if (doc!=null) {
301             openDocumentOnWorkspace(doc);
302         }
303     }
304
305     protected Document getInitialDocument() {
306         if (getOptions().getBoolean("open-most-recent-file-on-startup")) {
307             Document doc=null;
308             de.gulden.framework.amoda.generic.core.GenericRecentFilesList rfl=(de.gulden.framework.amoda.generic.core.GenericRecentFilesList)getRecentFilesList();
309             if (rfl!=null) {
310                 if (rfl.size()>0) {
311                     Object JavaDoc o=rfl.get(0);
312                     if (o instanceof java.io.File JavaDoc) {
313                         doc=openDocument((java.io.File JavaDoc)o);
314                     } else if (o instanceof java.net.URL JavaDoc) {
315                         doc=openDocument((java.net.URL JavaDoc)o);
316                     }
317                 }
318             }
319             if (doc!=null) {
320                 return doc;
321             }
322         }
323         //FALLSTHROUGH on all else-branches
324
return getDocumentHandler().newDocument();
325     }
326
327     protected RecentFilesList createRecentFilesList() {
328         return new GUIRecentFilesList();
329     }
330
331     protected void openDocumentOnWorkspace(Document document) {
332         if (document!=null) {
333             de.gulden.framework.amoda.model.document.DocumentView view=document.createDefaultView();
334             getWorkspace().addView(view);
335         }
336     }
337
338     protected JDialog createHelpDialog() {
339         de.gulden.framework.amoda.environment.gui.component.JDialogCloseable dialog=new de.gulden.framework.amoda.environment.gui.component.JDialogCloseable(((GUIApplicationEnvironment)getEnvironment()).getFrame(),"Help",false);
340         JPanel panel = new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("SecurityModern13.png"), "<html><body>"+createHelpHTML()+"</body></html>");
341         dialog.getContentPane().add(panel);
342         /*dialog.pack(); // will lead to extremely over-wide dialog
343         Dimension size=dialog.getSize();
344         size.height = 800;
345         size.height = 600;
346         dialog.setSize(size);*/

347         dialog.setSize(800,800);
348         return dialog;
349     }
350
351     protected String JavaDoc createUsageHTML() {
352         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
353         /*String description = getMetadata().get("description").trim();
354         if (description.length()!=0) {
355             sb.append("<p>"+description+"</p>");
356         }*/

357         String JavaDoc usageLine = getUsageLine();
358         if (usageLine!=null) {
359             sb.append("<h4>Usage:</h4><code>"+de.gulden.util.xml.XMLToolbox.xmlEscape(usageLine)+"</code></p>");
360         }
361         return sb.toString();
362     }
363
364     protected String JavaDoc createHelpHTML() {
365         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
366         sb.append(createAboutHTML());
367         sb.append(createUsageHTML());
368         Collection allOptions = getOptions().getAll(de.gulden.framework.amoda.model.option.OptionEntry.class, true).values();
369         if (!allOptions.isEmpty()) {
370             sb.append("<h4>options are:</h4><table>");
371             for (Iterator it=allOptions.iterator();it.hasNext();) {
372                 de.gulden.framework.amoda.generic.option.GenericOptionEntry o=(de.gulden.framework.amoda.generic.option.GenericOptionEntry)it.next();
373                 if (!(o.isSystem())) {
374                     String JavaDoc name = "-" + o.getId();
375                     String JavaDoc shortcut = o.getShortcut();
376                     if (shortcut != null) {
377                         name = "-" + shortcut + " or " + name;
378                     }
379                     String JavaDoc description=de.gulden.util.Toolbox.noNull(o.getMetadata().get("description"));
380                     Class JavaDoc typeClass = o.getType();
381                     if (typeClass == null) {
382                         typeClass = String JavaDoc.class;
383                     }
384                     String JavaDoc type="<i>"+de.gulden.util.Toolbox.unqualify(typeClass.getName()).toLowerCase()+"</i>";
385                     String JavaDoc defaultValue=o.getValue(o.STATE_DEFAULT).getString();
386                     if (defaultValue!=null) {
387                         description+=" (default: "+defaultValue+")";
388                     }
389                     String JavaDoc row="<tr><td valign='top' nowrap>"+ name +"</td><td valign='top'>"+type+"</td><td>"+description+"</td></tr>";
390                     sb.append(row);
391                 }
392             }
393             sb.append("</table>");
394         }
395         return sb.toString();
396     }
397
398     void internalSetGenericApplicationEnvironment(GenericApplicationEnvironment env) {
399         this.genericApplicationEnvironment = env; // set without backward-reference (for dummy-instances of GUIApplication)
400
}
401
402 } // end GUIApplication
403
Popular Tags