List of usage examples for javax.swing.tree DefaultMutableTreeNode getFirstLeaf
public DefaultMutableTreeNode getFirstLeaf()
From source file:uk.co.markfrimston.tasktree.Main.java
public Main(TaskTree taskTree) { super();//from w w w . j a v a2 s . co m this.taskTree = taskTree; this.setTitle("Task Tree"); this.setSize(new Dimension(300, 500)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel quickInPanel = new JPanel(new BorderLayout()); this.quickIn = new JTextArea(); this.quickIn.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent arg0) { if (arg0.getKeyCode() == KeyEvent.VK_ENTER) { String newText = quickIn.getText().trim(); if (newText != null && newText.length() > 0) { addTask(Main.this.taskTree.getRoot(), 0, newText, true); try { Main.this.taskTree.changesMade(); } catch (Exception e) { error(e.getMessage()); } } quickIn.setText(""); } } }); this.quickIn.setPreferredSize(new Dimension(300, 75)); this.quickIn.setBorder(BorderFactory.createTitledBorder("Quick Input")); quickInPanel.add(this.quickIn, BorderLayout.CENTER); this.syncButton = new JButton("Sync"); this.syncButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new SyncThread(Main.this).start(); } }); quickInPanel.add(this.syncButton, BorderLayout.EAST); this.getContentPane().add(quickInPanel, BorderLayout.NORTH); this.tree = new JTree(taskTree.getTreeModel()); DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object newVal = htmlFilter(String.valueOf(node.getUserObject())); if (node.getChildCount() > 0 && !tree.isExpanded(new TreePath(node.getPath()))) { DefaultMutableTreeNode firstLeaf = (DefaultMutableTreeNode) node.getFirstLeaf(); newVal = htmlFilter(String.valueOf(node.getUserObject())) + " <span style='color:silver;font-style:italic'>" + "(" + String.valueOf(firstLeaf.getUserObject()) + ")</span>"; } newVal = "<html>" + newVal + "</html>"; return super.getTreeCellRendererComponent(tree, newVal, selected, expanded, leaf, row, hasFocus); } }; ImageIcon bulletIcon = new ImageIcon(Main.class.getResource("bullet.gif")); renderer.setLeafIcon(bulletIcon); renderer.setOpenIcon(bulletIcon); renderer.setClosedIcon(bulletIcon); renderer.setBorder(BorderFactory.createEmptyBorder(4, 0, 4, 0)); this.tree.setCellRenderer(renderer); this.tree.setRootVisible(false); this.tree.setShowsRootHandles(true); this.tree.addMouseListener(new MouseAdapter() { protected void doSelectRow(MouseEvent arg0) { int row = tree.getRowForLocation(arg0.getX(), arg0.getY()); if (row != -1) { tree.setSelectionRow(row); if (arg0.isPopupTrigger()) { popup.show(tree, arg0.getX(), arg0.getY()); } } } public void mousePressed(MouseEvent arg0) { doSelectRow(arg0); } public void mouseReleased(MouseEvent arg0) { doSelectRow(arg0); } }); JScrollPane treeScroll = new JScrollPane(tree); treeScroll.setBorder(BorderFactory.createTitledBorder("Task List")); this.getContentPane().add(treeScroll, BorderLayout.CENTER); this.popup = new JPopupMenu(); JMenuItem addBefore = new JMenuItem("Add Before"); addBefore.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent(); int pos = parent.getIndex(selected); promptAndInsert(parent, pos); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(addBefore); JMenuItem addAfter = new JMenuItem("Add After"); addAfter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent(); int pos = parent.getIndex(selected) + 1; promptAndInsert(parent, pos); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(addAfter); JMenuItem addNested = new JMenuItem("Add Nested"); addNested.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); int pos = selected.getChildCount(); promptAndInsert(selected, pos); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { ex.getMessage(); } } }); this.popup.add(addNested); this.popup.add(new JSeparator()); JMenuItem moveTop = new JMenuItem("Move to Top"); moveTop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent(); moveTask(selected, parent, 0); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(moveTop); JMenuItem moveUp = new JMenuItem("Move Up"); moveUp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent(); int pos = Math.max(parent.getIndex(selected) - 1, 0); moveTask(selected, parent, pos); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(moveUp); JMenuItem moveDown = new JMenuItem("Move Down"); moveDown.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent(); int pos = Math.min(parent.getIndex(selected) + 1, parent.getChildCount() - 1); moveTask(selected, parent, pos); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(moveDown); JMenuItem moveBottom = new JMenuItem("Move to Bottom"); moveBottom.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selected.getParent(); moveTask(selected, parent, parent.getChildCount() - 1); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(moveBottom); this.popup.add(new JSeparator()); JMenuItem rename = new JMenuItem("Edit"); rename.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selected = getSelectedNode(); String newText = prompt((String) selected.getUserObject()); if (newText != null && newText.length() > 0) { selected.setUserObject(newText); Main.this.taskTree.getTreeModel().reload(selected); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } } }); this.popup.add(rename); JMenuItem delete = new JMenuItem("Delete"); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { promptAndRemove(getSelectedNode()); try { Main.this.taskTree.changesMade(); } catch (Exception ex) { error(ex.getMessage()); } } }); this.popup.add(delete); this.setVisible(true); loadConfig(); load(); syncButton.setVisible(this.taskTree.hasSyncCapability()); }
From source file:ai.aitia.meme.paramsweep.intellisweepPlugin.JgapGAPlugin.java
public void setRecordableVariables(final DefaultMutableTreeNode root) { final List<RecordableInfo> newList = new ArrayList<RecordableInfo>(); if (root.getChildCount() > 0) { final DefaultMutableTreeNode recorder = (DefaultMutableTreeNode) root.getChildAt(0); final ResultInfo resultInfo = (ResultInfo) recorder.getFirstLeaf().getUserObject(); workspace = new File(resultInfo.getFile()).getParentFile(); //first two children contains recorder meta data for (int j = 2; j < recorder.getChildCount(); ++j) { final RecordableElement re = (RecordableElement) ((DefaultMutableTreeNode) recorder.getChildAt(j)) .getUserObject();//from w ww . j a v a 2 s . c o m final RecordableInfo recInfo = new RecordableInfo( re.getAlias() != null ? re.getAlias() : re.getInfo().getName(), re.getInfo().getJavaType(), re.getInfo().getName()); if (!newList.contains(recInfo) && re.getInfo().isNumeric()) { newList.add(recInfo); } } } final RecordableInfo oldSelectedFunction = selectedFunction; removeAllFitnessFunctions(); fitnessFunctions.addAll(newList); if (listeners != null) { for (final ModelListener listener : listeners) { listener.fitnessFunctionAdded(); } } if (oldSelectedFunction != null) { RecordableInfo selectedFunctionInTheList = null; for (final RecordableInfo ri : fitnessFunctions) { if (ri.equals(oldSelectedFunction)) { // accessible name equality selectedFunctionInTheList = ri; } } if (selectedFunctionInTheList != null) { selectedFunction = selectedFunctionInTheList; if (listeners != null) { for (final ModelListener listener : listeners) { listener.fitnessFunctionSelected(selectedFunction); } } } } }
From source file:pt.ua.dicoogle.rGUI.client.windows.MainWindow.java
public void updateP2PThumbnail(SearchResult result) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) getjTreeResults().getLastSelectedPathComponent(); if (node == null) { return;/*from w ww . j av a 2s . com*/ } Object nodeInfo = null; DefaultMutableTreeNode nodeLeaf = null; if (node.getLevel() == 4 || (node.isLeaf() && node.getLevel() > 1)) { Object nodeInfoLeaf = null; if (node.getLevel() == 4) { nodeLeaf = node.getFirstLeaf(); nodeInfoLeaf = nodeLeaf.getUserObject(); } else { // Leaf nodeInfo = node.getUserObject(); nodeLeaf = node; nodeInfoLeaf = nodeInfo; } //SearchResult r = (SearchResult) nodeInfoLeaf; if (nodeInfoLeaf == result) { showThumbnail(result.getExtrafields().get("Thumbnail")); } } }
From source file:pt.ua.dicoogle.rGUI.client.windows.MainWindow.java
private void jTreeResultsValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeResultsValueChanged DefaultMutableTreeNode node = (DefaultMutableTreeNode) getjTreeResults().getLastSelectedPathComponent(); if (node == null) { return;//from w ww .jav a 2s . co m } this.jButtonDownload.setEnabled(false); Object nodeInfo = null; DefaultMutableTreeNode nodeLeaf = null; if (node.getLevel() == 4 || (node.isLeaf() && node.getLevel() > 1)) { Object nodeInfoLeaf = null; if (node.getLevel() == 4) { nodeLeaf = node.getFirstLeaf(); nodeInfoLeaf = nodeLeaf.getUserObject(); } else { // Leaf nodeInfo = node.getUserObject(); nodeLeaf = node; nodeInfoLeaf = nodeInfo; } SearchResult r = (SearchResult) nodeInfoLeaf; Hashtable extras = r.getExtrafields(); String thumb = (String) extras.get("Thumbnail"); //System.out.println("Filename: " + r.getFileName()); //System.out.println("FileHash: " + r.getFileHash()); if (thumb != null) { showThumbnail(thumb); } else if (!SearchResult.class.isInstance(nodeInfoLeaf) && SearchResult.class.isInstance(nodeInfoLeaf)) { SearchResult res = searchTree.searchThumbnail(r.getFileName(), r.getFileHash()); if (res != null) { Hashtable extras2 = res.getExtrafields(); if (extras2 != null) { thumb = (String) extras2.get("Thumbnail"); if (thumb != null) { extras.put("Thumbnail", thumb); // put the thumbnail in the original SearchResult showThumbnail(thumb); } else { cleanThumbnails(); } } } } else if (SearchResult.class.isInstance(nodeInfoLeaf)) { searchTree.searchP2PThumbnail((SearchResult) r); cleanThumbnails(); } else { cleanThumbnails(); } } else { cleanThumbnails(); } //Controll the enable buttons if (node.isLeaf()) { jButtonDump.setEnabled(true); IPluginControllerUser plugins = null; try { plugins = this.clientCore.getUser().getPluginController(); if ((SearchResult) nodeInfo == null) { } if (!plugins.isLocalPlugin(((SearchResult) nodeInfo).getPluginName())) { jButtonDownload.setEnabled(true); jButtonSend.setEnabled(false); } else { jButtonSend.setEnabled(true); jButtonView.setEnabled(true); } } catch (RemoteException ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); } } else { jButtonDump.setEnabled(false); jButtonDownload.setEnabled(false); jButtonView.setEnabled(false); jButtonSend.setEnabled(true); } }
From source file:streamme.visuals.Main.java
public void addToPlaylist(DefaultMutableTreeNode node, int listIdx, boolean recursive) { DefaultMutableTreeNode leaf = node.getFirstLeaf(); while (node.isNodeDescendant(leaf)) { DataNode dn = (DataNode) leaf.getUserObject(); javax.swing.tree.TreeNode[] tree = leaf.getPath(); String path = StreamMe.OPTIONS.getOutputPath(); for (int i = 1; i < tree.length; i++) { path += "\\" + ((DataNode) ((DefaultMutableTreeNode) tree[i]).getUserObject()).getName(); }//from ww w . j a va 2s .c o m PlaylistManager.get().addToPlaylist(listIdx, dn.toFileLink(path)); leaf = leaf.getNextLeaf(); } }