javax.swing.tree.MutableTreeNode Java Examples

The following examples show how to use javax.swing.tree.MutableTreeNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlotConfigurationTreeModel.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
private void rangeAxisConfigRemoved(int index, RangeAxisConfig rangeAxis) {
	removeNodeFromParent((MutableTreeNode) root.getChildAt(index + NUMBER_OF_PERMANENT_DIMENSIONS));

	reload();
	plotConfigTree.expandAll();

	// Acquire new selection element
	int childCount = root.getChildCount();
	Object newSelection = null;

	if (childCount > NUMBER_OF_PERMANENT_DIMENSIONS) {
		newSelection = root.getChildAt(childCount - 1);
	} else {
		newSelection = root;
	}

	// change selection path
	TreePath path = new TreePath(getPathToRoot((TreeNode) newSelection));
	makeVisibleAndSelect(path);
}
 
Example #2
Source File: TribeNode.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
@Override
public void insert(MutableTreeNode child, int index) {
    super.insert(child, index);
    Collections.sort(this.children, new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            if (o1 instanceof TagNode) {
                return -1;
            }
            if (o2 instanceof TagNode) {
                return 1;
            }
            return o1.toString().compareToIgnoreCase(o2.toString());
        }
    });
}
 
Example #3
Source File: SortTreeHelper.java    From ISO8583 with GNU General Public License v3.0 6 votes vote down vote up
public static void sort(DefaultMutableTreeNode parent) {
	int n = parent.getChildCount();
	for (int i = 0; i < n - 1; i++) {
		int min = i;
		for (int j = i + 1; j < n; j++) {
			if (tnc.compare((DefaultMutableTreeNode) parent.getChildAt(min), (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
				min = j;
			}
		}
		if (i != min) {
			MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
			MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
			parent.insert(b, i);
			parent.insert(a, min);
			
			updateTree = true;
		}
	}
}
 
Example #4
Source File: SunJDKParser.java    From tda with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean checkForDuplicateThreadItem(Map directChildMap, DefaultMutableTreeNode node1) {
    ThreadInfo mi1 = (ThreadInfo) node1.getUserObject();
    String name1 = mi1.getName();

    for (Iterator iter2 = directChildMap.entrySet().iterator(); iter2.hasNext();) {
        DefaultMutableTreeNode node2 = (DefaultMutableTreeNode) ((Map.Entry) iter2.next()).getValue();
        if (node1 == node2) {
            continue;
        }

        ThreadInfo mi2 = (ThreadInfo) node2.getUserObject();
        if (name1.equals(mi2.getName()) && node2.getChildCount() > 0) {
            node1.add((MutableTreeNode) node2.getFirstChild());
            iter2.remove();
            return true;
        }
    }

    return false;
}
 
Example #5
Source File: StructureTreeModel.java    From nextreports-designer with Apache License 2.0 6 votes vote down vote up
public void removeNodeFromParent(MutableTreeNode node) {
    if (!filterIsActive) {
        super.removeNodeFromParent(node);
    } else {
        MutableTreeNode parent = (MutableTreeNode) node.getParent();
        if (parent == null)
            throw new IllegalArgumentException("node does not have a parent.");

        int[] childIndex = new int[1];
        Object[] removedArray = new Object[1];

        childIndex[0] = ((StructureTreeNode)parent).getIndex(node, filterIsActive);            
        if ((childIndex[0] != -1) && ((StructureTreeNode)node).isVisible()) {
            node.removeFromParent();
            removedArray[0] = node;
            nodesWereRemoved(parent, childIndex, removedArray);
        }
    }
}
 
Example #6
Source File: TreeUtil.java    From Zettelkasten with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method selects the first entry in a jTree that start with the text
 * that is entered in the filter-textfield.
 *
 * @param tree the jTree where the item should be selected
 * @param textfield the related filtertextfield that contains the user-input
 */
public static void selectByTyping(javax.swing.JTree tree, javax.swing.JTextField textfield) {
    DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel();
    MutableTreeNode root = (MutableTreeNode) dtm.getRoot();
    String text = textfield.getText().toLowerCase();
    if (root != null && !text.isEmpty()) {
        for (int cnt = 0; cnt < dtm.getChildCount(root); cnt++) {
            MutableTreeNode child = (MutableTreeNode) dtm.getChild(root, cnt);
            String childtext = child.toString().toLowerCase();
            if (childtext.startsWith(text)) {
                TreePath tp = new TreePath(root);
                tp = tp.pathByAddingChild(child);
                tree.setSelectionPath(tp);
                tree.scrollPathToVisible(tp);
                return;
            }
        }
    }
}
 
Example #7
Source File: WebTreeNode.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Removes {@code child} from its present parent (if it has a parent), sets the child's parent to this node,
 * and then adds the child to this node's child {@link List} at index {@code childIndex}.
 * {@code child} must not be {@code null} and must not be an ancestor of this node.
 *
 * @param child child node to insert under this node
 * @param index index in this node's child {@link List} where this node is to be inserted
 * @throws IllegalStateException          if this node does not allow children
 * @throws IllegalArgumentException       if {@code child} is null or is an ancestor of this node
 * @throws ArrayIndexOutOfBoundsException if {@code childIndex} is out of bounds
 * @see #isNodeDescendant(WebTreeNode)
 */
@Override
public void insert ( @NotNull final MutableTreeNode child, final int index )
{
    if ( !allowsChildren )
    {
        throw new IllegalStateException ( "Node does not allow children" );
    }
    if ( isNodeAncestor ( ( N ) child ) )
    {
        throw new IllegalArgumentException ( "New child is an ancestor" );
    }

    final N oldParent = ( N ) child.getParent ();
    if ( oldParent != null )
    {
        oldParent.remove ( child );
    }
    child.setParent ( this );
    if ( children == null )
    {
        children = new ArrayList<N> ();
    }
    children.add ( index, ( N ) child );
}
 
Example #8
Source File: HierarchyViewer.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private MutableTreeNode wrap(final Tree tree) {
	if (tree == null) {
		return null;
	}
	DefaultMutableTreeNode root = new DefaultMutableTreeNode(tree) {
		@Override
		public String toString() {
			return treeTextProvider.getText((Tree) getUserObject());
		}


	};

	for (int i = 0; i < tree.getChildCount(); i++) {
		root.add(wrap(tree.getChild(i)));
	}
	return root;
}
 
Example #9
Source File: GroupNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Implementation of javax.swing.tree.DefaultTreeModel#removeNodeFromParent(javax.swing.tree.MutableTreeNode) for multiple nodes.
 * Fires a single event, or does nothing when nodes is empty.
 *
 * @param treeModel to fire the treeNodesRemoved event on
 * @param parent    the parent
 * @param nodes     must all be children of parent
 */
private static void removeNodesFromParent(@Nonnull DefaultTreeModel treeModel, @Nonnull GroupNode parent, @Nonnull List<? extends MutableTreeNode> nodes) {
  int count = nodes.size();
  if (count == 0) {
    return;
  }
  ObjectIntHashMap<MutableTreeNode> ordering = new ObjectIntHashMap<>(count);
  for (MutableTreeNode node : nodes) {
    ordering.put(node, parent.getIndex(node));
  }
  Collections.sort(nodes, Comparator.comparingInt(ordering::get)); // need ascending order
  int[] indices = ordering.getValues();
  Arrays.sort(indices);
  for (int i = count - 1; i >= 0; i--) {
    parent.remove(indices[i]);
  }
  treeModel.nodesWereRemoved(parent, indices, nodes.toArray());
}
 
Example #10
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static void sort2(DefaultMutableTreeNode parent) {
  int n = parent.getChildCount();
  for (int i = 0; i < n - 1; i++) {
    int min = i;
    for (int j = i + 1; j < n; j++) {
      if (tnc.compare((DefaultMutableTreeNode) parent.getChildAt(min),
          (DefaultMutableTreeNode) parent.getChildAt(j)) > 0) {
        min = j;
      }
    }
    if (i != min) {
      SWAP_COUNTER.getAndIncrement();
      MutableTreeNode a = (MutableTreeNode) parent.getChildAt(i);
      MutableTreeNode b = (MutableTreeNode) parent.getChildAt(min);
      parent.insert(b, i);
      parent.insert(a, min);
      // MutableTreeNode node = (MutableTreeNode) parent.getChildAt(min);
      // parent.insert(node, i);
      // COMPARE_COUNTER++;
    }
  }
}
 
Example #11
Source File: JSONTree.java    From rest-client with Apache License 2.0 6 votes vote down vote up
/**
* 
* @Title      : removeCurrentNode 
* @Description: Remove the currently selected node. 
* @Param      :  
* @Return     : void
* @Throws     :
 */
public void removeCurrentNode()
{
    TreePath currentSelection = tree.getSelectionPath();
    if (null == currentSelection)
    {
        return;
    }

    CheckBoxTreeNode currentNode = (CheckBoxTreeNode) (currentSelection.getLastPathComponent());
    MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
    if (null != parent)
    {
        treeModel.removeNodeFromParent(currentNode);
        return;
    }

    // Either there was no selection, or the root was selected.
    toolkit.beep();
}
 
Example #12
Source File: CompanionInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void setParent(MutableTreeNode newParent)
{
	super.setParent(newParent);
	if (newParent == null && children != null)
	{
		for (int i = 0; i < getChildCount(); i++)
		{
			CompanionNode child = (CompanionNode) getChildAt(i);
			child.companion.getNameRef().removeReferenceListener(this);
		}
	}
}
 
Example #13
Source File: CatalogTreeUtils.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public static void appendCatalogNode(MutableTreeNode parentNode, DefaultTreeModel treeModel, InvCatalogRef catalogRef) {
    final DefaultMutableTreeNode catalogNode = new DefaultMutableTreeNode(catalogRef.getName());
    final String catalogPath = catalogRef.getURI().toASCIIString();
    final CatalogNode opendapNode = new CatalogNode(catalogPath, catalogRef);
    opendapNode.setCatalogUri(catalogPath);
    catalogNode.add(new DefaultMutableTreeNode(opendapNode));
    treeModel.insertNodeInto(catalogNode, parentNode, parentNode.getChildCount());
}
 
Example #14
Source File: AllyNode.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
@Override
public void insert(MutableTreeNode child, int index) {
    super.insert(child, index);
    Collections.sort(this.children, new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            return o1.toString().compareToIgnoreCase(o2.toString());
        }
    });
    //recalc elem count
}
 
