List of usage examples for javax.swing.tree TreePath TreePath
public TreePath(Object lastPathComponent)
From source file:TreeEditTest.java
/** * Makes the buttons to add a sibling, add a child, and delete a node. */// w w w . j av a 2 s.c om public void makeButtons() { JPanel panel = new JPanel(); JButton addSiblingButton = new JButton("Add Sibling"); addSiblingButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) return; DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent(); if (parent == null) return; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New"); int selectedIndex = parent.getIndex(selectedNode); model.insertNodeInto(newNode, parent, selectedIndex + 1); // now display new node TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addSiblingButton); JButton addChildButton = new JButton("Add Child"); addChildButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) return; DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New"); model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount()); // now display new node TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); } }); panel.add(addChildButton); JButton deleteButton = new JButton("Delete"); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode != null && selectedNode.getParent() != null) model.removeNodeFromParent(selectedNode); } }); panel.add(deleteButton); add(panel, BorderLayout.SOUTH); }
From source file:uk.co.markfrimston.tasktree.Main.java
public Main(TaskTree taskTree) { super();//from w w w . j ava 2 s . c om 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:net.pms.newgui.SelectRenderers.java
/** * Create the GUI and show it.//from w ww .j ava2 s. c om */ public void showDialog() { if (!init) { // Initial call build(); init = true; } SrvTree.validate(); // Refresh setting if modified selectedRenderers = configuration.getSelectedRenderers(); TreePath root = new TreePath(allRenderers); if (selectedRenderers.isEmpty() || (selectedRenderers.size() == 1 && selectedRenderers.get(0) == null)) { checkTreeManager.getSelectionModel().clearSelection(); } else if (selectedRenderers.size() == 1 && selectedRenderers.get(0).equals(allRenderersTreeName)) { checkTreeManager.getSelectionModel().setSelectionPath(root); } else { if (root.getLastPathComponent() instanceof SearchableMutableTreeNode) { SearchableMutableTreeNode rootNode = (SearchableMutableTreeNode) root.getLastPathComponent(); SearchableMutableTreeNode node = null; List<TreePath> selectedRenderersPath = new ArrayList<>(selectedRenderers.size()); for (String selectedRenderer : selectedRenderers) { try { node = rootNode.findInBranch(selectedRenderer, true); } catch (IllegalChildException e) { } if (node != null) { selectedRenderersPath.add(new TreePath(node.getPath())); } } checkTreeManager.getSelectionModel().setSelectionPaths( selectedRenderersPath.toArray(new TreePath[selectedRenderersPath.size()])); } else { LOGGER.error("Illegal node class in SelectRenderers.showDialog(): {}", root.getLastPathComponent().getClass().getSimpleName()); } } int selectRenderers = JOptionPane.showOptionDialog((Component) PMS.get().getFrame(), this, Messages.getString("GeneralTab.5"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); if (selectRenderers == JOptionPane.OK_OPTION) { TreePath[] selected = checkTreeManager.getSelectionModel().getSelectionPaths(); if (selected.length == 0) { if (configuration.setSelectedRenderers("")) { PMS.get().getFrame().setReloadable(true); // notify the user to restart the server } } else if (selected.length == 1 && selected[0].getLastPathComponent() instanceof SearchableMutableTreeNode && ((SearchableMutableTreeNode) selected[0].getLastPathComponent()).getNodeName() .equals(allRenderers.getNodeName())) { if (configuration.setSelectedRenderers(allRenderersTreeName)) { PMS.get().getFrame().setReloadable(true); // notify the user to restart the server } } else { List<String> selectedRenderers = new ArrayList<>(); for (TreePath path : selected) { String rendererName = ""; if (path.getPathComponent(0).equals(allRenderers)) { for (int i = 1; i < path.getPathCount(); i++) { if (path.getPathComponent(i) instanceof SearchableMutableTreeNode) { if (!rendererName.isEmpty()) { rendererName += " "; } rendererName += ((SearchableMutableTreeNode) path.getPathComponent(i)) .getNodeName(); } else { LOGGER.error("Invalid tree node component class {}", path.getPathComponent(i).getClass().getSimpleName()); } } if (!rendererName.isEmpty()) { selectedRenderers.add(rendererName); } } else { LOGGER.warn("Invalid renderer treepath encountered: {}", path.toString()); } } if (configuration.setSelectedRenderers(selectedRenderers)) { PMS.get().getFrame().setReloadable(true); // notify the user to restart the server } } } }
From source file:appletComponentArch.DynamicTree.java
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = rootNode;//from ww w .j a va2s . c o m } //It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); //Make sure the user can see the lovely new node. if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; }
From source file:Main.java
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = rootNode;/*w w w .j av a 2 s.com*/ } treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; }
From source file:com.openbravo.pos.admin.RolesViewTree.java
private void createTree() { //Create the jtree root = new DefaultMutableTreeNode(); uTree = new CheckboxTree(root); root.setUserObject("All Permissions"); uTree.getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.PROPAGATE_PRESERVING_CHECK); uTree.clearSelection();/*from w ww. j a v a2 s . c o m*/ DefaultCheckboxTreeCellRenderer renderer = (DefaultCheckboxTreeCellRenderer) uTree.getCellRenderer(); renderer.setLeafIcon(null); renderer.setClosedIcon(null); renderer.setOpenIcon(null); // set up listeners MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent e) { int selRow = uTree.getRowForLocation(e.getX(), e.getY()); TreePath selPath = uTree.getPathForLocation(e.getX(), e.getY()); if (selPath != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent(); // If using Right to left Language change the way the check tree works if (!uTree.getComponentOrientation().isLeftToRight()) { if (uTree.isPathChecked(new TreePath(node.getPath()))) { uTree.removeCheckingPath(new TreePath(node.getPath())); } else { uTree.addCheckingPath(new TreePath(node.getPath())); } } jPermissionDesc.setText(descriptionMap.get(node)); } } }; uTree.addMouseListener(ml); // when this listener is fired changes state to dirty uTree.addTreeCheckingListener(new TreeCheckingListener() { public void valueChanged(TreeCheckingEvent e) { passedDirty.setDirty(true); } }); try { // Get list of all the permisions in the database // and the list of sections dbPermissions = (List) m_dlAdmin.getAlldbPermissions(); branches = m_dlAdmin.getSectionsList(); } catch (BasicException ex) { Logger.getLogger(RolesViewTree.class.getName()).log(Level.SEVERE, null, ex); } // Create the main branches in the tree for (Object branch : branches) { section = ((StringUtils.substring(branch.toString(), 0, 2)).equals("##")) ? AppLocal.getIntString(StringUtils.right(branch.toString(), branch.toString().length() - 2)) : branch.toString(); root.add(new DefaultMutableTreeNode(section)); } classMap = new HashMap(); descriptionMap = new HashMap(); nodePaths = new HashMap(); // Replace displayname, Section and Description // from the database with the correct details from the permissions locale for (DBPermissionsInfo Perm : dbPermissions) { Perm.setDisplayName(((StringUtils.substring(Perm.getDisplayName(), 0, 2)).equals("##")) ? AppLocal.getIntString( StringUtils.right(Perm.getDisplayName(), Perm.getDisplayName().length() - 2)) : Perm.getDisplayName()); Perm.setSection(((StringUtils.substring(Perm.getSection(), 0, 2)).equals("##")) ? AppLocal.getIntString(StringUtils.right(Perm.getSection(), Perm.getSection().length() - 2)) : Perm.getSection()); Perm.setDescription(((StringUtils.substring(Perm.getDescription(), 0, 2)).equals("##")) ? AppLocal.getIntString( StringUtils.right(Perm.getDescription(), Perm.getDescription().length() - 2)) : Perm.getDescription()); } //put the list into order by display name sort(); // Create the leaf nodes & fill in hashmap's for (DBPermissionsInfo Perm : dbPermissions) { DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(Perm.getDisplayName(), false); if (searchNode(Perm.getSection(), root) != null) { searchNode(Perm.getSection(), root).add(newNode); classMap.put("[All Permissions, " + Perm.getSection() + ", " + newNode + "]", Perm.getClassName()); descriptionMap.put(newNode, Perm.getDescription()); nodePaths.put(Perm.getClassName(), newNode); } } root = sortTree(root); jScrollPane1.setViewportView(uTree); uTree.expandAll(); }
From source file:EditableTree.java
public void removeNode(DefaultMutableTreeNode selNode) { if (selNode == null) { return;/*from w w w. j ava2s. c om*/ } MutableTreeNode parent = (MutableTreeNode) (selNode.getParent()); if (parent == null) { return; } MutableTreeNode toBeSelNode = getSibling(selNode); if (toBeSelNode == null) { toBeSelNode = parent; } TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode); TreePath path = new TreePath(nodes); m_tree.scrollPathToVisible(path); m_tree.setSelectionPath(path); m_model.removeNodeFromParent(selNode); }
From source file:edu.harvard.i2b2.query.QueryConceptTreePanel.java
public DefaultMutableTreeNode addNode(QueryConceptTreeNodeData node) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(node); QueryConceptTreeNodeData tmpData = new QueryConceptTreeNodeData(); tmpData.name("working ......"); tmpData.tooltip("A tmp node"); tmpData.visualAttribute("LAO"); DefaultMutableTreeNode tmpNode = new DefaultMutableTreeNode(tmpData); treeModel.insertNodeInto(childNode, top, top.getChildCount()); treeModel.insertNodeInto(tmpNode, childNode, childNode.getChildCount()); //Make sure the user can see the lovely new node. jTree1.scrollPathToVisible(new TreePath(childNode.getPath())); return childNode; }
From source file:com.igormaznitsa.sciareto.ui.tree.ExplorerTree.java
public void unfoldProject(@Nonnull final NodeProject node) { Utils.safeSwingCall(new Runnable() { @Override//w w w.ja va2 s . co m public void run() { projectTree.expandPath(new TreePath(new Object[] { getCurrentGroup(), node })); } }); }
From source file:com.mirth.connect.client.ui.DashboardTreeTableModel.java
@Override public void setValueAt(Object value, Object node, int column) { if (!isValidTreeTableNode(node)) { throw new IllegalArgumentException("node must be a valid node managed by this model"); }/*from ww w . j a v a 2s.c o m*/ if (column < 0 || column >= getColumnCount()) { throw new IllegalArgumentException("column must be a valid index"); } TreeTableNode ttn = (TreeTableNode) node; if (column < ttn.getColumnCount()) { ttn.setValueAt(value, column); modelSupport.firePathChanged(new TreePath(getPathToRoot(ttn))); } }