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

The following examples show how to use javax.swing.JTree#setSelectionPath() . 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: Utils.java    From IBC with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Selects the specified section in the Global Configuration dialog.
 * @param configDialog
 * the Global Configuration dialog
 * @param path
 * the path to the required configuration section in the Global Configuration dialog
 * @return
 * true if the specified section can be found; otherwise false
 * @throws IbcException
 * a UI component could not be found
 * @throws IllegalStateException
 * the method has not been called on the SWing event dispatch thread
 */
static boolean selectConfigSection(final JDialog configDialog, final String[] path) throws IbcException, IllegalStateException {
    if (!SwingUtilities.isEventDispatchThread()) throw new IllegalStateException("selectConfigSection must be run on the event dispatch thread");
    
    JTree configTree = SwingUtils.findTree(configDialog);
    if (configTree == null) throw new IbcException("could not find the config tree in the Global Configuration dialog");

    Object node = configTree.getModel().getRoot();
    TreePath tp = new TreePath(node);

    for (String pathElement: path) {
        node = SwingUtils.findChildNode(configTree.getModel(), node, pathElement);
        if (node == null) return false;
        tp = tp.pathByAddingChild(node);
    }

    configTree.setExpandsSelectedPaths(true);
    configTree.setSelectionPath(tp);
    return true;
}
 
Example 2
Source File: ProjectExplorer.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean canPerformAction(JTree targetTree, Object draggedNode, int action, Point location) {
	TreePath pathTarget = targetTree.getPathForLocation(location.x, location.y);
	if (pathTarget == null) {
		targetTree.setSelectionPath(null);
		return false;
	}
	targetTree.setSelectionPath(pathTarget);
	if (action == DnDConstants.ACTION_COPY) {
		return false;
	} else if (action == DnDConstants.ACTION_MOVE) {
		Object targetNode = pathTarget.getLastPathComponent();
		return canMove(draggedNode, targetNode);
	} else {
		return false;
	}
}
 
Example 3
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 4
Source File: MainView.java    From HiJson with Apache License 2.0 6 votes vote down vote up
private void findTreeChildValue(String findText,List<TreePath> treePathLst) {
        JTree tree = getTree();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot();
        Enumeration e = root.depthFirstEnumeration();
        treePathLst.clear();
        curPos = 0;
        while (e.hasMoreElements()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
            if (node.isLeaf()) {
                String str = node.toString();
                if (str.substring(2).indexOf(findText) >= 0) {
                    tree.expandPath(new TreePath(node.getPath()));
                    TreePath tp = expandTreeNode(tree,node.getPath(), true);
                    treePathLst.add(tp);
                }
            }
        }
        if(!treePathLst.isEmpty()){
            tree.setSelectionPath(treePathLst.get(0));
            tree.scrollPathToVisible(treePathLst.get(0));
        }
//        return treePathLst;
    }
 
Example 5
Source File: Utils.java    From ib-controller with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Selects the specified section in the Global Configuration dialog.
 * @param configDialog
 * the Global Configuration dialog
 * @param path
 * the path to the required configuration section in the Global Configuration dialog
 * @return
 * true if the specified section can be found; otherwise false
 * @throws IBControllerException
 * a UI component could not be found
 * @throws IllegalStateException
 * the method has not been called on the SWing event dispatch thread
 */
static boolean selectConfigSection(final JDialog configDialog, final String[] path) throws IBControllerException, IllegalStateException {
    if (!SwingUtilities.isEventDispatchThread()) throw new IllegalStateException("selectConfigSection must be run on the event dispatch thread");
    
    JTree configTree = SwingUtils.findTree(configDialog);
    if (configTree == null) throw new IBControllerException("could not find the config tree in the Global Configuration dialog");

    Object node = configTree.getModel().getRoot();
    TreePath tp = new TreePath(node);

    for (String pathElement: path) {
        node = SwingUtils.findChildNode(configTree.getModel(), node, pathElement);
        if (node == null) return false;
        tp = tp.pathByAddingChild(node);
    }

    configTree.setExpandsSelectedPaths(true);
    configTree.setSelectionPath(tp);
    return true;
}
 