Example #15
Source File: RefElementNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void add(MutableTreeNode newChild) {
  super.add(newChild);
  if (newChild instanceof ProblemDescriptionNode) {
    myHasDescriptorsUnder = true;
  }
}
 
Example #16
Source File: ExcitationEditorJPanel.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
/**
 * The sole place to add excitations to the window. It handles both the panel and the tree
 * Also makes sure that preferences for base shap display are observed.
 */
public void addExcitation(final DefaultMutableTreeNode columnNode, final Control nextControl, final int colIndex) {
    // Build an array of OpenSim::Functions for value, min, max
    Vector<XYFunctionInterface> functions = new Vector<XYFunctionInterface>(3);
    ControlLinear control = ControlLinear.safeDownCast(nextControl);
    // Create a panel to hold the control/min/max and return the array of underlying OpenSim::Functions
    ExcitationPanel nextExcitationPanel = createPanel(control, functions); 
    // Set some parameters of the renderer
    //.showBaseShape(0, ((JCheckBox)evt.getSource()).isSelected());
    nextExcitationPanel.showBaseShape(0, ShowExcBaseShapeCheckBox2.isSelected());
    nextExcitationPanel.showBaseShape(1, ShowMinBaseShapeCheckBox2.isSelected());
    nextExcitationPanel.showBaseShape(2, ShowMaxBaseShapeCheckBox2.isSelected());
    // commented out: sample usage of MIN_MAX_EXC shading mode
    //renderer.setFillMode(ExcitationFillMode.MIN_MAX_EXC);
    //renderer.setMaxFillPaint(new Color(150, 150, 250));
    //renderer.setExcFillPaint(new Color(150, 150, 150));
    //renderer.setMinFillPaint(new Color(250, 150, 150));
    ExcitationRenderer renderer = (ExcitationRenderer)nextExcitationPanel.getRenderer();
    if (MinMaxShadingCheckBox2.isSelected())
       renderer.setFillMode(ExcitationFillMode.MIN_MAX);
    else
       renderer.setFillMode(ExcitationFillMode.NONE);
    renderer.setMinMaxFillPaint(new Color(220, 220, 220));
    // Handle addition to the tree
    ExcitationObject excitationNode= new ExcitationObject(nextExcitationPanel, nextControl.getName());
    excitationNode.setUserObject(excitationNode);
    treeModel.insertNodeInto((MutableTreeNode)excitationNode, (MutableTreeNode)columnNode, columnNode.getChildCount());
    jExcitationsTree.scrollPathToVisible(new TreePath(excitationNode.getPath()));
    // Handle addition to the panel of excitations
    Vector<XYFunctionInterface> xyFunctions = new Vector<XYFunctionInterface>(3);
    xyFunctions.add(functions.get(0));
    xyFunctions.add(functions.get(1));
    xyFunctions.add(functions.get(2));
    getExcitationGridPanel().addExcitationPanel(colIndex, nextExcitationPanel, control, xyFunctions);
}
 
