View Javadoc

1   /*
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      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      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 ); // listen for double-clicks
95  
96          // set up keyboard events for cut-copy: Ctrl-C, Ctrl-X
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     // interface MouseListener
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                 /* do something here */
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     // interface ActionEventListener - for listening to key commands
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  * $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