Java Code Examples for javax.swing.tree.DefaultTreeModel#insertNodeInto()

The following examples show how to use javax.swing.tree.DefaultTreeModel#insertNodeInto() . 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: Test8015926.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 2
Source File: Test8015926.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 3
Source File: Test8015926.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 4
Source File: Test8015926.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 5
Source File: Editor.java    From jclic with GNU General Public License v2.0 6 votes vote down vote up
public boolean moveToIndex(int index, boolean updateSelection) {
  boolean result = false;
  DefaultTreeModel model = getTreeModel();
  Editor p = getEditorParent();
  if (p != null && model != null) {
    index = Math.min(Math.max(0, index), p.getChildCount());
    if (index != p.getIndex(this)) {
      p.setModified(true);
      model.removeNodeFromParent(this);
      model.insertNodeInto(this, p, index);
      result = true;
      if (updateSelection)
        select();
    }
  }
  return result;
}
 
Example 6
Source File: Test8015926.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 7
Source File: Test8015926.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 8
Source File: Test8015926.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 9
Source File: AquaLocationSourceList.java    From pumpernickel with MIT License 6 votes vote down vote up
public AquaLocationSourceList(LocationHistory directoryStack,
		GraphicCache graphicCache) {
	super(new DefaultTreeModel(new DefaultMutableTreeNode("root")));

	treeModel = (DefaultTreeModel) getModel();
	root = (DefaultMutableTreeNode) treeModel.getRoot();

	this.directoryStack = directoryStack;
	this.graphicCache = graphicCache;

	treeModel.insertNodeInto(devices, root, 0);
	treeModel.insertNodeInto(places, root, 1);
	addTreeSelectionListener(treeSelectionListener);
	directoryStack.addChangeListener(directoryStackListener);
	setExpandsSelectedPaths(true);
	setCellRenderer(new SourceListLocationRenderer(graphicCache, this));
	getSelectionModel().setSelectionMode(
			TreeSelectionModel.SINGLE_TREE_SELECTION);
	setUI(new SourceListTreeUI(this));

	expandPath(new TreePath(devices.getPath()));
	expandPath(new TreePath(places.getPath()));
}
 
Example 10
Source File: Test8015926.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    Thread.currentThread().setUncaughtExceptionHandler(this);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
    DefaultTreeModel model = new DefaultTreeModel(root);

    this.tree = new JTree();
    this.tree.setModel(model);

    JFrame frame = new JFrame(getClass().getSimpleName());
    frame.add(this.tree);

    model.addTreeModelListener(this); // frame is not visible yet
    model.insertNodeInto(child, root, root.getChildCount());
    model.removeNodeFromParent(child);

    frame.setSize(640, 480);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}
 
Example 11
Source File: ShelvedChangesViewManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private TreeModel buildChangesModel() {
  myRoot = new DefaultMutableTreeNode(ROOT_NODE_VALUE);   // not null for TreeState matching to work
  DefaultTreeModel model = new DefaultTreeModel(myRoot);
  final List<ShelvedChangeList> changeLists = new ArrayList<ShelvedChangeList>(myShelveChangesManager.getShelvedChangeLists());
  Collections.sort(changeLists, ChangelistComparator.getInstance());
  if (myShelveChangesManager.isShowRecycled()) {
    ArrayList<ShelvedChangeList> recycled =
            new ArrayList<ShelvedChangeList>(myShelveChangesManager.getRecycledShelvedChangeLists());
    Collections.sort(recycled, ChangelistComparator.getInstance());
    changeLists.addAll(recycled);
  }
  myMoveRenameInfo.clear();

  for(ShelvedChangeList changeList: changeLists) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(changeList);
    model.insertNodeInto(node, myRoot, myRoot.getChildCount());

    final List<Object> shelvedFilesNodes = new ArrayList<Object>();
    List<ShelvedChange> changes = changeList.getChanges(myProject);
    for(ShelvedChange change: changes) {
      putMovedMessage(change.getBeforePath(), change.getAfterPath());
      shelvedFilesNodes.add(change);
    }
    List<ShelvedBinaryFile> binaryFiles = changeList.getBinaryFiles();
    for(ShelvedBinaryFile file: binaryFiles) {
      putMovedMessage(file.BEFORE_PATH, file.AFTER_PATH);
      shelvedFilesNodes.add(file);
    }
    Collections.sort(shelvedFilesNodes, ShelvedFilePatchComparator.getInstance());
    for (int i = 0; i < shelvedFilesNodes.size(); i++) {
      final Object filesNode = shelvedFilesNodes.get(i);
      final DefaultMutableTreeNode pathNode = new DefaultMutableTreeNode(filesNode);
      model.insertNodeInto(pathNode, node, i);
    }
  }
  return model;
}
 
