1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.beans.Property;
25 import com.sun.star.beans.PropertyValue;
26 import com.sun.star.lang.NullPointerException;
27 import com.sun.star.reflection.XConstantTypeDescription;
28 import com.sun.star.reflection.XIdlMethod;
29 import com.sun.star.uno.AnyConverter;
30 import com.sun.star.uno.Type;
31 import java.awt.BorderLayout;
32 import java.awt.Container;
33 import java.awt.Dimension;
34 import java.awt.event.KeyAdapter;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import javax.swing.JLabel;
39 import javax.swing.JPanel;
40 import javax.swing.JProgressBar;
41 import javax.swing.JScrollBar;
42 import javax.swing.JScrollPane;
43 import javax.swing.JSplitPane;
44 import javax.swing.JTextArea;
45 import javax.swing.JTextField;
46 import javax.swing.JTree;
47 import javax.swing.event.TreeSelectionEvent;
48 import javax.swing.event.TreeSelectionListener;
49 import javax.swing.event.TreeWillExpandListener;
50 import javax.swing.tree.DefaultTreeSelectionModel;
51 import javax.swing.tree.ExpandVetoException;
52 import javax.swing.tree.TreePath;
53 import javax.swing.tree.TreeSelectionModel;
54 
55 
56 
57 public class SwingTreeControlProvider implements XTreeControlProvider{
58     private JTextArea jtxtGeneratedSourceCode = new JTextArea();
59     private JTextField jtxtFilter = new JTextField();
60     private javax.swing.JLabel jlblFilter = new javax.swing.JLabel("Set a filter");
61     private JPanel jPnlBottom = new JPanel(new BorderLayout());
62     private JPanel jPanelProgress = new JPanel(new BorderLayout());
63     private JPanel jPnlPath = new JPanel(new BorderLayout());
64     private JLabel jLblPath = new JLabel("Generated source code");
65     private JProgressBar jProgressBar1 = new JProgressBar();
66     private JTree jTree = new javax.swing.JTree();
67     private XDialogProvider m_xDialogProvider;
68     private HideableTreeModel treeModel;
69     private UnoTreeRenderer oUnoTreeRenderer;
70     private InspectorPane m_oInspectorPane;
71     private Object oUserDefinedObject = null;
72     private boolean bIsUserDefined = false;
73     private SwingUnoNode oRootNode;
74 
75     private final int nDIALOGWIDTH = 800;
76 
77 
78 
SwingTreeControlProvider(XDialogProvider _xDialogProvider)79     public SwingTreeControlProvider(XDialogProvider _xDialogProvider){
80         m_xDialogProvider = _xDialogProvider;
81     }
82 
83 
addInspectorPane(InspectorPane _oInspectorPane)84     public void addInspectorPane(InspectorPane _oInspectorPane){
85         m_oInspectorPane = _oInspectorPane;
86     }
87 
getInspectorPane()88     public InspectorPane getInspectorPane() throws NullPointerException{
89         if (m_oInspectorPane == null){
90             throw new NullPointerException("InspectorPage has not been added to TreeControl");
91         }
92         else{
93             return m_oInspectorPane;
94         }
95     }
96 
ComponentSelector(Object _oRootObject, String _sRootTreeNodeName)97     private void ComponentSelector(Object _oRootObject, String _sRootTreeNodeName) {
98         String sTreeNodeName =  _sRootTreeNodeName;
99         oRootNode = new SwingUnoNode(_oRootObject);
100         oRootNode.setLabel(_sRootTreeNodeName);
101         treeModel = new HideableTreeModel(oRootNode);
102         jTree.setModel(treeModel);
103         jTree.setRootVisible(true);
104         jTree.setVisible(true);
105         oRootNode.setFoldable(true);
106         enableFilterElements(null);
107     }
108 
109 
110 
insertTopPanel(JPanel _jPnlCenter)111     private void insertTopPanel(JPanel _jPnlCenter){
112         javax.swing.JPanel jPnlTop = new javax.swing.JPanel(new BorderLayout(10, 10));
113         jPnlTop.setPreferredSize(new java.awt.Dimension(nDIALOGWIDTH, 20));
114         jlblFilter.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
115         jlblFilter.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
116         jPnlTop.add(jlblFilter, java.awt.BorderLayout.WEST);
117         jtxtFilter.setHorizontalAlignment(javax.swing.JTextField.LEFT);
118         jtxtFilter.setPreferredSize(new java.awt.Dimension(200, 10));
119         jtxtFilter.addKeyListener(new InspectorKeyFilterAdapter());
120         jPnlTop.add(jtxtFilter, java.awt.BorderLayout.CENTER);
121         _jPnlCenter.add(jPnlTop, java.awt.BorderLayout.NORTH);
122     }
123 
124 
insertBottomPanel(JSplitPane _jSplitPane)125     private void insertBottomPanel(JSplitPane _jSplitPane){ //JPanel _jPnlCenter){
126         jtxtGeneratedSourceCode.setTabSize(4);
127         jtxtGeneratedSourceCode.getAccessibleContext().setAccessibleName("generated SourceCode");
128         JScrollPane jScrollPane = new JScrollPane(jtxtGeneratedSourceCode);
129         jScrollPane.setPreferredSize(new Dimension(nDIALOGWIDTH,205));
130         jtxtGeneratedSourceCode.setEditable(false);
131         _jSplitPane.setBottomComponent(jScrollPane);
132     }
133 
insertBorderPanes(Container _cp)134         private void insertBorderPanes(Container _cp){
135             JPanel jPnlEast  = new JPanel(new BorderLayout());
136             JPanel jPnlNorth = new JPanel(new BorderLayout());
137             JPanel jPnlSouth = new JPanel(new BorderLayout());
138             JPanel jPnlWest = new JPanel(new BorderLayout());
139             _cp.add(jPnlNorth, java.awt.BorderLayout.NORTH);
140             jPnlWest.setPreferredSize(new java.awt.Dimension(10, 10));
141             _cp.add(jPnlWest, java.awt.BorderLayout.WEST);
142             jPnlEast.setPreferredSize(new java.awt.Dimension(10, 10));
143             _cp.add(jPnlEast, java.awt.BorderLayout.EAST);
144             jPnlSouth.setPreferredSize(new java.awt.Dimension(10, 10));
145             _cp.add(jPnlSouth, java.awt.BorderLayout.SOUTH);
146         }
147 
148 
enableFilterElements(XUnoNode _oUnoNode)149         public String enableFilterElements(XUnoNode _oUnoNode){
150             String sFilter ="";
151             boolean bIsFacetteNode = isFacetteNode(_oUnoNode);
152             this.jtxtFilter.setEnabled(bIsFacetteNode);
153             this.jlblFilter.setEnabled(bIsFacetteNode);
154             sFilter = m_oInspectorPane.getFilter(_oUnoNode);
155             jtxtFilter.setText(sFilter);
156             return sFilter;
157         }
158 
159 
160         private class InspectorKeyFilterAdapter extends KeyAdapter{
keyReleased(KeyEvent e)161             public void keyReleased(KeyEvent e){
162                 String sFilter = jtxtFilter.getText();
163                 SwingTreePathProvider oSwingTreePathProvider = new SwingTreePathProvider(jTree.getSelectionPath());
164                 XUnoNode oUnoNode = oSwingTreePathProvider.getLastPathComponent();
165                 if (oUnoNode instanceof XUnoFacetteNode){
166                     m_oInspectorPane.applyFilter((XUnoFacetteNode) oUnoNode, sFilter);
167                 }
168             }
169         }
170 
171 
172 
173         /** Inspect the given object for methods, properties, interfaces, and
174          * services.
175          * @param a The object to inspect
176          * @throws RuntimeException If
177          */
inspect(java.lang.Object _oUserDefinedObject, String _sTitle)178         public Object inspect(java.lang.Object _oUserDefinedObject, String _sTitle) throws com.sun.star.uno.RuntimeException {
179         JPanel jPnlContainer = new javax.swing.JPanel(new BorderLayout(10, 10));
180         try {
181             javax.swing.JPanel jPnlCenter = new javax.swing.JPanel();
182             bIsUserDefined = (_oUserDefinedObject != null);
183             if (bIsUserDefined){
184                 oUserDefinedObject = _oUserDefinedObject;
185                 m_oInspectorPane.setTitle(_sTitle);
186             }
187             javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
188             TreeSelectionModel tsm = new DefaultTreeSelectionModel();
189             tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
190             jTree.setSelectionModel(tsm);
191             jTree.setVisible(false);
192             jPnlCenter.setLayout(new java.awt.BorderLayout(10, 10));
193             jPnlCenter.getAccessibleContext().setAccessibleName("inspection tab view");
194             insertTopPanel(jPnlCenter);
195             jScrollPane1.setViewportView(jTree);
196             jScrollPane1.setPreferredSize(new java.awt.Dimension(600, 600));
197             jScrollPane1.getAccessibleContext().setAccessibleName("API view scroll pane");
198 
199             JScrollBar jHScrollBar = jScrollPane1.createHorizontalScrollBar();
200             jHScrollBar.getAccessibleContext().setAccessibleName("API view horizontal scroll bar");
201             jScrollPane1.setHorizontalScrollBar(jHScrollBar);
202 
203             JScrollBar jVScrollBar = jScrollPane1.createVerticalScrollBar();
204             jVScrollBar.getAccessibleContext().setAccessibleName("API view vertical scroll bar");
205             jScrollPane1.setVerticalScrollBar(jVScrollBar);
206 
207             JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
208             jSplitPane.setTopComponent(jScrollPane1);
209             jPnlCenter.add(jSplitPane, java.awt.BorderLayout.CENTER);
210             jSplitPane.setDividerLocation(500);
211             insertBottomPanel(jSplitPane);
212             UnoTreeRenderer oUnoTreeRenderer = new UnoTreeRenderer();
213             jTree.setCellRenderer(oUnoTreeRenderer);
214             jTree.addTreeSelectionListener(
215                 new TreeSelectionListener() {
216                     public void valueChanged(TreeSelectionEvent event) {
217                         TreePath tp = event.getNewLeadSelectionPath();
218                         if (tp != null){
219                             XUnoNode oUnoNode = getSelectedNode();
220                             String sFilter = enableFilterElements(oUnoNode);
221                         }
222                     }
223                 }
224                 );
225             //  Add KeyListener for help
226             jTree.addKeyListener( new java.awt.event.KeyAdapter() {
227                 public void keyReleased(java.awt.event.KeyEvent event) {
228                     if ( event.getKeyCode() == KeyEvent.VK_F1 ) {
229                         //  function key F1 pressed
230                         TreePath aTreePath = jTree.getSelectionPath();
231                         SwingUnoNode oUnoNode = (SwingUnoNode) aTreePath.getLastPathComponent();
232                         oUnoNode.openIdlDescription(m_xDialogProvider.getIDLPath());
233                     }
234                 }
235             });
236             jTree.addMouseListener(new MouseAdapter() {
237                 public void mousePressed (MouseEvent e) {
238                     if (e.isPopupTrigger()){
239                         m_oInspectorPane.showPopUpMenu(e.getComponent(), e.getX(), e.getY());
240                     }
241                     //unfortunately under Windows the method "isPopupTrigger" always returns false
242                     else if ((e.getModifiersEx() & MouseEvent.BUTTON3_DOWN_MASK) == MouseEvent.BUTTON3_DOWN_MASK){
243                         m_oInspectorPane.showPopUpMenu(e.getComponent(), e.getX(), e.getY());
244                     }
245                 }
246             });
247             jPnlContainer.add(jPnlCenter, java.awt.BorderLayout.CENTER);
248             insertBorderPanes(jPnlContainer);
249             if (this.bIsUserDefined){
250                 ComponentSelector(oUserDefinedObject, m_oInspectorPane.getTitle());
251             }
252             m_xDialogProvider.addInspectorPage(m_oInspectorPane.getTitle(), jPnlContainer);
253             addTreeExpandListener();
254             }
255             catch( Exception exception ) {
256                 exception.printStackTrace(System.out);
257             }
258             return jPnlContainer;
259         }
260 
getModel()261         public HideableTreeModel getModel(){
262             return  treeModel;
263         }
264 
265 
addTreeExpandListener()266         public void addTreeExpandListener(){
267             jTree.addTreeWillExpandListener(
268                 new TreeWillExpandListener() {
269                     public void treeWillExpand(javax.swing.event.TreeExpansionEvent event) throws javax.swing.tree.ExpandVetoException {
270                         SwingTreePathProvider oSwingTreePathProvider = new SwingTreePathProvider(event.getPath());
271                         XUnoNode oUnoNode = oSwingTreePathProvider.getLastPathComponent();
272                         if (!m_oInspectorPane.expandNode(oUnoNode, oSwingTreePathProvider)){
273                             throw new ExpandVetoException(event);
274                         }
275                     }
276 
277                     public void treeWillCollapse( javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {
278                     }
279             });
280         }
281 
282 
setSourceCode(String _sSourceCode)283     public void setSourceCode(String _sSourceCode){
284         jtxtGeneratedSourceCode.setText(_sSourceCode);
285     }
286 
287 
getSelectedPath()288     public XTreePathProvider getSelectedPath(){
289         return new SwingTreePathProvider(jTree.getSelectionPath());
290     }
291 
292 
expandPath(XTreePathProvider _xTreePathProvider)293     public void expandPath(XTreePathProvider _xTreePathProvider) throws ClassCastException{
294         SwingTreePathProvider oSwingTreePathProvider = (SwingTreePathProvider) _xTreePathProvider;
295         jTree.expandPath(oSwingTreePathProvider.getSwingTreePath());
296     }
297 
298 
getSelectedNode()299     public XUnoNode getSelectedNode(){
300         XUnoNode oUnoNode = null;
301         TreePath aTreePath = jTree.getSelectionPath();
302         Object oNode = aTreePath.getLastPathComponent();
303         if (oNode instanceof XUnoNode){
304             oUnoNode = (XUnoNode) oNode;
305         }
306         return oUnoNode;
307     }
308 
309 
nodeInserted(XUnoNode _oParentNode, XUnoNode _oChildNode, int index)310     public void nodeInserted(XUnoNode _oParentNode, XUnoNode _oChildNode, int index) {
311         getModel().nodeInserted(_oParentNode, _oChildNode, _oParentNode.getChildCount()-1);
312     }
313 
314 
nodeChanged(XUnoNode _oNode)315     public void nodeChanged(XUnoNode _oNode) {
316         getModel().nodeChanged(_oNode);
317     }
318 
319 
setNodeVisible(Object node, boolean v)320     public boolean setNodeVisible(Object node, boolean v) {
321        return getModel().setNodeVisible(node, v);
322     }
323 
324 
isPropertyNode(XUnoNode _oUnoNode)325     public boolean isPropertyNode(XUnoNode _oUnoNode){
326         return (_oUnoNode instanceof SwingUnoPropertyNode);
327     }
328 
329 
isMethodNode(XUnoNode _oUnoNode)330     public boolean isMethodNode(XUnoNode _oUnoNode){
331         return (_oUnoNode instanceof SwingUnoMethodNode);
332     }
333 
334 
isFacetteNode(XUnoNode _oUnoNode)335     public boolean isFacetteNode(XUnoNode _oUnoNode){
336         return (_oUnoNode instanceof SwingUnoFacetteNode);
337     }
338 
339 
addUnoNode(Object _oUnoObject)340     public XUnoNode addUnoNode(Object _oUnoObject){
341         return new SwingUnoNode(_oUnoObject);
342     }
343 
344 
addUnoNode(Object _oUnoObject, Type _aType)345     public XUnoNode addUnoNode(Object _oUnoObject, Type _aType){
346         return new SwingUnoNode(_oUnoObject, _aType);
347     }
348 
349 
addUnoFacetteNode(XUnoNode _oParentNode, String _sNodeDescription, Object _oUnoObject)350     public XUnoFacetteNode addUnoFacetteNode(XUnoNode _oParentNode, String _sNodeDescription, Object _oUnoObject){
351         SwingUnoFacetteNode oSwingUnoFacetteNode = new SwingUnoFacetteNode(_oUnoObject);
352         oSwingUnoFacetteNode.setLabel(_sNodeDescription);
353         if (_oParentNode != null){
354             ((SwingUnoNode) _oParentNode).addChildNode(oSwingUnoFacetteNode);
355         }
356         return oSwingUnoFacetteNode;
357     }
358 
359 
addMethodNode(Object _objectElement, XIdlMethod _xIdlMethod)360     public XUnoMethodNode addMethodNode(Object _objectElement, XIdlMethod _xIdlMethod){
361         SwingUnoMethodNode oSwingUnoMethodNode = new SwingUnoMethodNode(_xIdlMethod, _objectElement, m_xDialogProvider);
362         return oSwingUnoMethodNode;
363     }
364 
365 
addUnoPropertyNodeWithName(Property _aProperty)366     public XUnoPropertyNode addUnoPropertyNodeWithName(Property _aProperty){
367         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
368         oUnoPropertyNode.setLabel("Name: " + _aProperty.Name);
369         return oUnoPropertyNode;
370     }
371 
372 
addUnoPropertyNodeWithHandle(Property _aProperty)373     public XUnoPropertyNode addUnoPropertyNodeWithHandle(Property _aProperty){
374         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
375         oUnoPropertyNode.setLabel("Handle: " + _aProperty.Handle);
376         return oUnoPropertyNode;
377     }
378 
379 
addUnoPropertyNodeWithType(Property _aProperty)380     public XUnoPropertyNode addUnoPropertyNodeWithType(Property _aProperty){
381         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
382         oUnoPropertyNode.setLabel("Type: " + _aProperty.Type.getTypeName());
383         return oUnoPropertyNode;
384     }
385 
386 
addUnoPropertyNodeWithAttributesDescription(Property _aProperty)387     public XUnoPropertyNode addUnoPropertyNodeWithAttributesDescription(Property _aProperty){
388         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty);
389         XConstantTypeDescription[] xPropertyAttributesTypeDescriptions = Introspector.getIntrospector().getFieldsOfConstantGroup("com.sun.star.beans.PropertyAttribute");
390         String sDisplay = Introspector.getIntrospector().getConstantDisplayString((int) _aProperty.Attributes, xPropertyAttributesTypeDescriptions, "Attributes: ");
391         oUnoPropertyNode.setLabel(sDisplay);
392         return oUnoPropertyNode;
393     }
394 
395 
addUnoPropertyNode(Object _oUnoObject, Property _aProperty)396     public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, Property _aProperty){
397         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty, _oUnoObject, null);
398         oUnoPropertyNode.setPropertyNodeType(XUnoPropertyNode.nPROPERTYSETINFOTYPE);
399         oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyDescription(_aProperty, null));
400         return oUnoPropertyNode;
401     }
402 
403 
addUnoPropertyNode(Object _oUnoObject, Property _aProperty, Object _oUnoReturnObject)404     public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, Property _aProperty, Object _oUnoReturnObject){
405         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aProperty, _oUnoObject, _oUnoReturnObject);
406         oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyDescription(_aProperty, _oUnoReturnObject));
407         return oUnoPropertyNode;
408     }
409 
410 
addUnoPropertyNode(Object _oUnoObject, PropertyValue _aPropertyValue, Object _oReturnObject)411     public XUnoPropertyNode addUnoPropertyNode(Object _oUnoObject, PropertyValue _aPropertyValue, Object _oReturnObject){
412         SwingUnoPropertyNode oUnoPropertyNode = new SwingUnoPropertyNode(_aPropertyValue, _oUnoObject, _oReturnObject);
413         oUnoPropertyNode.setLabel(UnoPropertyNode.getStandardPropertyValueDescription(_aPropertyValue));
414         return oUnoPropertyNode;
415     }
416 }
417