1*cd519653SAndrew Rist /**************************************************************
2*cd519653SAndrew Rist  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22cdf0e10cSrcweir import javax.swing.*;
23cdf0e10cSrcweir import javax.swing.tree.*;
24cdf0e10cSrcweir import javax.swing.table.*;
25cdf0e10cSrcweir import javax.swing.event.*;
26cdf0e10cSrcweir import javax.swing.border.*;
27cdf0e10cSrcweir import java.awt.*;
28cdf0e10cSrcweir import java.awt.event.*;
29cdf0e10cSrcweir import java.util.*;
30cdf0e10cSrcweir import java.beans.*;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import com.sun.star.uno.Exception;
34cdf0e10cSrcweir import com.sun.star.uno.Any;
35cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
36cdf0e10cSrcweir import com.sun.star.uno.Type;
37cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
40cdf0e10cSrcweir import com.sun.star.lang.XComponent;
41cdf0e10cSrcweir import com.sun.star.frame.XModel;
42cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag;
43cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
44cdf0e10cSrcweir import com.sun.star.frame.XDispatchHelper;
45cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
46cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
47cdf0e10cSrcweir import com.sun.star.beans.*;
48cdf0e10cSrcweir import com.sun.star.script.XInvocation;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir import com.sun.star.lib.uno.helper.PropertySet;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir import com.sun.star.script.browse.XBrowseNode;
53cdf0e10cSrcweir import com.sun.star.script.browse.BrowseNodeTypes;
54cdf0e10cSrcweir import com.sun.star.script.browse.XBrowseNodeFactory;
55cdf0e10cSrcweir import com.sun.star.script.browse.BrowseNodeFactoryViewTypes;
56cdf0e10cSrcweir import com.sun.star.script.provider.XScriptContext;
57cdf0e10cSrcweir import com.sun.star.script.provider.XScript;
58cdf0e10cSrcweir import com.sun.star.script.provider.XScriptProvider;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir public class ScriptSelector {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     private static final int BIG_GAP = 10;
63cdf0e10cSrcweir     private static final int MED_GAP = 5;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     private ScriptSelectorPanel selectorPanel;
66cdf0e10cSrcweir 
ScriptSelector()67cdf0e10cSrcweir     public ScriptSelector()
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
showOrganizer(final XScriptContext ctxt)71cdf0e10cSrcweir     public void showOrganizer(final XScriptContext ctxt)
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         try {
74cdf0e10cSrcweir             XBrowseNode root = getRootNode(ctxt);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             final XScriptProvider msp =
77cdf0e10cSrcweir                 (XScriptProvider)UnoRuntime.queryInterface(
78cdf0e10cSrcweir                  XScriptProvider.class, root);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             final JFrame client = new JFrame("Script");
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             selectorPanel = new ScriptSelectorPanel(root);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             final JButton runButton, closeButton, createButton,
85cdf0e10cSrcweir                           editButton, deleteButton;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             runButton = new JButton("Run");
88cdf0e10cSrcweir             runButton.setEnabled(false);
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             closeButton = new JButton("Close");
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             editButton = new JButton("Edit");
93cdf0e10cSrcweir             editButton.setEnabled(false);
94cdf0e10cSrcweir 
95cdf0e10cSrcweir             JPanel northButtons =
96cdf0e10cSrcweir                 new JPanel(new GridLayout(2, 1, MED_GAP, MED_GAP));
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             northButtons.add(runButton);
99cdf0e10cSrcweir             northButtons.add(closeButton);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir             createButton = new JButton("Create");
102cdf0e10cSrcweir             createButton.setEnabled(false);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             deleteButton = new JButton("Delete");
105cdf0e10cSrcweir             deleteButton.setEnabled(false);
106cdf0e10cSrcweir 
107cdf0e10cSrcweir             JPanel southButtons =
108cdf0e10cSrcweir                 new JPanel(new GridLayout(3, 1, MED_GAP, MED_GAP));
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             southButtons.add(editButton);
111cdf0e10cSrcweir             southButtons.add(createButton);
112cdf0e10cSrcweir             southButtons.add(deleteButton);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             selectorPanel.tree.addTreeSelectionListener(
115cdf0e10cSrcweir                 new TreeSelectionListener() {
116cdf0e10cSrcweir                     public void valueChanged(TreeSelectionEvent e) {
117cdf0e10cSrcweir                         XBrowseNode xbn = selectorPanel.getSelection();
118cdf0e10cSrcweir                         XPropertySet props = (XPropertySet)
119cdf0e10cSrcweir                             UnoRuntime.queryInterface(XPropertySet.class, xbn);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir                         checkEnabled(props, "Creatable", createButton);
122cdf0e10cSrcweir                         checkEnabled(props, "Deletable", deleteButton);
123cdf0e10cSrcweir                         checkEnabled(props, "Editable", editButton);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir                         if (xbn != null &&
126cdf0e10cSrcweir                             xbn.getType() == BrowseNodeTypes.SCRIPT)
127cdf0e10cSrcweir                         {
128cdf0e10cSrcweir                             runButton.setEnabled(true);
129cdf0e10cSrcweir                         }
130cdf0e10cSrcweir                         else
131cdf0e10cSrcweir                         {
132cdf0e10cSrcweir                             runButton.setEnabled(false);
133cdf0e10cSrcweir                         }
134cdf0e10cSrcweir                     }
135cdf0e10cSrcweir                 }
136cdf0e10cSrcweir             );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             ActionListener listener = new ActionListener() {
139cdf0e10cSrcweir                 public void actionPerformed(ActionEvent event) {
140cdf0e10cSrcweir                     if (event.getSource() == runButton) {
141cdf0e10cSrcweir                         String uri = selectorPanel.textField.getText();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir                         try {
144cdf0e10cSrcweir                             XScript script = msp.getScript(uri);
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                             Object[][] out = new Object[1][0];
147cdf0e10cSrcweir                             out[0] = new Object[0];
148cdf0e10cSrcweir 
149cdf0e10cSrcweir                             short[][] num = new short[1][0];
150cdf0e10cSrcweir                             num[0] = new short[0];
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                             script.invoke(new Object[0], num, out);
153cdf0e10cSrcweir                         }
154cdf0e10cSrcweir                         catch (Exception e) {
155cdf0e10cSrcweir                             e.printStackTrace();
156cdf0e10cSrcweir                         }
157cdf0e10cSrcweir                     }
158cdf0e10cSrcweir                     else if (event.getSource() == closeButton) {
159cdf0e10cSrcweir                         client.dispose();
160cdf0e10cSrcweir                     }
161cdf0e10cSrcweir                     else if (event.getSource() == editButton) {
162cdf0e10cSrcweir                         DefaultMutableTreeNode node =
163cdf0e10cSrcweir                           (DefaultMutableTreeNode)
164cdf0e10cSrcweir                           selectorPanel.tree.getLastSelectedPathComponent();
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                         if (node == null) return;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                         showEditor(ctxt, node);
169cdf0e10cSrcweir                     }
170cdf0e10cSrcweir                     else if (event.getSource() == createButton) {
171cdf0e10cSrcweir                         DefaultMutableTreeNode node =
172cdf0e10cSrcweir                           (DefaultMutableTreeNode)
173cdf0e10cSrcweir                           selectorPanel.tree.getLastSelectedPathComponent();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir                         if (node == null) return;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir                         doCreate(ctxt, node);
178cdf0e10cSrcweir                     }
179cdf0e10cSrcweir                     else if (event.getSource() == deleteButton) {
180cdf0e10cSrcweir                         DefaultMutableTreeNode node =
181cdf0e10cSrcweir                           (DefaultMutableTreeNode)
182cdf0e10cSrcweir                           selectorPanel.tree.getLastSelectedPathComponent();
183cdf0e10cSrcweir 
184cdf0e10cSrcweir                         if (node == null) return;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir                         doDelete(ctxt, node);
187cdf0e10cSrcweir                     }
188cdf0e10cSrcweir                 }
189cdf0e10cSrcweir             };
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             runButton.addActionListener(listener);
192cdf0e10cSrcweir             closeButton.addActionListener(listener);
193cdf0e10cSrcweir             createButton.addActionListener(listener);
194cdf0e10cSrcweir             editButton.addActionListener(listener);
195cdf0e10cSrcweir             deleteButton.addActionListener(listener);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir             JPanel buttonPanel = new JPanel(new BorderLayout());
198cdf0e10cSrcweir             buttonPanel.add(northButtons, BorderLayout.NORTH);
199cdf0e10cSrcweir             buttonPanel.add(southButtons, BorderLayout.SOUTH);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir             JPanel mainPanel = new JPanel(new BorderLayout(MED_GAP, MED_GAP));
202cdf0e10cSrcweir             mainPanel.setBorder(
203cdf0e10cSrcweir                 new EmptyBorder(BIG_GAP, BIG_GAP, BIG_GAP, BIG_GAP));
204cdf0e10cSrcweir             mainPanel.add(selectorPanel, BorderLayout.CENTER);
205cdf0e10cSrcweir             mainPanel.add(buttonPanel, BorderLayout.EAST);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir             client.getContentPane().add(mainPanel);
208cdf0e10cSrcweir             client.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
209cdf0e10cSrcweir             client.setSize(500, 350);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir             // set the x and y locations so that the frame is in the
212cdf0e10cSrcweir             // centre of the screen
213cdf0e10cSrcweir             Dimension d = client.getToolkit().getScreenSize();
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             int x = (int)((d.getWidth() - client.getWidth()) / 2);
216cdf0e10cSrcweir             int y = (int)((d.getHeight() - client.getHeight()) / 2);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             client.setLocation(x, y);
219cdf0e10cSrcweir 
220cdf0e10cSrcweir             client.show();
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir         catch (com.sun.star.uno.RuntimeException rue) {
223cdf0e10cSrcweir             rue.printStackTrace();
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir         catch (java.lang.Exception e) {
226cdf0e10cSrcweir             e.printStackTrace();
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir     }
229cdf0e10cSrcweir 
showOrganizer(final XScriptContext ctxt, final com.sun.star.awt.MouseEvent e)230cdf0e10cSrcweir     public void showOrganizer(final XScriptContext ctxt,
231cdf0e10cSrcweir         final com.sun.star.awt.MouseEvent e)
232cdf0e10cSrcweir     {
233cdf0e10cSrcweir         showOrganizer(ctxt);
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir 
showOrganizer(final XScriptContext ctxt, final com.sun.star.awt.ActionEvent e)236cdf0e10cSrcweir     public void showOrganizer(final XScriptContext ctxt,
237cdf0e10cSrcweir         final com.sun.star.awt.ActionEvent e)
238cdf0e10cSrcweir     {
239cdf0e10cSrcweir         showOrganizer(ctxt);
240cdf0e10cSrcweir     }
241cdf0e10cSrcweir 
doDelete( XScriptContext ctxt, DefaultMutableTreeNode node)242cdf0e10cSrcweir     private void doDelete(
243cdf0e10cSrcweir         XScriptContext ctxt, DefaultMutableTreeNode node)
244cdf0e10cSrcweir     {
245cdf0e10cSrcweir         Object obj = node.getUserObject();
246cdf0e10cSrcweir         XInvocation inv =
247cdf0e10cSrcweir             (XInvocation)UnoRuntime.queryInterface(
248cdf0e10cSrcweir             XInvocation.class, obj);
249cdf0e10cSrcweir         Object[] args = new Object[] { ctxt };
250cdf0e10cSrcweir         try {
251cdf0e10cSrcweir             Object result = inv.invoke("Deletable", args,
252cdf0e10cSrcweir                 new short[1][0], new Object[1][0]);
253cdf0e10cSrcweir 
254cdf0e10cSrcweir             if (result != null && AnyConverter.toBoolean(result) == true)
255cdf0e10cSrcweir             {
256cdf0e10cSrcweir                 selectorPanel.removeNode(node);
257cdf0e10cSrcweir             }
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir         catch (Exception e) {
260cdf0e10cSrcweir             e.printStackTrace();
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
doCreate( XScriptContext ctxt, DefaultMutableTreeNode node)264cdf0e10cSrcweir     private void doCreate(
265cdf0e10cSrcweir         XScriptContext ctxt, DefaultMutableTreeNode node)
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         Object obj = node.getUserObject();
268cdf0e10cSrcweir         XInvocation inv =
269cdf0e10cSrcweir             (XInvocation)UnoRuntime.queryInterface(
270cdf0e10cSrcweir             XInvocation.class, obj);
271cdf0e10cSrcweir         Object[] args = new Object[] { ctxt };
272cdf0e10cSrcweir         try {
273cdf0e10cSrcweir             Object result = inv.invoke("Creatable", args,
274cdf0e10cSrcweir                 new short[1][0], new Object[1][0]);
275cdf0e10cSrcweir 
276cdf0e10cSrcweir             if (result != null)
277cdf0e10cSrcweir             {
278cdf0e10cSrcweir                 XBrowseNode xbn = (XBrowseNode)
279cdf0e10cSrcweir                     AnyConverter.toObject(new Type(XBrowseNode.class), result);
280cdf0e10cSrcweir                 selectorPanel.addNode(node, xbn);
281cdf0e10cSrcweir             }
282cdf0e10cSrcweir         }
283cdf0e10cSrcweir         catch (Exception e) {
284cdf0e10cSrcweir             e.printStackTrace();
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir     }
287cdf0e10cSrcweir 
showEditor( XScriptContext ctxt, DefaultMutableTreeNode node)288cdf0e10cSrcweir     private void showEditor(
289cdf0e10cSrcweir         XScriptContext ctxt, DefaultMutableTreeNode node)
290cdf0e10cSrcweir     {
291cdf0e10cSrcweir         Object obj = node.getUserObject();
292cdf0e10cSrcweir         XInvocation inv =
293cdf0e10cSrcweir             (XInvocation)UnoRuntime.queryInterface(
294cdf0e10cSrcweir             XInvocation.class, obj);
295cdf0e10cSrcweir         Object[] args = new Object[] { ctxt };
296cdf0e10cSrcweir         try {
297cdf0e10cSrcweir             inv.invoke("Editable", args,
298cdf0e10cSrcweir                 new short[1][0], new Object[1][0]);
299cdf0e10cSrcweir         }
300cdf0e10cSrcweir         catch (Exception e) {
301cdf0e10cSrcweir             e.printStackTrace();
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir     }
304cdf0e10cSrcweir 
checkEnabled(XPropertySet props, String name, JButton button)305cdf0e10cSrcweir     private void checkEnabled(XPropertySet props, String name,
306cdf0e10cSrcweir         JButton button)
307cdf0e10cSrcweir     {
308cdf0e10cSrcweir         boolean enable = false;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir         try
311cdf0e10cSrcweir         {
312cdf0e10cSrcweir             if (props != null)
313cdf0e10cSrcweir             {
314cdf0e10cSrcweir                 Object o = props.getPropertyValue(name);
315cdf0e10cSrcweir                 enable = AnyConverter.toBoolean(
316cdf0e10cSrcweir                     props.getPropertyValue(name));
317cdf0e10cSrcweir             }
318cdf0e10cSrcweir         }
319cdf0e10cSrcweir         catch (com.sun.star.lang.IllegalArgumentException iae)
320cdf0e10cSrcweir         {
321cdf0e10cSrcweir             // leave enable set to false
322cdf0e10cSrcweir         }
323cdf0e10cSrcweir         catch (com.sun.star.beans.UnknownPropertyException upe)
324cdf0e10cSrcweir         {
325cdf0e10cSrcweir             // leave enable set to false
326cdf0e10cSrcweir         }
327cdf0e10cSrcweir         catch (com.sun.star.lang.WrappedTargetException wte)
328cdf0e10cSrcweir         {
329cdf0e10cSrcweir             // leave enable set to false
330cdf0e10cSrcweir         }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         button.setEnabled(enable);
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir 
getRootNode(XScriptContext ctxt)335cdf0e10cSrcweir     private XBrowseNode getRootNode(XScriptContext ctxt) {
336cdf0e10cSrcweir 
337cdf0e10cSrcweir         XBrowseNode result = null;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 
340cdf0e10cSrcweir         XComponentContext xcc = ctxt.getComponentContext();
341cdf0e10cSrcweir         XMultiComponentFactory xmcf = xcc.getServiceManager();
342cdf0e10cSrcweir         XBrowseNodeFactory xBrowseFac = (XBrowseNodeFactory)
343cdf0e10cSrcweir             UnoRuntime.queryInterface( XBrowseNodeFactory.class, xcc.getValueByName(
344cdf0e10cSrcweir                 "/singletons/com.sun.star.script.browse.theBrowseNodeFactory") );
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 
347cdf0e10cSrcweir         result = (XBrowseNode)UnoRuntime.queryInterface(
348cdf0e10cSrcweir            XBrowseNode.class, xBrowseFac.createView( BrowseNodeFactoryViewTypes.MACROORGANIZER ) );
349cdf0e10cSrcweir         return result;
350cdf0e10cSrcweir     }
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir class ScriptSelectorPanel extends JPanel {
354cdf0e10cSrcweir 
355cdf0e10cSrcweir     private XBrowseNode myrootnode = null;
356cdf0e10cSrcweir     public JTextField textField;
357cdf0e10cSrcweir     public JTree tree;
358cdf0e10cSrcweir     public DefaultTreeModel treeModel;
359cdf0e10cSrcweir 
ScriptSelectorPanel(XBrowseNode root)360cdf0e10cSrcweir     public ScriptSelectorPanel(XBrowseNode root)
361cdf0e10cSrcweir     {
362cdf0e10cSrcweir         this.myrootnode = root;
363cdf0e10cSrcweir         initUI();
364cdf0e10cSrcweir     }
365cdf0e10cSrcweir 
getSelection()366cdf0e10cSrcweir     public XBrowseNode getSelection() {
367cdf0e10cSrcweir         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
368cdf0e10cSrcweir             tree.getLastSelectedPathComponent();
369cdf0e10cSrcweir 
370cdf0e10cSrcweir         if (node == null) {
371cdf0e10cSrcweir             return null;
372cdf0e10cSrcweir         }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir         return (XBrowseNode)node.getUserObject();
375cdf0e10cSrcweir     }
376cdf0e10cSrcweir 
initUI()377cdf0e10cSrcweir     private void initUI() {
378cdf0e10cSrcweir         setLayout(new BorderLayout());
379cdf0e10cSrcweir 
380cdf0e10cSrcweir         DefaultMutableTreeNode top =
381cdf0e10cSrcweir             new DefaultMutableTreeNode(myrootnode) {
382cdf0e10cSrcweir                 public String toString() {
383cdf0e10cSrcweir                     return ((XBrowseNode)getUserObject()).getName();
384cdf0e10cSrcweir                 }
385cdf0e10cSrcweir             };
386cdf0e10cSrcweir         initNodes(myrootnode, top);
387cdf0e10cSrcweir         treeModel = new DefaultTreeModel(top);
388cdf0e10cSrcweir         tree = new JTree(treeModel);
389cdf0e10cSrcweir 
390cdf0e10cSrcweir         tree.setCellRenderer(new ScriptTreeRenderer());
391cdf0e10cSrcweir 
392cdf0e10cSrcweir         tree.getSelectionModel().setSelectionMode
393cdf0e10cSrcweir             (TreeSelectionModel.SINGLE_TREE_SELECTION);
394cdf0e10cSrcweir 
395cdf0e10cSrcweir         tree.addTreeSelectionListener(new TreeSelectionListener() {
396cdf0e10cSrcweir             public void valueChanged(TreeSelectionEvent e) {
397cdf0e10cSrcweir                 XBrowseNode xbn = getSelection();
398cdf0e10cSrcweir                 XPropertySet props = (XPropertySet)UnoRuntime.queryInterface(
399cdf0e10cSrcweir                     XPropertySet.class, xbn);
400cdf0e10cSrcweir 
401cdf0e10cSrcweir                 if (xbn == null) {
402cdf0e10cSrcweir                     textField.setText("");
403cdf0e10cSrcweir                     return;
404cdf0e10cSrcweir                 }
405cdf0e10cSrcweir 
406cdf0e10cSrcweir                 String str = xbn.getName();
407cdf0e10cSrcweir                 if (xbn.getType() == BrowseNodeTypes.SCRIPT && props != null)
408cdf0e10cSrcweir                 {
409cdf0e10cSrcweir                     try {
410cdf0e10cSrcweir                         str = AnyConverter.toString(
411cdf0e10cSrcweir                             props.getPropertyValue("URI"));
412cdf0e10cSrcweir                     }
413cdf0e10cSrcweir                     catch (Exception ignore) {
414cdf0e10cSrcweir                         // default will be used
415cdf0e10cSrcweir                     }
416cdf0e10cSrcweir                 }
417cdf0e10cSrcweir                 textField.setText(str);
418cdf0e10cSrcweir             }
419cdf0e10cSrcweir         });
420cdf0e10cSrcweir 
421cdf0e10cSrcweir         JScrollPane scroller = new JScrollPane(tree);
422cdf0e10cSrcweir         add(scroller, BorderLayout.CENTER);
423cdf0e10cSrcweir 
424cdf0e10cSrcweir         textField = new JTextField();
425cdf0e10cSrcweir         add(textField, BorderLayout.SOUTH);
426cdf0e10cSrcweir     }
427cdf0e10cSrcweir 
removeNode(DefaultMutableTreeNode node)428cdf0e10cSrcweir     public void removeNode(DefaultMutableTreeNode node) {
429cdf0e10cSrcweir         MutableTreeNode parent = (MutableTreeNode)(node.getParent());
430cdf0e10cSrcweir         if (parent != null) {
431cdf0e10cSrcweir             treeModel.removeNodeFromParent(node);
432cdf0e10cSrcweir         }
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir 
addNode(DefaultMutableTreeNode parent, XBrowseNode xbn)435cdf0e10cSrcweir     public void addNode(DefaultMutableTreeNode parent, XBrowseNode xbn) {
436cdf0e10cSrcweir         DefaultMutableTreeNode newNode =
437cdf0e10cSrcweir             new DefaultMutableTreeNode(xbn) {
438cdf0e10cSrcweir                 public String toString() {
439cdf0e10cSrcweir                     return ((XBrowseNode)getUserObject()).getName();
440cdf0e10cSrcweir                 }
441cdf0e10cSrcweir             };
442cdf0e10cSrcweir 
443cdf0e10cSrcweir         treeModel.insertNodeInto(newNode, parent, parent.getChildCount());
444cdf0e10cSrcweir         tree.scrollPathToVisible(new TreePath(newNode.getPath()));
445cdf0e10cSrcweir     }
446cdf0e10cSrcweir 
initNodes(XBrowseNode parent, DefaultMutableTreeNode top)447cdf0e10cSrcweir     private void initNodes(XBrowseNode parent, DefaultMutableTreeNode top) {
448cdf0e10cSrcweir         if ( parent == null || parent.hasChildNodes() == false )
449cdf0e10cSrcweir         {
450cdf0e10cSrcweir             return;
451cdf0e10cSrcweir         }
452cdf0e10cSrcweir 
453cdf0e10cSrcweir         XBrowseNode[] children = parent.getChildNodes();
454cdf0e10cSrcweir 
455cdf0e10cSrcweir         try {
456cdf0e10cSrcweir             if (children != null) {
457cdf0e10cSrcweir                 for (int i = 0; i < children.length; i++) {
458cdf0e10cSrcweir                     if ( children[i] == null )
459cdf0e10cSrcweir                     {
460cdf0e10cSrcweir                         continue;
461cdf0e10cSrcweir                     }
462cdf0e10cSrcweir                     DefaultMutableTreeNode newNode =
463cdf0e10cSrcweir                         new DefaultMutableTreeNode(children[i]) {
464cdf0e10cSrcweir                             public String toString() {
465cdf0e10cSrcweir                                 return ((XBrowseNode)getUserObject()).getName();
466cdf0e10cSrcweir                             }
467cdf0e10cSrcweir                         };
468cdf0e10cSrcweir                     top.add(newNode);
469cdf0e10cSrcweir                     initNodes(children[i], newNode);
470cdf0e10cSrcweir                 }
471cdf0e10cSrcweir             }
472cdf0e10cSrcweir         }
473cdf0e10cSrcweir         catch (java.lang.Exception e) {
474cdf0e10cSrcweir             e.printStackTrace();
475cdf0e10cSrcweir         }
476cdf0e10cSrcweir     }
477cdf0e10cSrcweir }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir class ScriptTreeRenderer extends DefaultTreeCellRenderer {
480cdf0e10cSrcweir 
481cdf0e10cSrcweir     private ImageIcon sofficeIcon;
482cdf0e10cSrcweir     private ImageIcon scriptIcon;
483cdf0e10cSrcweir     private ImageIcon containerIcon;
484cdf0e10cSrcweir 
ScriptTreeRenderer()485cdf0e10cSrcweir     public ScriptTreeRenderer() {
486cdf0e10cSrcweir         sofficeIcon = new ImageIcon(getClass().getResource("soffice.gif"));
487cdf0e10cSrcweir         scriptIcon = new ImageIcon(getClass().getResource("script.gif"));
488cdf0e10cSrcweir         containerIcon = new ImageIcon(getClass().getResource("container.gif"));
489cdf0e10cSrcweir     }
490cdf0e10cSrcweir 
getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)491cdf0e10cSrcweir     public Component getTreeCellRendererComponent(
492cdf0e10cSrcweir                         JTree tree,
493cdf0e10cSrcweir                         Object value,
494cdf0e10cSrcweir                         boolean sel,
495cdf0e10cSrcweir                         boolean expanded,
496cdf0e10cSrcweir                         boolean leaf,
497cdf0e10cSrcweir                         int row,
498cdf0e10cSrcweir                         boolean hasFocus) {
499cdf0e10cSrcweir 
500cdf0e10cSrcweir         super.getTreeCellRendererComponent(
501cdf0e10cSrcweir                         tree, value, sel,
502cdf0e10cSrcweir                         expanded, leaf, row,
503cdf0e10cSrcweir                         hasFocus);
504cdf0e10cSrcweir 
505cdf0e10cSrcweir         DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
506cdf0e10cSrcweir         XBrowseNode xbn = (XBrowseNode)node.getUserObject();
507cdf0e10cSrcweir         if (xbn.getType() == BrowseNodeTypes.SCRIPT) {
508cdf0e10cSrcweir             setIcon(scriptIcon);
509cdf0e10cSrcweir         }
510cdf0e10cSrcweir         else if(xbn.getType() == BrowseNodeTypes.CONTAINER) {
511cdf0e10cSrcweir             setIcon(containerIcon);
512cdf0e10cSrcweir         }
513cdf0e10cSrcweir         else if(xbn.getType() == BrowseNodeTypes.ROOT) {
514cdf0e10cSrcweir             setIcon(sofficeIcon);
515cdf0e10cSrcweir         }
516cdf0e10cSrcweir 
517cdf0e10cSrcweir         return this;
518cdf0e10cSrcweir     }
519cdf0e10cSrcweir }
520