Java Code Examples for javax.swing.JTree#getModel()

The following examples show how to use javax.swing.JTree#getModel() . 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: GTreeExpandNodeToDepthTask.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static void expandPath(JTree tree, TreePath treePath, int currentDepth,
		TaskMonitor monitor) throws CancelledException {

	if (currentDepth <= 0) {
		return;
	}

	GTreeNode treeNode = (GTreeNode) treePath.getLastPathComponent();
	TreeModel treeModel = tree.getModel();
	int childCount = treeModel.getChildCount(treeNode);

	if (childCount > 0) {
		for (int i = 0; i < childCount; i++) {
			monitor.checkCanceled();

			GTreeNode n = (GTreeNode) treeModel.getChild(treeNode, i);
			TreePath path = treePath.pathByAddingChild(n);

			expandPath(tree, path, currentDepth - 1, monitor);
		}
	}

	tree.expandPath(treePath);
}
 
Example 2
Source File: DasaTreeListener.java    From Astrosoft with GNU General Public License v2.0 6 votes vote down vote up
public void valueChanged(TreeSelectionEvent e) {
	JTree tree = (JTree) e.getSource();

	DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
	
	if (node != null){
	
		boolean isRoot = node.isRoot();
		Dasa dasa = (Dasa) node.getUserObject();
		
		//Add sub dasas only if not present already.
		if (!isRoot && node.isLeaf()){
			
			
			for(Dasa d : dasa.subDasas()){
				node.add(new DefaultMutableTreeNode(d, true));
			}
			DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
			model.reload(node);
		}
		handler.nodeSelected(node, e.getNewLeadSelectionPath());
			
	}
	//tree.collapsePath(e.getOldLeadSelectionPath());
}
 
Example 3
Source File: TreeUtils.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static public void expandPath(JTree tree, 
                              TreePath targetPath, 
                              TreePath tp, 
                              int pos) {
  Object[] nodes = tp.getPath();

  Object node   = targetPath.getLastPathComponent();
  Object tpNode = nodes[pos];
  
  if(node.equals(tpNode)) {
    tree.expandPath(targetPath);
  } else {
    return;
  }
  
  TreeModel model = tree.getModel();
  if(pos < nodes.length - 1) {
    int n = model.getChildCount(node);
    for(int i = 0; i < n; i++) {
      Object child = model.getChild(node, i);        
      if(child.equals(nodes[pos+1])) {
        expandPath(tree, targetPath.pathByAddingChild(child), tp, pos+1);
      }
    }
  }
}
 
Example 4
Source File: LanguagesNavigator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Component getTreeCellRendererComponent (
    JTree tree, Object value,
    boolean sel,
    boolean expanded,
    boolean leaf, int row,
    boolean hasFocus
) {
    JLabel l = (JLabel) super.getTreeCellRendererComponent (
        tree, value, sel, expanded, leaf, row, hasFocus
    );

    if (value instanceof DefaultMutableTreeNode) {
        l.setIcon (null);
        l.setText ((String) ((DefaultMutableTreeNode) value).getUserObject ());
        return l;
    }
    LanguagesNavigatorModel model = (LanguagesNavigatorModel) tree.getModel ();
    l.setIcon (getCIcon (model.getIcon (value)));
    l.setText (model.getDisplayName (value));
    return l;
}
 
Example 5
Source File: CatalogTree_simpleDifferentTests.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testThatGetComponentGetsAWellDefinedJTreeComponent() {
    Assume.assumeTrue(!GraphicsEnvironment.isHeadless());
    final CatalogTree catalogTree = new CatalogTree(null, new DefaultAppContext(""), null);
    final Component component = catalogTree.getComponent();

    assertNotNull(component);
    assertTrue(component instanceof JTree);
    final JTree tree = (JTree) component;
    assertEquals(false, tree.isRootVisible());
    assertNotNull(tree.getModel());
    assertEquals(true, tree.getModel() instanceof DefaultTreeModel);

    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    assertNotNull(model.getRoot());
    assertEquals(true, model.getRoot() instanceof DefaultMutableTreeNode);
    final DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) model.getRoot();
    assertNotNull(rootNode.getUserObject());
    assertEquals(true, rootNode.getUserObject() instanceof String);
    assertEquals("root", rootNode.getUserObject().toString());
}
 
Example 6
Source File: SortTreeHelper.java    From ISO8583 with GNU General Public License v3.0 6 votes vote down vote up
@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;
		}
	}
}
 
Example 7
Source File: TreeUtil.java    From nextreports-designer with Apache License 2.0 5 votes vote down vote up
/**
 * Expand a tree node and all its child nodes recursively.
 *
 * @param tree The tree whose nodes to expand.
 * @param path Path to the node to start at.
 */
public static void expandAll(JTree tree, TreePath path) {
    Object node = path.getLastPathComponent();
    TreeModel model = tree.getModel();
    if (model.isLeaf(node)) {
        return;
    }
    tree.expandPath(path);
    int num = model.getChildCount(node);
    for (int i = 0; i < num; i++) {
        expandAll(tree, path.pathByAddingChild(model.getChild(node, i)));
    }
}
 
Example 8
Source File: CheckTreeManager.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CheckTreeManager(JTree tree) {
    this.tree = tree;
    selectionModel = new CheckTreeSelectionModel(tree.getModel());
    tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel));        
    tree.addMouseListener(this); //鼠标监听  
    selectionModel.addTreeSelectionListener(this); //树选择监听  
}
 