Example 6
Source File: OQLQueryCustomizer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean saveQuery(final String query,
                                final OQLSupport.OQLTreeModel treeModel,
                                final JTree tree) {
    JButton okButton = new JButton();
    Mnemonics.setLocalizedText(okButton, Bundle.OQLQueryCustomizer_OkButtonText());

    CustomizerPanel customizer = new CustomizerPanel(okButton,  treeModel);
    final DialogDescriptor dd = new DialogDescriptor(customizer,
                                        Bundle.OQLQueryCustomizer_SaveQueryCaption(), true,
                                        new Object[] { okButton,
                                        DialogDescriptor.CANCEL_OPTION },
                                        okButton, 0, HELP_CTX_SAVE_QUERY, null);
    final Dialog d = DialogDisplayer.getDefault().createDialog(dd);
    d.pack();
    d.setVisible(true);

    if (dd.getValue() == okButton) {
        OQLSupport.OQLQueryNode node;
        if (customizer.isNewQuery()) {
            OQLSupport.Query q = new OQLSupport.Query(query,
                                    customizer.getQueryName(),
                                    customizer.getQueryDescription());
            node = new OQLSupport.OQLQueryNode(q);
            treeModel.customCategory().add(node);
            treeModel.nodeStructureChanged(treeModel.customCategory());
        } else {
            node = (OQLSupport.OQLQueryNode)customizer.getSelectedValue();
            node.getUserObject().setScript(query);
            treeModel.nodeChanged(node);
        }
        tree.setSelectionPath(new TreePath(treeModel.getPathToRoot(node)));
        return true;
    } else {
        return false;
    }
}
 
Example 7
Source File: TreeSearch.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
public static TreeSearch installForOR(JTree tree) {
    return new TreeSearch(tree) {
        @Override
        public void selectAndSrollTo(TreeNode node) {
            if (node instanceof ORObjectInf) {
                TreePath path = ((ORObjectInf) node).getTreePath();
                tree.setSelectionPath(path);
                tree.scrollPathToVisible(path);
            } else {
                super.selectAndSrollTo(node);
            }
        }
    };
}
 
Example 8
Source File: OQLQueryCustomizer.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public static boolean saveQuery(final String query,
                                final OQLSupport.OQLTreeModel treeModel,
                                final JTree tree) {
    JButton okButton = new JButton();
    Mnemonics.setLocalizedText(okButton, Bundle.OQLQueryCustomizer_OkButtonText());

    CustomizerPanel customizer = new CustomizerPanel(okButton,  treeModel);
    final DialogDescriptor dd = new DialogDescriptor(customizer,
                                        Bundle.OQLQueryCustomizer_SaveQueryCaption(), true,
                                        new Object[] { okButton,
                                        DialogDescriptor.CANCEL_OPTION },
                                        okButton, 0, HELP_CTX_SAVE_QUERY, null);
    final Dialog d = DialogDisplayer.getDefault().createDialog(dd);
    d.pack();
    d.setVisible(true);

    if (dd.getValue() == okButton) {
        OQLSupport.OQLQueryNode node;
        if (customizer.isNewQuery()) {
            OQLSupport.Query q = new OQLSupport.Query(query,
                                    customizer.getQueryName(),
                                    customizer.getQueryDescription());
            node = new OQLSupport.OQLQueryNode(q);
            treeModel.customCategory().add(node);
            treeModel.nodeStructureChanged(treeModel.customCategory());
        } else {
            node = (OQLSupport.OQLQueryNode)customizer.getSelectedValue();
            node.getUserObject().setScript(query);
            treeModel.nodeChanged(node);
        }
        tree.setSelectionPath(new TreePath(treeModel.getPathToRoot(node)));
        return true;
    } else {
        return false;
    }
}
 
Example 9
Source File: TreeKeyListener.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
protected boolean changeSelectionUsingText(KeyEvent e, String inputStr) {
	inputStr = inputStr.toUpperCase();
	JTree tree = (JTree) e.getComponent();

	int[] selection = tree.getSelectionRows();
	Arrays.sort(selection);
	int i = selection.length > 0 ? selection[selection.length - 1] : 0;
	int rowCount = tree.getRowCount();
	for (int offset = 0; offset < rowCount; offset++) {
		int row = (i + offset) % rowCount;
		TreePath path = tree.getPathForRow(row);
		TreeNode node = (TreeNode) path.getLastPathComponent();
		Component renderer = tree.getCellRenderer()
				.getTreeCellRendererComponent(tree, node, false,
						tree.isExpanded(path), node.isLeaf(), row,
						tree.isFocusOwner());
		String str = getText(renderer);
		if (str != null && str.length() >= 0
				&& str.toUpperCase().startsWith(inputStr)) {
			tree.setSelectionPath(path);
			return true;
		}
	}

	return false;
}
 
Example 10
Source File: RightClickMouseAdapter.java    From RobotBuilder with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void mousePressed(MouseEvent e) {
    if (SwingUtilities.isRightMouseButton(e)) {
        // Right click only
        JTree tree = MainFrame.getInstance().getCurrentRobotTree().tree;
        TreePath path = tree.getPathForLocation(e.getX(), e.getY());
        tree.setSelectionPath(path);
        Rectangle bounds = tree.getUI().getPathBounds(tree, path);
        if (bounds != null && bounds.contains(e.getX(), e.getY())) {
            final RobotComponent component = (RobotComponent) path.getLastPathComponent();
            JPopupMenu menu = generatePopupMenu(component);
            menu.show(tree, bounds.x, bounds.y + bounds.height);
        }
    }
}
 
Example 11
Source File: CNodeExpander.java    From binnavi with Apache License 2.0 5 votes vote down vote up
/**
 * Selects the project tree node that represents the given object.
 * 
 * @param tree The project tree.
 * @param object Object whose node is selected.
 */