Example #17
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override protected void exportDone(JComponent src, Transferable data, int action) {
  if (action == TransferHandler.MOVE && src instanceof JTree) {
    JTree tree = (JTree) src;
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    TreePath[] selectionPaths = tree.getSelectionPaths();
    if (selectionPaths != null) {
      for (TreePath path : selectionPaths) {
        model.removeNodeFromParent((MutableTreeNode) path.getLastPathComponent());
      }
    }
  }
}
 
Example #18
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override protected void exportDone(JComponent src, Transferable data, int action) {
  if (action == TransferHandler.MOVE && src instanceof JTree) {
    JTree tree = (JTree) src;
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    TreePath[] selectionPaths = tree.getSelectionPaths();
    if (selectionPaths != null) {
      for (TreePath path : selectionPaths) {
        model.removeNodeFromParent((MutableTreeNode) path.getLastPathComponent());
      }
    }
  }
}
 
Example #19
Source File: ExTreeModel.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Inserts new child node into parent node at the specified index.
 * This method might be used to manually change tree node children without causing any structure corruptions.
 *
 * @param child  new child node
 * @param parent parent node
 * @param index  insert index
 */
@Override
public void insertNodeInto ( @NotNull final MutableTreeNode child, @NotNull final MutableTreeNode parent, final int index )
{
    // Event Dispatch Thread check
    WebLookAndFeel.checkEventDispatchThread ();

    // Ensure model is installed
    checkInstalled ();

    final N childNode = ( N ) child;
    final N parentNode = ( N ) parent;

    // Caching node
    addRawChild ( parentNode, childNode, index );
    cacheNodeById ( childNode );
    cacheParentId ( childNode, parentNode.getId () );

    // Clearing nodes children caches
    // That might be required in case nodes were moved inside of the tree
    clearRawChildren ( childNode, false );

    // Inserting node
    super.insertNodeInto ( childNode, parentNode, Math.min ( index, parentNode.getChildCount () ) );

    // Loading data for newly added node
    loadTreeData ( childNode );

    // Updating parent node sorting and filtering
    filterAndSort ( parentNode, false );
}
 
