001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.gui.util;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Component;
021: import java.awt.Dimension;
022: import java.awt.Font;
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.Insets;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029: import java.io.IOException;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.text.NumberFormat;
033:
034: import javax.swing.BorderFactory;
035: import javax.swing.Box;
036: import javax.swing.JButton;
037: import javax.swing.JComponent;
038: import javax.swing.JDialog;
039: import javax.swing.JFormattedTextField;
040: import javax.swing.JFrame;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.JProgressBar;
044: import javax.swing.JScrollPane;
045: import javax.swing.JTabbedPane;
046: import javax.swing.KeyStroke;
047: import javax.swing.SwingUtilities;
048: import javax.swing.UIManager;
049:
050: import org.columba.core.gui.base.ButtonWithMnemonic;
051: import org.columba.core.io.DiskIO;
052: import org.columba.core.logging.Logging;
053: import org.columba.core.resourceloader.GlobalResourceLoader;
054: import org.columba.core.resourceloader.IconKeys;
055: import org.columba.core.resourceloader.ImageLoader;
056: import org.columba.core.versioninfo.VersionInfo;
057:
058: /**
059: * This dialog shows information about Columba.
060: */
061: public class AboutDialog extends JDialog implements ActionListener {
062: /**
063: * serialVersionUID
064: */
065: private static final long serialVersionUID = -4046263935468682814L;
066: public static final String CMD_CLOSE = "CLOSE";
067: private static final String RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog";
068: protected static AboutDialog instance;
069: protected JTabbedPane tabbedPane;
070:
071: /**
072: * Creates a new dialog. This constructor is protected because it should
073: * only get called from the static getInstance() method.
074: */
075: protected AboutDialog() {
076: super ((JFrame) null, GlobalResourceLoader.getString(
077: RESOURCE_BUNDLE_PATH, "about", "title"));
078: init();
079: }
080:
081: /**
082: * init method
083: */
084: protected void init() {
085: tabbedPane = new JTabbedPane();
086:
087: JPanel authorPanel = new JPanel(new GridBagLayout());
088: authorPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
089: 11, 11));
090:
091: GridBagConstraints c = new GridBagConstraints();
092:
093: // Font font = MainInterface.columbaTheme.getControlTextFont();
094: Font font = UIManager.getFont("Label.font");
095:
096: JLabel imageLabel = new JLabel(ImageLoader
097: .getMiscIcon("startup.png"));
098: c.gridx = 0;
099: c.gridy = 0;
100: c.anchor = GridBagConstraints.WEST;
101: c.gridwidth = GridBagConstraints.REMAINDER;
102: authorPanel.add(imageLabel, c);
103:
104: JLabel versionLabel = new JLabel(GlobalResourceLoader
105: .getString(RESOURCE_BUNDLE_PATH, "about", "version"));
106:
107: if (font != null) {
108: font = font.deriveFont(Font.BOLD);
109: versionLabel.setFont(font);
110: }
111:
112: c.gridx = 0;
113: c.gridy = 1;
114: c.gridwidth = 1;
115:
116: Component box = Box.createRigidArea(new Dimension(10, 10));
117: authorPanel.add(box, c);
118:
119: box = Box.createRigidArea(new Dimension(10, 10));
120: authorPanel.add(box, c);
121:
122: c.gridy = 1;
123: authorPanel.add(versionLabel, c);
124:
125: c.gridx = 1;
126: box = Box.createRigidArea(new Dimension(5, 15));
127: authorPanel.add(box, c);
128:
129: JLabel version = new JLabel(VersionInfo.getVersion());
130: c.gridx = 2;
131: c.gridwidth = GridBagConstraints.REMAINDER;
132: authorPanel.add(version, c);
133:
134: JLabel buildDateLabel = new JLabel(GlobalResourceLoader
135: .getString(RESOURCE_BUNDLE_PATH, "about", "build_date"));
136:
137: if (font != null) {
138: font = font.deriveFont(Font.BOLD);
139: buildDateLabel.setFont(font);
140: }
141:
142: c.gridx = 0;
143: c.gridy = 2;
144: c.gridwidth = 1;
145:
146: box = Box.createRigidArea(new Dimension(10, 10));
147: authorPanel.add(box, c);
148:
149: box = Box.createRigidArea(new Dimension(10, 10));
150: authorPanel.add(box, c);
151:
152: authorPanel.add(buildDateLabel, c);
153:
154: c.gridx = 1;
155: box = Box.createRigidArea(new Dimension(5, 15));
156: authorPanel.add(box, c);
157:
158: JLabel buildDate = new JLabel(VersionInfo.getBuildDate()
159: .toString());
160: c.gridx = 2;
161: c.gridwidth = GridBagConstraints.REMAINDER;
162: authorPanel.add(buildDate, c);
163:
164: JLabel authorLabel = new JLabel(GlobalResourceLoader.getString(
165: RESOURCE_BUNDLE_PATH, "about", "authors"));
166:
167: if (font != null) {
168: font = font.deriveFont(Font.BOLD);
169: authorLabel.setFont(font);
170: }
171:
172: c.gridx = 0;
173: c.gridy = 3;
174: c.gridwidth = 1;
175:
176: box = Box.createRigidArea(new Dimension(10, 10));
177: authorPanel.add(box, c);
178:
179: authorPanel.add(authorLabel, c);
180:
181: c.gridx = 1;
182: box = Box.createRigidArea(new Dimension(5, 15));
183: authorPanel.add(box, c);
184:
185: AddressLabel a1 = new AddressLabel(
186: "Frederik Dietz <fdietz@users.sourceforge.net>");
187: c.gridx = 2;
188: c.gridwidth = GridBagConstraints.REMAINDER;
189: authorPanel.add(a1, c);
190:
191: AddressLabel a2 = new AddressLabel(
192: "Timo Stich <tstich@users.sourceforge.net>");
193: c.gridy = 5;
194: authorPanel.add(a2, c);
195:
196: JLabel websiteLabel = new JLabel(GlobalResourceLoader
197: .getString(RESOURCE_BUNDLE_PATH, "about", "website"));
198:
199: if (font != null) {
200: websiteLabel.setFont(font);
201: }
202:
203: c.gridx = 0;
204: c.gridy = 6;
205: c.gridwidth = 1;
206: authorPanel.add(websiteLabel, c);
207:
208: URLLabel websiteUrl = null;
209:
210: try {
211: websiteUrl = new URLLabel(new URL("http://columbamail.org"));
212: } catch (MalformedURLException mue) {
213: }
214: // does not occur
215:
216: c.gridx = 2;
217: c.gridwidth = GridBagConstraints.REMAINDER;
218: authorPanel.add(websiteUrl, c);
219:
220: tabbedPane.addTab(GlobalResourceLoader.getString(
221: RESOURCE_BUNDLE_PATH, "about", "authorsPane"),
222: authorPanel);
223:
224: JPanel contributorPanel = new JPanel(new BorderLayout(0, 5));
225: contributorPanel.setBorder(BorderFactory.createEmptyBorder(12,
226: 12, 11, 11));
227:
228: JLabel contributorLabel = new JLabel(GlobalResourceLoader
229: .getString(RESOURCE_BUNDLE_PATH, "about",
230: "contributorLabel"));
231: contributorPanel.add(contributorLabel, BorderLayout.NORTH);
232:
233: InfoViewTextPane textPane = new InfoViewTextPane();
234: try {
235: textPane
236: .setPage(DiskIO
237: .getResourceURL("org/columba/core/aboutbox/CONTRIBUTORS"));
238: } catch (IOException ioe) {
239: textPane.setText(ioe.getLocalizedMessage());
240: }
241: contributorPanel.add(new JScrollPane(textPane));
242: tabbedPane.addTab(GlobalResourceLoader.getString(
243: RESOURCE_BUNDLE_PATH, "about", "contributorPane"),
244: contributorPanel);
245:
246: JPanel licensePanel = new JPanel(new BorderLayout());
247: licensePanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
248: 11, 11));
249: textPane = new InfoViewTextPane();
250: try {
251: textPane
252: .setPage(DiskIO
253: .getResourceURL("org/columba/core/aboutbox/LICENSE"));
254: } catch (IOException ioe) {
255: textPane.setText(ioe.getLocalizedMessage());
256: }
257: licensePanel.add(new JScrollPane(textPane));
258: tabbedPane
259: .addTab(GlobalResourceLoader.getString(
260: RESOURCE_BUNDLE_PATH, "about", "license"),
261: licensePanel);
262:
263: JPanel ackPanel = new JPanel(new BorderLayout());
264: ackPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11,
265: 11));
266: JLabel ackLabel = new JLabel(GlobalResourceLoader.getString(
267: RESOURCE_BUNDLE_PATH, "about", "ackLabel"));
268: ackPanel.add(ackLabel, BorderLayout.NORTH);
269: textPane = new InfoViewTextPane();
270: try {
271: textPane
272: .setPage(DiskIO
273: .getResourceURL("org/columba/core/aboutbox/ACKNOWLEDGEMENT"));
274: } catch (IOException ioe) {
275: textPane.setText(ioe.getLocalizedMessage());
276: }
277: ackPanel.add(new JScrollPane(textPane));
278: tabbedPane.addTab(GlobalResourceLoader.getString(
279: RESOURCE_BUNDLE_PATH, "about", "ackPane"), ackPanel);
280:
281: if (Logging.DEBUG) {
282: tabbedPane.addTab("Memory", new MemoryPanel());
283: }
284:
285: getContentPane().add(tabbedPane);
286:
287: JPanel buttonPanel = new JPanel(new BorderLayout(0, 0));
288: buttonPanel.setBorder(BorderFactory.createEmptyBorder(17, 12,
289: 11, 11));
290:
291: ButtonWithMnemonic closeButton = new ButtonWithMnemonic(
292: GlobalResourceLoader.getString("global", "global",
293: "close"));
294: closeButton.setActionCommand(CMD_CLOSE);
295: closeButton.addActionListener(this );
296: buttonPanel.add(closeButton, BorderLayout.EAST);
297: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
298: getRootPane().setDefaultButton(closeButton);
299: getRootPane().registerKeyboardAction(this , CMD_CLOSE,
300: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
301: JComponent.WHEN_IN_FOCUSED_WINDOW);
302: pack();
303: setSize(new Dimension(450, 350));
304: setLocationRelativeTo(null);
305: }
306:
307: /**
308: * Shows the tab with the given index.
309: */
310: public void showTab(int index) {
311: tabbedPane.setSelectedIndex(index);
312: }
313:
314: /**
315: * Called when the user clicks the "Close" button.
316: */
317: public void actionPerformed(ActionEvent e) {
318: if (CMD_CLOSE.equals(e.getActionCommand())) {
319: setVisible(false);
320: }
321: }
322:
323: /**
324: * AboutDialog method
325: * @return instance of AboutDialog()
326: */
327: public synchronized static AboutDialog getInstance() {
328: if (instance == null) {
329: instance = new AboutDialog();
330: }
331: return instance;
332: }
333:
334: /**
335: * MemoryPanel Class
336: * @author fdietz
337: *
338: */
339: protected static class MemoryPanel extends JPanel {
340: /**
341: * serialVersionUID
342: */
343: private static final long serialVersionUID = -711331922225872209L;
344: protected Thread updaterThread;
345: private int currentMemory = -1;
346: private int totalMemory = -1;
347: protected JFormattedTextField currentMemoryTextField;
348: protected JFormattedTextField maxMemoryTextField;
349: protected JProgressBar progressBar;
350: protected JFormattedTextField totalMemoryTextField;
351:
352: /**
353: * MemoryPanel method
354: */
355: public MemoryPanel() {
356: super (new GridBagLayout());
357: setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));
358: initComponents();
359:
360: if (updaterThread == null) {
361: updaterThread = new MemoryMonitorThread(this );
362: updaterThread.start();
363: }
364: }
365:
366: /**
367: * getCurrentMemory method
368: * @return currentMemory
369: */
370: public int getCurrentMemory() {
371: return currentMemory;
372: }
373:
374: /**
375: * setCurrentMemory method
376: * @param mem
377: */
378: public void setCurrentMemory(int mem) {
379: currentMemory = mem;
380: currentMemoryTextField.setValue(new Integer(mem));
381: progressBar.setValue(mem);
382: }
383:
384: /**
385: * getTotalMemory method
386: * @return totalMemory
387: */
388: public int getTotalMemory() {
389: return totalMemory;
390: }
391:
392: /**
393: * setTotalMemory method
394: * @param mem
395: */
396: public void setTotalMemory(int mem) {
397: totalMemory = mem;
398: totalMemoryTextField.setValue(new Integer(mem));
399: progressBar.setMaximum(mem);
400: }
401:
402: /**
403: * initComponents method
404: */
405: private void initComponents() {
406: GridBagConstraints c = new GridBagConstraints();
407:
408: currentMemoryTextField = new JFormattedTextField(
409: NumberFormat.getInstance());
410: totalMemoryTextField = new JFormattedTextField(NumberFormat
411: .getInstance());
412: maxMemoryTextField = new JFormattedTextField();
413:
414: JButton gcButton = new JButton(ImageLoader
415: .getSmallIcon(IconKeys.EDIT_DELETE));
416:
417: JLabel currentMemoryLabel = new JLabel("Used:");
418: c.gridx = 0;
419: c.gridy = 0;
420: c.anchor = GridBagConstraints.WEST;
421: add(currentMemoryLabel, c);
422:
423: currentMemoryTextField.setColumns(5);
424: currentMemoryTextField.setEditable(false);
425: c.gridx = 1;
426: c.insets = new Insets(0, 4, 0, 4);
427: add(currentMemoryTextField, c);
428:
429: JLabel currentMemoryKBLabel = new JLabel("KB");
430: c.gridx = 2;
431: c.insets = new Insets(0, 0, 0, 0);
432: add(currentMemoryKBLabel, c);
433:
434: JLabel totalMemoryLabel = new JLabel("Total:");
435: c.gridx = 3;
436: c.insets = new Insets(0, 10, 0, 0);
437: add(totalMemoryLabel, c);
438:
439: totalMemoryTextField.setColumns(5);
440: totalMemoryTextField.setEditable(false);
441: c.gridx = 4;
442: c.insets = new Insets(0, 4, 0, 4);
443: add(totalMemoryTextField, c);
444:
445: JLabel totalMemoryKBLabel = new JLabel("KB");
446: c.gridx = 5;
447: c.insets = new Insets(0, 0, 0, 0);
448: add(totalMemoryKBLabel, c);
449:
450: JLabel maxMemoryLabel = new JLabel("VM Max:");
451: c.gridx = 0;
452: c.gridy = 1;
453: c.insets = new Insets(6, 0, 0, 0);
454: add(maxMemoryLabel, c);
455:
456: maxMemoryTextField.setColumns(5);
457: maxMemoryTextField.setEditable(false);
458: maxMemoryTextField.setValue(new Integer((int) (Runtime
459: .getRuntime().maxMemory() / 1000)));
460: c.gridx = 1;
461: c.insets = new Insets(4, 4, 0, 4);
462: add(maxMemoryTextField, c);
463:
464: JLabel maxMemoryKBLabel = new JLabel("KB");
465: c.gridx = 2;
466: c.insets = new Insets(0, 0, 0, 0);
467: add(maxMemoryKBLabel, c);
468:
469: progressBar = new JProgressBar();
470: progressBar.setPreferredSize(gcButton.getPreferredSize());
471: progressBar.setStringPainted(true);
472: c.gridx = 0;
473: c.gridy = 2;
474: c.insets = new Insets(10, 0, 0, 0);
475: c.fill = GridBagConstraints.HORIZONTAL;
476: c.gridwidth = 5;
477: add(progressBar, c);
478:
479: gcButton.setContentAreaFilled(false);
480: gcButton.addActionListener(new ActionListener() {
481: public void actionPerformed(ActionEvent evt) {
482: System.gc();
483: }
484: });
485: c.gridx = 5;
486: c.gridwidth = 1;
487: c.insets = new Insets(10, 6, 0, 0);
488: c.fill = GridBagConstraints.NONE;
489: add(gcButton, c);
490: }
491: }
492:
493: /**
494: * MemoryMonitorThread Class
495: * @author fdietz
496: */
497: protected static class MemoryMonitorThread extends Thread {
498: protected static final int MEMORY_UPDATE_THRESHOLD = 50; // 50k
499: protected MemoryPanel memoryPanel;
500: protected Runtime runtime = Runtime.getRuntime();
501:
502: /**
503: * MemoryMonitorThread method
504: * @param memoryPanel
505: */
506: public MemoryMonitorThread(MemoryPanel memoryPanel) {
507: this .memoryPanel = memoryPanel;
508: setPriority(Thread.MIN_PRIORITY);
509: setDaemon(true);
510: }
511:
512: /* (non-Javadoc)
513: * @see java.lang.Thread#run()
514: */
515: public void run() {
516: while (!isInterrupted()) {
517: SwingUtilities.invokeLater(new Runnable() {
518: public void run() {
519: updateDisplay();
520: }
521: });
522:
523: try {
524: Thread.sleep(750);
525: } catch (InterruptedException ie) {
526: ie.printStackTrace();
527: }
528: }
529: }
530:
531: /**
532: * updateDisplay method
533: */
534: protected void updateDisplay() {
535: int totalMem = (int) (runtime.totalMemory() / 1000);
536: int currMem = (int) (totalMem - (runtime.freeMemory() / 1000));
537:
538: if ((memoryPanel.getTotalMemory() < (totalMem - MEMORY_UPDATE_THRESHOLD))
539: || (memoryPanel.getTotalMemory() > (totalMem + MEMORY_UPDATE_THRESHOLD))) {
540: memoryPanel.setTotalMemory(totalMem);
541: }
542:
543: if ((memoryPanel.getCurrentMemory() < (currMem - MEMORY_UPDATE_THRESHOLD))
544: || (memoryPanel.getCurrentMemory() > (currMem + MEMORY_UPDATE_THRESHOLD))) {
545: memoryPanel.setCurrentMemory(currMem);
546: }
547: }
548: }
549: }
|