1 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 73 public class SystemPropertiesPanel extends JPanel { 74 75 76 private JTable table; 77 78 79 private JPopupMenu copyPopupMenu; 80 81 82 private JMenuItem copyMenuItem; 83 84 85 private PopupListener copyPopupListener; 86 87 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 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 this.copyPopupListener = new PopupListener(); 117 this.table.addMouseListener(this.copyPopupListener); 118 119 } 120 121 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 153 protected final JPopupMenu getCopyPopupMenu() 154 { 155 return copyPopupMenu; 156 } 157 158 162 protected final JTable getTable() 163 { 164 return table; 165 } 166 167 170 private class PopupListener extends MouseAdapter { 171 172 175 public PopupListener() { 176 } 177 178 183 public void mousePressed(final MouseEvent e) { 184 maybeShowPopup(e); 185 } 186 187 192 public void mouseReleased(final MouseEvent e) { 193 maybeShowPopup(e); 194 } 195 196 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 } 212 213 | Popular Tags |