KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > qfs > apps > qflog > MainFrame


1 // {{{ copyright
2

3 /********************************************************************
4  *
5  * $Id: MainFrame.java,v 1.16 2000/07/05 14:07:43 gs Exp $
6  *
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is qfs.de code.
18  *
19  * The Initial Developer of the Original Code is Gregor Schmid.
20  * Portions created by Gregor Schmid are
21  * Copyright (C) 1999 Quality First Software, Gregor Schmid.
22  * All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  *******************************************************************/

27
28 // }}}
29

30 package de.qfs.apps.qflog;
31
32 // {{{ imports
33

34 import java.awt.Event JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.awt.event.MouseEvent JavaDoc;
39 import java.awt.event.MouseAdapter JavaDoc;
40 import java.awt.BorderLayout JavaDoc;
41 import java.awt.Point JavaDoc;
42
43 import javax.swing.ImageIcon JavaDoc;
44 import javax.swing.JComponent JavaDoc;
45 import javax.swing.JFrame JavaDoc;
46 import javax.swing.JMenuBar JavaDoc;
47 import javax.swing.JMenu JavaDoc;
48 import javax.swing.JMenuItem JavaDoc;
49 import javax.swing.JPanel JavaDoc;
50 import javax.swing.JScrollPane JavaDoc;
51 import javax.swing.JTable JavaDoc;
52 import javax.swing.KeyStroke JavaDoc;
53 import javax.swing.WindowConstants JavaDoc;
54 import javax.swing.table.JTableHeader JavaDoc;
55
56 import de.qfs.lib.config.Configurator;
57 import de.qfs.lib.config.ConfigWrapper;
58 import de.qfs.lib.gui.DefaultTableModelSorter;
59 import de.qfs.lib.gui.FocusFixer;
60 import de.qfs.lib.gui.SortedTableHelper;
61 import de.qfs.lib.log.Log;
62 import de.qfs.lib.log.Logger;
63 import de.qfs.lib.util.MapResourceBundle;
64
65 import de.qfs.apps.qflog.command.CommandFileExit;
66 import de.qfs.apps.qflog.command.CommandFileOpen;
67 import de.qfs.apps.qflog.command.CommandFileSaveAs;
68 import de.qfs.apps.qflog.command.CommandEditRemove;
69 import de.qfs.apps.qflog.command.CommandEditOptions;
70 import de.qfs.apps.qflog.command.CommandOpenLogView;
71 import de.qfs.apps.qflog.command.CommandHelpAbout;
72
73 // }}}
74

75 /**
76  * This is the main Frame of the qflog application.
77  *
78  * @author Gregor Schmid
79  * @version $Revision: 1.16 $
80  */

81 public class MainFrame extends JFrame JavaDoc
82 {
83     // {{{ variables
84

85     /**
86      * The Logger used for logging.
87      */

88     private final static Logger logger = new Logger (MainFrame.class);
89
90     /**
91      * The Configurator for the MainFrame.
92      */

93     private Configurator cfg = new Configurator ("LogFrame");
94
95     /**
96      * The table to display.
97      */

98     private JTable JavaDoc table;
99
100     // }}}
101

102     // {{{ constructor
103

104     /**
105      * Create a new MainFrame.
106      */

107     public MainFrame ()
108     {
109         if (logger.level >= Log.MTD) {
110             logger.log(Log.MTD, "MainFrame()", "");
111         }
112     }
113
114     // }}}
115
// {{{ initialization
116

117     // {{{ init
118

119     /**
120      * Initialize the MainFrame.
121      */

122     public void init ()
123     {
124         if (logger.level >= Log.MTD) {
125             logger.log(Log.MTD, "init()", "");
126         }
127         setTitle("qflog");
128         ImageIcon JavaDoc icon = (ImageIcon JavaDoc)
129             App.getResources().getIcon("logServer.icon", null);
130         if (icon != null) {
131             setIconImage(icon.getImage());
132         }
133
134         initMenu();
135         initTable();
136
137         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
138         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
139             public void windowClosing (java.awt.event.WindowEvent JavaDoc e) {
140                 App.getCommandDistributor().assignCommand
141                     (new CommandFileExit (e.getSource()));
142             }
143         });
144
145         cfg.add(ConfigWrapper.makeWrapper(this, "MainFrame"));
146         FocusFixer.instance().addWindow(this);
147     }
148
149     // }}}
150
// {{{ actions
151

152     /**
153      * Exit the application.
154      */

155     protected ActionListener JavaDoc actionFileExit = new ActionListener JavaDoc () {
156         public void actionPerformed (ActionEvent JavaDoc e) {
157             App.getCommandDistributor().assignCommand
158                 (new CommandFileExit (MainFrame.this));
159         }
160     };
161
162     /**
163      * Open a log file.
164      */

