KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > util > AboutDialog


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.gui.util;
18
19 import java.awt.BorderLayout JavaDoc;
20 import java.awt.Component JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.Font JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.GridBagLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.text.NumberFormat JavaDoc;
33
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.Box JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JFormattedTextField JavaDoc;
40 import javax.swing.JFrame JavaDoc;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JProgressBar JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.JTabbedPane JavaDoc;
46 import javax.swing.KeyStroke JavaDoc;
47 import javax.swing.SwingUtilities JavaDoc;
48 import javax.swing.UIManager JavaDoc;
49
50 import org.columba.core.gui.base.ButtonWithMnemonic;
51 import org.columba.core.io.DiskIO;
52 import org.columba.core.logging.Logging;
53 import org.columba.core.resourceloader.GlobalResourceLoader;
54 import org.columba.core.resourceloader.IconKeys;
55 import org.columba.core.resourceloader.ImageLoader;
56 import org.columba.core.versioninfo.VersionInfo;
57
58 /**
59  * This dialog shows information about Columba.
60  */

61 public class AboutDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
62     /**
63      * serialVersionUID
64      */

65     private static final long serialVersionUID = -4046263935468682814L;
66     public static final String JavaDoc CMD_CLOSE = "CLOSE";
67     private static final String JavaDoc RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog";
68     protected static AboutDialog instance;
69     protected JTabbedPane JavaDoc tabbedPane;
70
71     /**
72      * Creates a new dialog. This constructor is protected because it should
73      * only get called from the static getInstance() method.
74      */

75     protected AboutDialog() {
76         super((JFrame JavaDoc) null, GlobalResourceLoader.getString(
77                 RESOURCE_BUNDLE_PATH, "about", "title"));
78         init();
79     }
80
81     /**
82      * init method
83      */

