Example usage for javax.swing.tree DefaultMutableTreeNode getParent

List of usage examples for javax.swing.tree DefaultMutableTreeNode getParent

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode getParent.

Prototype

public TreeNode getParent() 

Source Link

Document

Returns this node's parent or null if this node has no parent.

Usage

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
    JTree sourceTree = (JTree) evt.getSource();
    TreePath[] paths = sourceTree.getSelectionPaths();
    if (paths != null) {
        theNodeInClipboard.clear();//  w ww.  j  av a  2 s  . c  o  m
        for (TreePath path : paths) {
            theNodeInClipboard.add((DefaultMutableTreeNode) path.getLastPathComponent());
        }
    }

    DefaultMutableTreeNode node = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();
    JTree tree = (JTree) evt.getSource();
    if (getTreeType(tree).equals(ETreeType.FileSystemTree)) {
        if (node.getUserObject() instanceof FileSystemObject) {
            tree.setEditable(false);
        }
    }
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
    while (parent != null && parent.getUserObject() instanceof FileSystemObject) {
        node = parent;
        parent = (DefaultMutableTreeNode) node.getParent();
    }
    tree.setEditable(true);
    if ((parent != null) && (parent.getUserObject() instanceof StructMap)) {
        if (node.getUserObject() instanceof FileSystemObject) {
            tree.setEditable(false);
        }
    }
}

From source file:nz.govt.natlib.ndha.manualdeposit.ManualDepositPresenter.java