Example #20
Source File: GrammarChapNode.java    From PolyGlot with MIT License 5 votes vote down vote up
public void doInsert(MutableTreeNode node, int index) {
    super.insert(node, index);
    
    if (parentManager != null && node instanceof GrammarChapNode) {
        parentManager.addChapterAtIndex((GrammarChapNode)node, index);
    }
}
 
Example #21
Source File: PackageDependenciesNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void add(MutableTreeNode newChild) {
  super.add(newChild);
  boolean hasUnmarked = ((PackageDependenciesNode)newChild).hasUnmarked();
  boolean hasMarked = ((PackageDependenciesNode)newChild).hasMarked();
  updateMarked(hasUnmarked, hasMarked);
}
 
Example #22
Source File: ActivitySequenceEditor.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void insert(MutableTreeNode newChild, int childIndex) {
  super.insert(newChild, childIndex);
  if (!initializing) {
    ActivitySequenceElementEditor asee = (ActivitySequenceElementEditor) newChild;
    getActivitySequence().insertElementAt(asee.getActivitySequenceElement(), childIndex);
    if (tagList != null) {
      String tag = asee.getTag();
      if (tag != null && getTag(tag) == null) {
        tagList.addElement(tag);
      }
    }
  }
}
 
Example #23
Source File: WebTreeModel.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void removeNodeFromParent ( @NotNull final MutableTreeNode node )
{
    // Removing nodes and collecting information on the operation
    final N parent = ( N ) node.getParent ();
    if ( parent == null )
    {
        throw new IllegalArgumentException ( "Removed node does not have a parent" );
    }
    final int index = parent.getIndex ( node );
    parent.remove ( index );

    // Firing nodes removal
    nodesWereRemoved ( parent, new int[]{ index }, new Object[]{ node } );
}
 
