Source for org.jfree.ui.about.SystemPropertiesPanel

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it
  10:  * under the terms of the GNU Lesser General Public License as published by
  11:  * the Free Software Foundation; either version 2.1 of the License, or
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  22:  * USA.
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25:  * in the United States and other countries.]
  26:  *
  27:  * --------------------------
  28:  * SystemPropertiesPanel.java
  29:  * --------------------------
  30:  * (C) Copyright 2001-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: SystemPropertiesPanel.java,v 1.7 2008/12/18 09:57:32 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 26-Nov-2001 : Version 1 (DG);
  40:  * 28-Feb-2002 : Changed package to com.jrefinery.ui.about (DG);
  41:  * 04-Mar-2002 : Added popup menu code by Carl ?? (DG);
  42:  * 15-Mar-2002 : Modified to use ResourceBundle for elements that require
  43:  *               localisation (DG);
  44:  * 26-Jun-2002 : Removed unnecessary import (DG);
  45:  * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  46:  * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
  47:  *               Jess Thrysoee (DG);
  48:  *
  49:  */
  50: 
  51: package org.jfree.ui.about;
  52: 
  53: import java.awt.BorderLayout;
  54: import java.awt.Toolkit;
  55: import java.awt.datatransfer.Clipboard;
  56: import java.awt.datatransfer.StringSelection;
  57: import java.awt.event.ActionEvent;
  58: import java.awt.event.ActionListener;
  59: import java.awt.event.MouseAdapter;
  60: import java.awt.event.MouseEvent;
  61: import java.util.ResourceBundle;
  62: 
  63: import javax.swing.JMenuItem;
  64: import javax.swing.JPanel;
  65: import javax.swing.JPopupMenu;
  66: import javax.swing.JScrollPane;
  67: import javax.swing.JTable;
  68: import javax.swing.KeyStroke;
  69: import javax.swing.ListSelectionModel;
  70: 
  71: import org.jfree.util.ResourceBundleWrapper;
  72: 
  73: /**
  74:  * A panel containing a table of system properties.
  75:  *
  76:  * @author David Gilbert
  77:  */
  78: public class SystemPropertiesPanel extends JPanel {
  79: 
  80:     /** The table that displays the system properties. */
  81:     private JTable table;
  82: 
  83:     /** Allows for a popup menu for copying. */
  84:     private JPopupMenu copyPopupMenu;
  85: 
  86:     /** A copy menu item. */
  87:     private JMenuItem copyMenuItem;
  88: 
  89:     /** A popup listener. */
  90:     private PopupListener copyPopupListener;
  91: 
  92:     /**
  93:      * Constructs a new panel.
  94:      */
  95:     public SystemPropertiesPanel() {
  96: 
  97:         final String baseName = "org.jfree.ui.about.resources.AboutResources";
  98:         final ResourceBundle resources = ResourceBundleWrapper.getBundle(
  99:                 baseName);
 100: 
 101:         setLayout(new BorderLayout());
 102:         this.table = SystemProperties.createSystemPropertiesTable();
 103:         add(new JScrollPane(this.table));
 104: 
 105:         // Add a popup menu to copy to the clipboard...
 106:         this.copyPopupMenu = new JPopupMenu();
 107: 
 108:         final String label = resources.getString(
 109:                 "system-properties-panel.popup-menu.copy");
 110:         final KeyStroke accelerator = (KeyStroke) resources.getObject(
 111:                     "system-properties-panel.popup-menu.copy.accelerator");
 112:         this.copyMenuItem = new JMenuItem(label);
 113:         this.copyMenuItem.setAccelerator(accelerator);
 114:         this.copyMenuItem.getAccessibleContext().setAccessibleDescription(
 115:                 label);
 116:         this.copyMenuItem.addActionListener(new ActionListener() {
 117:             public void actionPerformed(final ActionEvent e) {
 118:                 copySystemPropertiesToClipboard();
 119:             }
 120:         });
 121:         this.copyPopupMenu.add(this.copyMenuItem);
 122: 
 123:         // add popup Listener to the table
 124:         this.copyPopupListener = new PopupListener();
 125:         this.table.addMouseListener(this.copyPopupListener);
 126: 
 127:     }
 128: 
 129:     /**
 130:      * Copies the selected cells in the table to the clipboard, in
 131:      * tab-delimited format.
 132:      */
 133:     public void copySystemPropertiesToClipboard() {
 134: 
 135:         final StringBuffer buffer = new StringBuffer();
 136:         final ListSelectionModel selection = this.table.getSelectionModel();
 137:         final int firstRow = selection.getMinSelectionIndex();
 138:         final int lastRow = selection.getMaxSelectionIndex();
 139:         if ((firstRow != -1) && (lastRow != -1)) {
 140:             for (int r = firstRow; r <= lastRow; r++) {
 141:                 for (int c = 0; c < this.table.getColumnCount(); c++) {
 142:                     buffer.append(this.table.getValueAt(r, c));
 143:                     if (c != 2) {
 144:                         buffer.append("\t");
 145:                     }
 146:                 }
 147:                 buffer.append("\n");
 148:             }
 149:         }
 150:         final StringSelection ss = new StringSelection(buffer.toString());
 151:         final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
 152:         cb.setContents(ss, ss);
 153: 
 154:     }
 155: 
 156: 
 157:     /**
 158:      * Returns the copy popup menu.
 159:      *
 160:      * @return Returns the copyPopupMenu.
 161:      */
 162:     protected final JPopupMenu getCopyPopupMenu()
 163:     {
 164:       return this.copyPopupMenu;
 165:     }
 166: 
 167:     /**
 168:      * Returns the table containing the system properties.
 169:      * @return Returns the table.
 170:      */
 171:     protected final JTable getTable()
 172:     {
 173:       return this.table;
 174:     }
 175: 
 176:     /**
 177:      * A popup listener.
 178:      */
 179:     private class PopupListener extends MouseAdapter {
 180: 
 181:         /**
 182:          * Default constructor.
 183:          */
 184:         public PopupListener() {
 185:         }
 186: 
 187:         /**
 188:          * Mouse pressed event.
 189:          *
 190:          * @param e  the event.
 191:          */
 192:         public void mousePressed(final MouseEvent e) {
 193:             maybeShowPopup(e);
 194:         }
 195: 
 196:         /**
 197:          * Mouse released event.
 198:          *
 199:          * @param e  the event.
 200:          */
 201:         public void mouseReleased(final MouseEvent e) {
 202:             maybeShowPopup(e);
 203:         }
 204: 
 205:         /**
 206:          * Event handler.
 207:          *
 208:          * @param e  the event.
 209:          */
 210:         private void maybeShowPopup(final MouseEvent e) {
 211:             if (e.isPopupTrigger()) {
 212:                 getCopyPopupMenu().show(
 213:                     getTable(), e.getX(), e.getY()
 214:                 );
 215:             }
 216:         }
 217:     }
 218: 
 219: }