private boolean dropNodes(DefaultMutableTreeNode node, DepositTreeModel model) {
    boolean succeeded = false;
    try {//  w  ww.ja  v  a2s  .  c o m
        ArrayList<DefaultMutableTreeNode> nodeInClipboard = getClipboardCopy();
        if (!entityRootSet) { // Has to be a drag from the file system to
            // the entity
            succeeded = true;
            for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                if (nodeFromClipboard.getUserObject() instanceof FileSystemObject) {
                    FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                    if (fso.getIsFile()) {
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) nodeFromClipboard
                                .getParent();
                        theFsoRoot = (FileSystemObject) nodeParent.getUserObject();
                        theFsoRootFile = fso;
                    } else {
                        theFsoRoot = fso;
                        theFsoRootFile = null;
                    }
                    setRoot();
                }
            }
        } else {
            if (dragFromTreeType.equals(ETreeType.FileSystemTree)) {
                // Must have a FileSystemObject and dragging to entity tree
                nodeInClipboard = cleanDuplicateFSOs(nodeInClipboard);
                for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                    FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                    fso.setSortBy(SortBy.UserArranged);
                    if (node.getUserObject() instanceof FileSystemObject) {
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                        FileSystemObject fsoDestination = (FileSystemObject) node.getUserObject();
                        if (nodeParent.getUserObject() instanceof FileSystemObject) {
                            FileSystemObject fsoParent = (FileSystemObject) nodeParent.getUserObject();
                            fsoParent.getChildren().add(fso, fsoDestination.getSortOrder());
                        } else {
                            FileGroup entity = (FileGroup) nodeParent.getUserObject();
                            entity.getChildren().add(fso, fsoDestination.getSortOrder());
                        }
                    } else {
                        fso.setSortOrder(0);
                        addFileToEntity(fso, (FileGroup) node.getUserObject());
                    }
                }
                // Added 23/09/2013 by Ben. Ref: #996 - Jumbling issue
                sortFilesInEntity((FileGroup) node.getUserObject(), manualDepositFrame.getCurrentSortBy());
                dragFromFileSystemTree_startThread(getEntities(), theFsoRoot, true, false);

            } else if (dragFromTreeType.equals(ETreeType.EntityTree)) {
                if (model.getTreeType().equals(ETreeType.EntityTree)) {
                    // Must have a FileSystemObject
                    for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                        FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) nodeFromClipboard
                                .getParent();
                        deleteEntityNode(nodeFromClipboard);
                        if (node.getUserObject() instanceof FileSystemObject) {
                            FileSystemObject fsoDestination = (FileSystemObject) node.getUserObject();
                            if (fsoDestination.getIsFile()) {
                                nodeParent = (DefaultMutableTreeNode) node.getParent();
                                if (nodeParent.getUserObject() instanceof FileSystemObject) {
                                    FileSystemObject fsoParent = (FileSystemObject) nodeParent.getUserObject();
                                    fsoParent.getChildren().add(fso, fsoDestination.getSortOrder());
                                } else {
                                    FileGroup entity = (FileGroup) nodeParent.getUserObject();
                                    entity.getChildren().add(fso, fsoDestination.getSortOrder());
                                }
                            } else {
                                addFileToEntityFile(fso, (FileSystemObject) node.getUserObject());
                            }
                        } else {
                            addFileToEntity(fso, (FileGroup) node.getUserObject());
                        }
                    }
                    addIntellectualEntities(getEntities());
                    updateWorkerProgress(100);
                } else {
                    // Must have a FileSystemObject
                    for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                        FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                        // Must have a StructMap or a FileSystemObject parent
                        if (node.getUserObject() instanceof StructMap) {
                            StructMap map = (StructMap) node.getUserObject();
                            if (map.getFiles() == null) {
                                map.setFiles(new FSOCollection());
                            }
                            map.getFiles().add(fso);
                            map.getFiles().setSortBy(manualDepositFrame.getCurrentSortBy());
                            map.getFiles().reSortList();

                            int nodeIndex = 0;
                            for (FileSystemObject fso1 : map.getFiles()) {
                                if (fso.equals(fso1)) {
                                    break; // Exit the FOR loop
                                }
                                nodeIndex++;
                            }
                            addFileToStructMap(fso, node, nodeIndex, false);
                        } else {
                            DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                            StructMap mapParent = (StructMap) nodeParent.getUserObject();
                            FileSystemObject fsoDestination = (FileSystemObject) node.getUserObject();
                            mapParent.getFiles().add(fso, fsoDestination.getSortOrder());
                            addStructMap(getStructures());
                        }
                    }
                    // Reset the Struct Sort Order
                    if (node.getUserObject() instanceof StructMap) {
                        StructMap updatedStructMap = (StructMap) node.getUserObject();
                        int structSortOrder = 0;
                        for (FileSystemObject fsoUpdated : updatedStructMap.getFiles()) {
                            fsoUpdated.setStructSortOrder(structSortOrder);
                            structSortOrder++;
                        }
                    } else {
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                        StructMap mapParent = (StructMap) nodeParent.getUserObject();
                        int structSortOrder = 0;
                        for (FileSystemObject fsoUpdated : mapParent.getFiles()) {
                            fsoUpdated.setStructSortOrder(structSortOrder);
                            structSortOrder++;
                        }
                    }
                }
            } else {
                // Can drag a FileSystemObject or a StructMap
                // drag source parent can only be a StructMap
                // drag target can be a StructMap or a FileSystemObject
                // if it's a FileSystemObject, we are re-ordering
                for (DefaultMutableTreeNode nodeFromClipboard : nodeInClipboard) {
                    StructMap mapSourceParent = (StructMap) ((DefaultMutableTreeNode) nodeFromClipboard
                            .getParent()).getUserObject();
                    StructMap mapDestination;
                    int position = 0;
                    if (node.getUserObject() instanceof StructMap) {
                        mapDestination = (StructMap) node.getUserObject();
                    } else {
                        FileSystemObject fsoChild = (FileSystemObject) node.getUserObject();
                        position = fsoChild.getSortOrder();
                        DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode) node.getParent();
                        mapDestination = (StructMap) nodeParent.getUserObject();
                    }
                    deleteStructMapNode(nodeFromClipboard);
                    if (nodeFromClipboard.getUserObject() instanceof FileSystemObject) {
                        FileSystemObject fso = (FileSystemObject) nodeFromClipboard.getUserObject();
                        if (mapDestination.getFiles() == null) {
                            mapDestination.setFiles(new FSOCollection());
                        }
                        mapDestination.getFiles().add(fso, position);
                    } else {
                        StructMap mapFrom = (StructMap) nodeFromClipboard.getUserObject();
                        mapSourceParent.getChildren().remove(mapFrom);
                        if (mapDestination.getChildren() == null) {
                            mapDestination.setChildren(new StructMapCollection());
                        }
                        mapDestination.getChildren().add(mapFrom);
                    }
                }
                addStructMap(getStructures());
            }
        }
    } catch (Exception ex) {
        reportException(ex);
    }
    return succeeded;
}

From source file:org.apache.cayenne.modeler.ProjectTreeView.java

