public GAGenerationTreePanel(GAObjectMonitor monitor, String parentNodeTitle) { super(); selectedChildrenPaths = new Vector(); generationNumber = 1; saveNumber = 0; generations = new DefaultMutableTreeNode(parentNodeTitle); gaMonitor = monitor; setLayout(new BorderLayout()); tree = new JTree(generations); tree.setShowsRootHandles(true); tree.setRootVisible(true); tree.putClientProperty("JTree.lineStyle", "Angled"); // Add Action Listeners MyTreeSelectionListener listener = new MyTreeSelectionListener(monitor.getSelectionInfoPanel()); MyTreeClickListener mlistener = new MyTreeClickListener(); tree.addTreeSelectionListener(listener); tree.addMouseListener(mlistener); // Add Scroll Plane scrollPane = new JScrollPane(tree); add(scrollPane, BorderLayout.CENTER); // Add Menu Bar Functionality createMenuBar(); add(menuBar, BorderLayout.NORTH); }
// {{{ removeSelectedNode() method private void removeSelectedNode() { TreePath path = resultTree.getSelectionPath(); if (path == null) return; MutableTreeNode value = (MutableTreeNode) path.getLastPathComponent(); if (path.getPathCount() > 1) { // Adjust selection so that repeating some removals // behave naturally. TreePath parentPath = path.getParentPath(); MutableTreeNode parent = (MutableTreeNode) parentPath.getLastPathComponent(); int removingIndex = parent.getIndex(value); int nextIndex = removingIndex + 1; if (nextIndex < parent.getChildCount()) { TreeNode next = parent.getChildAt(nextIndex); resultTree.setSelectionPath(parentPath.pathByAddingChild(next)); } else { resultTree.setSelectionPath(parentPath); } resultTreeModel.removeNodeFromParent(value); } HyperSearchOperationNode.removeNodeFromCache(value); if (resultTreeRoot.getChildCount() == 0) { hideDockable(); } } // }}}
@Nullable public static Object getTagAtForRenderer( CheckboxTree.CheckboxTreeCellRenderer renderer, MouseEvent e) { JTree tree = (JTree) e.getSource(); Object tag = null; final TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path != null) { final Rectangle rectangle = tree.getPathBounds(path); assert rectangle != null; int dx = e.getX() - rectangle.x; final TreeNode treeNode = (TreeNode) path.getLastPathComponent(); final int row = tree.getRowForLocation(e.getX(), e.getY()); tree.getCellRenderer() .getTreeCellRendererComponent(tree, treeNode, false, false, true, row, true); if (treeNode instanceof RepositoryNode) { RepositoryNode repositoryNode = (RepositoryNode) treeNode; int checkBoxWidth = repositoryNode.isCheckboxVisible() ? renderer.getCheckbox().getWidth() : 0; tag = renderer.getTextRenderer().getFragmentTagAt(dx - checkBoxWidth); } else { tag = renderer.getTextRenderer().getFragmentTagAt(dx); } } return tag; }
@Nullable private TreePath getSelectedUniquePath() { JTree tree = getTree(); if (tree == null) return null; TreePath[] paths = tree.getSelectionPaths(); return paths == null || paths.length != 1 ? null : paths[0]; }
// reorder the nodes private void reorder(Vector nodes) { debug("reorder nodes"); // remove all the children of topNode (they'll be added back later) topNode.removeAllChildren(); // Create an array of the elements for sorting & copy the elements // into the array. DefaultMutableTreeNode[] array = new DefaultMutableTreeNode[nodes.size()]; nodes.copyInto(array); // Sort the array (Quick Sort) quickSort(array, 0, array.length - 1); // Reload the topNode. Everthing is in order now. for (int i = 0; i < array.length; i++) { topNode.add((DefaultMutableTreeNode) array[i]); } // Tell the tree to repaint itself ((DefaultTreeModel) tree.getModel()).reload(); tree.invalidate(); tree.repaint(); }
public static List<TreePath> collectExpandedPaths(final JTree tree, TreePath path) { final ArrayList<TreePath> result = new ArrayList<TreePath>(); if (!tree.isExpanded(path)) return result; final Object lastPathComponent = path.getLastPathComponent(); final TreeModel model = tree.getModel(); if (model.isLeaf(lastPathComponent)) { result.add(path); } else { boolean pathWasAdded = false; for (int i = model.getChildCount(lastPathComponent) - 1; i >= 0; i--) { final TreePath childPath = path.pathByAddingChild(model.getChild(lastPathComponent, i)); if (model.isLeaf(lastPathComponent)) { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } else if (tree.isExpanded(childPath)) { result.addAll(collectExpandedPaths(tree, childPath)); } else { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } } } return result; }
private static void drawSelection(JTree tree, Graphics g, final int width) { int y = 0; final int[] rows = tree.getSelectionRows(); final int height = tree.getRowHeight(); for (int row : rows) { final TreeCellRenderer renderer = tree.getCellRenderer(); final Object value = tree.getPathForRow(row).getLastPathComponent(); if (value == null) continue; final Component component = renderer.getTreeCellRendererComponent(tree, value, false, false, false, row, false); if (component.getFont() == null) { component.setFont(tree.getFont()); } g.translate(0, y); component.setBounds(0, 0, width, height); boolean wasOpaque = false; if (component instanceof JComponent) { final JComponent j = (JComponent) component; if (j.isOpaque()) wasOpaque = true; j.setOpaque(false); } component.paint(g); if (wasOpaque) { ((JComponent) component).setOpaque(true); } y += height; g.translate(0, -y); } }
/** Construct a FileTree */ public FilePanel(JPanel panel, final IDEWindow ideWindow) { pane = panel; setLayout(new BorderLayout()); // Make a tree list with all the nodes, and make it a JTree JTree tree = new JTree(addNodes(null, new File(System.getProperty("user.dir")))); // Add a listener tree.addTreeSelectionListener( new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent(); File file = null; if (node.isLeaf()) { file = new File(node.getParent() + "/" + node); ToolbarFunctions.open(file, ideWindow); } } }); setSize(panel.getPreferredSize()); // Lastly, put the JTree into a JScrollPane. JScrollPane scrollpane = new JScrollPane(); scrollpane.getViewport().add(tree); add(BorderLayout.CENTER, scrollpane); }
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); if (!showRootNodeCheckBox && tree.getModel().getRoot() == value) return renderer; TreePath path = tree.getPathForRow(row); if (path != null) { if (checkBoxCustomer != null && !checkBoxCustomer.showCheckBox(path)) return renderer; if (selectionModel.isPathSelected(path, selectionModel.isDigged())) checkBox.getTristateModel().setState(TristateState.SELECTED); else checkBox .getTristateModel() .setState( selectionModel.isDigged() && selectionModel.isPartiallySelected(path) ? TristateState.INDETERMINATE : TristateState.DESELECTED); } removeAll(); add(checkBox, BorderLayout.WEST); add(renderer, BorderLayout.CENTER); return this; }
public ReplicatedTreeView(ReplicatedTree tree, Object title) throws Exception { this.tree = tree; tree.addReplicatedTreeListener(this); addNotify(); setTitle("ReplicatedTreeDemo: mbr=" + title); tree_model = new DefaultTreeModel(root); jtree = new JTree(tree_model); jtree.setDoubleBuffered(true); jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); JScrollPane scroll_pane = new JScrollPane(jtree); populateTree(); getContentPane().add(scroll_pane, BorderLayout.CENTER); addWindowListener(this); table_model.setColumnIdentifiers(new String[] {"Name", "Value"}); table_model.addTableModelListener(this); setTableColumnWidths(); tablePanel = new JPanel(); tablePanel.setLayout(new BorderLayout()); tablePanel.add(table.getTableHeader(), BorderLayout.NORTH); tablePanel.add(table, BorderLayout.CENTER); getContentPane().add(tablePanel, BorderLayout.SOUTH); jtree.addTreeSelectionListener(this); // REVISIT MouseListener ml = new MouseAdapter() { public void mouseClicked(MouseEvent e) { int selRow = jtree.getRowForLocation(e.getX(), e.getY()); TreePath selPath = jtree.getPathForLocation(e.getX(), e.getY()); if (selRow != -1) { selected_node = makeFQN(selPath.getPath()); jtree.setSelectionPath(selPath); if (e.getModifiers() == java.awt.event.InputEvent.BUTTON3_MASK) { operationsPopup.show(e.getComponent(), e.getX(), e.getY()); } } } }; jtree.addMouseListener(ml); createMenus(); setLocation(50, 50); setSize( getInsets().left + getInsets().right + 485, getInsets().top + getInsets().bottom + 367); init(); setVisible(true); }
public static ActionCallback selectFirstNode(final JTree tree) { final TreeModel model = tree.getModel(); final Object root = model.getRoot(); TreePath selectionPath = new TreePath(root); if (!tree.isRootVisible() && model.getChildCount(root) > 0) selectionPath = selectionPath.pathByAddingChild(model.getChild(root, 0)); return selectPath(tree, selectionPath); }
/** * Constructor. * * @param tree a JTree */ public CheckTreeManager(JTree tree) { this.tree = tree; selectionModel = new CheckTreeSelectionModel(tree.getModel()); tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel)); tree.addMouseListener(this); tree.addMouseMotionListener(this); selectionModel.addTreeSelectionListener(this); }
public void addSelectionPathTo(final Object element) { DefaultMutableTreeNode node = myAbstractTreeBuilder.getNodeForElement(element); if (node != null) { final JTree tree = getTree(); final TreePath path = new TreePath(node.getPath()); if (node == tree.getModel().getRoot() && !tree.isExpanded(path)) tree.expandPath(path); tree.addSelectionPath(path); } }
/** * Handles mouse moved events. * * @param e the mouse event */ public void mouseMoved(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) return; if (e.getX() > tree.getPathBounds(path).x + hotspot - 3 || e.getX() < tree.getPathBounds(path).x + 2) tree.setCursor(Cursor.getDefaultCursor()); else { tree.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } }
/** * Set the currently selected Variable. * * @param v select this Variable, must be already in the tree. */ public void setSelected(VariableIF v) { if (v == null) return; // construct chain of variables List<VariableIF> vchain = new ArrayList<>(); vchain.add(v); VariableIF vp = v; while (vp.isMemberOfStructure()) { vp = vp.getParentStructure(); vchain.add(0, vp); // reverse } // construct chain of groups List<Group> gchain = new ArrayList<>(); Group gp = vp.getParentGroup(); gchain.add(gp); while (gp.getParentGroup() != null) { gp = gp.getParentGroup(); gchain.add(0, gp); // reverse } List<Object> pathList = new ArrayList<>(); // start at root, work down through the nested groups, if any GroupNode gnode = (GroupNode) model.getRoot(); pathList.add(gnode); Group parentGroup = gchain.get(0); // always the root group for (int i = 1; i < gchain.size(); i++) { parentGroup = gchain.get(i); gnode = gnode.findNestedGroup(parentGroup); assert gnode != null; pathList.add(gnode); } vp = vchain.get(0); VariableNode vnode = gnode.findNestedVariable(vp); if (vnode == null) return; // not found pathList.add(vnode); // now work down through the structure members, if any for (int i = 1; i < vchain.size(); i++) { vp = vchain.get(i); vnode = vnode.findNestedVariable(vp); if (vnode == null) return; // not found pathList.add(vnode); } // convert to TreePath, and select it Object[] paths = pathList.toArray(); TreePath treePath = new TreePath(paths); tree.setSelectionPath(treePath); tree.scrollPathToVisible(treePath); }
public static ActionCallback selectPath(final JTree tree, final TreePath path, boolean center) { tree.makeVisible(path); if (center) { return showRowCentred(tree, tree.getRowForPath(path)); } else { final int row = tree.getRowForPath(path); return showAndSelect( tree, row - ListScrollingUtil.ROW_PADDING, row + ListScrollingUtil.ROW_PADDING, row, -1); } }
public void actionPerformed(ActionEvent event) { JTree tree = getTree(); TreePath path = tree.getSelectionPath(); if (path == null) { sheet.getLogger().warning("Warning: User must select a node to attach a new config to"); // XXX add a message telling users to select a node } else { createAttrConfigForNode((Node) path.getLastPathComponent()); } }
@Override public Font getFont() { Font font = super.getFont(); // Cell renderers could have no parent and no explicit set font. // Take tree font in this case. if (font != null) return font; JTree tree = getTree(); return tree != null ? tree.getFont() : null; }
private static void expandTree(final JTree tree) { int oldRowCount = 0; do { int rowCount = tree.getRowCount(); if (rowCount == oldRowCount) break; oldRowCount = rowCount; for (int i = 0; i < rowCount; i++) { tree.expandRow(i); } } while (true); }
private static int getVisibleRowCount(final JTree tree) { final Rectangle visible = tree.getVisibleRect(); int count = 0; for (int i = 0; i < tree.getRowCount(); i++) { final Rectangle bounds = tree.getRowBounds(i); if (visible.y <= bounds.y && visible.y + visible.height >= bounds.y + bounds.height) { count++; } } return count; }
public static ActionCallback selectInTree( DefaultMutableTreeNode node, boolean requestFocus, JTree tree, boolean center) { if (node == null) return new ActionCallback.Done(); final TreePath treePath = new TreePath(node.getPath()); tree.expandPath(treePath); if (requestFocus) { tree.requestFocus(); } return selectPath(tree, treePath, center); }
public static void moveSelectedRow(final JTree tree, final int direction) { final TreePath selectionPath = tree.getSelectionPath(); final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeNode.getParent(); final int idx = parent.getIndex(treeNode); parent.remove(treeNode); parent.insert(treeNode, idx + direction); ((DefaultTreeModel) tree.getModel()).reload(parent); selectNode(tree, treeNode); }
public static void dropSelectionButUnderPoint(JTree tree, Point treePoint) { final TreePath toRetain = tree.getPathForLocation(treePoint.x, treePoint.y); if (toRetain == null) return; TreePath[] selection = tree.getSelectionModel().getSelectionPaths(); selection = selection == null ? new TreePath[0] : selection; for (TreePath each : selection) { if (toRetain.equals(each)) continue; tree.getSelectionModel().removeSelectionPath(each); } }
private static void printImpl( JTree tree, Object root, Collection<String> strings, int level, boolean withSelection, @Nullable Condition<String> nodePrintCondition) { DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) root; final Object userObject = defaultMutableTreeNode.getUserObject(); String nodeText; if (userObject != null) { nodeText = toString(userObject, null); } else { nodeText = "null"; } if (nodePrintCondition != null && !nodePrintCondition.value(nodeText)) return; final StringBuilder buff = new StringBuilder(); StringUtil.repeatSymbol(buff, ' ', level); final boolean expanded = tree.isExpanded(new TreePath(defaultMutableTreeNode.getPath())); if (!defaultMutableTreeNode.isLeaf()) { buff.append(expanded ? "-" : "+"); } final boolean selected = tree.getSelectionModel().isPathSelected(new TreePath(defaultMutableTreeNode.getPath())); if (withSelection && selected) { buff.append("["); } buff.append(nodeText); if (withSelection && selected) { buff.append("]"); } strings.add(buff.toString()); int childCount = tree.getModel().getChildCount(root); if (expanded) { for (int i = 0; i < childCount; i++) { printImpl( tree, tree.getModel().getChild(root, i), strings, level + 1, withSelection, nodePrintCondition); } } }
private static int getFirstVisibleRow(final JTree tree) { final Rectangle visible = tree.getVisibleRect(); int row = -1; for (int i = 0; i < tree.getRowCount(); i++) { final Rectangle bounds = tree.getRowBounds(i); if (visible.y <= bounds.y && visible.y + visible.height >= bounds.y + bounds.height) { row = i; break; } } return row; }
public static void expandAll(final JTree tree) { tree.expandPath(new TreePath(tree.getModel().getRoot())); int oldRowCount = 0; do { int rowCount = tree.getRowCount(); if (rowCount == oldRowCount) break; oldRowCount = rowCount; for (int i = 0; i < rowCount; i++) { tree.expandRow(i); } } while (true); }
public static void unselect(JTree tree, final DefaultMutableTreeNode node) { final TreePath rootPath = new TreePath(node.getPath()); final TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths != null) { for (TreePath selectionPath : selectionPaths) { if (selectionPath.getPathCount() > rootPath.getPathCount() && rootPath.isDescendant(selectionPath)) { tree.removeSelectionPath(selectionPath); } } } }
private void init() { JPanel quickSearchPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); final TreeModel treeModel = explorer.getTreeModel(); final QuickTreeFilterField field = new QuickTreeFilterField(treeModel) { protected FilterableTreeModel createDisplayTreeModel(TreeModel treeModel) { return new FilterableTreeModel(treeModel) { @Override protected void configureListModelWrapper(ListModelWrapper wrapper, Object node) { if (node instanceof DefaultMutableTreeNode) { // ImageFileExplorer.LazyNode inode = (ImageFileExplorer.LazyNode) node; DefaultMutableTreeNode inode = (DefaultMutableTreeNode) node; boolean expanded = explorer.getJTree().isExpanded(new TreePath(inode.getPath())); if (inode.isLeaf() || expanded) { super.configureListModelWrapper(wrapper, node); } } } }; } }; field.setSearchingDelay(200); quickSearchPanel.add(field); JPanel treePanel = new JPanel(new BorderLayout(2, 2)); field.setTree(explorer.getJTree()); SearchableUtils.installSearchable(field.getTree()); JTree tree = field.getTree(); tree.setModel(field.getDisplayTreeModel()); DataTipManager.get().register(tree); treePanel.add(explorer.getComponent()); mainPanel.add(treePanel); mainPanel.add(quickSearchPanel, BorderLayout.BEFORE_FIRST_LINE); EventBus.subscribeStrongly( DataSourceStatusEvent.class, new EventSubscriber<DataSourceStatusEvent>() { @Override public void onEvent(DataSourceStatusEvent event) { explorer.getJTree().repaint(); } }); mainPanel.putClientProperty(IActionProvider.KEY, this); }
public static int getDepthOffset(JTree aTree) { if (aTree.isRootVisible()) { if (aTree.getShowsRootHandles()) { return 1; } else { return 0; } } else if (!aTree.getShowsRootHandles()) { return -1; } else { return 0; } }
private static int getTreeBaseline(JTree tree, int height) { int rowHeight = tree.getRowHeight(); if (TREE_LABEL == null) { TREE_LABEL = new JLabel("X"); TREE_LABEL.setIcon(UIManager.getIcon("Tree.closedIcon")); } JLabel label = TREE_LABEL; label.setFont(tree.getFont()); if (rowHeight <= 0) { rowHeight = label.getPreferredSize().height; } return getLabelBaseline(label, rowHeight) + tree.getInsets().top; }