001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2008, 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-2008, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * $Id: SystemPropertiesPanel.java,v 1.7 2008/12/18 09:57:32 mungady 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
043 *               localisation (DG);
044 * 26-Jun-2002 : Removed unnecessary import (DG);
045 * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
046 * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
047 *               Jess Thrysoee (DG);
048 *
049 */
050
051package org.jfree.ui.about;
052
053import java.awt.BorderLayout;
054import java.awt.Toolkit;
055import java.awt.datatransfer.Clipboard;
056import java.awt.datatransfer.StringSelection;
057import java.awt.event.ActionEvent;
058import java.awt.event.ActionListener;
059import java.awt.event.MouseAdapter;
060import java.awt.event.MouseEvent;
061import java.util.ResourceBundle;
062
063import javax.swing.JMenuItem;
064import javax.swing.JPanel;
065import javax.swing.JPopupMenu;
066import javax.swing.JScrollPane;
067import javax.swing.JTable;
068import javax.swing.KeyStroke;
069import javax.swing.ListSelectionModel;
070
071import org.jfree.util.ResourceBundleWrapper;
072
073/**
074 * A panel containing a table of system properties.
075 *
076 * @author David Gilbert
077 */
078public class SystemPropertiesPanel extends JPanel {
079
080    /** The table that displays the system properties. */
081    private JTable table;
082
083    /** Allows for a popup menu for copying. */
084    private JPopupMenu copyPopupMenu;
085
086    /** A copy menu item. */
087    private JMenuItem copyMenuItem;
088
089    /** A popup listener. */
090    private PopupListener copyPopupListener;
091
092    /**
093     * Constructs a new panel.
094     */
095    public SystemPropertiesPanel() {
096
097        final String baseName = "org.jfree.ui.about.resources.AboutResources";
098        final ResourceBundle resources = ResourceBundleWrapper.getBundle(
099                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}
220