84     protected void init() {
85         tabbedPane = new JTabbedPane JavaDoc();
86
87         JPanel JavaDoc authorPanel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
88         authorPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
89
90         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
91
92         // Font font = MainInterface.columbaTheme.getControlTextFont();
93
Font JavaDoc font = UIManager.getFont("Label.font");
94
95         JLabel JavaDoc imageLabel = new JLabel JavaDoc(ImageLoader.getMiscIcon("startup.png"));
96         c.gridx = 0;
97         c.gridy = 0;
98         c.anchor = GridBagConstraints.WEST;
99         c.gridwidth = GridBagConstraints.REMAINDER;
100         authorPanel.add(imageLabel, c);
101
102         JLabel JavaDoc versionLabel = new JLabel JavaDoc(GlobalResourceLoader.getString(
103                 RESOURCE_BUNDLE_PATH, "about", "version"));
104
105         if (font != null) {
106             font = font.deriveFont(Font.BOLD);
107             versionLabel.setFont(font);
108         }
109
110         c.gridx = 0;
111         c.gridy = 1;
112         c.gridwidth = 1;
113
114         Component JavaDoc box = Box.createRigidArea(new Dimension JavaDoc(10, 10));
115         authorPanel.add(box, c);
116
117         box = Box.createRigidArea(new Dimension JavaDoc(10, 10));
118         authorPanel.add(box, c);
119
120         c.gridy = 1;
121         authorPanel.add(versionLabel, c);
122
123         c.gridx = 1;
124         box = Box.createRigidArea(new Dimension JavaDoc(5, 15));
125         authorPanel.add(box, c);
126
127         JLabel JavaDoc version = new JLabel JavaDoc(VersionInfo.getVersion());
128         c.gridx = 2;
129         c.gridwidth = GridBagConstraints.REMAINDER;
130         authorPanel.add(version, c);
131
132         JLabel JavaDoc buildDateLabel = new JLabel JavaDoc(GlobalResourceLoader.getString(
133                 RESOURCE_BUNDLE_PATH, "about", "build_date"));
134
135         if (font != null) {
136             font = font.deriveFont(Font.BOLD);
137             buildDateLabel.setFont(font);
138         }
139
140         c.gridx = 0;
141         c.gridy = 2;
142         c.gridwidth = 1;
143
144         box = Box.createRigidArea(new Dimension JavaDoc(10, 10));
145         authorPanel.add(box, c);
146
147         box = Box.createRigidArea(new Dimension JavaDoc(10, 10));
148         authorPanel.add(box, c);
149
150         authorPanel.add(buildDateLabel, c);
151
152         c.gridx = 1;
153         box = Box.createRigidArea(new Dimension JavaDoc(5, 15));
154         authorPanel.add(box, c);
155
156         JLabel JavaDoc buildDate = new JLabel JavaDoc(VersionInfo.getBuildDate().toString());
157         c.gridx = 2;
158         c.gridwidth = GridBagConstraints.REMAINDER;
159         authorPanel.add(buildDate, c);
160
161         JLabel JavaDoc authorLabel = new JLabel JavaDoc(GlobalResourceLoader.getString(
162                 RESOURCE_BUNDLE_PATH, "about", "authors"));
163
164         if (font != null) {
165             font = font.deriveFont(Font.BOLD);
166             authorLabel.setFont(font);
167         }
168
169         c.gridx = 0;
170         c.gridy = 3;
171         c.gridwidth = 1;
172
173         box = Box.createRigidArea(new Dimension JavaDoc(10, 10));
174         authorPanel.add(box, c);
175
176         authorPanel.add(authorLabel, c);
177
178         c.gridx = 1;
179         box = Box.createRigidArea(new Dimension JavaDoc(5, 15));
180         authorPanel.add(box, c);
181
182         AddressLabel a1 = new AddressLabel(
183                 "Frederik Dietz <fdietz@users.sourceforge.net>");
184         c.gridx = 2;
185         c.gridwidth = GridBagConstraints.REMAINDER;
186         authorPanel.add(a1, c);
187
188         AddressLabel a2 = new AddressLabel(
189                 "Timo Stich <tstich@users.sourceforge.net>");
190         c.gridy = 5;
191         authorPanel.add(a2, c);
192
193         JLabel JavaDoc websiteLabel = new JLabel JavaDoc(GlobalResourceLoader.getString(
194                 RESOURCE_BUNDLE_PATH, "about", "website"));
195
196         if (font != null) {
197             websiteLabel.setFont(font);
198         }
199
200         c.gridx = 0;
201         c.gridy = 6;
202         c.gridwidth = 1;
203         authorPanel.add(websiteLabel, c);
204
205         URLLabel websiteUrl = null;
206
207         try {
208             websiteUrl = new URLLabel(new URL JavaDoc("http://columbamail.org"));
209         } catch (MalformedURLException JavaDoc mue) {
210         }
211         // does not occur
212

213         c.gridx = 2;
214         c.gridwidth = GridBagConstraints.REMAINDER;
215         authorPanel.add(websiteUrl, c);
216
217         tabbedPane.addTab(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
218                 "about", "authorsPane"), authorPanel);
219
220         JPanel JavaDoc contributorPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 5));
221         contributorPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11,
222                 11));
223
224         JLabel JavaDoc contributorLabel = new JLabel JavaDoc(GlobalResourceLoader.getString(
225                 RESOURCE_BUNDLE_PATH, "about", "contributorLabel"));
226         contributorPanel.add(contributorLabel, BorderLayout.NORTH);
227
228         InfoViewTextPane textPane = new InfoViewTextPane();
229         try {
230             textPane.setPage(DiskIO.getResourceURL("org/columba/core/aboutbox/CONTRIBUTORS"));
231         } catch (IOException JavaDoc ioe) {
232             textPane.setText(ioe.getLocalizedMessage());
233         }
234         contributorPanel.add(new JScrollPane JavaDoc(textPane));
235         tabbedPane.addTab(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
236                 "about", "contributorPane"), contributorPanel);
237
238         JPanel JavaDoc licensePanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
239         licensePanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
240         textPane = new InfoViewTextPane();
241         try {
242             textPane.setPage(DiskIO.getResourceURL("org/columba/core/aboutbox/LICENSE"));
243         } catch (IOException JavaDoc ioe) {
244             textPane.setText(ioe.getLocalizedMessage());
245         }
246         licensePanel.add(new JScrollPane JavaDoc(textPane));
247         tabbedPane.addTab(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
248                 "about", "license"), licensePanel);
249
250         JPanel JavaDoc ackPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
251         ackPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
252         JLabel JavaDoc ackLabel = new JLabel JavaDoc(GlobalResourceLoader.getString(
253                 RESOURCE_BUNDLE_PATH, "about", "ackLabel"));
254         ackPanel.add(ackLabel, BorderLayout.NORTH);
255         textPane = new InfoViewTextPane();
256         try {
257             textPane.setPage(DiskIO.getResourceURL("org/columba/core/aboutbox/ACKNOWLEDGEMENT"));
258         } catch (IOException JavaDoc ioe) {
259             textPane.setText(ioe.getLocalizedMessage());
260         }
261         ackPanel.add(new JScrollPane JavaDoc(textPane));
262         tabbedPane.addTab(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH,
263                 "about", "ackPane"), ackPanel);
264
265         if (Logging.DEBUG) {
266             tabbedPane.addTab("Memory", new MemoryPanel());
267         }
268
269         getContentPane().add(tabbedPane);
270
271         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 0));
272         buttonPanel.setBorder(BorderFactory.createEmptyBorder(17, 12, 11, 11));
273
274         ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
275                 GlobalResourceLoader.getString("global", "global", "close"));
276         closeButton.setActionCommand(CMD_CLOSE);
277         closeButton.addActionListener(this);
278         buttonPanel.add(closeButton, BorderLayout.EAST);
279         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
280         getRootPane().setDefaultButton(closeButton);
281         getRootPane().registerKeyboardAction(this, CMD_CLOSE,
282                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
283                 JComponent.WHEN_IN_FOCUSED_WINDOW);
284         pack();
285         setSize(new Dimension JavaDoc(450, 350));
286         setLocationRelativeTo(null);
287     }
288
289     /**
290      * Shows the tab with the given index.
291      */

