    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * --------------------------
    028     * SystemPropertiesPanel.java
    029     * --------------------------
    030     * (C) Copyright 2001-2004, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: SystemPropertiesPanel.java,v 1.5 2007/11/02 17:50:36 taqua Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 26-Nov-2001 : Version 1 (DG);
    040     * 28-Feb-2002 : Changed package to com.jrefinery.ui.about (DG);
    041     * 04-Mar-2002 : Added popup menu code by Carl ?? (DG);
    042     * 15-Mar-2002 : Modified to use ResourceBundle for elements that require localisation (DG);
    043     * 26-Jun-2002 : Removed unnecessary import (DG);
    044     * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    045     *
    046     */
    047
    048    package org.jfree.ui.about;
    049
    050    import java.awt.BorderLayout;
    051    import java.awt.Toolkit;
    052    import java.awt.datatransfer.Clipboard;
    053    import java.awt.datatransfer.StringSelection;
    054    import java.awt.event.ActionEvent;
    055    import java.awt.event.ActionListener;
    056    import java.awt.event.MouseAdapter;
    057    import java.awt.event.MouseEvent;
    058    import java.util.ResourceBundle;
    059    import javax.swing.JMenuItem;
    060    import javax.swing.JPanel;
    061    import javax.swing.JPopupMenu;
    062    import javax.swing.JScrollPane;
    063    import javax.swing.JTable;
    064    import javax.swing.KeyStroke;
    065    import javax.swing.ListSelectionModel;
    066
    067    /**
    068     * A panel containing a table of system properties.
    069     *
    070     * @author David Gilbert
    071     */
    072    public class SystemPropertiesPanel extends JPanel {
    073
    074        /** The table that displays the system properties. */
    075        private JTable table;
    076
    077        /** Allows for a popup menu for copying. */
    078        private JPopupMenu copyPopupMenu;
    079
    080        /** A copy menu item. */
    081        private JMenuItem copyMenuItem;
    082
    083        /** A popup listener. */
    084        private PopupListener copyPopupListener;
    085
    086        /**
    087         * Constructs a new panel.
    088         */
    089        public SystemPropertiesPanel() {
    090
    091            final String baseName = "org.jfree.ui.about.resources.AboutResources";
    092            final ResourceBundle resources = ResourceBundle.getBundle(baseName);
    093
    094            setLayout(new BorderLayout());
    095            this.table = SystemProperties.createSystemPropertiesTable();
    096            add(new JScrollPane(this.table));
    097
    098            // Add a popup menu to copy to the clipboard...
    099            this.copyPopupMenu = new JPopupMenu();
    100
    101            final String label = resources.getString("system-properties-panel.popup-menu.copy");
    102            final KeyStroke accelerator = (KeyStroke)
    103                resources.getObject("system-properties-panel.popup-menu.copy.accelerator");
    104            this.copyMenuItem = new JMenuItem(label);
    105            this.copyMenuItem.setAccelerator(accelerator);
    106            this.copyMenuItem.getAccessibleContext().setAccessibleDescription(label);
    107            this.copyMenuItem.addActionListener(new ActionListener() {
    108                public void actionPerformed(final ActionEvent e) {
    109                    copySystemPropertiesToClipboard();
    110                }
    111            });
    112            this.copyPopupMenu.add(this.copyMenuItem);
    113
    114            // add popup Listener to the table
    115            this.copyPopupListener = new PopupListener();
    116            this.table.addMouseListener(this.copyPopupListener);
    117
    118        }
    119
    120        /**
    121         * Copies the selected cells in the table to the clipboard, in tab-delimited format.
    122         */
    123        public void copySystemPropertiesToClipboard() {
    124
    125            final StringBuffer buffer = new StringBuffer();
    126            final ListSelectionModel selection = this.table.getSelectionModel();
    127            final int firstRow = selection.getMinSelectionIndex();
    128            final int lastRow = selection.getMaxSelectionIndex();
    129            if ((firstRow != -1) && (lastRow != -1)) {
    130                for (int r = firstRow; r <= lastRow; r++) {
    131                    for (int c = 0; c < this.table.getColumnCount(); c++) {
    132                        buffer.append(this.table.getValueAt(r, c));
    133                        if (c != 2) {
    134                            buffer.append("\t");
    135                        }
    136                    }
    137                    buffer.append("\n");
    138                }
    139            }
    140            final StringSelection ss = new StringSelection(buffer.toString());
    141            final Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    142            cb.setContents(ss, ss);
    143
    144        }
    145
    146
    147        /**
    148         * Returns the copy popup menu.
    149         *
    150         * @return Returns the copyPopupMenu.
    151         */
    152        protected final JPopupMenu getCopyPopupMenu()
    153        {
    154          return copyPopupMenu;
    155        }
    156
    157        /**
    158         * Returns the table containing the system properties.
    159         * @return Returns the table.
    160         */
    161        protected final JTable getTable()
    162        {
    163          return table;
    164        }
    165
    166        /**
    167         * A popup listener.
    168         */
    169        private class PopupListener extends MouseAdapter {
    170
    171            /**
    172             * Default constructor.
    173             */
    174            public PopupListener() {
    175            }
    176
    177            /**
    178             * Mouse pressed event.
    179             *
    180             * @param e  the event.
    181             */
    182            public void mousePressed(final MouseEvent e) {
    183                maybeShowPopup(e);
    184            }
    185
    186            /**
    187             * Mouse released event.
    188             *
    189             * @param e  the event.
    190             */
    191            public void mouseReleased(final MouseEvent e) {
    192                maybeShowPopup(e);
    193            }
    194
    195            /**
    196             * Event handler.
    197             *
    198             * @param e  the event.
    199             */
    200            private void maybeShowPopup(final MouseEvent e) {
    201                if (e.isPopupTrigger()) {
    202                    getCopyPopupMenu().show(
    203                        getTable(), e.getX(), e.getY()
    204                    );
    205                }
    206            }
    207        }
    208
    209
    210    }
    211