1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.ui.swing.util;
20
21 import java.awt.BorderLayout;
22 import java.awt.Dimension;
23 import java.awt.Insets;
24 import java.awt.Toolkit;
25 import java.awt.datatransfer.Clipboard;
26 import java.awt.datatransfer.StringSelection;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.KeyEvent;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseListener;
32 import java.lang.reflect.Method;
33
34 import javax.swing.JComponent;
35 import javax.swing.JFrame;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTable;
39 import javax.swing.KeyStroke;
40 import javax.swing.border.EmptyBorder;
41 import javax.swing.table.TableModel;
42
43 import net.wotonomy.ui.swing.components.PropertyEditorTable;
44 import net.wotonomy.ui.swing.components.PropertyEditorTableModel;
45
46 /***
47 * The ObjectInspector displays a JFrame containing
48 * a PropertyEditorTable that displays an object. <br><br>
49 *
50 * @author michael@mpowers.net
51 * @author $Author: cgruber $
52 * @version $Revision: 904 $
53 */
54
55 public class ObjectInspector implements ActionListener, MouseListener
56 {
57 protected JTable table = null;
58
59
60 static public final String COPY = "COPY";
61
62 /***
63 * Displays the specified object in a frame.
64 */
65 public ObjectInspector( Object anObject )
66 {
67 initLayout( anObject );
68 }
69
70 protected void initLayout( Object aTargetObject )
71 {
72 PropertyEditorTableModel model =
73 new PropertyEditorTableModel();
74 model.setObject( aTargetObject );
75 table = new PropertyEditorTable()
76 {
77 public void methodInvoked( Object anObject, Method aMethod, Object aResult )
78 {
79 if
80 ( ( aResult == null )
81 || ( aResult instanceof Number )
82 || ( aResult instanceof Boolean )
83 || ( aResult instanceof String ) )
84 {
85 System.out.println( aMethod.getName() + ": " + aResult );
86 }
87 else
88 {
89 new ObjectInspector( aResult );
90 }
91 }
92 };
93 table.setModel( model );
94 table.addMouseListener( this );
95
96
97 table.registerKeyboardAction( this, COPY,
98 KeyStroke.getKeyStroke( KeyEvent.VK_C,
99 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() ),
100 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
101 table.registerKeyboardAction( this, COPY,
102 KeyStroke.getKeyStroke( KeyEvent.VK_X,
103 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() ),
104 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
105
106 JPanel panel = new JPanel();
107 panel.setBorder( new EmptyBorder( new Insets( 10, 10, 10, 10 ) ) );
108 panel.setLayout( new BorderLayout( 10, 10 ) );
109
110 JScrollPane scrollPane = new JScrollPane( table );
111 scrollPane.setPreferredSize( new Dimension( 325, 350 ) );
112 panel.add( scrollPane, BorderLayout.CENTER );
113
114 JFrame window = new JFrame();
115 window.setTitle( aTargetObject.getClass().getName() );
116 window.getContentPane().add( panel );
117
118 window.pack();
119 WindowUtilities.cascade( window );
120 window.show();
121 }
122
123
124
125 /***
126 * Double click to call invokeFileFromString.
127 */
128
129 public void mouseClicked(MouseEvent e)
130 {
131 if ( e.getSource() == table )
132 {
133 if ( e.getClickCount() > 1 )
134 {
135 int row = table.rowAtPoint( e.getPoint() );
136 int col = table.columnAtPoint( e.getPoint() );
137
138 if ( ( row == -1 ) || ( col != 0 ) ) return;
139
140
141 }
142 }
143 }
144
145 public void mouseReleased(MouseEvent e) {}
146 public void mousePressed(MouseEvent e) {}
147 public void mouseEntered(MouseEvent e) {}
148 public void mouseExited(MouseEvent e) {}
149
150
151
152
153 public void actionPerformed(ActionEvent evt)
154 {
155 if ( COPY.equals( evt.getActionCommand() ) )
156 {
157 copyToClipboard();
158 return;
159 }
160 }
161
162 /***
163 * Copies the contents of the table to the clipboard as a tab-delimited string.
164 */
165 public void copyToClipboard()
166 {
167 Toolkit toolkit = Toolkit.getDefaultToolkit();
168 Clipboard clipboard = toolkit.getSystemClipboard();
169 StringSelection selection =
170 new StringSelection( getTabDelimitedString() );
171 clipboard.setContents( selection, selection );
172 }
173
174 /***
175 * Converts the contents of the table to a tab-delimited string.
176 * @return A String containing the text contents of the table.
177 */
178 public String getTabDelimitedString()
179 {
180 StringBuffer result = new StringBuffer(64);
181
182 TableModel model = table.getModel();
183 int cols = model.getColumnCount();
184 int rows = model.getRowCount();
185
186 Object o = null;
187 for ( int y = 0; y < rows; y++ )
188 {
189 for ( int x = 0; x < cols; x++ )
190 {
191 o = model.getValueAt( y, x );
192 if ( o == null ) o = "";
193 result.append( o );
194 result.append( '\t' );
195 }
196 result.append( '\n' );
197 }
198
199 return result.toString();
200 }
201
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226