KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mullassery > act > gui > ACT


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met: 1.
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer. 2. Redistributions in
10  * binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other
12  * materials provided with the distribution. 3. The end-user documentation
13  * included with the redistribution, if any, must include the following
14  * acknowlegement: "This product includes software developed by the Apache
15  * Software Foundation (http://www.apache.org/)." Alternately, this
16  * acknowlegement may appear in the software itself, if and wherever such
17  * third-party acknowlegements normally appear. 4. The names "The Jakarta
18  * Project", "Ant", and "Apache Software Foundation" must not be used to
19  * endorse or promote products derived from this software without prior written
20  * permission. For written permission, please contact apache@apache.org. 5.
21  * Products derived from this software may not be called "Apache" nor may
22  * "Apache" appear in their names without prior written permission of the
23  * Apache Group.
24  *
25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
26  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
27  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  * ====================================================================
36  *
37  * This software consists of voluntary contributions made by many individuals
38  * on behalf of the Apache Software Foundation. For more information on the
39  * Apache Software Foundation, please see <http://www.apache.org/> .
40  */

41
42 package com.mullassery.act.gui;
43
44 import java.awt.BorderLayout JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.Toolkit JavaDoc;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.awt.event.ActionListener JavaDoc;
49 import java.awt.event.WindowAdapter JavaDoc;
50 import java.awt.event.WindowEvent JavaDoc;
51 import java.io.BufferedInputStream JavaDoc;
52 import java.io.File JavaDoc;
53
54 import javax.swing.BorderFactory JavaDoc;
55 import javax.swing.Box JavaDoc;
56 import javax.swing.ImageIcon JavaDoc;
57 import javax.swing.JFrame JavaDoc;
58 import javax.swing.JLabel JavaDoc;
59 import javax.swing.JMenu JavaDoc;
60 import javax.swing.JMenuBar JavaDoc;
61 import javax.swing.JMenuItem JavaDoc;
62 import javax.swing.JProgressBar JavaDoc;
63 import javax.swing.JWindow JavaDoc;
64 import javax.swing.Timer JavaDoc;
65 import javax.swing.ToolTipManager JavaDoc;
66 import javax.swing.border.Border JavaDoc;
67 import javax.swing.event.MenuEvent JavaDoc;
68 import javax.swing.event.MenuListener JavaDoc;
69
70 import org.w3c.dom.Element JavaDoc;
71 import org.w3c.dom.Node JavaDoc;
72
73 import com.mullassery.act.ACTException;
74 import com.mullassery.act.TaskMaster;
75 import com.mullassery.act.util.GUIUtil;
76 import com.mullassery.act.util.ResourceUtil;
77 import com.mullassery.act.util.XMLUtil;
78
79 /**
80  * @author Abey Mullassery
81  *
82  */

83 public class ACT extends JFrame JavaDoc {
84
85     public static final String JavaDoc ACT_FRAME_TITLE = "ACT.frameTitle"; //$NON-NLS-1$
86
private JMenuItem JavaDoc aboutMenuItem;
87     private JMenuItem JavaDoc contentsMenuItem;
88     private JMenuItem JavaDoc exitMenuItem;
89     private JMenu JavaDoc fileMenu;
90     private TaskPanel focusedTP;
91     private JMenu JavaDoc helpMenu;
92     private JMenuBar JavaDoc menuBar;
93
94     private JMenu JavaDoc taskMenu;
95     private JMenuItem JavaDoc openMenuItem;
96     private JMenuItem JavaDoc saveMenuItem;
97
98     private final SettingsManager sm = new SettingsManager(this);
99     private final MRUManager mru = new MRUManager(this);
100     private final TaskMaster taskMaster = new TaskMaster();
101     private final TaskManager tm = new TaskManager(this);
102     private int loadingPercentage;
103     private String JavaDoc loadingMessage = "Starting ACT..";
104
105     /**
106      * @param mainTask
107      * @throws Exception
108      */

109     public ACT() throws Exception JavaDoc {
110         this(null);
111     }
112     /**
113      * Loads the task list, initializes MRU and GUI components and fires
114      * ProgressEvents
115      *
116      * @param mainTask
117      * @param pl
118      * @throws Exception
119      */

120     public ACT(String JavaDoc mainTask) throws Exception JavaDoc {
121         super(GUIUtil.getString(ACT_FRAME_TITLE));
122         new Splash();
123         initComponents();
124         if (mainTask != null) {
125             Element JavaDoc elm =
126                 XMLUtil.getDocumentBuilder().newDocument().createElement(
127                     GUIUtil.TASK);
128             elm.setAttribute(GUIUtil.TASK_NAME, mainTask);
129             elm.setAttribute(GUIUtil.TASK_DISPLAY, mainTask);
130             createTaskPanel(elm);
131         }
132         setLoadingProgress(100, "ACT Loaded");
133     }
134
135     private void setLoadingProgress(int percentage, String JavaDoc message) {
136         synchronized (this) {
137             if (percentage <= 100)
138                 loadingPercentage = percentage;
139             else
140                 loadingPercentage = 100;
141             loadingMessage = message;
142         }
143     }
144
145     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
146         ACT act = null;
147         if (args.length == 0) {
148             act = new ACT();
149         } else {
150             if (args[0].equals("-dir")) {
151                 if (args.length < 2) {
152                     System.err.println(
153                         "Usage: ACT [-dir parent_dir_of_act-setting.xml] [taskname]");
154                     return;
155                 }
156                 File JavaDoc dir = new File JavaDoc(args[1]);
157                 if (dir.exists() && dir.isDirectory()) {
158                     ResourceUtil.actDir = args[1];
159                 } else {
160                     System.err.println(
161                         args[1]+" is not a valid directory!");
162                     return;
163                 }
164                 if (args.length > 2) {
165                     act = new ACT(args[2]);
166                 } else {
167                     act = new ACT();
168                 }
169             } else {
170                 act = new ACT(args[0]);
171             }
172         }
173     }
174
175     public TaskPanel createTaskPanel(Element JavaDoc tsk) throws ACTException {
176         final TaskPanel tp = new TaskPanel(ACT.this, taskMaster, tsk);
177         tp.addWindowListener(new TaskWindowListener());
178         focusedTP = tp;
179         return tp;
180     }
181
182     void exitForm() {
183         dispose();
184     }
185
186     public TaskPanel getFocusedTP() {
187         return focusedTP;
188     }
189
190     public TaskMaster getTaskMaster() {
191         return taskMaster;
192     }
193
194     /**
195      * All task loading and GUI building
196      */

197     private void initComponents() {
198         setLoadingProgress(10, "Opening Application");
199         addWindowListener(new WindowAdapter JavaDoc() {
200             public void windowClosing(WindowEvent JavaDoc evt) {
201                 exitForm();
202             }
203             public void windowClosed(WindowEvent JavaDoc evt) {
204                 System.exit(0);
205             }
206         });
207         setLoadingProgress(30, "Loading Tasks..");
208         ToolTipManager.sharedInstance().setEnabled(true);
209         ToolTipManager.sharedInstance().setInitialDelay(300);
210         setLoadingProgress(70, "Populating Task menu");
211         initMenu();
212         setLoadingProgress(90, "Completed loading menu");
213         pack();
214         GUIUtil.centralize(this);
215         show();
216     }
217
218     /**
219      * Creating all the menus, MRU list, etc.
220      */

221     private void initMenu() {
222         menuBar = new JMenuBar JavaDoc();
223         fileMenu = new JMenu JavaDoc("File"); //$NON-NLS-1$
224
openMenuItem = new JMenuItem JavaDoc("Open"); //$NON-NLS-1$
225
fileMenu.add(openMenuItem);
226         saveMenuItem = new JMenuItem JavaDoc("Save"); //$NON-NLS-1$
227
saveMenuItem.setEnabled(false);
228         fileMenu.add(saveMenuItem);
229         exitMenuItem = new JMenuItem JavaDoc("Exit"); //$NON-NLS-1$
230
fileMenu.add(exitMenuItem);
231         menuBar.add(fileMenu);
232
233         taskMenu = sm.getTaskMenu();
234         menuBar.add(taskMenu);
235
236         helpMenu = new JMenu JavaDoc("Help"); //$NON-NLS-1$
237
contentsMenuItem = new JMenuItem JavaDoc("Contents"); //$NON-NLS-1$
238
helpMenu.add(contentsMenuItem);
239         aboutMenuItem = new JMenuItem JavaDoc("About"); //$NON-NLS-1$
240
helpMenu.add(aboutMenuItem);
241
242         menuBar.add(Box.createHorizontalGlue());
243         menuBar.add(helpMenu);
244
245         setJMenuBar(menuBar);
246
247         openMenuItem.addActionListener(new ActionListener JavaDoc() {
248             public void actionPerformed(ActionEvent JavaDoc evt) {
249                 tm.openTask();
250             }
251         });
252
253         saveMenuItem.addActionListener(new ActionListener JavaDoc() {
254             public void actionPerformed(ActionEvent JavaDoc evt) {
255                 tm.saveTask(getFocusedTP());
256             }
257         });
258
259         exitMenuItem.addActionListener(new ActionListener JavaDoc() {
260             public void actionPerformed(ActionEvent JavaDoc evt) {
261                 exitForm();
262             }
263         });
264
265         aboutMenuItem.addActionListener(new ActionListener JavaDoc() {
266             public void actionPerformed(ActionEvent JavaDoc evt) {
267                 showAbout();
268             }
269         });
270
271         fileMenu.addMenuListener(new MenuListener JavaDoc() {
272             public void menuCanceled(MenuEvent JavaDoc e) {
273             }
274             public void menuDeselected(MenuEvent JavaDoc e) {
275             }
276             public void menuSelected(MenuEvent JavaDoc e) {
277                 mru.updateMRU(fileMenu);
278             }
279         });
280     }
281
282     /**
283      * Used to restart when new Categories/ tasks are added, edited or deleted
284      * Currently inefficiently done by re-creating it.
285      */

286     public void reStart() {
287         sm.saveSettings();
288         menuBar.remove(taskMenu);
289         menuBar.remove(helpMenu);
290         menuBar.add(sm.getTaskMenu());
291         menuBar.add(helpMenu);
292         this.pack();
293     }
294
295     void showAbout() {
296         GUIUtil.showInfoMessage(this, GUIUtil.getString("ACT.aboutMessage")); //$NON-NLS-1$
297
}
298
299     /**
300      * Listens to the current focussed TaskPanel to save values to file and to
301      * add to MRU on exit *
302      */

303     private class TaskWindowListener extends WindowAdapter JavaDoc {
304
305         public void windowClosing(WindowEvent JavaDoc evt) {
306             mru.addToMRU((TaskPanel) evt.getSource());
307             Element JavaDoc tv = focusedTP.getTaskValues();
308             Node JavaDoc tskChild =
309                 tv.getElementsByTagName(
310                     tv.getAttribute(GUIUtil.TASK_NAME)).item(
311                     0);
312             if ((tskChild != null
313                 && (tskChild.getAttributes().getLength() > 0
314                     || tskChild.getChildNodes().getLength() > 0))
315                 && GUIUtil.getConfirmation(
316                     focusedTP,
317                     "Would you like to save this task and its values to a file?")) {
318                 if (!tm.saveTask(focusedTP)) {
319                     return;
320                 }
321             }
322             focusedTP.dispose();
323             focusedTP = null;
324             saveMenuItem.setEnabled(false);
325
326         }
327
328         public void windowGainedFocus(WindowEvent JavaDoc evt) {
329             updateFocusedTP(evt);
330         }
331
332         public void windowOpened(WindowEvent JavaDoc evt) {
333             updateFocusedTP(evt);
334         }
335
336         public void windowActivated(WindowEvent JavaDoc evt) {
337             updateFocusedTP(evt);
338         }
339
340         private void updateFocusedTP(WindowEvent JavaDoc evt) {
341             focusedTP = (TaskPanel) evt.getSource();
342             saveMenuItem.setEnabled(true);
343         }
344     }
345
346     private class Splash extends JWindow JavaDoc {
347         Timer JavaDoc timer;
348         Splash() {
349             init();
350         }
351
352         void init() {
353             ImageIcon JavaDoc ic = null;
354             JLabel JavaDoc imagePanel = null;
355             final JProgressBar JavaDoc progressBar = new JProgressBar JavaDoc(0, 100);
356
357             try {
358                 BufferedInputStream JavaDoc bin = new BufferedInputStream JavaDoc(ACT.class.getResourceAsStream("splash.jpg")); //$NON-NLS-1$
359
byte[] img = new byte[bin.available()];
360                 bin.read(img);
361                 ic = new ImageIcon JavaDoc(img);
362                 imagePanel = new JLabel JavaDoc(ic);
363             } catch (Exception JavaDoc e) {
364                 imagePanel = new JLabel JavaDoc();
365             }
366
367             progressBar.setStringPainted(true);
368             progressBar.setString("Starting ACT....");
369             final Border JavaDoc bdr = BorderFactory.createLineBorder(Color.black, 1);
370             imagePanel.setBorder(bdr);
371             progressBar.setBorder(bdr);
372
373             this.getContentPane().setLayout(new BorderLayout JavaDoc());
374             this.getContentPane().add(imagePanel, BorderLayout.CENTER);
375             this.getContentPane().add(progressBar, BorderLayout.SOUTH);
376             //Create a timer.
377
ActionListener JavaDoc poller = new ActionListener JavaDoc() {
378                 public void actionPerformed(ActionEvent JavaDoc evt) {
379                     progressBar.setValue(loadingPercentage);
380                     progressBar.setString(loadingMessage);
381                     if (loadingPercentage >= 100) {
382                         Toolkit.getDefaultToolkit().beep();
383                         timer.stop();
384                         timer = null;
385                         Splash.this.dispose();
386                     }
387                 }
388             };
389             timer = new Timer JavaDoc(200, poller);
390             timer.setRepeats(true);
391             timer.start();
392
393             pack();
394             GUIUtil.centralize(this);
395             show();
396         }
397     }
398
399 }
400
Popular Tags