public void dataNodeChanged(DataNodeEvent e) {

    DefaultMutableTreeNode node = getProjectModel().getNodeForObjectPath(new Object[] {
            e.getDomain() != null ? e.getDomain() : (DataChannelDescriptor) mediator.getProject().getRootNode(),
            e.getDataNode() });/*from  w w w.j  a  v  a2  s  .  c  om*/

    if (node != null) {

        if (e.isNameChange()) {
            positionNode((DefaultMutableTreeNode) node.getParent(), node,
                    Comparators.getDataDomainChildrenComparator());
            showNode(node);
        } else {

            getProjectModel().nodeChanged(node);

            DataChannelDescriptor domain = (DataChannelDescriptor) mediator.getProject().getRootNode();

            // check for DataMap additions/removals...
            Object[] mapsName = e.getDataNode().getDataMapNames().toArray();
            int mapCount = mapsName.length;

            // DataMap was linked
            if (mapCount > node.getChildCount()) {

                for (int i = 0; i < mapCount; i++) {
                    boolean found = false;
                    for (int j = 0; j < node.getChildCount(); j++) {
                        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(j);
                        if (domain.getDataMap(mapsName[i].toString()) == child.getUserObject()) {
                            found = true;
                            break;
                        }
                    }

                    if (!found) {
                        DefaultMutableTreeNode newMapNode = new DefaultMutableTreeNode(
                                domain.getDataMap(mapsName[i].toString()), false);
                        positionNode(node, newMapNode, Comparators.getNamedObjectComparator());
                        break;
                    }
                }
            }
            // DataMap was unlinked
            else if (mapCount < node.getChildCount()) {
                int j = 0;
                while (j < node.getChildCount()) {
                    boolean found = false;
                    DefaultMutableTreeNode child;
                    child = (DefaultMutableTreeNode) node.getChildAt(j);
                    Object obj = child.getUserObject();
                    for (int i = 0; i < mapCount; i++) {
                        if (domain.getDataMap(mapsName[i].toString()) == obj) {
                            found = true;
                            j++;
                        }
                    }
                    if (!found) {
                        removeNode(child);
                    }
                }
            }
        }
    }
}

From source file:org.apache.cayenne.modeler.ProjectTreeView.java

/**
 * Removes current node from the tree. Selects a new node adjacent to the currently
 * selected node instead.//from  ww  w.j a v a  2 s .com
 */
protected void removeNode(DefaultMutableTreeNode toBeRemoved) {

    // lookup for the new selected node
    Object selectedNode = null;

    TreePath selectionPath = getSelectionPath();
    if (selectionPath != null) {
        selectedNode = selectionPath.getLastPathComponent();
    }

    if (toBeRemoved == selectedNode) {

        // first search siblings
        DefaultMutableTreeNode newSelection = toBeRemoved.getNextSibling();
        if (newSelection == null) {
            newSelection = toBeRemoved.getPreviousSibling();

            // try parent
            if (newSelection == null) {
                newSelection = (DefaultMutableTreeNode) toBeRemoved.getParent();

                // search the whole tree
                if (newSelection == null) {

                    newSelection = toBeRemoved.getNextNode();
                    if (newSelection == null) {

                        newSelection = toBeRemoved.getPreviousNode();
                    }
                }
            }
        }

        showNode(newSelection);
    }

    // remove this node
    getProjectModel().removeNodeFromParent(toBeRemoved);
}

From source file:org.apache.cayenne.modeler.ProjectTreeView.java

/**
 * Returns array of the user objects ending with this and starting with one under
 * root. That is the array of actual objects rather than wrappers.
 *//* w ww  .  ja va2  s . c  om*/
private Object[] getUserObjects(DefaultMutableTreeNode node) {
    List list = new ArrayList();
    while (!node.isRoot()) {
        list.add(0, node.getUserObject());
        node = (DefaultMutableTreeNode) node.getParent();
    }
    return list.toArray();
}

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultTreeView.java