Example 12
Source File: DependencyReport.java    From jsonde with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void createTree(
        Map<Long, Set<Long>> dependencies,
        DefaultTreeModel dependencyTreeModel,
        MutableTreeNode parent,
        Set<Long> codeSourceIds,
        Set<Long> shownSourceIds) {

    int i = 0;

    for (Long codeSourceId : codeSourceIds) {

        try {

            CodeSource codeSource = DaoFactory.getCodeSourceDao().get(codeSourceId);

            MutableTreeNode dependencyNode = new DefaultMutableTreeNode(codeSource.getSource());
            dependencyTreeModel.insertNodeInto(dependencyNode, parent, i);

            if (!shownSourceIds.contains(codeSourceId)) {
                shownSourceIds.add(codeSourceId);
                createTree(dependencies, dependencyTreeModel, dependencyNode, dependencies.get(codeSourceId), shownSourceIds);
            }

        } catch (DaoException e) {
            Main.getInstance().processException(e);
        }

        i++;

    }

}
 
Example 13
Source File: GroupNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
void addTargetsNode(@Nonnull Node node, @Nonnull DefaultTreeModel treeModel) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  int index;
  synchronized (this) {
    index = getNodeInsertionIndex(node, getSwingChildren());
    myChildren.add(index, node);
  }
  treeModel.insertNodeInto(node, this, index);
}
 
Example 14
Source File: LastNodeLowerHalfDrop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean importData(TransferHandler.TransferSupport support) {
    if (!canImport(support)) {
        return false;
    }
    // Extract transfer data.
    DefaultMutableTreeNode[] nodes = null;
    try {
        Transferable t = support.getTransferable();
        nodes = (DefaultMutableTreeNode[]) t.getTransferData(nodesFlavor);
    } catch (UnsupportedFlavorException ufe) {
        System.out.println("UnsupportedFlavor: " + ufe.getMessage());
    } catch (java.io.IOException ioe) {
        System.out.println("I/O error: " + ioe.getMessage());
    }
    // Get drop location info.
    JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
    int childIndex = dl.getChildIndex();
    TreePath dest = dl.getPath();
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode)
            dest.getLastPathComponent();
    JTree tree = (JTree) support.getComponent();
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    // Configure for drop mode.
    int index = childIndex;    // DropMode.INSERT
    if (childIndex == -1) {     // DropMode.ON
        index = parent.getChildCount();
    }
    // Add data to model.
    for (DefaultMutableTreeNode node : nodes) {
        model.insertNodeInto(node, parent, index++);
    }
    return true;
}
 
Example 15
Source File: SettingsDialog.java    From desktop with GNU General Public License v3.0 5 votes vote down vote up
public void addProfileToTree(Profile profile, boolean selectRow) {
    // Panels
    ProfilePanel profilePanel = new ProfilePanel(profile);
    RepositoryPanel repoPanel = new RepositoryPanel(profile);
    FoldersPanel foldersPanel = new FoldersPanel(profile);
    
    profilePanel.load();
    repoPanel.load();
    foldersPanel.load();

    // Add to tree
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

    DefaultMutableTreeNode profileNode = new DefaultMutableTreeNode(new ShowPanelTreeItem(profile.getName(), "profile", profilePanel));

    profileNode.add(new DefaultMutableTreeNode(new ShowPanelTreeItem(resourceBundle.getString("sd_remote_storage"), "repository", repoPanel)));
    profileNode.add(new DefaultMutableTreeNode(new ShowPanelTreeItem(resourceBundle.getString("sd_sync_folders"), "folders", foldersPanel)));
    
    model.insertNodeInto(profileNode, root, root.getChildCount() - 1);

    if (selectRow) {
        int profileNodeIndex = tree.getRowForPath(new TreePath(profileNode.getPath()));

        tree.expandRow(profileNodeIndex);
        tree.setSelectionRow(profileNodeIndex);
    }
}
 