292     public void showTab(int index) {
293         tabbedPane.setSelectedIndex(index);
294     }
295
296     /**
297      * Called when the user clicks the "Close" button.
298      */

299     public void actionPerformed(ActionEvent JavaDoc e) {
300         if (CMD_CLOSE.equals(e.getActionCommand())) {
301             setVisible(false);
302         }
303     }
304
305     /**
306      * AboutDialog method
307      * @return instance of AboutDialog()
308      */

309     public synchronized static AboutDialog getInstance() {
310         if (instance == null) {
311             instance = new AboutDialog();
312         }
313         return instance;
314     }
315
316     /**
317      * MemoryPanel Class
318      * @author fdietz
319      *
320      */

321     protected static class MemoryPanel extends JPanel JavaDoc {
322         /**
323          * serialVersionUID
324          */

325         private static final long serialVersionUID = -711331922225872209L;
326         protected Thread JavaDoc updaterThread;
327         private int currentMemory = -1;
328         private int totalMemory = -1;
329         protected JFormattedTextField JavaDoc currentMemoryTextField;
330         protected JFormattedTextField JavaDoc maxMemoryTextField;
331         protected JProgressBar JavaDoc progressBar;
332         protected JFormattedTextField JavaDoc totalMemoryTextField;
333
334         /**
335          * MemoryPanel method
336          */

337         public MemoryPanel() {
338             super(new GridBagLayout JavaDoc());
339             setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
340             initComponents();
341
342             if (updaterThread == null) {
343                 updaterThread = new MemoryMonitorThread(this);
344                 updaterThread.start();
345             }
346         }
347
348         /**
349          * getCurrentMemory method
350          * @return currentMemory
351          */

352         public int getCurrentMemory() {
353             return currentMemory;
354         }
355
356         /**
357          * setCurrentMemory method
358          * @param mem
359          */

360         public void setCurrentMemory(int mem) {
361             currentMemory = mem;
362             currentMemoryTextField.setValue(new Integer JavaDoc(mem));
363             progressBar.setValue(mem);
364         }
365
366         /**
367          * getTotalMemory method
368          * @return totalMemory
369          */

370         public int getTotalMemory() {
371             return totalMemory;
372         }
373
374         /**
375          * setTotalMemory method
376          * @param mem
377          */

378         public void setTotalMemory(int mem) {
379             totalMemory = mem;
380             totalMemoryTextField.setValue(new Integer JavaDoc(mem));
381             progressBar.setMaximum(mem);
382         }
383
384         /**
385          * initComponents method
386          */

387         private void initComponents() {
388             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
389
390             currentMemoryTextField = new JFormattedTextField JavaDoc(NumberFormat
391                     .getInstance());
392             totalMemoryTextField = new JFormattedTextField JavaDoc(NumberFormat
393                     .getInstance());
394             maxMemoryTextField = new JFormattedTextField JavaDoc();
395
396             JButton JavaDoc gcButton = new JButton JavaDoc(ImageLoader
397                     .getSmallIcon(IconKeys.EDIT_DELETE));
398
399             JLabel JavaDoc currentMemoryLabel = new JLabel JavaDoc("Used:");
400             c.gridx = 0;
401             c.gridy = 0;
402             c.anchor = GridBagConstraints.WEST;
403             add(currentMemoryLabel, c);
404
405             currentMemoryTextField.setColumns(5);
406             currentMemoryTextField.setEditable(false);
407             c.gridx = 1;
408             c.insets = new Insets JavaDoc(0, 4, 0, 4);
409             add(currentMemoryTextField, c);
410
411             JLabel JavaDoc currentMemoryKBLabel = new JLabel JavaDoc("KB");
412             c.gridx = 2;
413             c.insets = new Insets JavaDoc(0, 0, 0, 0);
414             add(currentMemoryKBLabel, c);
415
416             JLabel JavaDoc totalMemoryLabel = new JLabel JavaDoc("Total:");
417             c.gridx = 3;
418             c.insets = new Insets JavaDoc(0, 10, 0, 0);
419             add(totalMemoryLabel, c);
420
421             totalMemoryTextField.setColumns(5);
422             totalMemoryTextField.setEditable(false);
423             c.gridx = 4;
424             c.insets = new Insets JavaDoc(0, 4, 0, 4);
425             add(totalMemoryTextField, c);
426
427             JLabel JavaDoc totalMemoryKBLabel = new JLabel JavaDoc("KB");
428             c.gridx = 5;
429             c.insets = new Insets JavaDoc(0, 0, 0, 0);
430             add(totalMemoryKBLabel, c);
431
432             JLabel JavaDoc maxMemoryLabel = new JLabel JavaDoc("VM Max:");
433             c.gridx = 0;
434             c.gridy = 1;
435             c.insets = new Insets JavaDoc(6, 0, 0, 0);
436             add(maxMemoryLabel, c);
437
438             maxMemoryTextField.setColumns(5);
439             maxMemoryTextField.setEditable(false);
440             maxMemoryTextField.setValue(new Integer JavaDoc((int) (Runtime.getRuntime()
441                     .maxMemory() / 1000)));
442             c.gridx = 1;
443             c.insets = new Insets JavaDoc(4, 4, 0, 4);
444             add(maxMemoryTextField, c);
445
446             JLabel JavaDoc maxMemoryKBLabel = new JLabel JavaDoc("KB");
447             c.gridx = 2;
448             c.insets = new Insets JavaDoc(0, 0, 0, 0);
449             add(maxMemoryKBLabel, c);
450
451             progressBar = new JProgressBar JavaDoc();
452             progressBar.setPreferredSize(gcButton.getPreferredSize());
453             progressBar.setStringPainted(true);
454             c.gridx = 0;
455             c.gridy = 2;
456             c.insets = new Insets JavaDoc(10, 0, 0, 0);
457             c.fill = GridBagConstraints.HORIZONTAL;
458             c.gridwidth = 5;
459             add(progressBar, c);
460
461             gcButton.setContentAreaFilled(false);
462             gcButton.addActionListener(new ActionListener JavaDoc() {
463                 public void actionPerformed(ActionEvent JavaDoc evt) {
464                     System.gc();
465                 }
466             });
467             c.gridx = 5;
468             c.gridwidth = 1;
469             c.insets = new Insets JavaDoc(10, 6, 0, 0);
470             c.fill = GridBagConstraints.NONE;
471             add(gcButton, c);
472         }
473     }
474
475     /**
476      * MemoryMonitorThread Class
477      * @author fdietz
478      */

