public static TreeSearch installForOR(JTree tree) { return new TreeSearch(tree) { @Override public void selectAndSrollTo(TreeNode node) { if (node instanceof ORObjectInf) { TreePath path = ((ORObjectInf) node).getTreePath(); tree.setSelectionPath(path); tree.scrollPathToVisible(path); } else { super.selectAndSrollTo(node); } } }; }
private boolean areSiblingsSelected(TreePath path) { TreePath parent = path.getParentPath(); if (parent == null) { return true; } Object node = path.getLastPathComponent(); Object parentNode = parent.getLastPathComponent(); int childCount = model.getChildCount(parentNode); for (int i = 0; i < childCount; i++) { Object childNode = model.getChild(parentNode, i); if (childNode == node) { continue; } if (!isPathSelected(parent.pathByAddingChild(childNode))) { return false; } } return true; }
private boolean toggle( TreePath treePath ) { if( treePath == null ) return false; Node node = Visualizer.findNode( treePath.getLastPathComponent() ); if( node == null ) return false ; ElementNode.Description description = node.getLookup().lookup(ElementNode.Description.class); if (description != null ) { if( description.isSelectable() ) { description.setSelected( !description.isSelected() ); return true; } else { boolean newState = !description.isSelected(); toggleChildren( description.getSubs(), newState ); // first toggle children, then itself. If children were not expanded, // the self-toggle will fire appropriate events and controls will be reevaluated. description.setSelected(newState); } } return false; }
void selectPath(TreePath path, boolean scrollToVisible) { internal = true; markExpansionTransaction(); try { // tree.expandPath(path); // actually should not be needed, automatically expands the node further down tree.setSelectionPath(path); // Clear and select again to make sure the underlying tree is ready tree.setSelectionPath(null); tree.setSelectionPath(path); } finally { clearExpansionTransaction(); internal = false; } if (scrollToVisible) { Rectangle bounds = tree.getPathBounds(path); if (bounds != null) scrollRectToVisible(bounds); } }
/** * Opens all paths in the given node and all nodes below that. * * @param path * the tree path to the node to expand * @param treeModel * the tree model * @see JTree#expandPath(TreePath) */ protected void expandAllPaths(TreePath path, TreeModel treeModel) { expandPath(path); final Object node = path.getLastPathComponent(); final int n = treeModel.getChildCount(node); for (int index = 0; index < n; index++) { final Object child = treeModel.getChild(node, index); expandAllPaths(path.pathByAddingChild(child)); } }
@SuppressWarnings("rawtypes") public static void sortTree(PnlGuiConfig pnlGuiConfig, DefaultMutableTreeNode root, JTree treeTypes) { if (root != null) { Enumeration e = root.depthFirstEnumeration(); while (e.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (!node.isLeaf()) { sort(node); //selection sort } } //Atualizando a arvore if (updateTree) { TreePath treePath = treeTypes.getSelectionPath(); DefaultTreeModel model = (DefaultTreeModel) treeTypes.getModel(); model.reload(); treeTypes.setSelectionPath(treePath); updateTree = false; } pnlGuiConfig.getPnlFieldCondition().ckDynamicClick(); } }
/** * @return {@link List} of the currently selected nodes */ public List<String> getSelectedNodes() { TreePath[] paths = tree.getSelectionPaths(); List<String> selectedNodes = new ArrayList<String>(); if (paths != null) { for (TreePath path : paths) { StringBuilder sb = new StringBuilder(); Object[] pathArray = path.getPath(); for (Object o : pathArray) { String nodeName = o.toString(); if (nodeName.length() > 0) { sb.append("/"); sb.append(o.toString()); } } selectedNodes.add(sb.toString()); } } return selectedNodes; }
/** * Constructs a tree, with cell renderer and editor set by * {@link #createRenderer()} and {@link #createEditor()}. */ public CheckboxTree() { setCellRenderer(createRenderer()); setCellEditor(createEditor()); setEditable(true); setRootVisible(false); setShowsRootHandles(true); // make sure the checkbox never selects the label // note that the BasicTreeUI may not be what is used in the current LAF, // but I don't know any other way to modify the selection behaviour BasicTreeUI ui = new BasicTreeUI() { @Override protected void selectPathForEvent(TreePath path, MouseEvent event) { if (!isOverCheckBox(path, event.getPoint().x)) { super.selectPathForEvent(path, event); } } }; setUI(ui); // initialise the tree model this.topNode = new DefaultMutableTreeNode(); this.treeModel = new DefaultTreeModel(this.topNode); setModel(this.treeModel); // set selection mode getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); }
/** Synchronize the explored context from the manager of this Explorer. */ final void synchronizeExploredContext() { final Node n = manager.getExploredContext(); if (n == null) { return; } // run safely to be sure all preceding events are processed (especially VisualizerEvent.Added) // otherwise VisualizerNodes may not be in hierarchy yet (see #140629) VisualizerNode.runSafe(new Runnable() { @Override public void run() { final TreePath tp = getTreePath(n); LOG.log(Level.FINE, "synchronizeExploredContext {0} path {1}", new Object[] { n, tp }); showPath(tp); } }); }
/** Construct a Tree object with the given font size. */ public OurTree(int fontSize) { Font font = OurUtil.getVizFont().deriveFont((float)fontSize); OurTreeRenderer renderer = new OurTreeRenderer(fontSize); renderer.setFont(font); renderer.invalidate(); renderer.validate(); setRowHeight(0); // To allow variable row height on Mac OS X setCellRenderer(renderer); setFont(font); setBorder(new EmptyBorder(8, 8, 2, 2)); getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); putClientProperty("JTree.lineStyle", "Angled"); setRootVisible(true); setForeground(Color.BLACK); setBackground(Color.WHITE); setOpaque(true); addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath path = OurTree.this.getSelectionPath(); if (path!=null) OurTree.this.listeners.fire(OurTree.this, Listener.Event.CLICK, path.getLastPathComponent()); } }); }
@Override public void valueChanged(TreeSelectionEvent ev) { TreePath[] paths = tree.getSelectionPaths(); if (paths == null) { // part of bugfix #37279, if DnD is active then is useless select a nearby node if (ExplorerDnDManager.getDefault().isDnDActive()) { return; } callSelectionChanged(new Node[0]); } else { // we need to force no changes to nodes hierarchy => // we are requesting read request, but it is not necessary // to execute the next action immediatelly, so postReadRequest // should be enough readAccessPaths = paths; Children.MUTEX.postReadRequest(this); } }
@Override public boolean canPerformAction(JTree targetTree, Object draggedNode, int action, Point location) { TreePath pathTarget = targetTree.getPathForLocation(location.x, location.y); if (pathTarget == null) { targetTree.setSelectionPath(null); return false; } targetTree.setSelectionPath(pathTarget); if (action == DnDConstants.ACTION_COPY) { return false; } else if (action == DnDConstants.ACTION_MOVE) { Object targetNode = pathTarget.getLastPathComponent(); return canMove(draggedNode, targetNode); } else { return false; } }
@Override public void treeNodesRemoved(TreeModelEvent e) { // called to removed from JTree.expandedState super.treeNodesRemoved(e); boolean wasSelected = removedNodeWasSelected; removedNodeWasSelected = false; // part of bugfix #37279, if DnD is active then is useless select a nearby node if (ExplorerDnDManager.getDefault().isDnDActive()) { return; } if (wasSelected && tree.getSelectionCount() == 0) { TreePath path = findSiblingTreePath(e.getTreePath(), e.getChildIndices()); // bugfix #39564, don't select again the same object if ((path == null) || e.getChildIndices().length == 0) { return; } else if (path.getPathCount() > 0) { tree.setSelectionPath(path); } } }
/** * Applies a force to the given body if the user accepts the input. */ private void applyForceAction() { // the current selection should have the body selected TreePath path = this.tree.getSelectionPath(); // make sure that something is selected if (path != null) { // getProperty the currently selected node DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); // make sure the selected node is a body if (node.getUserObject() instanceof SandboxBody) { // getProperty the body from the node SandboxBody body = (SandboxBody) node.getUserObject(); // show the force input dialog Vector2 f = ApplyForceDialog.show(ControlUtilities.getParentWindow(this)); // make sure the user accepted the input if (f != null) { synchronized (Simulation.LOCK) { body.applyForce(f); } } } } }
public AvailableTreeTable() { super(); setTreeTableModel(new AvailableTreeTableModel(data.getRoot())); sizeColumns(); setRowHeight(CELL_HEIGHT); setSelectionMode(ListSelectionModel.SINGLE_SELECTION); getTableHeader().setReorderingAllowed(false); addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getPath(); TreeTableNode node = (TreeTableNode) path.getLastPathComponent(); if ("template".equals(node.getType())) { tempDownload.setEnabled(true); } else { tempDownload.setEnabled(false); } } }); }
private boolean haveCompleteNode(JTree tree) { int[] selRows = tree.getSelectionRows(); TreePath path = tree.getPathForRow(selRows[0]); DefaultMutableTreeNode first = (DefaultMutableTreeNode) path.getLastPathComponent(); int childCount = first.getChildCount(); // first has children and no children are selected. if (childCount > 0 && selRows.length == 1) { return false; } // first may have children. for (int i = 1; i < selRows.length; i++) { path = tree.getPathForRow(selRows[i]); DefaultMutableTreeNode next = (DefaultMutableTreeNode) path.getLastPathComponent(); if (first.isNodeChild(next)) { // Found a child of first. if (childCount > selRows.length - 1) { // Not all children of first are selected. return false; } } } return true; }
/** Make a path visible. * @param path the path */ private void showPathWithoutExpansion(TreePath path) { Rectangle rect = tree.getPathBounds(path); if (rect != null) { //PENDING TreeUI tmp = tree.getUI(); int correction = 0; if (tmp instanceof BasicTreeUI) { correction = ((BasicTreeUI) tmp).getLeftChildIndent(); correction += ((BasicTreeUI) tmp).getRightChildIndent(); } rect.x = Math.max(0, rect.x - correction); rect.y += rect.height; if (rect.y >= 0) { //#197514 - do not scroll to negative y values tree.scrollRectToVisible(rect); } } }
public boolean hasSelectedItems() { TreePath[] sel = getSelectionPaths(); if (sel == null || sel.length == 0) return false; for (int i = 0; i < sel.length; i++) { Object last = sel[i].getLastPathComponent(); if (last instanceof OptionNode) { return true; } else if (last instanceof ComponentNode) { if (((ComponentNode) last).opts == null) return true; } } return false; }
/** * Shows an edit body dialog. * <p> * If the user clicks the save button, the selected body is updated with the new values. */ private void editBodyAction() { // getProperty the currently selected body TreePath path = this.tree.getSelectionPath(); // make sure something is selected if (path != null) { // getProperty the selected node DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); // make sure its a body that is selected if (node.getUserObject() instanceof SandboxBody) { // getProperty the body from the node SandboxBody body = (SandboxBody) node.getUserObject(); // pass the body to the edit dialog synchronized (Simulation.LOCK) { EditBodyDialog.show(ControlUtilities.getParentWindow(this), body); } } } }
@Override public void setSelectionPaths(TreePath[] treePaths) { ArrayList<TreePath> paths = new ArrayList<>(treePaths.length); for (TreePath treePath : treePaths) { Object lastPathComponent = treePath.getLastPathComponent(); if (lastPathComponent instanceof DefaultMutableTreeNode) { Object obj = ((DefaultMutableTreeNode) lastPathComponent).getUserObject(); if (obj != MODULEPATH && obj != CLASSPATH) { paths.add(treePath); } } } if (treePaths.length > 0 && paths.isEmpty()) { paths.addAll(Arrays.asList(getSelectionPaths())); } super.setSelectionPaths(paths.toArray(new TreePath[paths.size()])); }
/** * Compute real table row indices of nodes that are affected by the event. * * @param e Event description. * @return Indices of rows in the table where the nodes (affected by the * event) are displayed, or null if they are not available. */ private int[] computeRowIndices(TreeModelEvent e) { int[] rowIndices; int parentRow = getLayout().getRowForPath(e.getTreePath()); if (e.getChildren() != null) { rowIndices = new int[e.getChildren().length]; for (int i = 0; i < e.getChildren().length; i++) { TreePath childPath = e.getTreePath().pathByAddingChild(e.getChildren()[i]); int index = getLayout().getRowForPath(childPath); rowIndices[i] = index < 0 // child node is not shown yet, compute child row from // parent row and index of the child ? parentRow + e.getChildIndices()[i] + 1 : index; } } else { rowIndices = null; } return rowIndices; }
/** Set whether or not the root is visible */ public void setRootVisible (boolean val) { if (getOutlineModel() == null) { cachedRootVisible = val ? Boolean.TRUE : Boolean.FALSE; } if (val != isRootVisible()) { getLayoutCache().setRootVisible(val); if( getLayoutCache().getRowCount() > 0 ) { TreePath rootPath = getLayoutCache().getPathForRow(0); if( null != rootPath ) getLayoutCache().treeStructureChanged(new TreeModelEvent(this, rootPath)); } sortAndFilter(); firePropertyChange("rootVisible", !val, val); //NOI18N } }
private void dimensionConfigChanged(PlotConfigurationChangeEvent change) { PlotConfigurationTreeNode rootNode = (PlotConfigurationTreeNode) root; DimensionConfigChangeEvent dimensionChange = change.getDimensionChange(); DimensionConfigTreeNode dimensionConfigNode = (DimensionConfigTreeNode) rootNode.getChild(dimensionChange .getDimension()); nodeChanged(dimensionConfigNode); if (dimensionChange.getType() == DimensionConfigChangeType.COLUMN) { // change selection path TreePath path = new TreePath(getPathToRoot(dimensionConfigNode)); makeVisibleAndSelect(path); } }
private void maybeShowPopup(MouseEvent evt) { if (evt.isPopupTrigger()) { TreePath selectedPath = getPathForLocation(evt.getX(), evt.getY()); TreeNode selectedNode = selectedPath == null ? null : (TreeNode) selectedPath.getLastPathComponent(); StateTree.this.requestFocus(); createPopupMenu(selectedNode).show(evt.getComponent(), evt.getX(), evt.getY()); } }
public void valueChanged(TreeSelectionEvent e) { TreePath[] paths = e.getPaths(); selectedNodes.clear(); for (TreePath path : paths) { boolean appended = false; StringBuilder sb = new StringBuilder(); Object[] pathArray = path.getPath(); for (Object o : pathArray) { if (o != null) { String nodeName = o.toString(); if (nodeName != null) { if (nodeName.length() > 0) { appended = true; sb.append("/"); //$NON-NLS-1$ sb.append(o.toString()); } } } } if (appended) { selectedNodes.add(sb.toString()); } } for (int i = 0; i < needsReload.size(); i++) { this.needsReload.set(i, true); } reloadSelectedViewer(); }
private void processSelection() { TreePath tp = getSelectionPath(); if (tp == null) return; DefaultMutableTreeNode lf = (DefaultMutableTreeNode)tp.getLastPathComponent(); Object o = lf.getUserObject(); if (o instanceof Entrant) { Entrant entrant = (Entrant)lf.getUserObject(); Messenger.sendEvent(MT.CAR_ADD, entrant.getCarId()); } }
@Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { TreePath path = getPathForLocation(e.getX(), e.getY()); if (path != null && listener != null) { listener.doubleClicked(new Event(path)); } } }
private TreePath substPath( final TreePath tp, final TreePath oldPath, final TreePath path) { final TFile file = (TFile) tp.getLastPathComponent(); if (file.equals(oldPath.getLastPathComponent())) { return path; } else { final TreePath parent = substPath(tp.getParentPath(), oldPath, path); return parent.pathByAddingChild( new net.java.truevfs.access.TFile((TFile) parent.getLastPathComponent(), file.getName())); } }
private String getPathText(JTree tree, TreePath path) { Object lastPathComponent = path.getLastPathComponent(); if (lastPathComponent == null) { return ""; } return getTextForNodeObject(tree, lastPathComponent); }
StringArraySubPathChooser(TreePath parentPath, String[] arr, int[] indices, JTreeOperator.StringComparator comparator) { this.arr = arr; this.comparator = comparator; this.indices = indices; this.parentPath = parentPath; this.parentPathCount = parentPath.getPathCount(); }
public SelectionTreeItemListener(SelectionPanel owner) { if (owner == null) throw new IllegalArgumentException(); this.owner = owner; TreeModel model = owner.getTree().getModel(); Object root = owner.getTree().getModel().getRoot(); ePath = new TreePath(new Object[] { root, model.getChild(root, 0) }); vPath = new TreePath(new Object[] { root, model.getChild(root, 1) }); }
@Override public void valueChanged(TreeSelectionEvent e) { TreePath newLeadSelectionPath = e.getNewLeadSelectionPath(); if (newLeadSelectionPath != null) { if (newLeadSelectionPath.getLastPathComponent() instanceof DimensionConfigTreeNode) { adaptGUI(); } } }
@Override public void valueChanged(TreeSelectionEvent event) { TreePath path = event.getPath(); if (path.getPathCount() >= 2) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getPathComponent(1); this.selected = (OptionGroup)node.getUserObject(); } }
@Override public String pathToString(TreePath path) { StringBuilder sb = new StringBuilder(); Object[] items = path.getPath(); for (int i=1; i<items.length; i++) { sb.append(items[i]).append('.'); } if (sb.length() > 0) { sb.deleteCharAt(sb.length()-1); } String value = sb.toString().trim(); return "null".equals(value) ? "null" : designSupport.elWrap(sb.toString()); // NOI18N }
public TreePath getTreePath() { List<RegistryNode> nodes = new LinkedList<RegistryNode>(); RegistryNode node = this; while (node != null) { nodes.add(0, node); node = node.getParent(); } return new TreePath(nodes.toArray()); }
/** * Test if the tree path is visible (the parent path is expanded). * @param path The tree path to test * @return <code>true</code> if the path is visible, <code>false</code> otherwise. */ public boolean isVisible(TreePath path) { if(path != null) { TreePath parentPath = path.getParentPath(); if(parentPath != null) { return isExpanded(parentPath); } // Root. return true; } return false; }
public static void expandPaths(JTree tree, ArrayList<String> paths_expanded) { for(int i = 0; i < tree.getRowCount(); i++) { TreePath tp = tree.getPathForRow(i); if(paths_expanded.contains(tp.toString())) { tree.expandRow(i); } } }
/** * Notifies all listeners that have registered interest for notification on * this event type. The event instance is lazily created using the * parameters passed into the fire method. * * @param source the node being changed * @param path the path to the root node * @param childIndices the indices of the changed elements * @param children the changed elements * @see EventListenerList */ private void fireTreeNodesInserted(TreePath path, int[] childIndices, Object[] children) { TreeModelListener[] listeners = listenerList.getListeners(TreeModelListener.class); if( listeners != null && listeners.length > 0 ) { TreeModelEvent e = new TreeModelEvent(this, path, childIndices, children); for( TreeModelListener listener : listeners ) { listener.treeNodesInserted(e); } } }
/** * Default constructor. */ public SimulationTreePanel() { this.root = new DefaultMutableTreeNode(null, true); this.model = new DefaultTreeModel(this.root); this.tree = new JTree(this.model); this.tree.setCellRenderer(new Renderer()); this.tree.setEditable(false); this.tree.setShowsRootHandles(true); this.tree.setBorder(new EmptyBorder(5, 0, 0, 0)); // the bounds this.bounds = new DefaultMutableTreeNode(new NullBounds()); this.model.insertNodeInto(this.bounds, this.root, this.root.getChildCount()); this.tree.expandPath(new TreePath(this.bounds).getParentPath()); // folder to contain the bodies this.bodyFolder = new DefaultMutableTreeNode(Messages.getString("panel.tree.bodyFolder")); this.model.insertNodeInto(this.bodyFolder, this.root, this.root.getChildCount()); this.tree.expandPath(new TreePath(this.bodyFolder.getPath()).getParentPath()); // folder to contain the joints this.jointFolder = new DefaultMutableTreeNode(Messages.getString("panel.tree.jointFolder")); this.model.insertNodeInto(this.jointFolder, this.root, this.root.getChildCount()); this.tree.expandPath(new TreePath(this.jointFolder.getPath()).getParentPath()); // folder to contain the rays this.rayFolder = new DefaultMutableTreeNode(Messages.getString("panel.tree.rayFolder")); this.model.insertNodeInto(this.rayFolder, this.root, this.root.getChildCount()); this.tree.expandPath(new TreePath(this.rayFolder.getPath()).getParentPath()); this.scroller = new JScrollPane(this.tree); this.scroller.setBorder(BorderFactory.createLineBorder(Color.GRAY)); this.setLayout(new BorderLayout()); this.add(this.scroller); this.tree.addMouseListener(this); this.createContextMenus(); }