@Override
public void refreshView(final ViewState state) {
    Rectangle visibleRect = null;
    if (this.tree != null) {
        visibleRect = this.tree.getVisibleRect();
    }//from w w  w. ja v  a  2 s  .c  o  m

    this.removeAll();

    this.actionsMenu = this.createPopupMenu(state);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("WORKFLOWS");
    for (ModelGraph graph : state.getGraphs()) {
        root.add(this.buildTree(graph, state));
    }
    tree = new JTree(root);
    tree.setShowsRootHandles(true);
    tree.setRootVisible(false);
    tree.add(this.actionsMenu);

    if (state.getSelected() != null) {
        // System.out.println("SELECTED: " + state.getSelected());
        TreePath treePath = this.getTreePath(root, state.getSelected());
        if (state.getCurrentMetGroup() != null) {
            treePath = this.getTreePath(treePath, state);
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_STATIC_METADATA))) {
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("static-metadata")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_PRECONDITIONS))) {
            if (treePath == null) {
                treePath = this.getTreePath(root, state.getSelected().getPreConditions());
            }
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("pre-conditions")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        } else if (Boolean.parseBoolean(state.getFirstPropertyValue(EXPAND_POSTCONDITIONS))) {
            if (treePath == null) {
                treePath = this.getTreePath(root, state.getSelected().getPostConditions());
            }
            DefaultMutableTreeNode baseNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int i = 0; i < baseNode.getChildCount(); i++) {
                if (((DefaultMutableTreeNode) baseNode.getChildAt(i)).getUserObject()
                        .equals("post-conditions")) {
                    treePath = new TreePath(((DefaultMutableTreeNode) baseNode.getChildAt(i)).getPath());
                    break;
                }
            }
        }
        this.tree.expandPath(treePath);
        this.tree.setSelectionPath(treePath);
    }

    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            if (e.getPath().getLastPathComponent() instanceof DefaultMutableTreeNode) {
                DefaultTreeView.this.resetProperties(state);
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                if (node.getUserObject() instanceof ModelGraph) {
                    state.setSelected((ModelGraph) node.getUserObject());
                    state.setCurrentMetGroup(null);
                    DefaultTreeView.this.notifyListeners();
                } else if (node.getUserObject().equals("static-metadata")
                        || node.getUserObject().equals("pre-conditions")
                        || node.getUserObject().equals("post-conditions")) {
                    state.setSelected((ModelGraph) ((DefaultMutableTreeNode) node.getParent()).getUserObject());
                    state.setCurrentMetGroup(null);
                    state.setProperty(EXPAND_STATIC_METADATA,
                            Boolean.toString(node.getUserObject().equals("static-metadata")));
                    state.setProperty(EXPAND_PRECONDITIONS,
                            Boolean.toString(node.getUserObject().equals("pre-conditions")));
                    state.setProperty(EXPAND_POSTCONDITIONS,
                            Boolean.toString(node.getUserObject().equals("post-conditions")));
                    DefaultTreeView.this.notifyListeners();
                } else if (node.getUserObject() instanceof ConcurrentHashMap) {
                    DefaultMutableTreeNode metNode = null;
                    String group = null;
                    Object[] path = e.getPath().getPath();
                    for (int i = path.length - 1; i >= 0; i--) {
                        if (((DefaultMutableTreeNode) path[i]).getUserObject() instanceof ModelGraph) {
                            metNode = (DefaultMutableTreeNode) path[i];
                            break;
                        } else if (((DefaultMutableTreeNode) path[i])
                                .getUserObject() instanceof ConcurrentHashMap) {
                            if (group == null) {
                                group = (String) ((ConcurrentHashMap<String, String>) ((DefaultMutableTreeNode) path[i])
                                        .getUserObject()).keySet().iterator().next();
                            } else {
                                group = (String) ((ConcurrentHashMap<String, String>) ((DefaultMutableTreeNode) path[i])
                                        .getUserObject()).keySet().iterator().next() + "/" + group;
                            }
                        }
                    }
                    ModelGraph graph = (ModelGraph) metNode.getUserObject();
                    state.setSelected(graph);
                    state.setCurrentMetGroup(group);
                    DefaultTreeView.this.notifyListeners();
                } else {
                    state.setSelected(null);
                    DefaultTreeView.this.notifyListeners();
                }
            }
        }

    });
    tree.setCellRenderer(new TreeCellRenderer() {

        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            if (node.getUserObject() instanceof String) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel label = new JLabel((String) node.getUserObject());
                label.setForeground(Color.blue);
                panel.add(label, BorderLayout.CENTER);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else if (node.getUserObject() instanceof ModelGraph) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                JLabel iconLabel = new JLabel(
                        ((ModelGraph) node.getUserObject()).getModel().getExecutionType() + ": ");
                iconLabel.setForeground(((ModelGraph) node.getUserObject()).getModel().getColor());
                iconLabel.setBackground(Color.white);
                JLabel idLabel = new JLabel(((ModelGraph) node.getUserObject()).getModel().getModelName());
                idLabel.setBackground(Color.white);
                panel.add(iconLabel, BorderLayout.WEST);
                panel.add(idLabel, BorderLayout.CENTER);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else if (node.getUserObject() instanceof ConcurrentHashMap) {
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                String group = (String) ((ConcurrentHashMap<String, String>) node.getUserObject()).keySet()
                        .iterator().next();
                JLabel nameLabel = new JLabel(group + " : ");
                nameLabel.setForeground(Color.blue);
                nameLabel.setBackground(Color.white);
                JLabel valueLabel = new JLabel(
                        ((ConcurrentHashMap<String, String>) node.getUserObject()).get(group));
                valueLabel.setForeground(Color.darkGray);
                valueLabel.setBackground(Color.white);
                panel.add(nameLabel, BorderLayout.WEST);
                panel.add(valueLabel, BorderLayout.EAST);
                panel.setBackground(selected ? Color.lightGray : Color.white);
                return panel;
            } else {
                return new JLabel();
            }
        }

    });
    tree.addMouseListener(new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 && DefaultTreeView.this.tree.getSelectionPath() != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) DefaultTreeView.this.tree
                        .getSelectionPath().getLastPathComponent();
                if (node.getUserObject() instanceof String && !(node.getUserObject().equals("pre-conditions")
                        || node.getUserObject().equals("post-conditions"))) {
                    return;
                }
                orderSubMenu.setEnabled(node.getUserObject() instanceof ModelGraph
                        && !((ModelGraph) node.getUserObject()).isCondition()
                        && ((ModelGraph) node.getUserObject()).getParent() != null);
                DefaultTreeView.this.actionsMenu.show(DefaultTreeView.this.tree, e.getX(), e.getY());
            }
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }
    });
    this.scrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    this.add(this.scrollPane, BorderLayout.CENTER);
    if (visibleRect != null) {
        this.tree.scrollRectToVisible(visibleRect);
    }

    this.revalidate();
}

