1 17 package org.columba.core.gui.util; 18 19 import java.awt.BorderLayout ; 20 import java.awt.Component ; 21 import java.awt.Dimension ; 22 import java.awt.Font ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyEvent ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.text.NumberFormat ; 33 34 import javax.swing.BorderFactory ; 35 import javax.swing.Box ; 36 import javax.swing.JButton ; 37 import javax.swing.JComponent ; 38 import javax.swing.JDialog ; 39 import javax.swing.JFormattedTextField ; 40 import javax.swing.JFrame ; 41 import javax.swing.JLabel ; 42 import javax.swing.JPanel ; 43 import javax.swing.JProgressBar ; 44 import javax.swing.JScrollPane ; 45 import javax.swing.JTabbedPane ; 46 import javax.swing.KeyStroke ; 47 import javax.swing.SwingUtilities ; 48 import javax.swing.UIManager ; 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 61 public class AboutDialog extends JDialog implements ActionListener { 62 65 private static final long serialVersionUID = -4046263935468682814L; 66 public static final String CMD_CLOSE = "CLOSE"; 67 private static final String RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog"; 68 protected static AboutDialog instance; 69 protected JTabbedPane tabbedPane; 70 71 75 protected AboutDialog() { 76 super((JFrame ) null, GlobalResourceLoader.getString( 77 RESOURCE_BUNDLE_PATH, "about", "title")); 78 init(); 79 } 80 81 84 protected void init() { 85 tabbedPane = new JTabbedPane (); 86 87 JPanel authorPanel = new JPanel (new GridBagLayout ()); 88 authorPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11)); 89 90 GridBagConstraints c = new GridBagConstraints (); 91 92 Font font = UIManager.getFont("Label.font"); 94 95 JLabel imageLabel = new JLabel (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 versionLabel = new JLabel (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 box = Box.createRigidArea(new Dimension (10, 10)); 115 authorPanel.add(box, c); 116 117 box = Box.createRigidArea(new Dimension (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 (5, 15)); 125 authorPanel.add(box, c); 126 127 JLabel version = new JLabel (VersionInfo.getVersion()); 128 c.gridx = 2; 129 c.gridwidth = GridBagConstraints.REMAINDER; 130 authorPanel.add(version, c); 131 132 JLabel buildDateLabel = new JLabel (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 (10, 10)); 145 authorPanel.add(box, c); 146 147 box = Box.createRigidArea(new Dimension (10, 10)); 148 authorPanel.add(box, c); 149 150 authorPanel.add(buildDateLabel, c); 151 152 c.gridx = 1; 153 box = Box.createRigidArea(new Dimension (5, 15)); 154 authorPanel.add(box, c); 155 156 JLabel buildDate = new JLabel (VersionInfo.getBuildDate().toString()); 157 c.gridx = 2; 158 c.gridwidth = GridBagConstraints.REMAINDER; 159 authorPanel.add(buildDate, c); 160 161 JLabel authorLabel = new JLabel (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 (10, 10)); 174 authorPanel.add(box, c); 175 176 authorPanel.add(authorLabel, c); 177 178 c.gridx = 1; 179 box = Box.createRigidArea(new Dimension (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 websiteLabel = new JLabel (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 ("http://columbamail.org")); 209 } catch (MalformedURLException mue) { 210 } 211 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 contributorPanel = new JPanel (new BorderLayout (0, 5)); 221 contributorPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 222 11)); 223 224 JLabel contributorLabel = new JLabel (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 ioe) { 232 textPane.setText(ioe.getLocalizedMessage()); 233 } 234 contributorPanel.add(new JScrollPane (textPane)); 235 tabbedPane.addTab(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, 236 "about", "contributorPane"), contributorPanel); 237 238 JPanel licensePanel = new JPanel (new BorderLayout ()); 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 ioe) { 244 textPane.setText(ioe.getLocalizedMessage()); 245 } 246 licensePanel.add(new JScrollPane (textPane)); 247 tabbedPane.addTab(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, 248 "about", "license"), licensePanel); 249 250 JPanel ackPanel = new JPanel (new BorderLayout ()); 251 ackPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11)); 252 JLabel ackLabel = new JLabel (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 ioe) { 259 textPane.setText(ioe.getLocalizedMessage()); 260 } 261 ackPanel.add(new JScrollPane (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 buttonPanel = new JPanel (new BorderLayout (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 (450, 350)); 286 setLocationRelativeTo(null); 287 } 288 289 292 public void showTab(int index) { 293 tabbedPane.setSelectedIndex(index); 294 } 295 296 299 public void actionPerformed(ActionEvent e) { 300 if (CMD_CLOSE.equals(e.getActionCommand())) { 301 setVisible(false); 302 } 303 } 304 305 309 public synchronized static AboutDialog getInstance() { 310 if (instance == null) { 311 instance = new AboutDialog(); 312 } 313 return instance; 314 } 315 316 321 protected static class MemoryPanel extends JPanel { 322 325 private static final long serialVersionUID = -711331922225872209L; 326 protected Thread updaterThread; 327 private int currentMemory = -1; 328 private int totalMemory = -1; 329 protected JFormattedTextField currentMemoryTextField; 330 protected JFormattedTextField maxMemoryTextField; 331 protected JProgressBar progressBar; 332 protected JFormattedTextField totalMemoryTextField; 333 334 337 public MemoryPanel() { 338 super(new GridBagLayout ()); 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 352 public int getCurrentMemory() { 353 return currentMemory; 354 } 355 356 360 public void setCurrentMemory(int mem) { 361 currentMemory = mem; 362 currentMemoryTextField.setValue(new Integer (mem)); 363 progressBar.setValue(mem); 364 } 365 366 370 public int getTotalMemory() { 371 return totalMemory; 372 } 373 374 378 public void setTotalMemory(int mem) { 379 totalMemory = mem; 380 totalMemoryTextField.setValue(new Integer (mem)); 381 progressBar.setMaximum(mem); 382 } 383 384 387 private void initComponents() { 388 GridBagConstraints c = new GridBagConstraints (); 389 390 currentMemoryTextField = new JFormattedTextField (NumberFormat 391 .getInstance()); 392 totalMemoryTextField = new JFormattedTextField (NumberFormat 393 .getInstance()); 394 maxMemoryTextField = new JFormattedTextField (); 395 396 JButton gcButton = new JButton (ImageLoader 397 .getSmallIcon(IconKeys.EDIT_DELETE)); 398 399 JLabel currentMemoryLabel = new JLabel ("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 (0, 4, 0, 4); 409 add(currentMemoryTextField, c); 410 411 JLabel currentMemoryKBLabel = new JLabel ("KB"); 412 c.gridx = 2; 413 c.insets = new Insets (0, 0, 0, 0); 414 add(currentMemoryKBLabel, c); 415 416 JLabel totalMemoryLabel = new JLabel ("Total:"); 417 c.gridx = 3; 418 c.insets = new Insets (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 (0, 4, 0, 4); 425 add(totalMemoryTextField, c); 426 427 JLabel totalMemoryKBLabel = new JLabel ("KB"); 428 c.gridx = 5; 429 c.insets = new Insets (0, 0, 0, 0); 430 add(totalMemoryKBLabel, c); 431 432 JLabel maxMemoryLabel = new JLabel ("VM Max:"); 433 c.gridx = 0; 434 c.gridy = 1; 435 c.insets = new Insets (6, 0, 0, 0); 436 add(maxMemoryLabel, c); 437 438 maxMemoryTextField.setColumns(5); 439 maxMemoryTextField.setEditable(false); 440 maxMemoryTextField.setValue(new Integer ((int) (Runtime.getRuntime() 441 .maxMemory() / 1000))); 442 c.gridx = 1; 443 c.insets = new Insets (4, 4, 0, 4); 444 add(maxMemoryTextField, c); 445 446 JLabel maxMemoryKBLabel = new JLabel ("KB"); 447 c.gridx = 2; 448 c.insets = new Insets (0, 0, 0, 0); 449 add(maxMemoryKBLabel, c); 450 451 progressBar = new JProgressBar (); 452 progressBar.setPreferredSize(gcButton.getPreferredSize()); 453 progressBar.setStringPainted(true); 454 c.gridx = 0; 455 c.gridy = 2; 456 c.insets = new Insets (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 () { 463 public void actionPerformed(ActionEvent evt) { 464 System.gc(); 465 } 466 }); 467 c.gridx = 5; 468 c.gridwidth = 1; 469 c.insets = new Insets (10, 6, 0, 0); 470 c.fill = GridBagConstraints.NONE; 471 add(gcButton, c); 472 } 473 } 474 475 479 protected static class MemoryMonitorThread extends Thread { 480 protected static final int MEMORY_UPDATE_THRESHOLD = 50; protected MemoryPanel memoryPanel; 482 protected Runtime runtime = Runtime.getRuntime(); 483 484 488 public MemoryMonitorThread(MemoryPanel memoryPanel) { 489 this.memoryPanel = memoryPanel; 490 setPriority(Thread.MIN_PRIORITY); 491 setDaemon(true); 492 } 493 494 497 public void run() { 498 while (!isInterrupted()) { 499 SwingUtilities.invokeLater(new Runnable () { 500 public void run() { 501 updateDisplay(); 502 } 503 }); 504 505 try { 506 Thread.sleep(750); 507 } catch (InterruptedException ie) { 508 ie.printStackTrace(); 509 } 510 } 511 } 512 513 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 |