001: package snow.sortabletable;
002:
003: import javax.swing.event.ListSelectionEvent;
004: import javax.swing.event.ListSelectionListener;
005: import snow.utils.gui.CloseControlPanel;
006: import javax.swing.*;
007: import javax.swing.table.*;
008: import java.util.*;
009: import java.awt.event.*;
010: import java.awt.*;
011:
012: /** view / edit the system properties
013: */
014: public class SystemPropertiesViewer extends JDialog {
015:
016: final private String[] columnNames = new String[] { "Key", "Value" };
017:
018: // properties
019: final private JTable propertiesTable = new JTable();
020: final private PropertiesTableModel propertiesTableModel = new PropertiesTableModel();
021: final private SortableTableModel propertiesSortableTableModel = new SortableTableModel(
022: propertiesTableModel, 0, true);
023: final private JTextField propCodeField = new JTextField(50);
024:
025: // environment
026: final private JTable environmentTable = new JTable();
027: final private EnvironmentTableModel environmentTableModel = new EnvironmentTableModel();
028: final private SortableTableModel environmentSortableTableModel = new SortableTableModel(
029: environmentTableModel, 0, true);
030: final private JTextField envCodeField = new JTextField(50);
031:
032: public SystemPropertiesViewer(final JFrame parent) {
033: super (
034: parent,
035: "Current System Properties and Environment variables values",
036: false);
037: setLayout(new BorderLayout());
038:
039: JTabbedPane pane = new JTabbedPane();
040:
041: JPanel systemPropertiesPanel = createSystemPropertiesPanel();
042: JPanel systemEnvironmentPanel = createSystemEnvironmentPanel();
043:
044: pane.addTab("System Properties ( System.getProperties() )",
045: systemPropertiesPanel);
046: pane.addTab("System Environment ( System.getenv() )",
047: systemEnvironmentPanel);
048:
049: add(pane, BorderLayout.CENTER);
050:
051: tryToWriteAllProperties();
052:
053: add(new CloseControlPanel(this , false, true, "Close"),
054: BorderLayout.SOUTH);
055:
056: // map escape key
057: ActionListener escapeActionListener = new ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: closeButtonPressed();
060: }
061: };
062: this .getRootPane().registerKeyboardAction(escapeActionListener,
063: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
064: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
065: propertiesTable.registerKeyboardAction(escapeActionListener,
066: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
067: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
068:
069: setSize(700, 700);
070: setLocationRelativeTo(null); // screen
071: setVisible(true);
072: }
073:
074: private JPanel createSystemPropertiesPanel() {
075: JPanel p = new JPanel(new BorderLayout());
076: p.add(new JScrollPane(propertiesTable), BorderLayout.CENTER);
077: propertiesTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
078: propertiesTable.setModel(propertiesSortableTableModel);
079: propertiesSortableTableModel.installGUI(propertiesTable);
080:
081: AdvancedSearchPanel asp = new AdvancedSearchPanel("Filter:",
082: null, propertiesSortableTableModel, false);
083: p.add(asp, BorderLayout.NORTH);
084: asp.setAdvancedMode(true);
085:
086: JPanel sp = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0));
087: p.add(sp, BorderLayout.SOUTH);
088: sp.add(new JLabel("Code to paste: "));
089: sp.add(propCodeField);
090: propCodeField.addFocusListener(new FocusAdapter() {
091: @Override
092: public void focusGained(FocusEvent fe) {
093: propCodeField.selectAll();
094: }
095: });
096: propCodeField.setEditable(false);
097:
098: propertiesTable.getSelectionModel().addListSelectionListener(
099: new ListSelectionListener() {
100: public void valueChanged(ListSelectionEvent lse) {
101: propCodeField.setText("");
102: int rowView = propertiesTable.getSelectedRow();
103: if (rowView < 0)
104: return;
105: int rowMod = propertiesSortableTableModel
106: .getIndexInUnsortedFromTablePos(rowView);
107: propCodeField.setText("System.getProperty(\""
108: + propertiesTableModel.getValueAt(
109: rowMod, 0) + "\")");
110: }
111:
112: });
113:
114: return p;
115: }
116:
117: private JPanel createSystemEnvironmentPanel() {
118: JPanel p = new JPanel(new BorderLayout());
119: p.add(new JScrollPane(environmentTable), BorderLayout.CENTER);
120: environmentTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
121: environmentTable.setModel(environmentSortableTableModel);
122: environmentSortableTableModel.installGUI(environmentTable);
123:
124: AdvancedSearchPanel asp = new AdvancedSearchPanel("Filter:",
125: null, environmentSortableTableModel, false);
126: p.add(asp, BorderLayout.NORTH);
127: asp.setAdvancedMode(true);
128:
129: JPanel sp = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0));
130: p.add(sp, BorderLayout.SOUTH);
131: sp.add(new JLabel("Code to paste: "));
132: sp.add(envCodeField);
133: envCodeField.addFocusListener(new FocusAdapter() {
134: @Override
135: public void focusGained(FocusEvent fe) {
136: envCodeField.selectAll();
137: }
138: });
139: envCodeField.setEditable(false);
140:
141: environmentTable.getSelectionModel().addListSelectionListener(
142: new ListSelectionListener() {
143: public void valueChanged(ListSelectionEvent lse) {
144: envCodeField.setText("");
145: int rowView = environmentTable.getSelectedRow();
146: if (rowView < 0)
147: return;
148: int rowMod = environmentSortableTableModel
149: .getIndexInUnsortedFromTablePos(rowView);
150: envCodeField.setText("System.getenv(\""
151: + environmentTableModel.getValueAt(
152: rowMod, 0) + "\")");
153: }
154:
155: });
156:
157: return p;
158: }
159:
160: private final void closeButtonPressed() {
161: setVisible(false);
162: dispose();
163: }
164:
165: private final void tryToWriteAllProperties() {
166: // some security managers doesn't allow this (with unsigned WebStart apps, for example)
167: try {
168: Properties props = System.getProperties();
169: Enumeration enume = props.propertyNames();
170: //System.out.println("Properties");
171: while (enume.hasMoreElements()) {
172: String prn = (String) enume.nextElement();
173: writeprop(prn);
174: //System.out.print("\""+prn+"\", ");
175: }
176: } catch (Exception e) {
177: e.printStackTrace();
178: }
179: }
180:
181: private final class PropertiesTableModel extends
182: FineGrainTableModel {
183: private final Vector<String> keys = new Vector<String>();
184:
185: @Override
186: public final String getColumnName(int col) {
187: return columnNames[col];
188: }
189:
190: public final int getRowCount() {
191: return keys.size();
192: }
193:
194: public final int getColumnCount() {
195: return 2;
196: }
197:
198: @Override
199: public final int getPreferredColumnWidth(int col) {
200: if (col == 0)
201: return 15;
202: return 35;
203:
204: }
205:
206: @Override
207: public final Object getValueAt(int row, int col) {
208:
209: String key = keys.get(row);
210: if (col == 0)
211: return key;
212: if (col == 1) {
213: try {
214: return System.getProperty(key, "<NOT FOUND>"); //values.elementAt(row);
215: } catch (Exception e) {
216: return " *** no permission *** ";
217: }
218: }
219: return "?";
220: }
221:
222: @Override
223: public final boolean isCellEditable(int row, int col) {
224: return col == 1;
225: }
226:
227: @Override
228: public final void setValueAt(Object value, int row, int col) {
229: if (col != 1)
230: return;
231:
232: String key = keys.get(row);
233: this .fireTableModelWillChange();
234: try {
235: System.setProperty(key, (String) value);
236: } catch (Exception e) {
237: e.printStackTrace();
238: }
239:
240: this .fireTableDataChanged();
241: this .fireTableModelHasChanged();
242: }
243:
244: public final void addLine(String key) {
245: keys.add(key);
246: fireTableCellUpdated(keys.size() - 1, 0);
247: }
248: }
249:
250: /** @since Java 1.5
251: */
252: private final class EnvironmentTableModel extends
253: FineGrainTableModel {
254: private final Vector<String> keys = new Vector<String>();
255:
256: public EnvironmentTableModel() {
257: keys.addAll(System.getenv().keySet());
258: }
259:
260: @Override
261: public final String getColumnName(int col) {
262: return columnNames[col];
263: }
264:
265: public final int getRowCount() {
266: return keys.size();
267: }
268:
269: public final int getColumnCount() {
270: return 2;
271: }
272:
273: @Override
274: public final int getPreferredColumnWidth(int col) {
275: if (col == 0)
276: return 15;
277: return 35;
278:
279: }
280:
281: @Override
282: public final Object getValueAt(int row, int col) {
283:
284: String key = keys.get(row);
285: if (col == 0)
286: return key;
287: if (col == 1) {
288: try {
289: return System.getenv(key);
290: } catch (Exception e) {
291: return " *** no permission *** ";
292: }
293: }
294: return "?";
295: }
296:
297: @Override
298: public final boolean isCellEditable(int row, int col) {
299: return false;
300: }
301:
302: @Override
303: public final void setValueAt(Object value, int row, int col) {
304: }
305:
306: }
307:
308: public final void writeprop(String key) {
309: String prop = null;
310: try {
311: // try to write
312: System.getProperty(key);
313: } catch (Exception e) {
314: prop = "*** No permission ***";
315: }
316: propertiesTableModel.addLine(key);
317: }
318:
319: /** test
320: */
321: public static void main(String[] args) {
322: EventQueue.invokeLater(new Runnable() {
323: public void run() {
324: new SystemPropertiesViewer(new JFrame());
325: }
326: });
327: }
328:
329: }
|