Example 16
Source File: InspectionRVContentProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected static void add(@Nullable final DefaultTreeModel model, final InspectionTreeNode child, final InspectionTreeNode parent) {
  if (model == null) {
    insertByIndex(child, parent);
  }
  else {
    if (parent.getIndex(child) < 0) {
      model.insertNodeInto(child, parent, child.getParent() == parent ? parent.getChildCount() - 1 : parent.getChildCount());
    }
  }
}
 
Example 17
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 18
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
@Override public void drop(DropTargetDropEvent dtde) {
  // System.out.println("drop");
  // if (!isWebStart()) {
  //   try {
  //     draggingObject = dtde.getTransferable().getTransferData(FLAVOR);
  //   } catch (Exception ex) {
  //     rejectDrag(dtde);
  //     return;
  //   }
  // } else {
  //   draggingObject = getSelectionPath().getLastPathComponent();
  // }
  Object draggingObject = getSelectionPath().getLastPathComponent();
  Point pt = dtde.getLocation();
  TreePath path = getPathForLocation(pt.x, pt.y);
  if (Objects.isNull(path) || !(draggingObject instanceof MutableTreeNode)) {
    dtde.dropComplete(false);
    return;
  }
  // System.out.println("drop path is " + path);
  MutableTreeNode draggingNode = (MutableTreeNode) draggingObject;
  DefaultMutableTreeNode targetNode = (DefaultMutableTreeNode) path.getLastPathComponent();
  if (targetNode.equals(draggingNode)) {
    // Cannot move the node to the node itself
    dtde.dropComplete(false);
    return;
  }
  dtde.acceptDrop(DnDConstants.ACTION_MOVE);

  DefaultTreeModel model = (DefaultTreeModel) getModel();
  model.removeNodeFromParent(draggingNode);

  TreeNode parent = targetNode.getParent();
  if (parent instanceof MutableTreeNode && targetNode.isLeaf()) {
    model.insertNodeInto(draggingNode, (MutableTreeNode) parent, parent.getIndex(targetNode));
  } else {
    model.insertNodeInto(draggingNode, targetNode, targetNode.getChildCount());
  }
  dtde.dropComplete(true);

  dropTargetNode = null;
  draggedNode = null;
  repaint();
}
 
Example 19
Source File: LibraryTreePanel.java    From osp with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Inserts a child into a parent node at a specified index.
 *
 * @param child the child node
 * @param parent the parent node
 * @param index the index
 * @return true if added
 */
protected boolean insertChildAt(final LibraryTreeNode child, LibraryTreeNode parent, int index) {
	if (tree==null || parent.getChildCount()<index) return false;
	DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
	model.insertNodeInto(child, parent, index);  	
  return true;
}
 
Example 20
Source File: DependencyReport.java    From jsonde with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
public JComponent generateReport() {

        Map<Long, Set<Long>> dependencies = DaoFactory.getReportDao().getDependencies();

        JTree dependencyTree = new JTree();

        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Dependency tree");
        DefaultTreeModel dependencyTreeModel = new DefaultTreeModel(rootNode);

        int i = 0;

        for (Long codeSourceId : dependencies.keySet()) {

            try {

                CodeSource codeSource = DaoFactory.getCodeSourceDao().get(codeSourceId);

                MutableTreeNode dependencyNode = new DefaultMutableTreeNode(codeSource.getSource());
                dependencyTreeModel.insertNodeInto(dependencyNode, rootNode, i);

                createTree(
                        dependencies,
                        dependencyTreeModel,
                        dependencyNode,
                        dependencies.get(codeSourceId),
                        new LinkedHashSet<Long>(Arrays.asList(codeSourceId)));

            } catch (DaoException e) {
                Main.getInstance().processException(e);
            }

            i++;

        }

        dependencyTree.setModel(dependencyTreeModel);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setViewportView(dependencyTree);

        return scrollPane;
    }