165     protected ActionListener JavaDoc actionFileOpen = new ActionListener JavaDoc () {
166         public void actionPerformed (ActionEvent JavaDoc e) {
167             App.getCommandDistributor().assignCommand
168                 (new CommandFileOpen (MainFrame.this, MainFrame.this));
169         }
170     };
171
172     // protected ActionListener actionFileSave = new ActionListener () {
173
// public void actionPerformed (ActionEvent e) {
174
// App.getCommandDistributor().assignCommand
175
// (new CommandFileSave (MainFrame.this));
176
// }
177
// };
178

179     /**
180      * Save a log file.
181      */

182     protected ActionListener JavaDoc actionFileSaveAs = new ActionListener JavaDoc () {
183         public void actionPerformed (ActionEvent JavaDoc e) {
184             App.getCommandDistributor().assignCommand
185                 (new CommandFileSaveAs (MainFrame.this, MainFrame.this,
186                                         table));
187         }
188     };
189
190     /**
191      * Remove a listener.
192      */

193     protected ActionListener JavaDoc actionEditRemove = new ActionListener JavaDoc () {
194         public void actionPerformed (ActionEvent JavaDoc e) {
195             App.getCommandDistributor().assignCommand
196                 (new CommandEditRemove (MainFrame.this, MainFrame.this,
197                                         table));
198         }
199     };
200
201     /**
202      * Edit the Options.
203      */

204     protected ActionListener JavaDoc actionEditOptions = new ActionListener JavaDoc () {
205         public void actionPerformed (ActionEvent JavaDoc e) {
206             App.getCommandDistributor().assignCommand
207                 (new CommandEditOptions (MainFrame.this, MainFrame.this));
208         }
209     };
210
211     /**
212      * Open a view for one listener.
213      */

214     protected ActionListener JavaDoc actionOpenLogView = new ActionListener JavaDoc () {
215         public void actionPerformed (ActionEvent JavaDoc e) {
216             App.getCommandDistributor().assignCommand
217                 (new CommandOpenLogView (MainFrame.this, table));
218         }
219     };
220
221     /**
222      * Show version information
223      */

224     protected ActionListener JavaDoc actionHelpAbout = new ActionListener JavaDoc () {
225         public void actionPerformed (ActionEvent JavaDoc e) {
226             App.getCommandDistributor().assignCommand
227                 (new CommandHelpAbout (MainFrame.this, MainFrame.this));
228         }
229     };
230
231     // }}}
232
// {{{ menus
233

234     // {{{ initMenu
235

236     /**
237      * Initialize the menus.
238      */

239     protected void initMenu ()
240     {
241         if (logger.level >= Log.MTD) {
242             logger.log(Log.MTD, "initMenu()", "");
243         }
244         JMenuBar JavaDoc mb = new JMenuBar JavaDoc ();
245         setJMenuBar(mb);
246
247         // File
248
JMenu JavaDoc mFile = makeMenu("file");
249         mb.add(mFile);
250
251         JMenuItem JavaDoc mi = makeMenuItem ("fileOpen", actionFileOpen);
252         mFile.add(mi);
253
254         mi = makeMenuItem ("fileSaveAs", actionFileSaveAs);
255         mFile.add(mi);
256
257         mi = makeMenuItem ("fileExit", actionFileExit);
258         mFile.add(mi);
259
260         // Edit
261
JMenu JavaDoc mEdit = makeMenu("edit");
262         mb.add(mEdit);
263
264         mi = makeMenuItem ("editShow", actionOpenLogView);
265         mEdit.add(mi);
266         mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
267
268         mi = makeMenuItem ("editRemove", actionEditRemove);
269         mEdit.add(mi);
270         mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
271
272         mi = makeMenuItem ("editOptions", actionEditOptions);
273         mEdit.add(mi);
274
275         // Edit
276
JMenu JavaDoc mHelp = makeMenu("help");
277         mb.add(mHelp);
278
279         mi = makeMenuItem ("helpAbout", actionHelpAbout);
280         mHelp.add(mi);
281
282     }
283
284     // }}}
285
// {{{ makeMenu
286

287     /**
288      * Build a menu from information in the application's resources.
289      *
290      * @param resource The base name for the resource Strings to use.
291      *
292      * @return The new JMenu.
293      */