public static void setSelectionPath(final JTree tree, final Object object) {
  tree.setSelectionPath(new TreePath(new Object[] {tree.getModel().getRoot(),
      findNode(tree, object)}));

  tree.validate();
}
 
Example 12
Source File: MenuEditor.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
public boolean createNewMenu(int index, boolean prompt, Component dlgParent) {
  boolean result = false;
  ProjectLibrary pl = getProjectLibrary();

  if (pl != null) {
    JTree ct = getCurrentTree();
    TreePath savePath = null;

    if (ct != null)
      savePath = ct.getSelectionPath();

    Menu nm = new Menu(pl);
    nm.name = Long.toString(System.currentTimeMillis());

    MenuEditor med = (MenuEditor) nm.getEditor(null);
    med.setDescription(getOptions().getMsg("menu_newMenuName"));

    if (prompt) {
      result = med.createEditorPanel(getOptions()).showDialog(med, "menu_newMenuElement_caption", dlgParent, true);
    } else {
      result = true;
    }

    if (result) {
      result = insertEditor(med, true, index, true);
    } else if (savePath != null && ct != null) {
      ct.clearSelection();
      ct.setSelectionPath(savePath);
    }
  }
  return result;
}
 
Example 13
Source File: MenuEditor.java    From jclic with GNU General Public License v2.0 5 votes vote down vote up
public boolean createNewMenuElement(int index, boolean prompt, Component dlgParent) {

    MenuElement me = new MenuElement();
    boolean result;
    TreePath savePath = null;
    JTree ct = getCurrentTree();
    MenuElementEditor mee;

    me.caption = getOptions().getMsg("menu_newMenuElementName");

    if (ct != null)
      savePath = ct.getSelectionPath();

    mee = (MenuElementEditor) me.getEditor(null);
    mee.projectLibrary = getProjectLibrary();
    mee.createChildren();

    if (prompt) {
      result = mee.createEditorPanel(getOptions()).showDialog(mee, "menu_newMenuElement_caption", dlgParent, true);
    } else {
      result = true;
    }

    if (index < 0)
      index = getChildCount();
    else
      index = Math.min(index, getChildCount());

    if (result) {
      result = insertEditor(mee, true, index, true);
    } else if (savePath != null && ct != null) {
      ct.clearSelection();
      ct.setSelectionPath(savePath);
    }
    return result;
  }
 
Example 14
Source File: VimDasaView.java    From Astrosoft with GNU General Public License v2.0 2 votes vote down vote up
public VimDasaView(String title, Vimshottari v) {
	
	super(title, viewSize);
	this.v = v;
	Font treeFont = UIUtil.getFont("Tahoma", Font.PLAIN, 11);
	Font tableFont = UIUtil.getFont(Font.BOLD, 12);
	
	JTree dasaTree = new JTree(v.getDasaTreeModel());
	dasaTree.setFont(treeFont);
	
	dasaTree.getSelectionModel().setSelectionMode
        (TreeSelectionModel.SINGLE_TREE_SELECTION);
	
	dasaTree.setCellRenderer(new DasaTreeCellRenderer());
	ToolTipManager.sharedInstance().registerComponent(dasaTree);
	
	JScrollPane treePane = new JScrollPane(dasaTree);
	treePane.setPreferredSize(treeSize);
	
	dasaTableModel = new AstrosoftTableModel(
			v.getVimDasaTableData(), Vimshottari.getVimDasaTableColumnMetaData());
	dasaTable = new AstrosoftTable(dasaTableModel, TableStyle.SCROLL_SINGLE_ROW_SELECT);
	
	dasaTable.getTableHeader().setFont(tableFont);
	dasaTable.getSelectionModel().setLeadSelectionIndex(-1);

	dasaTitle = new TitleLabel(DisplayStrings.VIM_DASA_STR);

	JPanel dasaPanel = new JPanel();
	dasaPanel.add(new TitledTable(dasaTitle, dasaTable, tableSize));
	
	JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePane, dasaPanel);
	
	//splitPane.setBackground(Color.WHITE);
	treePane.setBorder(BorderFactory.createEtchedBorder());
	dasaPanel.setBorder(BorderFactory.createEmptyBorder());
	splitPane.setBorder(BorderFactory.createEtchedBorder());
	
	add(splitPane,BorderLayout.CENTER);
	
	this.setVisible(true);
	
	//Make Current Dasa as Selected
	TreePath currentDasaPath = v.getCurrentDasaPath();
	dasaTree.setSelectionPath(currentDasaPath);
	
	DefaultMutableTreeNode currentDasaNode = (DefaultMutableTreeNode) dasaTree.getLastSelectedPathComponent();
	
	nodeSelected(currentDasaNode, currentDasaPath);
	
	dasaTree.addTreeSelectionListener(new DasaTreeListener(this));
}