update dev300-m58
[ooovba.git] / odk / examples / java / Inspector / SwingTreeControlProvider.java
blob567c8ba8920e55a79c82b74b1d6602c6264a770d
1 /*************************************************************************
3 * $RCSfile: SwingTreeControlProvider.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2007-11-06 15:08:01 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.beans.Property;
42 import com.sun.star.beans.PropertyValue;
43 import com.sun.star.lang.NullPointerException;
44 import com.sun.star.reflection.XConstantTypeDescription;
45 import com.sun.star.reflection.XIdlMethod;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import java.awt.BorderLayout;
49 import java.awt.Container;
50 import java.awt.Dimension;
51 import java.awt.event.KeyAdapter;
52 import java.awt.event.KeyEvent;
53 import java.awt.event.MouseAdapter;
54 import java.awt.event.MouseEvent;
55 import javax.swing.JLabel;
56 import javax.swing.JPanel;
57 import javax.swing.JProgressBar;
58 import javax.swing.JScrollBar;
59 import javax.swing.JScrollPane;
60 import javax.swing.JSplitPane;
61 import javax.swing.JTextArea;
62 import javax.swing.JTextField;
63 import javax.swing.JTree;
64 import javax.swing.event.TreeSelectionEvent;
65 import javax.swing.event.TreeSelectionListener;
66 import javax.swing.event.TreeWillExpandListener;
67 import javax.swing.tree.DefaultTreeSelectionModel;
68 import javax.swing.tree.ExpandVetoException;
69 import javax.swing.tree.TreePath;
70 import javax.swing.tree.TreeSelectionModel;
74 public class SwingTreeControlProvider implements XTreeControlProvider{
75 private JTextArea jtxtGeneratedSourceCode = new JTextArea();
76 private JTextField jtxtFilter = new JTextField();
77 private javax.swing.JLabel jlblFilter = new javax.swing.JLabel("Set a filter");
78 private JPanel jPnlBottom = new JPanel(new BorderLayout());
79 private JPanel jPanelProgress = new JPanel(new BorderLayout());
80 private JPanel jPnlPath = new JPanel(new BorderLayout());
81 private JLabel jLblPath = new JLabel("Generated source code");
82 private JProgressBar jProgressBar1 = new JProgressBar();
83 private JTree jTree = new javax.swing.JTree();
84 private XDialogProvider m_xDialogProvider;
85 private HideableTreeModel treeModel;
86 private UnoTreeRenderer oUnoTreeRenderer;
87 private InspectorPane m_oInspectorPane;
88 private Object oUserDefinedObject = null;
89 private boolean bIsUserDefined = false;
90 private SwingUnoNode oRootNode;
92 private final int nDIALOGWIDTH = 800;
96 public SwingTreeControlProvider(XDialogProvider _xDialogProvider){
97 m_xDialogProvider = _xDialogProvider;
101 public void addInspectorPane(InspectorPane _oInspectorPane){
102 m_oInspectorPane = _oInspectorPane;
105 public InspectorPane getInspectorPane() throws NullPointerException{
106 if (m_oInspectorPane == null){
107 throw new NullPointerException("InspectorPage has not been added to TreeControl");
109 else{
110 return m_oInspectorPane;
114 private void ComponentSelector(Object _oRootObject, String _sRootTreeNodeName) {
115 String sTreeNodeName = _sRootTreeNodeName;
116 oRootNode = new SwingUnoNode(_oRootObject);
117 oRootNode.setLabel(_sRootTreeNodeName);
118 treeModel = new HideableTreeModel(oRootNode);
119 jTree.setModel(treeModel);
120 jTree.setRootVisible(true);
121 jTree.setVisible(true);
122 oRootNode.setFoldable(true);
123 enableFilterElements(null);
128 private void insertTopPanel(JPanel _jPnlCenter){
129 javax.swing.JPanel jPnlTop = new javax.swing.JPanel(new BorderLayout(10, 10));
130 jPnlTop.setPreferredSize(new java.awt.Dimension(nDIALOGWIDTH, 20));
131 jlblFilter.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
132 jlblFilter.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
133 jPnlTop.add(jlblFilter, java.awt.BorderLayout.WEST);
134 jtxtFilter.setHorizontalAlignment(javax.swing.JTextField.LEFT);
135 jtxtFilter.setPreferredSize(new java.awt.Dimension(200, 10));
136 jtxtFilter.addKeyListener(new InspectorKeyFilterAdapter());
137 jPnlTop.add(jtxtFilter, java.awt.BorderLayout.CENTER);
138 _jPnlCenter.add(jPnlTop, java.awt.BorderLayout.NORTH);
142 private void insertBottomPanel(JSplitPane _jSplitPane){ //JPanel _jPnlCenter){
143 jtxtGeneratedSourceCode.setTabSize(4);
144 jtxtGeneratedSourceCode.getAccessibleContext().setAccessibleName("generated SourceCode");
145 JScrollPane jScrollPane = new JScrollPane(jtxtGeneratedSourceCode);
146 jScrollPane.setPreferredSize(new Dimension(nDIALOGWIDTH,205));
147 jtxtGeneratedSourceCode.setEditable(false);
148 _jSplitPane.setBottomComponent(jScrollPane);
151 private void insertBorderPanes(Container _cp){
152 JPanel jPnlEast = new JPanel(new BorderLayout());
153 JPanel jPnlNorth = new JPanel(new BorderLayout());
154 JPanel jPnlSouth = new JPanel(new BorderLayout());
155 JPanel jPnlWest = new JPanel(new BorderLayout());
156 _cp.add(jPnlNorth, java.awt.BorderLayout.NORTH);
157 jPnlWest.setPreferredSize(new java.awt.Dimension(10, 10));
158 _cp.add(jPnlWest, java.awt.BorderLayout.WEST);
159 jPnlEast.setPreferredSize(new java.awt.Dimension(10, 10));
160 _cp.add(jPnlEast, java.awt.BorderLayout.EAST);
161 jPnlSouth.setPreferredSize(new java.awt.Dimension(10, 10));
162 _cp.add(jPnlSouth, java.awt.BorderLayout.SOUTH);
166 public String enableFilterElements(XUnoNode _oUnoNode){
167 String sFilter ="";
168 boolean bIsFacetteNode = isFacetteNode(_oUnoNode);
169 this.jtxtFilter.setEnabled(bIsFacetteNode);
170 this.jlblFilter.setEnabled(bIsFacetteNode);
171 sFilter = m_oInspectorPane.getFilter(_oUnoNode);
172 jtxtFilter.setText(sFilter);
173 return sFilter;
177 private class InspectorKeyFilterAdapter extends KeyAdapter{
178 public void keyReleased(KeyEvent e){
179 String sFilter = jtxtFilter.getText();
180 SwingTreePathProvider oSwingTreePathProvider = new SwingTreePathProvider(jTree.getSelectionPath());
181 XUnoNode oUnoNode = oSwingTreePathProvider.getLastPathComponent();
182 if (oUnoNode instanceof XUnoFacetteNode){
183 m_oInspectorPane.applyFilter((XUnoFacetteNode) oUnoNode, sFilter);
190 /** Inspect the given object for methods, properties, interfaces, and
191 * services.
192 * @param a The object to inspect
193 * @throws RuntimeException If
195 public Object inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
196 JPanel jPnlContainer = new javax.swing.JPanel(new BorderLayout(10, 10));
197 try {
198 javax.swing.JPanel jPnlCenter = new javax.swing.JPanel();
199 bIsUserDefined = (_oUserDefinedObject != null);
200 if (bIsUserDefined){
201 oUserDefinedObject = _oUserDefinedObject;
202 m_oInspectorPane.setTitle(_sTitle);
204 javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
205 TreeSelectionModel tsm = new DefaultTreeSelectionModel();
206 tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
207 jTree.setSelectionModel(tsm);
208 jTree.setVisible(false);
209 jPnlCenter.setLayout(new java.awt.BorderLayout(10, 10));
210 jPnlCenter.getAccessibleContext().setAccessibleName("inspection tab view");
211 insertTopPanel(jPnlCenter);
212 jScrollPane1.setViewportView(jTree);
213 jScrollPane1.setPreferredSize(new java.awt.Dimension(600, 600));
214 jScrollPane1.getAccessibleContext().setAccessibleName("API view scroll pane");
216 JScrollBar jHScrollBar = jScrollPane1.createHorizontalScrollBar();
217 jHScrollBar.getAccessibleContext().setAccessibleName("API view horizontal scroll bar");
218 jScrollPane1.setHorizontalScrollBar(jHScrollBar);
220 JScrollBar jVScrollBar = jScrollPane1.createVerticalScrollBar();
221 jVScrollBar.getAccessibleContext().setAccessibleName("API view vertical scroll bar");
222 jScrollPane1.setVerticalScrollBar(jVScrollBar);
224 JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
225 jSplitPane.setTopComponent(jScrollPane1);
226 jPnlCenter.add(jSplitPane, java.awt.BorderLayout.CENTER);
227 jSplitPane.setDividerLocation(500);
228 insertBottomPanel(jSplitPane);
229 UnoTreeRenderer oUnoTreeRenderer = new UnoTreeRenderer();
230 jTree.setCellRenderer(oUnoTreeRenderer);
231 jTree.addTreeSelectionListener(
232 new TreeSelectionListener() {
233 public void valueChanged(TreeSelectionEvent event) {
234 TreePath tp = event.getNewLeadSelectionPath();
235 if (tp != null){
236 XUnoNode oUnoNode = getSelectedNode();
237 String sFilter = enableFilterElements(oUnoNode);
242 // Add KeyListener for help
243 jTree.addKeyListener( new java.awt.event.KeyAdapter() {
244 public void keyReleased(java.awt.event.KeyEvent event) {
245 if ( event.getKeyCode() == KeyEvent.VK_F1 ) {
246 // function key F1 pressed
247 TreePath aTreePath = jTree.getSelectionPath();
248 SwingUnoNode oUnoNode = (SwingUnoNode) aTreePath.getLastPathComponent();
249 oUnoNode.openIdlDescription(m_xDialogProvider.getIDLPath());
253 jTree.addMouseListener(new MouseAdapter() {
254 public void mousePressed (MouseEvent e) {
255 if (e.isPopupTrigger()){
256 m_oInspectorPane.showPopUpMenu(e.getComponent(), e.getX(), e.getY());
258 //unfortunately under Windows the method "isPopupTrigger" always returns false
259 else if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == MouseEvent.BUTTON3_DOWN_MASK){
260 m_oInspectorPane.showPopUpMenu(e.getComponent(), e.getX(), e.getY());
264 jPnlContainer.add(jPnlCenter, java.awt.BorderLayout.CENTER);
265 insertBorderPanes(jPnlContainer);
266 if (this.bIsUserDefined){
267 ComponentSelector(oUserDefinedObject, m_oInspectorPane.getTitle());
269 m_xDialogProvider.addInspectorPage(m_oInspectorPane.getTitle(), jPnlContainer);
270 addTreeExpandListener();
272 catch( Exception exception ) {
273 exception.printStackTrace(System.out);
275 return jPnlContainer;
278 public HideableTreeModel getModel(){
279 return treeModel;
283 public void addTreeExpandListener(){
284 jTree.addTreeWillExpandListener(
285 new TreeWillExpandListener() {
286 public void treeWillExpand(javax.swing.event.TreeExpansionEvent event) throws javax.swing.tree.ExpandVetoException {
287 SwingTreePathProvider oSwingTreePathProvider = new SwingTreePathProvider(event.getPath());
288 XUnoNode oUnoNode = oSwingTreePathProvider.getLastPathComponent();
289 if (!m_oInspectorPane.expandNode(oUnoNode, oSwingTreePathProvider)){
290 throw new ExpandVetoException(event);
294 public void treeWillCollapse( javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
300 public void setSourceCode(String _sSourceCode){
301 jtxtGeneratedSourceCode.setText(_sSourceCode);
305 public XTreePathProvider getSelectedPath(){
306 return new SwingTreePathProvider(jTree.getSelectionPath());
310 public void expandPath(XTreePathProvider _xTreePathProvider) throws ClassCastException{
311 SwingTreePathProvider oSwingTreePathProvider = (SwingTreePathProvider) _xTreePathProvider;
312 jTree.expandPath(oSwingTreePathProvider.getSwingTreePath());
316 public XUnoNode getSelectedNode(){
317 XUnoNode oUnoNode = null;
318 TreePath aTreePath = jTree.getSelectionPath();
319 Object oNode = aTreePath.getLastPathComponent();
320 if (oNode instanceof XUnoNode){
321 oUnoNode = (XUnoNode) oNode;
323 return oUnoNode;
327 public void nodeInserted(XUnoNode _oParentNode, XUnoNode _oChildNode, int index) {
328 getModel().nodeInserted(_oParentNode, _oChildNode, _oParentNode.getChildCount()-1);
332 public void nodeChanged(XUnoNode _oNode) {
333 getModel().nodeChanged(_oNode);
337 public boolean setNodeVisible(Object node, boolean v) {
338 return getModel().setNodeVisible(node, v);
342 public boolean isPropertyNode(XUnoNode _oUnoNode){
343 return (_oUnoNode instanceof SwingUnoPropertyNode);
347 public boolean isMethodNode(XUnoNode _oUnoNode){
348 return (_oUnoNode instanceof SwingUnoMethodNode);
352 public boolean isFacetteNode(XUnoNode _oUnoNode){
353 return (_oUnoNode instanceof SwingUnoFacetteNode);
357 public XUnoNode addUnoNode(Object _oUnoObject){
358 return new SwingUnoNode(_oUnoObject);
362 public XUnoNode addUnoNode(Object _oUnoObject, Type _aType){
363 return new SwingUnoNode(_oUnoObject, _aType);
367 public XUnoFacetteNode addUnoFacetteNode(XUnoNode _oParentNode, String _sNodeDescription, Object _oUnoObject){
368 SwingUnoFacetteNode oSwingUnoFacetteNode = new SwingUnoFacetteNode(_oUnoObject);
369 oSwingUnoFacetteNode.setLabel(_sNodeDescription);
370 if (_oParentNode != null){
371 ((SwingUnoNode) _oParentNode).addChildNode(oSwingUnoFacetteNode);
373 return oSwingUnoFacetteNode;
377 public XUnoMethodNode addMethodNode(Object _objectElement, XIdlMethod _xIdlMethod){
378 SwingUnoMethodNode oSwingUnoMethodNode = new SwingUnoMethodNode(_xIdlMethod, _objectElement, m_xDialogProvider);
379 return oSwingUnoMethodNode;
383 public XUnoPropertyNode addUnoPropertyNodeWithName(Property _aProperty){
384 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
385 oUnoPropertyNode.setLabel("Name: " + _aProperty.Name);
386 return oUnoPropertyNode;
390 public XUnoPropertyNode addUnoPropertyNodeWithHandle(Property _aProperty){
391 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
392 oUnoPropertyNode.setLabel("Handle: " + _aProperty.Handle);
393 return oUnoPropertyNode;
397 public XUnoPropertyNode addUnoPropertyNodeWithType(Property _aProperty){
398 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
399 oUnoPropertyNode.setLabel("Type: " + _aProperty.Type.getTypeName());
400 return oUnoPropertyNode;
404 public XUnoPropertyNode addUnoPropertyNodeWithAttributesDescription(Property _aProperty){
405 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
406 XConstantTypeDescription[] xPropertyAttributesTypeDescriptions = Introspector.getIntrospector().getFieldsOfConstantGroup("com.sun.star.beans.PropertyAttribute");
407 String sDisplay = Introspector.getIntrospector().getConstantDisplayString((int) _aProperty.Attributes, xPropertyAttributesTypeDescriptions, "Attributes: ");
408 oUnoPropertyNode.setLabel(sDisplay);
409 return oUnoPropertyNode;
413 public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, Property _aProperty){
414 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty, _oUnoObject, null);
415 oUnoPropertyNode.setPropertyNodeType(XUnoPropertyNode.nPROPERTYSETINFOTYPE);
416 oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyDescription(_aProperty, null));
417 return oUnoPropertyNode;
421 public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, Property _aProperty, Object _oUnoReturnObject){
422 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty, _oUnoObject, _oUnoReturnObject);
423 oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyDescription(_aProperty, _oUnoReturnObject));
424 return oUnoPropertyNode;
428 public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, PropertyValue _aPropertyValue, Object _oReturnObject){
429 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aPropertyValue, _oUnoObject, _oReturnObject);
430 oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyValueDescription(_aPropertyValue));
431 return oUnoPropertyNode;