294     protected JMenu JavaDoc makeMenu (String JavaDoc resource)
295     {
296         if (logger.level >= Log.MTD) {
297             logger.log(Log.MTD, "makeMenu(String)",
298                        logger.level < Log.MTDDETAIL ? "" :
299                        "resource: " + resource);
300         }
301         MapResourceBundle rb = App.instance().getResources();
302
303         JMenu JavaDoc menu = new JMenu JavaDoc (rb.getString("menu." + resource + ".name",
304                                              "MISSING: " + resource));
305         String JavaDoc mnemo = rb.getString("menu." + resource + ".mnemonic", null);
306         if (mnemo != null) {
307             menu.setMnemonic(mnemo.charAt(0));
308         }
309         return menu;
310     }
311
312     // }}}
313
// {{{ makeMenuItem
314

315     /**
316      * Build a menu item from information in the application's resources.
317      *
318      * @param resource The base name for the resource Strings to use.
319      * @param action The action to associate with the menu item.
320      *
321      * @return The new JMenuItem.
322      */

323     protected JMenuItem JavaDoc makeMenuItem (String JavaDoc resource, ActionListener JavaDoc action)
324     {
325         if (logger.level >= Log.MTD) {
326             logger.log(Log.MTD, "makeMenuItem(String,ActionListener)",
327                        logger.level < Log.MTDDETAIL ? "" :
328                        "resource: " + resource + ", " +
329                        "action: " + action);
330         }
331         MapResourceBundle rb = App.instance().getResources();
332
333         JMenuItem JavaDoc mi = new JMenuItem JavaDoc
334             (rb.getString("action." + resource + ".name",
335                           "MISSING NAME: " + resource));
336         String JavaDoc mnemo = rb.getString("action." + resource + ".mnemonic", null);
337         if (mnemo != null) {
338             mi.setMnemonic(mnemo.charAt(0));
339         }
340         mi.setActionCommand
341             (rb.getString("action." + resource + ".command",
342                           "MISSING COMMAND: " + resource));
343         mi.addActionListener(action);
344
345         return mi;
346
347     }
348
349     // }}}
350

351     // }}}
352
// {{{ initTable
353

354     /**
355      * Initialize the table.
356      */

357     private void initTable()
358     {
359         if (logger.level >= Log.MTD) {
360             logger.log(Log.MTD, "initTable()", "");
361         }
362
363         getContentPane().setLayout(new BorderLayout JavaDoc ());
364
365         table = new ViewTable (Model.instance());
366
367         JTableHeader JavaDoc header = new JTableHeader JavaDoc (table.getColumnModel()) {
368             public boolean isFocusTraversable()
369             {
370                 return false;
371             }
372         };
373         header.setRequestFocusEnabled(false);
374         table.setTableHeader(header);
375
376         SortedTableHelper helper = new SortedTableHelper (table);
377         helper.prepareTable();
378         helper.saveSelection(true);
379         cfg.add(ConfigWrapper.makeWrapper(table, "Table"));
380         DefaultTableModelSorter sorter =
381             (DefaultTableModelSorter) helper.getTableModelSorter();
382         sorter.setConfigurableName("sorter");
383         cfg.add(sorter);
384
385         JPanel JavaDoc bg = new JPanel JavaDoc ();
386         bg.setLayout(new BorderLayout JavaDoc ());
387         bg.setBackground(table.getBackground());
388         bg.add(new JScrollPane JavaDoc (table), BorderLayout.CENTER);
389         getContentPane().add(bg, BorderLayout.CENTER);
390
391         table.registerKeyboardAction
392             (actionOpenLogView, "open",
393              KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
394              JComponent.WHEN_IN_FOCUSED_WINDOW);
395
396         table.addMouseListener(new MouseAdapter JavaDoc () {
397             public void mousePressed(MouseEvent JavaDoc e) {
398                 if (e.getClickCount() > 1) {
399                     int row = table.rowAtPoint(new Point JavaDoc (e.getX(), e.getY()));
400                     if (row >= 0) {
401                         App.getCommandDistributor().assignCommand
402                             (new CommandOpenLogView (e.getSource(), table,
403                                                      row));
404                     }
405                 }
406             }
407         });
408     }
409
410     // }}}
411

412     // }}}
413

414     // {{{ getTable
415

416     /**
417      * Get the table.
418      *
419      * @return The table.
420      */

421     public final JTable JavaDoc getTable()
422     {
423         return table;
424     }
425
426     // }}}
427
// {{{ getConfigurator
428

429     /**
430      * Get the configurator of the LogFrame.
431      *
432      * @return The configurator of the LogFrame.
433      */

434     public final Configurator getConfigurator()
435     {
436         if (logger.level >= Log.MTD) {
437             logger.log(Log.MTD, "getConfigurator()", "");
438         }
439         return cfg;
440     }
441
442     // }}}
443

444 }
445
Popular Tags