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