List of usage examples for javax.swing.tree TreePath getPathCount
public int getPathCount()
From source file:cool.pandora.modeller.ui.jpanel.base.BagView.java
/** * registerTreeListener.//from w ww . j av a2s . c o m * * @param label String * @param tree JTree */ public void registerTreeListener(final String label, final JTree tree) { if (AbstractBagConstants.DATA_DIRECTORY.equals(label)) { tree.addTreeSelectionListener(e -> { final TreePath[] paths = tree.getSelectionPaths(); if (paths == null || paths.length == 0) { return; } for (final TreePath path : paths) { if (path.getPathCount() == 1) { removeDataToolBarAction.setEnabled(false); return; } } removeDataToolBarAction.setEnabled(true); }); } else { tree.addTreeSelectionListener(e -> { final TreePath[] paths = tree.getSelectionPaths(); if (paths == null || paths.length == 0) { return; } for (final TreePath path : paths) { if (path.getPathCount() == 1) { removeTagFileToolbarAction.setEnabled(false); return; } } removeTagFileToolbarAction.setEnabled(true); }); } }
From source file:edu.purdue.cc.bionet.ui.DistributionAnalysisDisplayPanel.java
/** * Sets the SampleGroups for this panel. * //from w w w . j a va 2 s. com * @param sampleGroups The new set of groups */ public void setSampleGroups(Collection<SampleGroup> sampleGroups) { super.setSampleGroups(sampleGroups); // clear the listeners CheckboxTree tree = this.selectorTree.getTree(); for (TreeSelectionListener t : tree.getTreeSelectionListeners()) { tree.removeTreeSelectionListener(t); // CheckboxTree doesn't have a method for getting TreeCheckingListeners, // so we'll try this and catch any exceptions. try { tree.removeTreeCheckingListener((TreeCheckingListener) t); } catch (ClassCastException e) { } } for (ItemListener i : this.noFitButton.getItemListeners()) { this.noFitButton.removeItemListener(i); } for (ItemListener i : this.robustFitButton.getItemListeners()) { this.robustFitButton.removeItemListener(i); } for (ItemListener i : this.chiSquareFitButton.getItemListeners()) { this.chiSquareFitButton.removeItemListener(i); } this.experimentGraphPanel.removeAll(); this.bottomPanel.removeAll(); int cols = (int) Math.ceil(Math.sqrt(sampleGroups.size())); int rows = (int) Math.ceil(sampleGroups.size() / cols); GridLayout layout = (GridLayout) this.experimentGraphPanel.getLayout(); layout.setRows(rows); layout.setColumns(cols); layout = (GridLayout) this.bottomPanel.getLayout(); layout.setRows(rows); layout.setColumns(cols); TreePath path = selectorTree.getTree().getSelectionPath(); int level = path.getPathCount(); DefaultMutableTreeNode selectedNode = null; if (level > MOLECULE) { selectedNode = (DefaultMutableTreeNode) path.getPathComponent(MOLECULE); } for (SampleGroup sampleGroup : sampleGroups) { SampleGraph sampleGraph = new SampleGraph(experiments, sampleGroup); ExperimentGraph experimentGraph = new ExperimentGraph(experiments, sampleGroup); this.bottomPanel.add(sampleGraph); this.selectorTree.getTree().addTreeSelectionListener(sampleGraph); this.experimentGraphPanel.add(experimentGraph, BorderLayout.CENTER); // add the listeners this.selectorTree.getTree().addTreeCheckingListener(sampleGraph); this.selectorTree.getTree().addTreeSelectionListener(experimentGraph); this.selectorTree.getTree().addTreeCheckingListener(experimentGraph); this.noFitButton.addItemListener(experimentGraph); this.robustFitButton.addItemListener(experimentGraph); this.chiSquareFitButton.addItemListener(experimentGraph); if (selectedNode != null) { sampleGraph.setGraph(selectedNode); experimentGraph.setGraph(selectedNode); } } this.removeSampleGroupsMenuItem.setEnabled(sampleGroups != null && sampleGroups.size() > 1); this.experimentGraphPanel.validate(); this.bottomPanel.validate(); }
From source file:edu.ku.brc.af.tasks.subpane.formeditor.ViewSetSelectorPanel.java
/** * @return// www . j a v a2 s.c o m */ protected FormViewDef getViewDefFromSelection() { if (tree != null && tree.getSelectionModel() != null && tree.getSelectionModel().getSelectionPath() != null) { TreePath path = tree.getSelectionModel().getSelectionPath(); for (int i = 0; i < path.getPathCount(); i++) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(i); if (node.getUserObject() instanceof FormViewDef) { return (FormViewDef) node.getUserObject(); } } } return null; }
From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java
/** * /* w ww . j a v a 2 s. co m*/ */ private void updateBtnUI() { containerAssocIcon.setEnabled(false); colObjAssocIcon.setEnabled(false); colObjIcon.setEnabled(false); containerIcon.setEnabled(false); if (tree != null) { boolean isNodeSelected = tree.getSelectionCount() > 0; if (isNodeSelected) { TreePath path = tree.getSelectionModel().getSelectionPath(); if (path != null && path.getPathCount() > 0) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path .getPathComponent(path.getPathCount() - 1); if (node != null) { Object dataObj = node.getUserObject(); if (dataObj != null) { boolean isContainer = dataObj instanceof Container; if (isContainer) { int[] selInxs = tree.getSelectionRows(); Container cntr = (Container) dataObj; edaContnrPanel.getEditBtn().setEnabled(selInxs[0] > 0); if (!isViewMode) { edaContnrPanel.getAddBtn().setEnabled(true); edaContnrPanel.getDelBtn().setEnabled(selInxs[0] > 0); } else { edaContnrPanel.getEditBtn().setEnabled(selInxs[0] > -1); } if (!isViewMode) { colObjAssocIcon.setEnabled(true); containerAssocIcon.setEnabled(true); } else { containerAssocIcon.setEnabled(cntr.getCollectionObject() != null); colObjAssocIcon.setEnabled(cntr.getCollectionObject() != null); } colObjIcon.setEnabled(!isViewMode); containerIcon.setEnabled(true); if (!isViewMode) { edaColObjPanel.getAddBtn().setEnabled(true); edaColObjPanel.getDelBtn().setEnabled(false); } edaColObjPanel.getEditBtn().setEnabled(false); searchCOBtn.setEnabled(true); searchCNBtn.setEnabled(true); } else { edaContnrPanel.setEnabled(false); containerIcon.setEnabled(false); colObjIcon.setEnabled(true); edaColObjPanel.getEditBtn().setEnabled(true); if (!isViewMode) { edaColObjPanel.getDelBtn().setEnabled(true); edaColObjPanel.getAddBtn().setEnabled(false); searchCOBtn.setEnabled(false); searchCNBtn.setEnabled(false); } containerAssocIcon.setEnabled(false); colObjAssocIcon.setEnabled(false); } } } } } else { edaColObjPanel.setEnabled(false); edaContnrPanel.setEnabled(false); colObjIcon.setEnabled(false); containerIcon.setEnabled(false); searchCOBtn.setEnabled(false); searchCNBtn.setEnabled(false); containerAssocIcon.setEnabled(false); colObjAssocIcon.setEnabled(false); } } }
From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java
/** * //from www . ja va 2s .co m */ private DefaultMutableTreeNode getSelectedTreeNode() { boolean isNodeSelected = tree.getSelectionCount() > -1; if (isNodeSelected) { TreePath path = tree.getSelectionModel().getSelectionPath(); if (path != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path .getPathComponent(path.getPathCount() - 1); if (node != null) { return node; } } } return null; }
From source file:edu.ku.brc.af.tasks.subpane.formeditor.ViewSetSelectorPanel.java
/** * // w w w .j a v a 2s .com */ protected void treeSelected() { TreePath treePath = tree.getSelectionModel().getSelectionPath(); if (treePath == null) { selectedRow = null; selectedCell = null; return; } DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent(); Object nodeObj = node.getUserObject(); if (nodeObj instanceof FormRow) { if (nodeObj == selectedRow) { return; } selectedRow = (FormRow) nodeObj; selectedCell = null; } else if (nodeObj instanceof FormCell) { if (nodeObj == selectedCell) { return; } selectedCell = (FormCell) nodeObj; DefaultMutableTreeNode rowNode = (DefaultMutableTreeNode) node.getParent(); selectedRow = (FormRow) rowNode.getUserObject(); } else { selectedRow = null; selectedCell = null; } if (treePath != null) { updateUIControls(); if (treePath.getPathCount() == 1) { panel.loadView("ViewDefProps", null); //$NON-NLS-1$ panel.setData(selectedViewDef); return; } if (selectedCell != null) { int col = 0; for (FormCellIFace fc : selectedRow.getCells()) { if (fc == nodeObj) { break; } col++; } showPropertiesPanel((FormCell) nodeObj, formViewDef.getClassName(), (formViewDef.getRows().size() * 2) - 1, formViewDef.getRowDefItem().getNumItems(), selectedRow.getRowNumber(), (selectedRow.getCells().size() * 2) - 1, formViewDef.getColumnDefItem().getNumItems(), col); } } }
From source file:com.pironet.tda.TDA.java
/** * expand or collapse all nodes of the specified tree * * @param catTree the tree to expand all/collapse all * @param parent the parent to start with * @param expand expand=true, collapse=false */// w w w. j a va2s . c om private void expandAll(JTree catTree, TreePath parent, boolean expand) { // Traverse children TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements();) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(catTree, path, expand); } } if (parent.getPathCount() > 1) { // Expansion or collapse must be done bottom-up if (expand) { catTree.expandPath(parent); } else { catTree.collapsePath(parent); } } }
From source file:net.jradius.client.gui.JRadiusSimulator.java
/** * This method initializes addButton /*from w w w . j av a2 s .co m*/ * * @return javax.swing.JButton */ private JButton getAddButton() { if (addButton == null) { addButton = new JButton(); addButton.setText("Add"); addButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { int rows[] = attributeTree.getSelectionRows(); if (rows != null) { for (int i = 0; i < rows.length; i++) { TreePath path = attributeTree.getPathForRow(rows[i]); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path .getPathComponent(path.getPathCount() - 1); String attr = (String) node.getUserObject(); try { attributesTableModel.addAttribute(AttributeFactory.newAttribute(attr)); } catch (Exception ex) { ex.printStackTrace(); } } attributesTableModel.fireTableDataChanged(); } } }); } return addButton; }
From source file:ca.uhn.hl7v2.testpanel.ui.v2tree.Hl7V2MessageTree.java
private Set<String> getOpenPaths() { Set<String> retVal = new HashSet<String>(); TableModel model = getModel(); AbstractLayoutCache layout = ((OutlineModel) model).getLayout(); int messageIndex = -1; for (int i = 0; i < layout.getRowCount(); i++) { TreePath path = layout.getPathForRow(i); Object baseObj = path.getLastPathComponent(); if (baseObj instanceof TreeNodeMessage || baseObj instanceof TreeNodeUnknown) { messageIndex++;/* w ww. j a v a 2 s .c o m*/ if (layout.getExpandedState(path)) { retVal.add(Integer.toString(messageIndex)); } } else { baseObj = path.getPathComponent(path.getPathCount() - 2); if (baseObj instanceof TreeNodeBase) { retVal.add(Integer.toString(messageIndex) + ((TreeNodeBase) baseObj).getTerserPath()); } } } return retVal; }
From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java
private JPanel createAParameterBox(final boolean first) { final JLabel runLabel = new JLabel("<html><b>Number of runs:</b> 0</html>"); final JLabel warningLabel = new JLabel(); final JButton closeButton = new JButton(); closeButton.setOpaque(false);//w ww .j av a 2s . co m closeButton.setBorder(null); closeButton.setFocusable(false); if (!first) { closeButton.setRolloverIcon(PARAMETER_BOX_REMOVE); closeButton.setRolloverEnabled(true); closeButton.setIcon(RGBGrayFilter.getDisabledIcon(closeButton, PARAMETER_BOX_REMOVE)); closeButton.setActionCommand(ACTIONCOMMAND_REMOVE_BOX); } final JScrollPane treeScrPane = new JScrollPane(); final DefaultMutableTreeNode treeRoot = new DefaultMutableTreeNode(); final JTree tree = new JTree(treeRoot); ToolTipManager.sharedInstance().registerComponent(tree); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setCellRenderer(new ParameterBoxTreeRenderer()); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(final TreeSelectionEvent e) { final TreePath selectionPath = tree.getSelectionPath(); boolean success = true; if (editedNode != null && (selectionPath == null || !editedNode.equals(selectionPath.getLastPathComponent()))) success = modify(); if (success) { if (selectionPath != null) { cancelAllSelectionBut(tree); final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath .getLastPathComponent(); if (!node.equals(editedNode)) { ParameterInATree userObj = null; final DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); if (!node.isRoot() && selectionPath.getPathCount() == model.getPathToRoot(node).length) { userObj = (ParameterInATree) node.getUserObject(); final ParameterInfo info = userObj.info; editedNode = node; editedTree = tree; edit(info); } else { tree.setSelectionPath(null); if (cancelButton.isEnabled()) cancelButton.doClick(); resetSettings(); enableDisableSettings(false); editedNode = null; editedTree = null; } updateDescriptionField(userObj); } else updateDescriptionField(); } else updateDescriptionField(); enableDisableParameterCombinationButtons(); } else { final DefaultTreeModel model = (DefaultTreeModel) editedTree.getModel(); final DefaultMutableTreeNode storedEditedNode = editedNode; editedNode = null; tree.setSelectionPath(null); editedNode = storedEditedNode; editedTree.setSelectionPath(new TreePath(model.getPathToRoot(editedNode))); } } }); treeScrPane.setViewportView(tree); treeScrPane.setBorder(null); treeScrPane.setViewportBorder(null); treeScrPane.setPreferredSize(new Dimension(450, 250)); final JButton upButton = new JButton(); upButton.setOpaque(false); upButton.setRolloverEnabled(true); upButton.setIcon(PARAMETER_UP_ICON); upButton.setRolloverIcon(PARAMETER_UP_ICON_RO); upButton.setDisabledIcon(PARAMETER_UP_ICON_DIS); upButton.setBorder(null); upButton.setToolTipText("Move up the selected parameter"); upButton.setActionCommand(ACTIONCOMMAND_MOVE_UP); final JButton downButton = new JButton(); downButton.setOpaque(false); downButton.setRolloverEnabled(true); downButton.setIcon(PARAMETER_DOWN_ICON); downButton.setRolloverIcon(PARAMETER_DOWN_ICON_RO); downButton.setDisabledIcon(PARAMETER_DOWN_ICON_DIS); downButton.setBorder(null); downButton.setToolTipText("Move down the selected parameter"); downButton.setActionCommand(ACTIONCOMMAND_MOVE_DOWN); final JPanel mainPanel = FormsUtils.build("~ f:p:g ~ p ~ r:p", "012||" + "333||" + "44_||" + "445||" + "446||" + "44_ f:p:g", runLabel, first ? "" : warningLabel, first ? warningLabel : closeButton, new FormsUtils.Separator(""), treeScrPane, upButton, downButton) .getPanel(); mainPanel.setBorder(BorderFactory.createTitledBorder("")); final JButton addButton = new JButton(); addButton.setOpaque(false); addButton.setRolloverEnabled(true); addButton.setIcon(PARAMETER_ADD_ICON); addButton.setRolloverIcon(PARAMETER_ADD_ICON_RO); addButton.setDisabledIcon(PARAMETER_ADD_ICON_DIS); addButton.setBorder(null); addButton.setToolTipText("Add selected parameter"); addButton.setActionCommand(ACTIONCOMMAND_ADD_PARAM); final JButton removeButton = new JButton(); removeButton.setOpaque(false); removeButton.setRolloverEnabled(true); removeButton.setIcon(PARAMETER_REMOVE_ICON); removeButton.setRolloverIcon(PARAMETER_REMOVE_ICON_RO); removeButton.setDisabledIcon(PARAMETER_REMOVE_ICON_DIS); removeButton.setBorder(null); removeButton.setToolTipText("Remove selected parameter"); removeButton.setActionCommand(ACTIONCOMMAND_REMOVE_PARAM); final JPanel result = FormsUtils.build("p ~ f:p:g", "_0 f:p:g||" + "10 p ||" + "20 p||" + "_0 f:p:g", mainPanel, addButton, removeButton).getPanel(); Style.registerCssClasses(result, Dashboard.CSS_CLASS_COMMON_PANEL); final ParameterCombinationGUI pcGUI = new ParameterCombinationGUI(tree, treeRoot, runLabel, warningLabel, addButton, removeButton, upButton, downButton); parameterTreeBranches.add(pcGUI); final ActionListener boxActionListener = new ActionListener() { //==================================================================================================== // methods //---------------------------------------------------------------------------------------------------- public void actionPerformed(final ActionEvent e) { final String cmd = e.getActionCommand(); if (ACTIONCOMMAND_ADD_PARAM.equals(cmd)) handleAddParameter(pcGUI); else if (ACTIONCOMMAND_REMOVE_PARAM.equals(cmd)) handleRemoveParameter(tree); else if (ACTIONCOMMAND_REMOVE_BOX.equals(cmd)) handleRemoveBox(tree); else if (ACTIONCOMMAND_MOVE_UP.equals(cmd)) handleMoveUp(); else if (ACTIONCOMMAND_MOVE_DOWN.equals(cmd)) handleMoveDown(); } //---------------------------------------------------------------------------------------------------- private void handleAddParameter(final ParameterCombinationGUI pcGUI) { final Object[] selectedValues = parameterList.getSelectedValues(); if (selectedValues != null && selectedValues.length > 0) { final AvailableParameter[] params = new AvailableParameter[selectedValues.length]; System.arraycopy(selectedValues, 0, params, 0, selectedValues.length); addParameterToTree(params, pcGUI); enableDisableParameterCombinationButtons(); } } //---------------------------------------------------------------------------------------------------- private void handleRemoveParameter(final JTree tree) { final TreePath selectionPath = tree.getSelectionPath(); if (selectionPath != null) { cancelButton.doClick(); final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath .getLastPathComponent(); if (!node.isRoot()) { final DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent(); if (parentNode.isRoot()) { removeParameter(tree, node, parentNode); enableDisableParameterCombinationButtons(); } } } } //---------------------------------------------------------------------------------------------------- private void handleRemoveBox(final JTree tree) { final int answer = Utilities.askUser(dashboard, false, "Comfirmation", "This operation deletes the combination.", "All related parameter returns back to the list on the left side.", "Are you sure?"); if (answer == 1) { final DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel(); if (tree.getSelectionCount() > 0) { editedNode = null; tree.setSelectionPath(null); if (cancelButton.isEnabled()) cancelButton.doClick(); } final DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot(); for (int i = 0; i < root.getChildCount(); ++i) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i); removeParameter(tree, node, root); } enableDisableParameterCombinationButtons(); parameterTreeBranches.remove(pcGUI); combinationsPanel.remove(result); combinationsPanel.revalidate(); updateNumberOfRuns(); } } //---------------------------------------------------------------------------------------------------- private void removeParameter(final JTree tree, final DefaultMutableTreeNode node, final DefaultMutableTreeNode parentNode) { final ParameterInATree userObj = (ParameterInATree) node.getUserObject(); final ParameterInfo originalInfo = findOriginalInfo(userObj.info); if (originalInfo != null) { final DefaultListModel model = (DefaultListModel) parameterList.getModel(); model.addElement(new AvailableParameter(originalInfo, currentModelHandler.getModelClass())); final DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel(); treeModel.removeNodeFromParent(node); updateNumberOfRuns(); tree.expandPath(new TreePath(treeModel.getPathToRoot(parentNode))); } else throw new IllegalStateException( "Parameter " + userObj.info.getName() + " is not found in the model."); } //---------------------------------------------------------------------------------------------------- private void handleMoveUp() { final TreePath selectionPath = tree.getSelectionPath(); if (selectionPath != null) { boolean success = true; if (editedNode != null) success = modify(); if (success) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath .getLastPathComponent(); final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent(); if (parent == null || parent.getFirstChild().equals(node)) { tree.setSelectionPath(null); // we need this to preserve the state of the parameter settings panel tree.setSelectionPath(new TreePath(node.getPath())); return; } final int index = parent.getIndex(node); final DefaultTreeModel treemodel = (DefaultTreeModel) tree.getModel(); treemodel.removeNodeFromParent(node); treemodel.insertNodeInto(node, parent, index - 1); tree.setSelectionPath(new TreePath(node.getPath())); } } } //---------------------------------------------------------------------------------------------------- private void handleMoveDown() { final TreePath selectionPath = tree.getSelectionPath(); if (selectionPath != null) { boolean success = true; if (editedNode != null) success = modify(); if (success) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath .getLastPathComponent(); final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent(); if (parent == null || parent.getLastChild().equals(node)) { tree.setSelectionPath(null); // we need this to preserve the state of the parameter settings panel tree.setSelectionPath(new TreePath(node.getPath())); return; } final int index = parent.getIndex(node); final DefaultTreeModel treemodel = (DefaultTreeModel) tree.getModel(); treemodel.removeNodeFromParent(node); treemodel.insertNodeInto(node, parent, index + 1); tree.setSelectionPath(new TreePath(node.getPath())); } } } }; GUIUtils.addActionListener(boxActionListener, closeButton, upButton, downButton, addButton, removeButton); result.setPreferredSize(new Dimension(500, 250)); enableDisableParameterCombinationButtons(); Style.apply(result, dashboard.getCssStyle()); return result; }