Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Inspector / SwingTreeControlProvider.java
blob1ad7d276dde2684314b90567507eb1a250fe255f
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
35 import com.sun.star.beans.Property;
36 import com.sun.star.beans.PropertyValue;
37 import com.sun.star.lang.NullPointerException;
38 import com.sun.star.reflection.XConstantTypeDescription;
39 import com.sun.star.reflection.XIdlMethod;
40 import com.sun.star.uno.Type;
41 import java.awt.BorderLayout;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.event.KeyAdapter;
45 import java.awt.event.KeyEvent;
46 import java.awt.event.MouseAdapter;
47 import java.awt.event.MouseEvent;
48 import javax.swing.JPanel;
49 import javax.swing.JScrollBar;
50 import javax.swing.JScrollPane;
51 import javax.swing.JSplitPane;
52 import javax.swing.JTextArea;
53 import javax.swing.JTextField;
54 import javax.swing.JTree;
55 import javax.swing.event.TreeSelectionEvent;
56 import javax.swing.event.TreeSelectionListener;
57 import javax.swing.event.TreeWillExpandListener;
58 import javax.swing.tree.DefaultTreeSelectionModel;
59 import javax.swing.tree.ExpandVetoException;
60 import javax.swing.tree.TreePath;
61 import javax.swing.tree.TreeSelectionModel;
65 public class SwingTreeControlProvider implements XTreeControlProvider{
66 private JTextArea jtxtGeneratedSourceCode = new JTextArea();
67 private JTextField jtxtFilter = new JTextField();
68 private javax.swing.JLabel jlblFilter = new javax.swing.JLabel("Set a filter");
69 private JTree jTree = new javax.swing.JTree();
70 private XDialogProvider m_xDialogProvider;
71 private HideableTreeModel treeModel;
72 private InspectorPane m_oInspectorPane;
73 private Object oUserDefinedObject = null;
74 private boolean bIsUserDefined = false;
75 private SwingUnoNode oRootNode;
77 private final int nDIALOGWIDTH = 800;
81 public SwingTreeControlProvider(XDialogProvider _xDialogProvider){
82 m_xDialogProvider = _xDialogProvider;
86 public void addInspectorPane(InspectorPane _oInspectorPane){
87 m_oInspectorPane = _oInspectorPane;
90 public InspectorPane getInspectorPane() throws NullPointerException{
91 if (m_oInspectorPane == null){
92 throw new NullPointerException("InspectorPage has not been added to TreeControl");
94 else{
95 return m_oInspectorPane;
99 private void ComponentSelector(Object _oRootObject, String _sRootTreeNodeName) {
100 oRootNode = new SwingUnoNode(_oRootObject);
101 oRootNode.setLabel(_sRootTreeNodeName);
102 treeModel = new HideableTreeModel(oRootNode);
103 jTree.setModel(treeModel);
104 jTree.setRootVisible(true);
105 jTree.setVisible(true);
106 oRootNode.setFoldable(true);
107 enableFilterElements(null);
112 private void insertTopPanel(JPanel _jPnlCenter){
113 javax.swing.JPanel jPnlTop = new javax.swing.JPanel(new BorderLayout(10, 10));
114 jPnlTop.setPreferredSize(new java.awt.Dimension(nDIALOGWIDTH, 20));
115 jlblFilter.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
116 jlblFilter.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
117 jPnlTop.add(jlblFilter, java.awt.BorderLayout.WEST);
118 jtxtFilter.setHorizontalAlignment(javax.swing.JTextField.LEFT);
119 jtxtFilter.setPreferredSize(new java.awt.Dimension(200, 10));
120 jtxtFilter.addKeyListener(new InspectorKeyFilterAdapter());
121 jPnlTop.add(jtxtFilter, java.awt.BorderLayout.CENTER);
122 _jPnlCenter.add(jPnlTop, java.awt.BorderLayout.NORTH);
126 private void insertBottomPanel(JSplitPane _jSplitPane){ //JPanel _jPnlCenter){
127 jtxtGeneratedSourceCode.setTabSize(4);
128 jtxtGeneratedSourceCode.getAccessibleContext().setAccessibleName("generated SourceCode");
129 JScrollPane jScrollPane = new JScrollPane(jtxtGeneratedSourceCode);
130 jScrollPane.setPreferredSize(new Dimension(nDIALOGWIDTH,205));
131 jtxtGeneratedSourceCode.setEditable(false);
132 _jSplitPane.setBottomComponent(jScrollPane);
135 private void insertBorderPanes(Container _cp){
136 JPanel jPnlEast = new JPanel(new BorderLayout());
137 JPanel jPnlNorth = new JPanel(new BorderLayout());
138 JPanel jPnlSouth = new JPanel(new BorderLayout());
139 JPanel jPnlWest = new JPanel(new BorderLayout());
140 _cp.add(jPnlNorth, java.awt.BorderLayout.NORTH);
141 jPnlWest.setPreferredSize(new java.awt.Dimension(10, 10));
142 _cp.add(jPnlWest, java.awt.BorderLayout.WEST);
143 jPnlEast.setPreferredSize(new java.awt.Dimension(10, 10));
144 _cp.add(jPnlEast, java.awt.BorderLayout.EAST);
145 jPnlSouth.setPreferredSize(new java.awt.Dimension(10, 10));
146 _cp.add(jPnlSouth, java.awt.BorderLayout.SOUTH);
150 public String enableFilterElements(XUnoNode _oUnoNode){
151 String sFilter ="";
152 boolean bIsFacetteNode = isFacetteNode(_oUnoNode);
153 this.jtxtFilter.setEnabled(bIsFacetteNode);
154 this.jlblFilter.setEnabled(bIsFacetteNode);
155 sFilter = m_oInspectorPane.getFilter(_oUnoNode);
156 jtxtFilter.setText(sFilter);
157 return sFilter;
161 private class InspectorKeyFilterAdapter extends KeyAdapter{
162 @Override
163 public void keyReleased(KeyEvent e){
164 String sFilter = jtxtFilter.getText();
165 SwingTreePathProvider oSwingTreePathProvider = new SwingTreePathProvider(jTree.getSelectionPath());
166 XUnoNode oUnoNode = oSwingTreePathProvider.getLastPathComponent();
167 if (oUnoNode instanceof XUnoFacetteNode){
168 m_oInspectorPane.applyFilter((XUnoFacetteNode) oUnoNode, sFilter);
175 /** Inspect the given object for methods, properties, interfaces, and
176 * services.
177 * @param _oUserDefinedObject The object to inspect
178 * @throws RuntimeException If
180 public Object inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
181 JPanel jPnlContainer = new javax.swing.JPanel(new BorderLayout(10, 10));
182 try {
183 javax.swing.JPanel jPnlCenter = new javax.swing.JPanel();
184 bIsUserDefined = (_oUserDefinedObject != null);
185 if (bIsUserDefined){
186 oUserDefinedObject = _oUserDefinedObject;
187 m_oInspectorPane.setTitle(_sTitle);
189 javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
190 TreeSelectionModel tsm = new DefaultTreeSelectionModel();
191 tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
192 jTree.setSelectionModel(tsm);
193 jTree.setVisible(false);
194 jPnlCenter.setLayout(new java.awt.BorderLayout(10, 10));
195 jPnlCenter.getAccessibleContext().setAccessibleName("inspection tab view");
196 insertTopPanel(jPnlCenter);
197 jScrollPane1.setViewportView(jTree);
198 jScrollPane1.setPreferredSize(new java.awt.Dimension(600, 600));
199 jScrollPane1.getAccessibleContext().setAccessibleName("API view scroll pane");
201 JScrollBar jHScrollBar = jScrollPane1.createHorizontalScrollBar();
202 jHScrollBar.getAccessibleContext().setAccessibleName("API view horizontal scroll bar");
203 jScrollPane1.setHorizontalScrollBar(jHScrollBar);
205 JScrollBar jVScrollBar = jScrollPane1.createVerticalScrollBar();
206 jVScrollBar.getAccessibleContext().setAccessibleName("API view vertical scroll bar");
207 jScrollPane1.setVerticalScrollBar(jVScrollBar);
209 JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
210 jSplitPane.setTopComponent(jScrollPane1);
211 jPnlCenter.add(jSplitPane, java.awt.BorderLayout.CENTER);
212 jSplitPane.setDividerLocation(500);
213 insertBottomPanel(jSplitPane);
214 UnoTreeRenderer oUnoTreeRenderer = new UnoTreeRenderer();
215 jTree.setCellRenderer(oUnoTreeRenderer);
216 jTree.addTreeSelectionListener(
217 new TreeSelectionListener() {
218 public void valueChanged(TreeSelectionEvent event) {
219 TreePath tp = event.getNewLeadSelectionPath();
220 if (tp != null){
221 XUnoNode oUnoNode = getSelectedNode();
222 String sFilter = enableFilterElements(oUnoNode);
227 // Add KeyListener for help
228 jTree.addKeyListener( new java.awt.event.KeyAdapter() {
229 @Override
230 public void keyReleased(java.awt.event.KeyEvent event) {
231 if ( event.getKeyCode() == KeyEvent.VK_F1 ) {
232 // function key F1 pressed
233 TreePath aTreePath = jTree.getSelectionPath();
234 SwingUnoNode oUnoNode = (SwingUnoNode) aTreePath.getLastPathComponent();
235 oUnoNode.openIdlDescription(m_xDialogProvider.getIDLPath());
239 jTree.addMouseListener(new MouseAdapter() {
240 @Override
241 public void mousePressed (MouseEvent e) {
242 if (e.isPopupTrigger()){
243 m_oInspectorPane.showPopUpMenu(e.getComponent(), e.getX(), e.getY());
245 //unfortunately under Windows the method "isPopupTrigger" always returns false
246 else if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == MouseEvent.BUTTON3_DOWN_MASK){
247 m_oInspectorPane.showPopUpMenu(e.getComponent(), e.getX(), e.getY());
251 jPnlContainer.add(jPnlCenter, java.awt.BorderLayout.CENTER);
252 insertBorderPanes(jPnlContainer);
253 if (this.bIsUserDefined){
254 ComponentSelector(oUserDefinedObject, m_oInspectorPane.getTitle());
256 m_xDialogProvider.addInspectorPage(m_oInspectorPane.getTitle(), jPnlContainer);
257 addTreeExpandListener();
259 catch( Exception exception ) {
260 exception.printStackTrace(System.err);
262 return jPnlContainer;
265 public HideableTreeModel getModel(){
266 return treeModel;
270 public void addTreeExpandListener(){
271 jTree.addTreeWillExpandListener(
272 new TreeWillExpandListener() {
273 public void treeWillExpand(javax.swing.event.TreeExpansionEvent event) throws javax.swing.tree.ExpandVetoException {
274 SwingTreePathProvider oSwingTreePathProvider = new SwingTreePathProvider(event.getPath());
275 XUnoNode oUnoNode = oSwingTreePathProvider.getLastPathComponent();
276 if (!m_oInspectorPane.expandNode(oUnoNode)){
277 throw new ExpandVetoException(event);
281 public void treeWillCollapse( javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
287 public void setSourceCode(String _sSourceCode){
288 jtxtGeneratedSourceCode.setText(_sSourceCode);
292 public XTreePathProvider getSelectedPath(){
293 return new SwingTreePathProvider(jTree.getSelectionPath());
297 public void expandPath(XTreePathProvider _xTreePathProvider) throws ClassCastException{
298 SwingTreePathProvider oSwingTreePathProvider = (SwingTreePathProvider) _xTreePathProvider;
299 jTree.expandPath(oSwingTreePathProvider.getSwingTreePath());
303 public XUnoNode getSelectedNode(){
304 XUnoNode oUnoNode = null;
305 TreePath aTreePath = jTree.getSelectionPath();
306 Object oNode = aTreePath.getLastPathComponent();
307 if (oNode instanceof XUnoNode){
308 oUnoNode = (XUnoNode) oNode;
310 return oUnoNode;
314 public void nodeInserted(XUnoNode _oParentNode, XUnoNode _oChildNode, int index) {
315 getModel().nodeInserted(_oParentNode, _oChildNode, _oParentNode.getChildCount()-1);
319 public void nodeChanged(XUnoNode _oNode) {
320 getModel().nodeChanged(_oNode);
324 public boolean setNodeVisible(Object node, boolean v) {
325 return getModel().setNodeVisible(node, v);
329 public boolean isPropertyNode(XUnoNode _oUnoNode){
330 return (_oUnoNode instanceof SwingUnoPropertyNode);
334 public boolean isMethodNode(XUnoNode _oUnoNode){
335 return (_oUnoNode instanceof SwingUnoMethodNode);
339 public boolean isFacetteNode(XUnoNode _oUnoNode){
340 return (_oUnoNode instanceof SwingUnoFacetteNode);
344 public XUnoNode addUnoNode(Object _oUnoObject){
345 return new SwingUnoNode(_oUnoObject);
349 public XUnoNode addUnoNode(Object _oUnoObject, Type _aType){
350 return new SwingUnoNode(_oUnoObject, _aType);
354 public XUnoFacetteNode addUnoFacetteNode(XUnoNode _oParentNode, String _sNodeDescription, Object _oUnoObject){
355 SwingUnoFacetteNode oSwingUnoFacetteNode = new SwingUnoFacetteNode(_oUnoObject);
356 oSwingUnoFacetteNode.setLabel(_sNodeDescription);
357 if (_oParentNode != null){
358 ((SwingUnoNode) _oParentNode).addChildNode(oSwingUnoFacetteNode);
360 return oSwingUnoFacetteNode;
364 public XUnoMethodNode addMethodNode(Object _objectElement, XIdlMethod _xIdlMethod){
365 SwingUnoMethodNode oSwingUnoMethodNode = new SwingUnoMethodNode(_xIdlMethod, _objectElement, m_xDialogProvider);
366 return oSwingUnoMethodNode;
370 public XUnoPropertyNode addUnoPropertyNodeWithName(Property _aProperty){
371 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
372 oUnoPropertyNode.setLabel("Name: " + _aProperty.Name);
373 return oUnoPropertyNode;
377 public XUnoPropertyNode addUnoPropertyNodeWithHandle(Property _aProperty){
378 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
379 oUnoPropertyNode.setLabel("Handle: " + _aProperty.Handle);
380 return oUnoPropertyNode;
384 public XUnoPropertyNode addUnoPropertyNodeWithType(Property _aProperty){
385 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
386 oUnoPropertyNode.setLabel("Type: " + _aProperty.Type.getTypeName());
387 return oUnoPropertyNode;
391 public XUnoPropertyNode addUnoPropertyNodeWithAttributesDescription(Property _aProperty){
392 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
393 XConstantTypeDescription[] xPropertyAttributesTypeDescriptions = Introspector.getIntrospector().getFieldsOfConstantGroup("com.sun.star.beans.PropertyAttribute");
394 String sDisplay = Introspector.getIntrospector().getConstantDisplayString(_aProperty.Attributes, xPropertyAttributesTypeDescriptions, "Attributes: ");
395 oUnoPropertyNode.setLabel(sDisplay);
396 return oUnoPropertyNode;
400 public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, Property _aProperty){
401 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty, _oUnoObject, null);
402 oUnoPropertyNode.setPropertyNodeType(XUnoPropertyNode.nPROPERTYSETINFOTYPE);
403 oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyDescription(_aProperty, null));
404 return oUnoPropertyNode;
408 public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, Property _aProperty, Object _oUnoReturnObject){
409 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty, _oUnoObject, _oUnoReturnObject);
410 oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyDescription(_aProperty, _oUnoReturnObject));
411 return oUnoPropertyNode;
415 public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, PropertyValue _aPropertyValue, Object _oReturnObject){
416 SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aPropertyValue, _oUnoObject, _oReturnObject);
417 oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyValueDescription(_aPropertyValue));
418 return oUnoPropertyNode;