Example 9
Source File: Utils.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@ImplementationNote("Must be called from Swing UI thread")
public static void foldUnfoldTree(@Nonnull final JTree tree, final boolean unfold) {
  final TreeModel model = tree.getModel();
  if (model != null) {
    final Object root = model.getRoot();
    if (root != null) {
      final TreePath thePath = new TreePath(root);
      setTreeState(tree, thePath, true, unfold);
      if (!unfold) {
        setTreeState(tree, thePath, false, true);
      }
    }
  }
}
 
Example 10
Source File: CSelectionHistoryChooser.java    From binnavi with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the selection history tree where the previous selection states are shown.
 * 
 * @return The created tree component.
 */
private JTree createUndoTree() {
  final CSelectionHistoryTreeNode rootNode = new CSelectionHistoryTreeNode("Selection History");

  final JTree tree = new CUndoTree(rootNode);
  m_model = (DefaultTreeModel) tree.getModel();
  // m_model = new DefaultTreeModel(rootNode);
  // m_model.nodeStructureChanged(rootNode);

  tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
  tree.setRootVisible(true);
  tree.setCellRenderer(new CSelectionTreeCellRenderer());

  return tree;
}
 
Example 11
Source File: CatalogTree.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void appendToNode(JTree jTree, InvDataset dataset, MutableTreeNode parentNode) {
    final DefaultTreeModel treeModel = (DefaultTreeModel) jTree.getModel();
    if (dataset instanceof InvCatalogRef) {
        CatalogTreeUtils.appendCatalogNode(parentNode, treeModel, (InvCatalogRef) dataset);
    } else {
        appendLeafNode(parentNode, treeModel, dataset);
    }
}
 
Example 12
Source File: Preferences.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
synchronized void apply(JTree tree) {
     expanded = new boolean[expandedPaths.length];

     expandAll(tree);
     
     TreeModel model = tree.getModel();
     if(model != null)
model.addTreeModelListener(new JTreeExpander(this, tree));
   }
 
Example 13
Source File: GUIBrowser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void expandAll(JTree tree, TreePath path) {
    tree.expandPath(path);
    TreeModel model = tree.getModel();
    Object lastComponent = path.getLastPathComponent();
    for (int i = 0; i < model.getChildCount(lastComponent); i++) {
        expandAll(tree,
                path.pathByAddingChild(model.getChild(lastComponent, i)));
    }
}
 
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: LastNodeLowerHalfDrop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void exportDone(JComponent source, Transferable data, int action) {
    if ((action & MOVE) == MOVE) {
        JTree tree = (JTree) source;
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        // Remove nodes saved in nodesToRemove in createTransferable.
        for (DefaultMutableTreeNode nodesToRemove1 : nodesToRemove) {
            model.removeNodeFromParent(nodesToRemove1);
        }
    }
}
 
Example 16
Source File: JTreeJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public static String[][] getContent(JTree component) {
    TreeModel model = component.getModel();
    int rowCount = getNodeCount(model, model.getRoot()) + 1;
    String[][] content = new String[1][rowCount];
    List<String> treeContent = new Vector<String>(rowCount);
    getTreeContent(model, model.getRoot(), treeContent);
    treeContent.toArray(content[0]);
    return content;
}
 
Example 17
Source File: JTreeJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private TreePath getPath(JTree tree, String path) {
    String[] tokens = path.substring(1).split("(?<!\\\\)/");
    TreeModel treeModel = tree.getModel();
    if (treeModel == null) {
        throw new RuntimeException("Could not find model for tree");
    }
    Object rootNode = treeModel.getRoot();
    int start = tree.isRootVisible() ? 1 : 0;
    TreePath treePath = new TreePath(rootNode);
    StringBuilder searchedPath = new StringBuilder();
    if (tree.isRootVisible()) {
        String rootNodeText = unescapeSpecialCharacters(tokens[0]);
        searchedPath.append("/" + rootNodeText);
        assertTrue("JTree does not have a root node!", rootNode != null);
        assertTrue("JTree root node does not match: Expected </" + getPathText(tree, treePath) + "> Actual: <"
                + searchedPath.toString() + ">", searchedPath.toString().equals("/" + getPathText(tree, treePath)));
    }
    for (int i = start; i < tokens.length; i++) {
        String childText = unescapeSpecialCharacters(tokens[i]);
        searchedPath.append("/" + childText);
        boolean matched = false;
        tree.expandPath(treePath);
        for (int j = 0; j < treeModel.getChildCount(treePath.getLastPathComponent()); j++) {
            Object child = treeModel.getChild(treePath.getLastPathComponent(), j);
            TreePath childPath = treePath.pathByAddingChild(child);
            if (childText.equals(getPathText(tree, childPath))) {
                treePath = childPath;
                matched = true;
                break;
            }
        }
        if (!matched) {
            return null;
        }
    }
    return treePath;
}
 
Example 18
Source File: MyTree.java    From myqq with MIT License 4 votes vote down vote up
public MyTree(JTree tree)
{
	this.tree=tree;
	treeModel=(DefaultTreeModel) tree.getModel();
	
}
 
Example 19
Source File: RefactoringTestCase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected void browseRoot(JTree tree) {
    TreeModel model = tree.getModel();
    Object root = model.getRoot();
    browseChildren(model, root, 0);
}
 
Example 20
Source File: TreeTestUtils.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static TreePath findTreePathToText(JTree tree, String text) {
	TreeModel tm = tree.getModel();
	GTreeNode rootNode = (GTreeNode) tm.getRoot();
	return findPathToText(tree, rootNode, text);
}