479     protected static class MemoryMonitorThread extends Thread JavaDoc {
480         protected static final int MEMORY_UPDATE_THRESHOLD = 50; // 50k
481
protected MemoryPanel memoryPanel;
482         protected Runtime JavaDoc runtime = Runtime.getRuntime();
483
484         /**
485          * MemoryMonitorThread method
486          * @param memoryPanel
487          */

488         public MemoryMonitorThread(MemoryPanel memoryPanel) {
489             this.memoryPanel = memoryPanel;
490             setPriority(Thread.MIN_PRIORITY);
491             setDaemon(true);
492         }
493
494         /* (non-Javadoc)
495          * @see java.lang.Thread#run()
496          */

497         public void run() {
498             while (!isInterrupted()) {
499                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
500                     public void run() {
501                         updateDisplay();
502                     }
503                 });
504
505                 try {
506                     Thread.sleep(750);
507                 } catch (InterruptedException JavaDoc ie) {
508                     ie.printStackTrace();
509                 }
510             }
511         }
512
513         /**
514          * updateDisplay method
515          */

516         protected void updateDisplay() {
517             int totalMem = (int) (runtime.totalMemory() / 1000);
518             int currMem = (int) (totalMem - (runtime.freeMemory() / 1000));
519
520             if ((memoryPanel.getTotalMemory() < (totalMem - MEMORY_UPDATE_THRESHOLD))
521                     || (memoryPanel.getTotalMemory() > (totalMem + MEMORY_UPDATE_THRESHOLD))) {
522                 memoryPanel.setTotalMemory(totalMem);
523             }
524
525             if ((memoryPanel.getCurrentMemory() < (currMem - MEMORY_UPDATE_THRESHOLD))
526                     || (memoryPanel.getCurrentMemory() > (currMem + MEMORY_UPDATE_THRESHOLD))) {
527                 memoryPanel.setCurrentMemory(currMem);
528             }
529         }
530     }
531 }
532
Popular Tags