1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Michael Powers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.util;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Method;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import net.wotonomy.ui.swing.components.PropertyEditorTable;
import net.wotonomy.ui.swing.components.PropertyEditorTableModel;
/**
* The ObjectInspector displays a JFrame containing a PropertyEditorTable that
* displays an object. <br>
* <br>
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 904 $
*/
public class ObjectInspector implements ActionListener, MouseListener {
protected JTable table = null;
// key command to copy contents to clipboard
static public final String COPY = "COPY";
/**
* Displays the specified object in a frame.
*/
public ObjectInspector(Object anObject) {
initLayout(anObject);
}
protected void initLayout(Object aTargetObject) {
PropertyEditorTableModel model = new PropertyEditorTableModel();
model.setObject(aTargetObject);
table = new PropertyEditorTable() {
public void methodInvoked(Object anObject, Method aMethod, Object aResult) {
if ((aResult == null) || (aResult instanceof Number) || (aResult instanceof Boolean)
|| (aResult instanceof String)) {
System.out.println(aMethod.getName() + ": " + aResult);
} else {
new ObjectInspector(aResult);
}
}
};
table.setModel(model);
table.addMouseListener(this); // listen for double-clicks
// set up keyboard events for cut-copy: Ctrl-C, Ctrl-X
table.registerKeyboardAction(this, COPY,
KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
table.registerKeyboardAction(this, COPY,
KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
panel.setLayout(new BorderLayout(10, 10));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(325, 350));
panel.add(scrollPane, BorderLayout.CENTER);
JFrame window = new JFrame();
window.setTitle(aTargetObject.getClass().getName());
window.getContentPane().add(panel);
window.pack();
WindowUtilities.cascade(window);
window.show();
}
// interface MouseListener
/**
* Double click to call invokeFileFromString.
*/
public void mouseClicked(MouseEvent e) {
if (e.getSource() == table) {
if (e.getClickCount() > 1) {
int row = table.rowAtPoint(e.getPoint());
int col = table.columnAtPoint(e.getPoint());
if ((row == -1) || (col != 0))
return;
/* do something here */
}
}
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
// interface ActionEventListener - for listening to key commands
public void actionPerformed(ActionEvent evt) {
if (COPY.equals(evt.getActionCommand())) {
copyToClipboard();
return;
}
}
/**
* Copies the contents of the table to the clipboard as a tab-delimited string.
*/
public void copyToClipboard() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
StringSelection selection = new StringSelection(getTabDelimitedString());
clipboard.setContents(selection, selection);
}
/**
* Converts the contents of the table to a tab-delimited string.
*
* @return A String containing the text contents of the table.
*/
public String getTabDelimitedString() {
StringBuffer result = new StringBuffer(64);
TableModel model = table.getModel();
int cols = model.getColumnCount();
int rows = model.getRowCount();
Object o = null;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
o = model.getValueAt(y, x);
if (o == null)
o = "";
result.append(o);
result.append('\t');
}
result.append('\n');
}
return result.toString();
}
}
/*
* $Log$ Revision 1.2 2006/02/18 23:19:05 cgruber Update imports and maven
* dependencies.
*
* Revision 1.1 2006/02/16 13:22:22 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.3 2003/08/06 23:07:53 chochos general code cleanup (mostly,
* removing unused imports)
*
* Revision 1.2 2002/11/16 16:33:31 mpowers Now using platform-specific
* accelerator key for shortcuts.
*
* Revision 1.1.1.1 2000/12/21 15:51:27 mpowers Contributing wotonomy.
*
* Revision 1.3 2000/12/20 16:25:45 michael Added log to all files.
*
*
*/
|