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

The following examples show how to use javax.swing.JTree#getLastSelectedPathComponent() . 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: PopupInstantiateTemplate.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {
            JTree tree = (JTree) invoker;
            ScriptNode node = (ScriptNode) tree.getLastSelectedPathComponent();

            if (node == null
                    || !node.isTemplate()
                    || node.getUserObject() == null
                    || !(node.getUserObject() instanceof ScriptWrapper)) {
                return false;
            }

            if (((ScriptWrapper) node.getUserObject()).getEngine() == null) {
                return false;
            }

            return extension.getScriptsPanel().getSelectedScript() != null;
        } catch (Exception e) {
        }
    }
    return false;
}
 
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: ObjectSelector.java    From jaamsim with Apache License 2.0 6 votes vote down vote up
@Override
public void valueChanged( TreeSelectionEvent e ) {
	JTree tree = (JTree) e.getSource();
	DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
	if(node == null) {
		// This occurs when we set no selected entity (null) and then
		// force the tree to have a null selected node
		return;
	}

	Object userObj = node.getUserObject();
	if (userObj instanceof Entity) {
		FrameBox.setSelectedEntity((Entity)userObj, false);
	}
	else {
		FrameBox.setSelectedEntity(null, false);
	}
}
 
Example 4
Source File: PopupDuplicateScript.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {
            JTree tree = (JTree) invoker;
            ScriptNode node = (ScriptNode) tree.getLastSelectedPathComponent();

            if (node == null
                    || node.isTemplate()
                    || node.getUserObject() == null
                    || !(node.getUserObject() instanceof ScriptWrapper)) {
                return false;
            }

            if (((ScriptWrapper) node.getUserObject()).getEngine() == null) {
                return false;
            }

            return extension.getScriptsPanel().getSelectedScript() != null;
        } catch (Exception e) {
        }
    }
    return false;
}
 
Example 5
Source File: PopupRemoveScript.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {
            JTree tree = (JTree) invoker;
            ScriptNode node = (ScriptNode) tree.getLastSelectedPathComponent();

            if (node == null
                    || node.isTemplate()
                    || node.getUserObject() == null
                    || !(node.getUserObject() instanceof ScriptWrapper)) {
                return false;
            }

            return extension.getScriptsPanel().getSelectedScript() != null;
        } catch (Exception e) {
        }
    }
    return false;
}
 
Example 6
Source File: PopupNewScriptFromType.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {
            JTree tree = (JTree) invoker;
            ScriptNode node = (ScriptNode) tree.getLastSelectedPathComponent();

            // Enable if this is a type node - doesnt matter if its a template or not..
            if (node == null || node.getUserObject() != null || node.getType() == null) {
                return false;
            }
            this.type = node.getType();

            return true;
        } catch (Exception e) {
        }
    }
    return false;
}
 
Example 7
Source File: TreeTransferHandler.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof JTree) || (t == null)) {
        return false;
    }

    boolean result = false;
    JTree tree = (JTree) comp;
    NodeInterface destinationTreeNode = (NodeInterface) tree.getLastSelectedPathComponent();

    DataFlavor destDataFlavour = destinationTreeNode.getDataFlavour();

    TransferredData transferredData = null;
    try {
        transferredData = (TransferredData) t.getTransferData(destDataFlavour);
        result = DataFlavourManager.copy(destinationTreeNode, transferredData);
    } catch (IOException | UnsupportedFlavorException e) {
        ConsoleManager.getInstance().exception(this, e);
    }

    return result;
}
 
