2 import javax
.swing
.tree
.*;
3 import javax
.swing
.table
.*;
4 import javax
.swing
.event
.*;
5 import javax
.swing
.border
.*;
7 import java
.awt
.event
.*;
11 import com
.sun
.star
.uno
.UnoRuntime
;
12 import com
.sun
.star
.uno
.Exception
;
13 import com
.sun
.star
.uno
.Any
;
14 import com
.sun
.star
.uno
.AnyConverter
;
15 import com
.sun
.star
.uno
.Type
;
16 import com
.sun
.star
.uno
.XComponentContext
;
18 import com
.sun
.star
.lang
.XMultiComponentFactory
;
19 import com
.sun
.star
.lang
.XComponent
;
20 import com
.sun
.star
.frame
.XModel
;
21 import com
.sun
.star
.frame
.FrameSearchFlag
;
22 import com
.sun
.star
.frame
.XDispatchProvider
;
23 import com
.sun
.star
.frame
.XDispatchHelper
;
24 import com
.sun
.star
.frame
.XDispatch
;
25 import com
.sun
.star
.util
.XURLTransformer
;
26 import com
.sun
.star
.beans
.*;
27 import com
.sun
.star
.script
.XInvocation
;
29 import com
.sun
.star
.lib
.uno
.helper
.PropertySet
;
31 import com
.sun
.star
.script
.browse
.XBrowseNode
;
32 import com
.sun
.star
.script
.browse
.BrowseNodeTypes
;
33 import com
.sun
.star
.script
.browse
.XBrowseNodeFactory
;
34 import com
.sun
.star
.script
.browse
.BrowseNodeFactoryViewTypes
;
35 import com
.sun
.star
.script
.provider
.XScriptContext
;
36 import com
.sun
.star
.script
.provider
.XScript
;
37 import com
.sun
.star
.script
.provider
.XScriptProvider
;
39 public class ScriptSelector
{
41 private static final int BIG_GAP
= 10;
42 private static final int MED_GAP
= 5;
44 private ScriptSelectorPanel selectorPanel
;
46 public ScriptSelector()
50 public void showOrganizer(final XScriptContext ctxt
)
53 XBrowseNode root
= getRootNode(ctxt
);
55 final XScriptProvider msp
=
56 (XScriptProvider
)UnoRuntime
.queryInterface(
57 XScriptProvider
.class, root
);
59 final JFrame client
= new JFrame("Script");
61 selectorPanel
= new ScriptSelectorPanel(root
);
63 final JButton runButton
, closeButton
, createButton
,
64 editButton
, deleteButton
;
66 runButton
= new JButton("Run");
67 runButton
.setEnabled(false);
69 closeButton
= new JButton("Close");
71 editButton
= new JButton("Edit");
72 editButton
.setEnabled(false);
75 new JPanel(new GridLayout(2, 1, MED_GAP
, MED_GAP
));
77 northButtons
.add(runButton
);
78 northButtons
.add(closeButton
);
80 createButton
= new JButton("Create");
81 createButton
.setEnabled(false);
83 deleteButton
= new JButton("Delete");
84 deleteButton
.setEnabled(false);
87 new JPanel(new GridLayout(3, 1, MED_GAP
, MED_GAP
));
89 southButtons
.add(editButton
);
90 southButtons
.add(createButton
);
91 southButtons
.add(deleteButton
);
93 selectorPanel
.tree
.addTreeSelectionListener(
94 new TreeSelectionListener() {
95 public void valueChanged(TreeSelectionEvent e
) {
96 XBrowseNode xbn
= selectorPanel
.getSelection();
97 XPropertySet props
= (XPropertySet
)
98 UnoRuntime
.queryInterface(XPropertySet
.class, xbn
);
100 checkEnabled(props
, "Creatable", createButton
);
101 checkEnabled(props
, "Deletable", deleteButton
);
102 checkEnabled(props
, "Editable", editButton
);
105 xbn
.getType() == BrowseNodeTypes
.SCRIPT
)
107 runButton
.setEnabled(true);
111 runButton
.setEnabled(false);
117 ActionListener listener
= new ActionListener() {
118 public void actionPerformed(ActionEvent event
) {
119 if (event
.getSource() == runButton
) {
120 String uri
= selectorPanel
.textField
.getText();
123 XScript script
= msp
.getScript(uri
);
125 Object
[][] out
= new Object
[1][0];
126 out
[0] = new Object
[0];
128 short[][] num
= new short[1][0];
129 num
[0] = new short[0];
131 script
.invoke(new Object
[0], num
, out
);
133 catch (Exception e
) {
137 else if (event
.getSource() == closeButton
) {
140 else if (event
.getSource() == editButton
) {
141 DefaultMutableTreeNode node
=
142 (DefaultMutableTreeNode
)
143 selectorPanel
.tree
.getLastSelectedPathComponent();
145 if (node
== null) return;
147 showEditor(ctxt
, node
);
149 else if (event
.getSource() == createButton
) {
150 DefaultMutableTreeNode node
=
151 (DefaultMutableTreeNode
)
152 selectorPanel
.tree
.getLastSelectedPathComponent();
154 if (node
== null) return;
156 doCreate(ctxt
, node
);
158 else if (event
.getSource() == deleteButton
) {
159 DefaultMutableTreeNode node
=
160 (DefaultMutableTreeNode
)
161 selectorPanel
.tree
.getLastSelectedPathComponent();
163 if (node
== null) return;
165 doDelete(ctxt
, node
);
170 runButton
.addActionListener(listener
);
171 closeButton
.addActionListener(listener
);
172 createButton
.addActionListener(listener
);
173 editButton
.addActionListener(listener
);
174 deleteButton
.addActionListener(listener
);
176 JPanel buttonPanel
= new JPanel(new BorderLayout());
177 buttonPanel
.add(northButtons
, BorderLayout
.NORTH
);
178 buttonPanel
.add(southButtons
, BorderLayout
.SOUTH
);
180 JPanel mainPanel
= new JPanel(new BorderLayout(MED_GAP
, MED_GAP
));
182 new EmptyBorder(BIG_GAP
, BIG_GAP
, BIG_GAP
, BIG_GAP
));
183 mainPanel
.add(selectorPanel
, BorderLayout
.CENTER
);
184 mainPanel
.add(buttonPanel
, BorderLayout
.EAST
);
186 client
.getContentPane().add(mainPanel
);
187 client
.setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
188 client
.setSize(500, 350);
190 // set the x and y locations so that the frame is in the
191 // centre of the screen
192 Dimension d
= client
.getToolkit().getScreenSize();
194 int x
= (int)((d
.getWidth() - client
.getWidth()) / 2);
195 int y
= (int)((d
.getHeight() - client
.getHeight()) / 2);
197 client
.setLocation(x
, y
);
201 catch (com
.sun
.star
.uno
.RuntimeException rue
) {
202 rue
.printStackTrace();
204 catch (java
.lang
.Exception e
) {
209 public void showOrganizer(final XScriptContext ctxt
,
210 final com
.sun
.star
.awt
.MouseEvent e
)
215 public void showOrganizer(final XScriptContext ctxt
,
216 final com
.sun
.star
.awt
.ActionEvent e
)
221 private void doDelete(
222 XScriptContext ctxt
, DefaultMutableTreeNode node
)
224 Object obj
= node
.getUserObject();
226 (XInvocation
)UnoRuntime
.queryInterface(
227 XInvocation
.class, obj
);
228 Object
[] args
= new Object
[] { ctxt
};
230 Object result
= inv
.invoke("Deletable", args
,
231 new short[1][0], new Object
[1][0]);
233 if (result
!= null && AnyConverter
.toBoolean(result
) == true)
235 selectorPanel
.removeNode(node
);
238 catch (Exception e
) {
243 private void doCreate(
244 XScriptContext ctxt
, DefaultMutableTreeNode node
)
246 Object obj
= node
.getUserObject();
248 (XInvocation
)UnoRuntime
.queryInterface(
249 XInvocation
.class, obj
);
250 Object
[] args
= new Object
[] { ctxt
};
252 Object result
= inv
.invoke("Creatable", args
,
253 new short[1][0], new Object
[1][0]);
257 XBrowseNode xbn
= (XBrowseNode
)
258 AnyConverter
.toObject(new Type(XBrowseNode
.class), result
);
259 selectorPanel
.addNode(node
, xbn
);
262 catch (Exception e
) {
267 private void showEditor(
268 XScriptContext ctxt
, DefaultMutableTreeNode node
)
270 Object obj
= node
.getUserObject();
272 (XInvocation
)UnoRuntime
.queryInterface(
273 XInvocation
.class, obj
);
274 Object
[] args
= new Object
[] { ctxt
};
276 inv
.invoke("Editable", args
,
277 new short[1][0], new Object
[1][0]);
279 catch (Exception e
) {
284 private void checkEnabled(XPropertySet props
, String name
,
287 boolean enable
= false;
293 Object o
= props
.getPropertyValue(name
);
294 enable
= AnyConverter
.toBoolean(
295 props
.getPropertyValue(name
));
298 catch (com
.sun
.star
.lang
.IllegalArgumentException iae
)
300 // leave enable set to false
302 catch (com
.sun
.star
.beans
.UnknownPropertyException upe
)
304 // leave enable set to false
306 catch (com
.sun
.star
.lang
.WrappedTargetException wte
)
308 // leave enable set to false
311 button
.setEnabled(enable
);
314 private XBrowseNode
getRootNode(XScriptContext ctxt
) {
316 XBrowseNode result
= null;
319 XComponentContext xcc
= ctxt
.getComponentContext();
320 XMultiComponentFactory xmcf
= xcc
.getServiceManager();
321 XBrowseNodeFactory xBrowseFac
= (XBrowseNodeFactory
)
322 UnoRuntime
.queryInterface( XBrowseNodeFactory
.class, xcc
.getValueByName(
323 "/singletons/com.sun.star.script.browse.theBrowseNodeFactory") );
326 result
= (XBrowseNode
)UnoRuntime
.queryInterface(
327 XBrowseNode
.class, xBrowseFac
.createView( BrowseNodeFactoryViewTypes
.MACROORGANIZER
) );
332 class ScriptSelectorPanel
extends JPanel
{
334 private XBrowseNode myrootnode
= null;
335 public JTextField textField
;
337 public DefaultTreeModel treeModel
;
339 public ScriptSelectorPanel(XBrowseNode root
)
341 this.myrootnode
= root
;
345 public XBrowseNode
getSelection() {
346 DefaultMutableTreeNode node
= (DefaultMutableTreeNode
)
347 tree
.getLastSelectedPathComponent();
353 return (XBrowseNode
)node
.getUserObject();
356 private void initUI() {
357 setLayout(new BorderLayout());
359 DefaultMutableTreeNode top
=
360 new DefaultMutableTreeNode(myrootnode
) {
361 public String
toString() {
362 return ((XBrowseNode
)getUserObject()).getName();
365 initNodes(myrootnode
, top
);
366 treeModel
= new DefaultTreeModel(top
);
367 tree
= new JTree(treeModel
);
369 tree
.setCellRenderer(new ScriptTreeRenderer());
371 tree
.getSelectionModel().setSelectionMode
372 (TreeSelectionModel
.SINGLE_TREE_SELECTION
);
374 tree
.addTreeSelectionListener(new TreeSelectionListener() {
375 public void valueChanged(TreeSelectionEvent e
) {
376 XBrowseNode xbn
= getSelection();
377 XPropertySet props
= (XPropertySet
)UnoRuntime
.queryInterface(
378 XPropertySet
.class, xbn
);
381 textField
.setText("");
385 String str
= xbn
.getName();
386 if (xbn
.getType() == BrowseNodeTypes
.SCRIPT
&& props
!= null)
389 str
= AnyConverter
.toString(
390 props
.getPropertyValue("URI"));
392 catch (Exception ignore
) {
393 // default will be used
396 textField
.setText(str
);
400 JScrollPane scroller
= new JScrollPane(tree
);
401 add(scroller
, BorderLayout
.CENTER
);
403 textField
= new JTextField();
404 add(textField
, BorderLayout
.SOUTH
);
407 public void removeNode(DefaultMutableTreeNode node
) {
408 MutableTreeNode parent
= (MutableTreeNode
)(node
.getParent());
409 if (parent
!= null) {
410 treeModel
.removeNodeFromParent(node
);
414 public void addNode(DefaultMutableTreeNode parent
, XBrowseNode xbn
) {
415 DefaultMutableTreeNode newNode
=
416 new DefaultMutableTreeNode(xbn
) {
417 public String
toString() {
418 return ((XBrowseNode
)getUserObject()).getName();
422 treeModel
.insertNodeInto(newNode
, parent
, parent
.getChildCount());
423 tree
.scrollPathToVisible(new TreePath(newNode
.getPath()));
426 private void initNodes(XBrowseNode parent
, DefaultMutableTreeNode top
) {
427 if ( parent
== null || parent
.hasChildNodes() == false )
432 XBrowseNode
[] children
= parent
.getChildNodes();
435 if (children
!= null) {
436 for (int i
= 0; i
< children
.length
; i
++) {
437 if ( children
[i
] == null )
441 DefaultMutableTreeNode newNode
=
442 new DefaultMutableTreeNode(children
[i
]) {
443 public String
toString() {
444 return ((XBrowseNode
)getUserObject()).getName();
448 initNodes(children
[i
], newNode
);
452 catch (java
.lang
.Exception e
) {
458 class ScriptTreeRenderer
extends DefaultTreeCellRenderer
{
460 private ImageIcon sofficeIcon
;
461 private ImageIcon scriptIcon
;
462 private ImageIcon containerIcon
;
464 public ScriptTreeRenderer() {
465 sofficeIcon
= new ImageIcon(getClass().getResource("soffice.gif"));
466 scriptIcon
= new ImageIcon(getClass().getResource("script.gif"));
467 containerIcon
= new ImageIcon(getClass().getResource("container.gif"));
470 public Component
getTreeCellRendererComponent(
479 super.getTreeCellRendererComponent(
484 DefaultMutableTreeNode node
= (DefaultMutableTreeNode
)value
;
485 XBrowseNode xbn
= (XBrowseNode
)node
.getUserObject();
486 if (xbn
.getType() == BrowseNodeTypes
.SCRIPT
) {
489 else if(xbn
.getType() == BrowseNodeTypes
.CONTAINER
) {
490 setIcon(containerIcon
);
492 else if(xbn
.getType() == BrowseNodeTypes
.ROOT
) {
493 setIcon(sofficeIcon
);