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-2005, 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-2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: SystemPropertiesPanel.java,v 1.4 2005/10/18 13:19:13 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 localisation (DG);
  43:  * 26-Jun-2002 : Removed unnecessary import (DG);
  44:  * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  45:  *
  46:  */
  47: 
  48: package org.jfree.ui.about;
  49: 
  50: import java.awt.BorderLayout;
  51: import java.awt.Toolkit;
  52: import java.awt.datatransfer.Clipboard;
  53: import java.awt.datatransfer.StringSelection;
  54: import java.awt.event.ActionEvent;
  55: import java.awt.event.ActionListener;
  56: import java.awt.event.MouseAdapter;
  57: import java.awt.event.MouseEvent;
  58: import java.util.ResourceBundle;
  59: 
  60: import javax.swing.JMenuItem;
  61: import javax.swing.JPanel;
  62: import javax.swing.JPopupMenu;
  63: import javax.swing.JScrollPane;
  64: import javax.swing.JTable;
  65: import javax.swing.KeyStroke;
  66: import javax.swing.ListSelectionModel;
  67: 
  68: /**
  69:  * A panel containing a table of system properties.
  70:  *
  71:  * @author David Gilbert
  72:  */
  73: public class SystemPropertiesPanel extends JPanel {
  74: 
  75:     /** The table that displays the system properties. */
  76:     private JTable table;
  77: 
  78:     /** Allows for a popup menu for copying. */
  79:     private JPopupMenu copyPopupMenu;
  80: 
  81:     /** A copy menu item. */
  82:     private JMenuItem copyMenuItem;
  83: 
  84:     /** A popup listener. */
  85:     private PopupListener copyPopupListener;
  86: 
  87:     /**
  88:      * Constructs a new panel.
  89:      */
  90:     public SystemPropertiesPanel() {
  91: 
  92:         final String baseName = "org.jfree.ui.about.resources.AboutResources";
  93:         final ResourceBundle resources = ResourceBundle.getBundle(baseName);
  94: 
  95:         setLayout(new BorderLayout());
  96:         this.table = SystemProperties.createSystemPropertiesTable();
  97:         add(new JScrollPane(this.table));
  98: 
  99:         // Add a popup menu to copy to the clipboard...
 100:         this.copyPopupMenu = new JPopupMenu();
 101: 
 102:         final String label = resources.getString("system-properties-panel.popup-menu.copy");
 103:         final KeyStroke accelerator = (KeyStroke)
 104:             resources.getObject("system-properties-panel.popup-menu.copy.accelerator");
 105:         this.copyMenuItem = new JMenuItem(label);
 106:         this.copyMenuItem.setAccelerator(accelerator);
 107:         this.copyMenuItem.getAccessibleContext().setAccessibleDescription(label);
 108:         this.copyMenuItem.addActionListener(new ActionListener() {
 109:             public void actionPerformed(final ActionEvent e) {
 110:                 copySystemPropertiesToClipboard();
 111:             }
 112:         });
 113:         this.copyPopupMenu.add(this.copyMenuItem);
 114: 
 115:         // add popup Listener to the table
 116:         this.copyPopupListener = new PopupListener();
 117:         this.table.addMouseListener(this.copyPopupListener);
 118: 
 119:     }
 120: 
 121:     /**
 122:      * Copies the selected cells in the table to the clipboard, in tab-delimited format.
 123:      */
 124:     public void copySystemPropertiesToClipboard() {
 125: 
 126:         final StringBuffer buffer = new StringBuffer();
 127:         final ListSelectionModel selection = this.table.getSelectionModel();
 128:         final int firstRow = selection.getMinSelectionIndex();
 129:         final int lastRow = selection.getMaxSelectionIndex();
 130:         if ((firstRow != -1) && (lastRow != -1)) {
 131:             for (int r = firstRow; r <= lastRow; r++) {
 132:                 for (int c = 0; c < this.table.getColumnCount(); c++) {
 133:                     buffer.append(this.table.getValueAt(r, c));
 134:                     if (c != 2) {
 135:                         buffer.append("\t");
 136:                     }
 137:                 }
 138:                 buffer.append("\n");
 139:             }
 140:         }
 141:         final StringSelection ss = new StringSelection(buffer.toString());
 142:         final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
 143:         cb.setContents(ss, ss);
 144: 
 145:     }
 146: 
 147:     
 148:     /**
 149:      * Returns the copy popup menu.
 150:      *
 151:      * @return Returns the copyPopupMenu.
 152:      */
 153:     protected final JPopupMenu getCopyPopupMenu()
 154:     {
 155:       return copyPopupMenu;
 156:     }
 157:     
 158:     /**
 159:      * Returns the table containing the system properties.
 160:      * @return Returns the table.
 161:      */
 162:     protected final JTable getTable()
 163:     {
 164:       return table;
 165:     }
 166:     
 167:     /**
 168:      * A popup listener.
 169:      */
 170:     private class PopupListener extends MouseAdapter {
 171: 
 172:         /**
 173:          * Default constructor.
 174:          */
 175:         public PopupListener() {
 176:         }
 177: 
 178:         /**
 179:          * Mouse pressed event.
 180:          *
 181:          * @param e  the event.
 182:          */
 183:         public void mousePressed(final MouseEvent e) {
 184:             maybeShowPopup(e);
 185:         }
 186: 
 187:         /**
 188:          * Mouse released event.
 189:          *
 190:          * @param e  the event.
 191:          */
 192:         public void mouseReleased(final MouseEvent e) {
 193:             maybeShowPopup(e);
 194:         }
 195: 
 196:         /**
 197:          * Event handler.
 198:          *
 199:          * @param e  the event.
 200:          */
 201:         private void maybeShowPopup(final MouseEvent e) {
 202:             if (e.isPopupTrigger()) {
 203:                 getCopyPopupMenu().show(
 204:                     getTable(), e.getX(), e.getY()
 205:                 );
 206:             }
 207:         }
 208:     }
 209: 
 210: 
 211: }