From source file:org.apache.syncope.ide.netbeans.view.ResourceExplorerTopComponent.java

private void resourceExplorerTreeMouseClicked(final java.awt.event.MouseEvent evt) {
    if (evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 2) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) resourceExplorerTree
                .getLastSelectedPathComponent();
        DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent();
        String parentNodeName = parentNode == null ? null : String.valueOf(parentNode.getUserObject());
        if (selectedNode.isLeaf() && StringUtils.isNotBlank(parentNodeName)) {
            String leafNodeName = (String) selectedNode.getUserObject();
            try {
                if (PluginConstants.MAIL_TEMPLATES.equals(parentNodeName)) {
                    openMailEditor(leafNodeName);
                } else if (PluginConstants.REPORT_XSLTS.equals(parentNodeName)) {
                    openReportEditor(leafNodeName);
                }//from w  w w .  j  a v a 2  s . co m
            } catch (IOException e) {
                Exceptions.printStackTrace(e);
            }
        }
    } else if (evt.getButton() == MouseEvent.BUTTON3 && evt.getClickCount() == 1) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) resourceExplorerTree
                .getLastSelectedPathComponent();
        String selectedNodeName = (String) selectedNode.getUserObject();
        if (selectedNode.isLeaf() && !PluginConstants.ROOT_NAME.equals(selectedNodeName)
                && !PluginConstants.MAIL_TEMPLATES.equals(selectedNodeName)
                && !PluginConstants.REPORT_XSLTS.equals(selectedNodeName)) {
            leafRightClickAction(evt, selectedNode);
        } else if (PluginConstants.MAIL_TEMPLATES.equals(selectedNodeName)) {
            folderRightClickAction(evt, mailTemplates);
        } else if (PluginConstants.REPORT_XSLTS.equals(selectedNodeName)) {
            folderRightClickAction(evt, reportXslts);
        } else if (PluginConstants.ROOT_NAME.equals(selectedNodeName)) {
            rootRightClickAction(evt);
        }
    }
}

From source file:org.apache.syncope.ide.netbeans.view.ResourceExplorerTopComponent.java

