/*
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.
*
*
* @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.
*
*
*/