Example #24
Source File: TagNode.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
@Override
public void insert(MutableTreeNode child, int index) {
    super.insert(child, index);
    Collections.sort(this.children, new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            return o1.toString().compareToIgnoreCase(o2.toString());
        }
    });
    //recalc elem count
}
 
Example #25
Source File: CompanionInfoTab.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void keyRemoved(MapEvent<String, Integer> e)
{
	int index = types.indexOf(e.getKey());
	types.remove(index);
	removeNodeFromParent((MutableTreeNode) getChildAt(index));
}
 
Example #26
Source File: GroupNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
int removeUsagesBulk(@Nonnull Set<UsageNode> usages, @Nonnull DefaultTreeModel treeModel) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  int removed = 0;
  synchronized (this) {
    List<MutableTreeNode> removedNodes = new SmartList<>();
    for (UsageNode usage : usages) {
      if (myChildren.remove(usage)) {
        removedNodes.add(usage);
        removed++;
      }
    }

    if (removed == 0) {
      for (GroupNode groupNode : getSubGroups()) {
        int delta = groupNode.removeUsagesBulk(usages, treeModel);
        if (delta > 0) {
          if (groupNode.getRecursiveUsageCount() == 0) {
            myChildren.remove(groupNode);
            removedNodes.add(groupNode);
          }
          removed += delta;
          if (removed == usages.size()) break;
        }
      }
    }
    if (!myChildren.isEmpty()) {
      removeNodesFromParent(treeModel, this, removedNodes);
    }
  }

  if (removed > 0) {
    myRecursiveUsageCount -= removed;
    if (myRecursiveUsageCount != 0) {
      treeModel.nodeChanged(this);
    }
  }

  return removed;
}
 
Example #27
Source File: LazyLoadingTree.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
private void setChildren(List<MyNode> children) {
	removeAllChildren();
	setAllowsChildren(!children.isEmpty());
	for (MutableTreeNode node : children) {
		add(node);
	}
}
 
Example #28
Source File: TreeEventMethodsImpl.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Shortcut method for double-click mouse event on specific tree node with condition.
 *
 * @param tree      tree to handle events for
 * @param condition node condition
 * @param runnable  tree node event runnable
 * @param <N>       {@link MutableTreeNode} type
 * @return used mouse adapter
 */
@NotNull
public static <N extends MutableTreeNode> MouseAdapter onNodeDoubleClick ( @NotNull final WebTree<N> tree,
                                                                           @Nullable final Predicate<N> condition,
                                                                           @NotNull final TreeNodeEventRunnable<N> runnable )
{
    final MouseAdapter mouseAdapter = new MouseAdapter ()
    {
        @Override
        public void mouseClicked ( @NotNull final MouseEvent e )
        {
            if ( SwingUtils.isDoubleClick ( e ) )
            {
                final int row = tree.getUI ().getExactRowForLocation ( e.getPoint () );
                if ( row != -1 )
                {
                    final N node = tree.getNodeForRow ( row );
                    if ( node != null && ( condition == null || condition.test ( node ) ) )
                    {
                        runnable.run ( node );
                    }
                }
            }
        }
    };
    tree.addMouseListener ( mouseAdapter );
    return mouseAdapter;
}
 
Example #29
Source File: PToDoTreeModel.java    From PolyGlot with MIT License 5 votes vote down vote up
@Override
public void removeNodeFromParent(MutableTreeNode node) {
    if (node instanceof ToDoTreeNode) {
        ToDoTreeNode toNode = (ToDoTreeNode)node;
        Object userObject = toNode.getUserObject();
        if (userObject instanceof ToDoNode) {
            ToDoNode userNode = (ToDoNode) userObject;
            userNode.deleteFromParent();
        }
    }
    
    super.removeNodeFromParent(node);
}
 
Example #30
Source File: PlotConfigurationTreeModel.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
private void valueSourcesCleared(RangeAxisConfig source) {
	RangeAxisConfigTreeNode rangeAxisNode = getRangeAxisTreeNode(source);
	if (rangeAxisNode != null) {
		int childCount = rangeAxisNode.getChildCount();
		for (int i = 0; i < childCount; ++i) {
			removeNodeFromParent((MutableTreeNode) rangeAxisNode.getChildAt(i));
		}

		// change selection path
		TreePath path = new TreePath(getPathToRoot(rangeAxisNode));
		makeVisibleAndSelect(path);
	} else {
		throw new RuntimeException("RangeAxisConfig source is not present in TreeModel. This should not happen.");
	}
}