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