Example 8
Source File: ZestRecordOnOffPopupMenu.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (extension.isScriptTree(invoker)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length != 1) {
                    // Start by just supporting one at a time..
                    return false;
                }
                ScriptNode node = extension.getSelectedZestNode();
                if (node != null && node.getUserObject() instanceof ZestScriptWrapper) {
                    ZestScriptWrapper script = (ZestScriptWrapper) node.getUserObject();
                    if (script.getType().hasCapability(ScriptType.CAPABILITY_APPEND)
                            && record != script.isRecording()) {
                        this.setEnabled(true);
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 9
Source File: PopupMenuItemSaveScript.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (!ScriptsListPanel.TREE.equals(invoker.getName())) {
        return false;
    }

    JTree scriptsTree = (JTree) invoker;
    ScriptNode node = (ScriptNode) scriptsTree.getLastSelectedPathComponent();
    if (node == null || node.isTemplate() || !(node.getUserObject() instanceof ScriptWrapper)) {
        return false;
    }

    if (scriptsTree.getSelectionCount() != 1) {
        setEnabled(false);
        return true;
    }

    ScriptWrapper selectedScript = (ScriptWrapper) node.getUserObject();
    if (selectedScript.isChanged()) {
        setEnabled(true);
        this.selectedScript = selectedScript;
    } else {
        setEnabled(false);
    }

    return true;
}
 
Example 10
Source File: PopupUseScriptAsAuthenticationScript.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
private boolean isEnable(Component invoker) {
    // Enable the popup just for the scripts tree
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {

            JTree tree = (JTree) invoker;
            ScriptNode node = (ScriptNode) tree.getLastSelectedPathComponent();

            // And only for a script node
            if (node == null
                    || node.isTemplate()
                    || node.getUserObject() == null
                    || !(node.getUserObject() instanceof ScriptWrapper)) {
                return false;
            }

            // And only if the script's type is Authentication
            ScriptWrapper script = extension.getScriptsPanel().getSelectedScript();
            return script != null
                    && script.getEngine() != null
                    && script.getTypeName()
                            .equals(ScriptBasedAuthenticationMethodType.SCRIPT_TYPE_AUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}
 
Example 11
Source File: ZestPopupNodePaste.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (extension.isScriptTree(invoker)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length != 1) {
                    // Start by just supporting one at a time..
                    return false;
                }
                ScriptNode node = extension.getSelectedZestNode();
                ZestElement elmt = ZestZapUtils.getElement(node);
                this.setEnabled(false);

                if (node == null || node.isRoot() || elmt == null) {
                    return false;

                } else if (elmt instanceof ZestContainer && extension.canPasteNodesTo(node)) {
                    this.setEnabled(true);

                } else if (elmt instanceof ZestStatement
                        && extension.canPasteNodesTo(node.getParent())) {
                    this.setEnabled(true);
                }

                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 12
Source File: AlertReportExportMenuItem.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && "treeAlert".equals(invoker.getName())) {
        JTree tree = (JTree) invoker;
        if (tree.getLastSelectedPathComponent() != null) {
            DefaultMutableTreeNode node =
                    (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            this.treeAlert = tree;
            if (!node.isRoot()) {
                return true;
            }
        }
    }
    return false;
}
 
Example 13
Source File: TreeTransferHandler.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void exportDone(JComponent source, Transferable data, int action) {
    if (!(source instanceof JTree) || (data == null)) {
        return;
    }

    JTree tree = (JTree) source;
    NodeInterface destinationTreeNode = (NodeInterface) tree.getLastSelectedPathComponent();

    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DataFlavor destDataFlavour = destinationTreeNode.getDataFlavour();

    TransferredData transferredData = null;
    try {
        transferredData = (TransferredData) data.getTransferData(destDataFlavour);
    } catch (IOException | UnsupportedFlavorException e) {
        ConsoleManager.getInstance().exception(this, e);
    }

    if (action == MOVE) {
        DataFlavourManager.deleteNodes(model, transferredData);
    }

    if (action != NONE) {
        DataFlavourManager.displayMessages(destinationTreeNode, transferredData, action);
    }

    setDragging(false);
}
 
Example 14
Source File: PopupEnableDisableScript.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (invoker.getName() != null && invoker.getName().equals(ScriptsListPanel.TREE)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length == 0) {
                    // None selected
                    return false;
                }

                this.setEnabled(false);
                Boolean enable = null; // We dont know whetehr it will be Enable or Disable yet

                for (TreePath tp : tree.getSelectionPaths()) {
                    ScriptNode node = (ScriptNode) tp.getLastPathComponent();

                    if (node == null
                            || node.isTemplate()
                            || node.getUserObject() == null
                            || !(node.getUserObject() instanceof ScriptWrapper)) {
                        return false;

                    } else {
                        ScriptWrapper script = (ScriptWrapper) node.getUserObject();
                        if (script.getEngine() == null) {
                            return false;
                        }

                        if (script.getType().isEnableable()) {
                            if (enable == null) {
                                // First one
                                enable = !script.isEnabled();
                                if (script.isEnabled()) {
                                    this.setText(
                                            Constant.messages.getString(
                                                    "scripts.disable.popup"));
                                } else {
                                    this.setText(
                                            Constant.messages.getString(
                                                    "scripts.enable.popup"));
                                }
                            } else if (enable.equals(script.isEnabled())) {
                                // Some are enabled, some disabled, cant tell which to do
                                return false;
                            }
                            this.setEnabled(true);
                        }
                    }
                }
                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 15
Source File: TreeTransferHandler.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Transferable createTransferable(JComponent c) {
    TransferableDataItem transferredDataItem = null;

    if (!(c instanceof JTree)) {
        return null;
    }

    JTree tree = (JTree) c;
    if (tree.isEditing()) {
        DefaultMutableTreeNode node =
                (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        TreePath editPath = tree.getEditingPath();
        DefaultMutableTreeNode editNode =
                (DefaultMutableTreeNode) editPath.getLastPathComponent();
        if (node == editNode) {
            tree.stopEditing();
        }
    }

    TreePath[] paths = tree.getSelectionPaths();
    if (paths == null) {
        return null;
    } else {
        Map<NodeInterface, TreePath> map = new LinkedHashMap<>();

        for (TreePath path : paths) {
            if (path.getLastPathComponent() instanceof NodeInterface) {
                NodeInterface selection = (NodeInterface) path.getLastPathComponent();

                map.put(selection, path);
            }
        }

        if (map.isEmpty()) {
            return null;
        }
        transferredDataItem = new TransferableDataItem(map);
        setDragging(true);
        return transferredDataItem;
    }
}
 
Example 16
Source File: ZestPopupZestMove.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnableForComponent(Component invoker) {
    if (extension.isScriptTree(invoker)) {
        try {
            JTree tree = (JTree) invoker;
            if (tree.getLastSelectedPathComponent() != null) {
                if (tree.getSelectionPaths().length != 1) {
                    // Start by just supporting one at a time..
                    return false;
                }
                ScriptNode node = extension.getExtScript().getScriptUI().getSelectedNode();
                this.setEnabled(false);

                if (node == null || node.isRoot() || ZestZapUtils.getElement(node) == null) {
                    return false;
                } else if ((ZestZapUtils.getElement(node) instanceof ZestScript)) {
                    return false;
                } else if (ZestZapUtils.getShadowLevel(node) > 0) {
                    // Cant move these
                    /* TODO
                    } else if ((ZestZapUtils.getElement(node) instanceof ZestControl)) {
                    	return false;
                    } else if (ZestTreeElement.isSubclass(node.getZestElement(), ZestTreeElement.Type.COMMON_TESTS)) {
                    	// Cantmove these either
                    	 */
                } else if (up) {
                    ScriptNode prev = (ScriptNode) node.getPreviousSibling();
                    while (prev != null && ZestZapUtils.getShadowLevel(prev) > 0) {
                        prev = (ScriptNode) prev.getPreviousSibling();
                    }
                    if (prev != null) {
                        if (ZestZapUtils.getElement(node)
                                        .isSameSubclass(ZestZapUtils.getElement(prev))
                                || (ZestZapUtils.getElement(node) instanceof ZestStatement
                                        && ZestZapUtils.getElement(prev)
                                                instanceof ZestStatement)) {
                            this.setEnabled(true);
                        }
                    }
                } else {
                    // Down
                    ScriptNode next = (ScriptNode) node.getNextSibling();
                    while (next != null && ZestZapUtils.getShadowLevel(next) > 0) {
                        next = (ScriptNode) next.getNextSibling();
                    }
                    if (next != null) {
                        if (!(ZestZapUtils.getElement(next) instanceof ZestControl)
                                && (ZestZapUtils.getElement(node)
                                                .isSameSubclass(ZestZapUtils.getElement(next))
                                        || (ZestZapUtils.getElement(node)
                                                        instanceof ZestStatement
                                                && ZestZapUtils.getElement(next)
                                                        instanceof ZestStatement))) {
                            this.setEnabled(true);
                        }
                    }
                }

                return true;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    return false;
}
 
Example 17
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));
}