private void leafRightClickAction(final MouseEvent evt, final DefaultMutableTreeNode node) {
    JPopupMenu menu = new JPopupMenu();
    JMenuItem deleteItem = new JMenuItem("Delete");
    menu.add(deleteItem);//from w  ww.j  a  v a2s  .  co  m

    deleteItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            int result = JOptionPane.showConfirmDialog(null, "Are you sure to delete the item?");
            if (result == JOptionPane.OK_OPTION) {
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
                boolean deleted;
                if (parent.getUserObject().equals(PluginConstants.MAIL_TEMPLATES)) {
                    deleted = mailTemplateManagerService.delete((String) node.getUserObject());
                } else {
                    deleted = reportTemplateManagerService.delete((String) node.getUserObject());
                }
                if (deleted) {
                    node.removeFromParent();
                    treeModel.reload(parent);
                } else {
                    JOptionPane.showMessageDialog(null, "Error while deleting new element", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    });

    menu.show(evt.getComponent(), evt.getX(), evt.getY());
}

From source file:org.artifactory.webapp.wicket.actionable.tree.ActionableItemsTree.java

public TreeNode getNextParent(DefaultMutableTreeNode node) {
    DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
    if (parent == null) {
        return null;
    }//from  ww  w  . j  av a  2s . co m
    TreeNode nextNode = parent.getChildAfter(node);
    if (nextNode == null) {
        return getNextParent(parent);
    }
    return nextNode;
}

From source file:org.company.processmaker.TreeFilesTopComponent.java

public TreeFilesTopComponent() {
    initComponents();//from   www  .  jav a  2s  .  c o  m

    setName(Bundle.CTL_TreeFilesTopComponent());
    setToolTipText(Bundle.HINT_TreeFilesTopComponent());
    // new codes
    instance = this;

    MouseListener ml = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            int selRow = jTree1.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = jTree1.getPathForLocation(e.getX(), e.getY());

            if (selRow != -1) {
                if (e.getClickCount() == 1) {
                    //mySingleClick(selRow, selPath);
                } else if (e.getClickCount() == 2) {

                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1
                            .getLastSelectedPathComponent();
                    if (node == null) {
                        return;
                    }
                    Object nodeInfo = node.getUserObject();
                    int node_level = node.getLevel();

                    if (node_level < 2) {
                        return;
                    }

                    // for each dyna form
                    if (node_level == 2) {

                        Global gl_obj = Global.getInstance();

                        //myDoubleClick(selRow, selPath);
                        conf = Config.getInstance();

                        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
                        String parentName = (String) parent.getUserObject();

                        // handle triggers
                        if (parentName.equals("Triggers")) {

                            String filePath = "";
                            for (String[] s : res_trigger) {

                                if (s[0].equals(nodeInfo.toString())) {
                                    // get path of dyna in xml forms
                                    filePath = conf.tmp + "triggers/" + s[3] + "/" + s[2] + ".php";
                                    break;
                                }

                            }

                            File toAdd = new File(filePath);

                            try {
                                DataObject dObject = DataObject.find(FileUtil.toFileObject(toAdd));

                                dObject.getLookup().lookup(OpenCookie.class).open();
                                // dont listen for exist listen files
                                if (existFile(filePath)) {
                                    return;
                                }
                                dObject.addPropertyChangeListener(new PropertyChangeListener() {
                                    @Override
                                    public void propertyChange(PropertyChangeEvent evt) {
                                        if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) {
                                            //fire a dummy event
                                            if (!Boolean.TRUE.equals(evt.getNewValue())) {

                                                /*String msg = "Saved to" + evt.toString();
                                                NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
                                                DialogDisplayer.getDefault().notify(nd);*/
                                                TopComponent activeTC = TopComponent.getRegistry()
                                                        .getActivated();
                                                DataObject dataLookup = activeTC.getLookup()
                                                        .lookup(DataObject.class);
                                                String filePath = FileUtil.toFile(dataLookup.getPrimaryFile())
                                                        .getAbsolutePath();

                                                File userFile = new File(filePath);
                                                String fileName = userFile.getName();
                                                fileName = fileName.substring(0, fileName.lastIndexOf("."));

                                                try {
                                                    String content = new String(
                                                            Files.readAllBytes(Paths.get(filePath)));
                                                    // remote php tag and info "<?php //don't remove this tag! \n"
                                                    content = content.substring(6, content.length());

                                                    String query = "update triggers set TRI_WEBBOT = '"
                                                            + StringEscapeUtils.escapeSql(content)
                                                            + "' where TRI_UID = '" + fileName + "'";
                                                    GooglePanel.updateQuery(query);

                                                } catch (Exception e) {
                                                    //Exceptions.printStackTrace(e);
                                                    String msg = "Can not update trigger";
                                                    NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                                            NotifyDescriptor.INFORMATION_MESSAGE);
                                                    DialogDisplayer.getDefault().notify(nd);
                                                }

                                            }

                                        }

                                    }
                                });

                            } catch (DataObjectNotFoundException ex) {
                                //Exceptions.printStackTrace(ex);
                                String msg = "Trigger not found";
                                NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                        NotifyDescriptor.INFORMATION_MESSAGE);
                                DialogDisplayer.getDefault().notify(nd);
                            }

                            return;
                        }

                        List<String[]> res_dyna = gl_obj.getDyna();
                        String FileDir = "";
                        for (String[] s : res_dyna) {

                            if (s[1].equals(nodeInfo.toString())) {
                                // get path of dyna in xml forms
                                FileDir = s[3];
                                break;
                            }

                        }

                        //String msg = "selRow" + nodeInfo.toString() + "|" + conf.getXmlForms() + FileDir;
                        String filePath = conf.getXmlForms() + FileDir + ".xml";
                        if (conf.isRemote()) {
                            String[] res = FileDir.split("/");
                            filePath = conf.get("local_tmp_for_remote") + "/" + FileDir + "/" + res[1] + ".xml";
                        }

                        File toAdd = new File(filePath);

                        //Result will be null if the user clicked cancel or closed the dialog w/o OK
                        if (toAdd != null) {
                            try {
                                DataObject dObject = DataObject.find(FileUtil.toFileObject(toAdd));

                                dObject.getLookup().lookup(OpenCookie.class).open();
                                // dont listen for exist listen files
                                if (existFile(filePath)) {
                                    return;
                                }
                                dObject.addPropertyChangeListener(new PropertyChangeListener() {
                                    @Override
                                    public void propertyChange(PropertyChangeEvent evt) {
                                        if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) {
                                            //fire a dummy event
                                            if (!Boolean.TRUE.equals(evt.getNewValue())) {

                                                /*String msg = "Saved to" + evt.toString();
                                                NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
                                                DialogDisplayer.getDefault().notify(nd);*/
                                                TopComponent activeTC = TopComponent.getRegistry()
                                                        .getActivated();
                                                DataObject dataLookup = activeTC.getLookup()
                                                        .lookup(DataObject.class);
                                                String filePath = FileUtil.toFile(dataLookup.getPrimaryFile())
                                                        .getAbsolutePath();

                                                File userFile = new File(filePath);
                                                String fileName = userFile.getName();
                                                fileName = fileName.substring(0, fileName.lastIndexOf("."));

                                                Global gl_obj = Global.getInstance();

                                                List<String[]> res_dyna = gl_obj.getDyna();
                                                String FileDir = "";
                                                for (String[] s : res_dyna) {

                                                    if (filePath.contains(s[0])) {

                                                        FileDir = s[3];
                                                        break;

                                                    }

                                                }

                                                if (conf.isRemote()) {
                                                    boolean res_Upload = SSH.getInstance().uplaodFile(FileDir);
                                                    if (res_Upload) {
                                                        String msg = "file upload Successfully!";
                                                        NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                                                NotifyDescriptor.INFORMATION_MESSAGE);
                                                        DialogDisplayer.getDefault().notify(nd);
                                                    } else {
                                                        String msg = "error in uploading file!";
                                                        NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                                                NotifyDescriptor.INFORMATION_MESSAGE);
                                                        DialogDisplayer.getDefault().notify(nd);
                                                    }
                                                }

                                            }

                                        }

                                    }
                                });

                            } catch (DataObjectNotFoundException ex) {
                                //Exceptions.printStackTrace(ex);
                                String msg = "Can not find xml file";
                                NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                        NotifyDescriptor.INFORMATION_MESSAGE);
                                DialogDisplayer.getDefault().notify(nd);
                            }
                        }
                    }

                    // for each js file
                    if (node_level == 3) {

                        TreeNode parentInfo = node.getParent();

                        Global gl_obj = Global.getInstance();

                        List<String[]> res_dyna = gl_obj.getDyna();
                        String FileDir = "";
                        for (String[] s : res_dyna) {

                            if (s[1].equals(parentInfo.toString())) {
                                // get path of dyna in xml forms
                                FileDir = s[3];
                                break;
                            }

                        }

                        //myDoubleClick(selRow, selPath);
                        conf = Config.getInstance();
                        String filePath = conf.tmp + "xmlForms/" + FileDir + "/" + nodeInfo.toString() + ".js";
                        if (conf.isRemote()) {
                            filePath = conf.get("local_tmp_for_remote") + FileDir + "/" + nodeInfo.toString()
                                    + ".js";
                        }

                        File toAdd = new File(filePath);

                        //Result will be null if the user clicked cancel or closed the dialog w/o OK
                        if (toAdd != null) {
                            try {
                                DataObject dObject = DataObject.find(FileUtil.toFileObject(toAdd));

                                dObject.getLookup().lookup(OpenCookie.class).open();
                                // dont listen for exist listen files
                                if (existFile(filePath)) {
                                    return;
                                }
                                dObject.addPropertyChangeListener(new PropertyChangeListener() {
                                    @Override
                                    public void propertyChange(PropertyChangeEvent evt) {
                                        if (DataObject.PROP_MODIFIED.equals(evt.getPropertyName())) {
                                            //fire a dummy event
                                            if (!Boolean.TRUE.equals(evt.getNewValue())) {

                                                JTextComponent ed = EditorRegistry.lastFocusedComponent();
                                                String jsDoc = "";
                                                try {
                                                    jsDoc = ed.getText();
                                                } catch (Exception ex) {
                                                    //Exceptions.printStackTrace(ex);
                                                    String msg = "Can not get text from editor";
                                                    NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                                            NotifyDescriptor.INFORMATION_MESSAGE);
                                                    DialogDisplayer.getDefault().notify(nd);
                                                }

                                                TopComponent activeTC = TopComponent.getRegistry()
                                                        .getActivated();
                                                DataObject dataLookup = activeTC.getLookup()
                                                        .lookup(DataObject.class);
                                                String filePath = FileUtil.toFile(dataLookup.getPrimaryFile())
                                                        .getAbsolutePath();

                                                File userFile = new File(filePath);
                                                String fileName = userFile.getName();
                                                fileName = fileName.substring(0, fileName.lastIndexOf("."));

                                                Global gl_obj = Global.getInstance();

                                                List<String[]> res_dyna = gl_obj.getDyna();
                                                String FileDir = "";
                                                for (String[] s : res_dyna) {

                                                    if (filePath.contains(s[0])) {

                                                        FileDir = s[3];
                                                        break;

                                                    }

                                                }

                                                String fullPath = conf.getXmlForms() + FileDir + ".xml";
                                                if (conf.isRemote()) {
                                                    String[] res = FileDir.split("/");
                                                    fullPath = conf.get("local_tmp_for_remote") + FileDir + "/"
                                                            + res[1] + ".xml";
                                                }
                                                try {
                                                    DocumentBuilderFactory factory = DocumentBuilderFactory
                                                            .newInstance();
                                                    DocumentBuilder builder = factory.newDocumentBuilder();
                                                    Document mainDoc = builder.parse(fullPath);

                                                    XPath xPath = XPathFactory.newInstance().newXPath();
                                                    Node startDateNode = (Node) xPath
                                                            .compile("//dynaForm/" + fileName)
                                                            .evaluate(mainDoc, XPathConstants.NODE);
                                                    Node cdata = mainDoc.createCDATASection(jsDoc);
                                                    startDateNode.setTextContent("");
                                                    startDateNode.appendChild(cdata);

                                                    /*String msg = evt.getPropertyName() + "-" + fileName;
                                                    NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE);
                                                    DialogDisplayer.getDefault().notify(nd);*/
                                                    // write the content into xml file
                                                    TransformerFactory transformerFactory = TransformerFactory
                                                            .newInstance();
                                                    Transformer transformer = transformerFactory
                                                            .newTransformer();
                                                    DOMSource source = new DOMSource(mainDoc);
                                                    StreamResult result = new StreamResult(new File(fullPath));
                                                    transformer.transform(source, result);

                                                    if (conf.isRemote()) {
                                                        boolean res_Upload = SSH.getInstance()
                                                                .uplaodFile(FileDir);
                                                        if (res_Upload) {
                                                            String msg = "file upload Successfully!";
                                                            NotifyDescriptor nd = new NotifyDescriptor.Message(
                                                                    msg, NotifyDescriptor.INFORMATION_MESSAGE);
                                                            DialogDisplayer.getDefault().notify(nd);
                                                        } else {
                                                            String msg = "error in uploading file!";
                                                            NotifyDescriptor nd = new NotifyDescriptor.Message(
                                                                    msg, NotifyDescriptor.INFORMATION_MESSAGE);
                                                            DialogDisplayer.getDefault().notify(nd);
                                                        }
                                                    }

                                                } catch (Exception ex) {
                                                    //Exceptions.printStackTrace(ex);
                                                    String msg = "Can not save to xml form";
                                                    NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                                            NotifyDescriptor.INFORMATION_MESSAGE);
                                                    DialogDisplayer.getDefault().notify(nd);
                                                }

                                            }

                                        }

                                    }
                                });

                            } catch (DataObjectNotFoundException ex) {
                                //Exceptions.printStackTrace(ex);
                                String msg = "Can not save to xml form";
                                NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
                                        NotifyDescriptor.INFORMATION_MESSAGE);
                                DialogDisplayer.getDefault().notify(nd);
                            }
                        }
                    }
                }
            }
        }
    };
    jTree1.addMouseListener(ml);
    jTree1.setModel(null);
}