/** Gets the value for the given node at the given column. */ public Object getValueAt(Object node, int col) { DefaultMutableTreeNode tree_node = (DefaultMutableTreeNode) node; Object value = tree_node.getUserObject(); switch (col) { // Name case 0: return value; // Value case 1: if (value instanceof ConfigElement) { return null; } else if (value instanceof PropertyDefinition) { // Only provide comma delimited editing for simple types if (((PropertyDefinition) value).getType() != ConfigElement.class) { StringBuffer buffer = new StringBuffer(); for (Enumeration e = tree_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode child_node = (DefaultMutableTreeNode) e.nextElement(); buffer.append(child_node.getUserObject()); if (e.hasMoreElements()) { buffer.append(", "); } } return buffer.toString(); } return null; } return value; } return null; }
/** * This gets called whenever one of the ConfigElements we are editing removes a property value. */ public void propertyValueRemoved(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Get the node containing the property description under the source // ConfigElement node for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { // The newly removed property value must be a child to this node System.out.println("Removing child " + idx + " from node: " + node.getUserObject()); DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(idx); // If the child is an embedded element, stop listening to it if (child.getUserObject() instanceof ConfigElement) { ConfigElement removed_elt = (ConfigElement) child.getUserObject(); removed_elt.removeConfigElementListener(this); } // Physically remove the child from the tree removeNodeFromParent(child); } } }
@Nullable private static String getActionId(DefaultMutableTreeNode node) { return (String) (node.getUserObject() instanceof String ? node.getUserObject() : node.getUserObject() instanceof Pair ? ((Pair) node.getUserObject()).first : null); }
@Override public String convertValueToText( Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { String s = super.convertValueToText(value, selected, expanded, leaf, row, hasFocus); String newProp = jEdit.getProperty(HIGHLIGHT_PROP); if (newProp == null || newProp.isEmpty()) return s; DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; while (node != null && !(node.getUserObject() instanceof HyperSearchOperationNode)) { node = (DefaultMutableTreeNode) node.getParent(); } if (node == null) return s; if (!newProp.equals(prop)) { prop = newProp; Font f = (resultTree != null) ? resultTree.getFont() : UIManager.getFont("Tree.font"); styleTag = HtmlUtilities.style2html(prop, f); } SearchMatcher matcher = ((HyperSearchOperationNode) node.getUserObject()).getSearchMatcher(); int i = s.indexOf(": "); if (i > 0) i += 2; else i = 0; Match m; List<Integer> matches = new ArrayList<Integer>(); while ((m = matcher.nextMatch(s.substring(i), true, true, true, false)) != null) { matches.add(i + m.start); matches.add(i + m.end); i += m.end; } return HtmlUtilities.highlightString(s, styleTag, matches); }
private void goToSelectedNode(int mode) { TreePath path = resultTree.getSelectionPath(); if (path == null) return; DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); Object value = node.getUserObject(); // do nothing if clicked "foo (showing n occurrences in m files)" if (node.getParent() != resultTreeRoot && value instanceof HyperSearchNode) { HyperSearchNode n = (HyperSearchNode) value; Buffer buffer = n.getBuffer(view); if (buffer == null) return; EditPane pane; switch (mode) { case M_OPEN: pane = view.goToBuffer(buffer); break; case M_OPEN_NEW_VIEW: pane = jEdit.newView(view, buffer, false).getEditPane(); break; case M_OPEN_NEW_PLAIN_VIEW: pane = jEdit.newView(view, buffer, true).getEditPane(); break; case M_OPEN_NEW_SPLIT: pane = view.splitHorizontally(); break; default: throw new IllegalArgumentException("Bad mode: " + mode); } n.goTo(pane); } } // }}}
private void doSave() { ObjectOutputStream objectStream = getObjectOutputStream(); if (objectStream != null) { try { System.out.println("Saving " + selectedChildrenPaths.size() + " Selected Generations..."); for (int i = 0; i < selectedChildrenPaths.size(); i++) { // Get the userObject at the supplied path Object selectedPath = ((TreePath) selectedChildrenPaths.elementAt(i)).getLastPathComponent(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectedPath; objectStream.writeObject(selectedNode.getUserObject()); } objectStream.close(); System.out.println("Save completed successfully."); } catch (IOException e) { System.err.println(e); } } else { System.out.println("Save Selected Files has been aborted!"); } }
/** * This gets called whenever one of the ConfigElements we are editing adds a new property value. */ public void propertyValueAdded(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Get the node containing the property description under the source // ConfigElement node for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { // The newly inserted property value must be added as a child to // this node if (prop_def.getType() != ConfigElement.class) { DefaultMutableTreeNode new_child = new DefaultMutableTreeNode(evt.getValue()); insertNodeInto(new_child, node, idx); } else { // Embedded elements are handled specially in that all of their // respective child properties and such also need to be added to // the tree at this time. addEmbeddedElement(node, (ConfigElement) evt.getValue(), idx); } } } }
public DefaultMutableTreeNode findNode(DefaultMutableTreeNode parent, String match) { if (parent == null) return null; // Commented by Balan on 15/03/03 // String treename = (String)parent.getUserObject (); // Comment Ends // Added by Balan on 15/03/03 String treename = "" + ((Hashtable) parent.getUserObject()).get("TREE-NAME"); // Add Ends if (treename != null) { if (treename.equals(match)) return parent; } if (frame.model.isLeaf(parent)) return null; Enumeration en = parent.children(); if ((en == null) || (!en.hasMoreElements())) return null; for (; en.hasMoreElements(); ) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) en.nextElement(); DefaultMutableTreeNode returnNode = findNode(child, match); if (returnNode != null) return returnNode; } return null; }
@Override public void actionPerformed(ActionEvent evt) { JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) evt.getSource(); boolean curState = menuItem.isSelected(); TreePath path = resultTree.getSelectionPath(); DefaultMutableTreeNode operNode = (DefaultMutableTreeNode) path.getLastPathComponent(); HyperSearchOperationNode operNodeObj = (HyperSearchOperationNode) operNode.getUserObject(); if (curState) operNodeObj.cacheResultNodes(operNode); operNode.removeAllChildren(); if (curState) { Exception excp = null; try { operNodeObj.insertTreeNodes(resultTree, operNode); } catch (Exception ex) { operNodeObj.restoreFlatNodes(resultTree, operNode); menuItem.setSelected(false); excp = ex; } finally { ((DefaultTreeModel) resultTree.getModel()).nodeStructureChanged(operNode); expandAllNodes(operNode); resultTree.scrollPathToVisible(new TreePath(operNode.getPath())); } if (excp != null) throw new RuntimeException(excp); } else operNodeObj.restoreFlatNodes(resultTree, operNode); operNodeObj.setTreeViewDisplayed(menuItem.isSelected()); }
/** * Called when the selection changed in the tree. Loads the selected certificate. * * @param e the event */ private void valueChangedPerformed(TreeSelectionEvent e) { Object o = e.getNewLeadSelectionPath().getLastPathComponent(); if (o instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) o; infoTextPane.setText(toString(node.getUserObject())); } }
// {{{ getTreeCellRendererComponent() method @Override protected void configureTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { setIcon(null); DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; if (node.getUserObject() instanceof HyperSearchOperationNode) { setFont(boldFont); CountNodes countNodes = new CountNodes(); traverseNodes(node, countNodes); setText( jEdit.getProperty( "hypersearch-results.result-caption", new Object[] { node.toString(), Integer.valueOf(countNodes.resultCount), Integer.valueOf(countNodes.bufferCount) })); } else if (node.getUserObject() instanceof HyperSearchFolderNode) { setFont(plainFont); setText(node.toString() + " (" + node.getChildCount() + " files/folders)"); } else if (node.getUserObject() instanceof HyperSearchFileNode) { // file name setFont(boldFont); HyperSearchFileNode hyperSearchFileNode = (HyperSearchFileNode) node.getUserObject(); setText( jEdit.getProperty( "hypersearch-results.file-caption", new Object[] { hyperSearchFileNode, Integer.valueOf(hyperSearchFileNode.getCount()), Integer.valueOf(node.getChildCount()) })); } else { setFont(plainFont); } } // }}}
@Nullable public static TreeNode findNodeWithObject( final Object object, final TreeModel model, final Object parent) { for (int i = 0; i < model.getChildCount(parent); i++) { final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) model.getChild(parent, i); if (childNode.getUserObject().equals(object)) return childNode; } return null; }
@Override public boolean processNode(DefaultMutableTreeNode node) { Object userObject = node.getUserObject(); if (userObject instanceof HyperSearchFileNode) { resultCount += ((HyperSearchFileNode) userObject).getCount(); bufferCount++; } return true; }
@Nullable private static Navigatable getNavigatableForNode(@NotNull DefaultMutableTreeNode node) { Object userObject = node.getUserObject(); if (userObject instanceof Navigatable) { final Navigatable navigatable = (Navigatable) userObject; return navigatable.canNavigate() ? navigatable : null; } return null; }
@Nullable private static TreePath findNodePath(MavenArchetype object, TreeModel model, Object parent) { for (int i = 0; i < model.getChildCount(parent); i++) { DefaultMutableTreeNode each = (DefaultMutableTreeNode) model.getChild(parent, i); if (each.getUserObject().equals(object)) return new TreePath(each.getPath()); TreePath result = findNodePath(object, model, each); if (result != null) return result; } return null; }
public void editSelected() { TreePath selected = tree.getSelectionPath(); if (selected != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) selected.getLastPathComponent(); Object obj = node.getUserObject(); if (obj instanceof CavityDBObject) { CavityDBObject dbObj = (CavityDBObject) obj; new ObjectEditingFrame(new ObjectEditingPanel(dbObj)); } } }
/** * Update the layouts tree. * * @param current The name of the current layout or <CODE>null</CODE> if none. */ public void updateLayouts(Path current) throws PipelineException { DefaultMutableTreeNode root = null; { root = new DefaultMutableTreeNode(new TreeData(), true); { Path path = new Path(PackageInfo.getSettingsPath(), "layouts"); rebuildTreeModel(path, new Path("/"), root); } DefaultTreeModel model = (DefaultTreeModel) pTree.getModel(); model.setRoot(root); { Enumeration e = root.depthFirstEnumeration(); if (e != null) { while (e.hasMoreElements()) { DefaultMutableTreeNode tnode = (DefaultMutableTreeNode) e.nextElement(); pTree.expandPath(new TreePath(tnode.getPath())); } } } } pTree.clearSelection(); if (current != null) { TreePath tpath = null; DefaultMutableTreeNode tnode = root; for (String comp : current.getComponents()) { DefaultMutableTreeNode next = null; Enumeration e = tnode.children(); if (e != null) { while (e.hasMoreElements()) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement(); TreeData data = (TreeData) child.getUserObject(); if (data.toString().equals(comp)) { tpath = new TreePath(child.getPath()); next = child; break; } } } if (next == null) break; tnode = next; } if (tpath != null) { pTree.setSelectionPath(tpath); pTree.makeVisible(tpath); } } }
/** Clears the data in the model. */ private void clear() { // Stop listening to all config elements in the tree List nodes = getNodesOfClass(ConfigElement.class); for (Iterator itr = nodes.iterator(); itr.hasNext(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) itr.next(); ConfigElement elt = (ConfigElement) node.getUserObject(); elt.removeConfigElementListener(mElementListener); } // Clear out all the old nodes from the tree. ((DefaultMutableTreeNode) getRoot()).removeAllChildren(); }
@Override public void actionPerformed(ActionEvent evt) { TreePath path = resultTree.getSelectionPath(); DefaultMutableTreeNode operNode = (DefaultMutableTreeNode) path.getLastPathComponent(); HyperSearchFolderNode nodeObj = (HyperSearchFolderNode) operNode.getUserObject(); String glob = "*"; SearchFileSet dirList = SearchAndReplace.getSearchFileSet(); if (dirList instanceof DirectoryListSet) glob = ((DirectoryListSet) dirList).getFileFilter(); SearchAndReplace.setSearchFileSet( new DirectoryListSet(nodeObj.getNodeFile().getAbsolutePath(), glob, true)); SearchDialog.showSearchDialog(view, null, SearchDialog.DIRECTORY); }
@Override public boolean processNode(DefaultMutableTreeNode node) { Object userObject = node.getUserObject(); if (userObject instanceof HyperSearchFileNode) nodesString.append(((HyperSearchFileNode) userObject).path); else if (userObject instanceof HyperSearchResult) { HyperSearchResult hsr = (HyperSearchResult) userObject; // Copy the ORIGINAL line from the buffer! nodesString.append(hsr.buffer == null ? hsr.toString() : hsr.buffer.getLineText(hsr.line)); } else nodesString.append(userObject.toString()); nodesString.append('\n'); return true; }
protected boolean doSetIcon( DefaultMutableTreeNode node, @Nullable String path, Component component) { if (StringUtil.isNotEmpty(path) && !new File(path).isFile()) { Messages.showErrorDialog( component, IdeBundle.message("error.file.not.found.message", path), IdeBundle.message("title.choose.action.icon")); return false; } String actionId = getActionId(node); if (actionId == null) return false; final AnAction action = ActionManager.getInstance().getAction(actionId); if (action != null && action.getTemplatePresentation() != null) { if (StringUtil.isNotEmpty(path)) { Image image = null; try { image = ImageLoader.loadFromStream( VfsUtil.convertToURL(VfsUtil.pathToUrl(path.replace(File.separatorChar, '/'))) .openStream()); } catch (IOException e) { LOG.debug(e); } Icon icon = new File(path).exists() ? IconLoader.getIcon(image) : null; if (icon != null) { if (icon.getIconWidth() > EmptyIcon.ICON_18.getIconWidth() || icon.getIconHeight() > EmptyIcon.ICON_18.getIconHeight()) { Messages.showErrorDialog( component, IdeBundle.message("custom.icon.validation.message"), IdeBundle.message("title.choose.action.icon")); return false; } node.setUserObject(Pair.create(actionId, icon)); mySelectedSchema.addIconCustomization(actionId, path); } } else { node.setUserObject(Pair.create(actionId, null)); mySelectedSchema.removeIconCustomization(actionId); final DefaultMutableTreeNode nodeOnToolbar = findNodeOnToolbar(actionId); if (nodeOnToolbar != null) { editToolbarIcon(actionId, nodeOnToolbar); node.setUserObject(nodeOnToolbar.getUserObject()); } } return true; } return false; }
/** Indicates whether the value for the given node at the given column is editable. */ public boolean isCellEditable(Object node, int col) { // The tree column has to be editable so it can get the events if (col == 0) { return true; } DefaultMutableTreeNode tree_node = (DefaultMutableTreeNode) node; Object value = tree_node.getUserObject(); // config elements nodes are not editable if (value instanceof ConfigElement) { return false; } else { return true; } }
@Nullable public Set<Object> getTreeSelectedActionIds() { TreePath[] paths = myTree.getSelectionPaths(); if (paths == null) return null; Set<Object> actions = new HashSet<Object>(); for (TreePath path : paths) { Object node = path.getLastPathComponent(); if (node instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode defNode = (DefaultMutableTreeNode) node; Object userObject = defNode.getUserObject(); actions.add(userObject); } } return actions; }
@Nullable public static DefaultMutableTreeNode findNodeWithObject( final DefaultMutableTreeNode aRoot, final Object aObject) { if (Comparing.equal(aRoot.getUserObject(), aObject)) { return aRoot; } else { for (int i = 0; i < aRoot.getChildCount(); i++) { final DefaultMutableTreeNode candidate = findNodeWithObject((DefaultMutableTreeNode) aRoot.getChildAt(i), aObject); if (null != candidate) { return candidate; } } return null; } }
/** * Gets a list of the nodes in this model that contain an object of the given class starting with * the given node. * * @param cls the class to search for * @param node the node whose subtree will be searched * @return a list of matching DefaultMutableTreeNodes */ public List getNodesOfClass(Class cls, DefaultMutableTreeNode node) { List matches = new ArrayList(); // Check if the current node matches if (cls.isInstance(node.getUserObject())) { matches.add(node); } // Recurse to the children of the current node for (Enumeration e = node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement(); matches.addAll(getNodesOfClass(cls, child)); } return matches; }
@NotNull public static <T> List<T> collectSelectedObjectsOfType(JTree tree, Class<T> clazz) { final TreePath[] selections = tree.getSelectionPaths(); if (selections != null) { final ArrayList<T> result = new ArrayList<T>(); for (TreePath selection : selections) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selection.getLastPathComponent(); final Object userObject = node.getUserObject(); if (clazz.isInstance(userObject)) { //noinspection unchecked result.add((T) userObject); } } return result; } return Collections.emptyList(); }
/** * This gets called whenever one of the ConfigElements we are editing has the values of a * property get reordered. */ public void propertyValueOrderChanged(ConfigElementEvent evt) { ConfigElement src = (ConfigElement) evt.getSource(); int idx = evt.getIndex(); PropertyDefinition prop_def = src.getDefinition().getPropertyDefinition(evt.getProperty()); DefaultMutableTreeNode elt_node = getNodeFor(src); // Get the node containing the property description under the source // ConfigElement node. for (Enumeration e = elt_node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.getUserObject().equals(prop_def)) { int start_index = Math.min(evt.getIndex0(), evt.getIndex1()); int end_index = Math.max(evt.getIndex0(), evt.getIndex1()); List removed_children = new ArrayList(); for (int c = start_index; c <= end_index; ++c) { removed_children.add(getChild(node, c)); } for (Iterator c = removed_children.iterator(); c.hasNext(); ) { removeNodeFromParent((MutableTreeNode) c.next()); } String prop_token = prop_def.getToken(); for (int v = start_index; v <= end_index; ++v) { if (prop_def.getType() != ConfigElement.class) { // Create a new node for the reordered property value. DefaultMutableTreeNode new_node = new DefaultMutableTreeNode(src.getProperty(prop_token, v)); // Add the new node into the tree. insertNodeInto(new_node, node, v); } else { // Embedded elements are handled specially in that all of // their respective child properties and such also need to // be added to the tree at this time. ConfigElement cur_value = (ConfigElement) src.getProperty(prop_token, v); addEmbeddedElement(node, cur_value, v); } } } } }
protected void doOKAction() { if (myNode != null) { if (!doSetIcon(myNode, myTextField.getText(), getContentPane())) { return; } final Object userObject = myNode.getUserObject(); if (userObject instanceof Pair) { String actionId = (String) ((Pair) userObject).first; final AnAction action = ActionManager.getInstance().getAction(actionId); final Icon icon = (Icon) ((Pair) userObject).second; action.getTemplatePresentation().setIcon(icon); action.setDefaultIcon(icon == null); editToolbarIcon(actionId, myNode); } myActionsTree.repaint(); } setCustomizationSchemaForCurrentProjects(); super.doOKAction(); }
/** Gets the node for the given object */ private DefaultMutableTreeNode getNodeFor(Object obj, DefaultMutableTreeNode node) { // System.out.println("getNodeFor() node: " + node.getUserObject()); // Check if we found a match if (obj.equals(node.getUserObject())) { return node; } // Check all children of the current node for (Enumeration e = node.children(); e.hasMoreElements(); ) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement(); DefaultMutableTreeNode result = getNodeFor(obj, child); if (result != null) { return result; } } // Didn't find anything :( return null; }
private void doSaveAll() { ObjectOutputStream objectStream = getObjectOutputStream(); if (objectStream != null) { try { System.out.println("Saving All " + generations.getLeafCount() + " Generations..."); for (Enumeration e = generations.depthFirstEnumeration(); e.hasMoreElements(); ) { DefaultMutableTreeNode tmpNode = (DefaultMutableTreeNode) e.nextElement(); if (tmpNode.isLeaf()) objectStream.writeObject(tmpNode.getUserObject()); } objectStream.close(); System.out.println("Save completed successfully."); } catch (IOException e) { System.err.println(e); } } else { System.out.println("Save All Files has been aborted!"); } }