Example usage for javax.swing.tree DefaultMutableTreeNode equals

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

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java

private JPanel createAParameterBox(final boolean first) {

    final JLabel runLabel = new JLabel("<html><b>Number of runs:</b> 0</html>");
    final JLabel warningLabel = new JLabel();

    final JButton closeButton = new JButton();
    closeButton.setOpaque(false);// www .  j  a  v  a  2s.  com
    closeButton.setBorder(null);
    closeButton.setFocusable(false);

    if (!first) {
        closeButton.setRolloverIcon(PARAMETER_BOX_REMOVE);
        closeButton.setRolloverEnabled(true);
        closeButton.setIcon(RGBGrayFilter.getDisabledIcon(closeButton, PARAMETER_BOX_REMOVE));
        closeButton.setActionCommand(ACTIONCOMMAND_REMOVE_BOX);
    }

    final JScrollPane treeScrPane = new JScrollPane();
    final DefaultMutableTreeNode treeRoot = new DefaultMutableTreeNode();
    final JTree tree = new JTree(treeRoot);
    ToolTipManager.sharedInstance().registerComponent(tree);

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setCellRenderer(new ParameterBoxTreeRenderer());
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(final TreeSelectionEvent e) {
            final TreePath selectionPath = tree.getSelectionPath();
            boolean success = true;
            if (editedNode != null
                    && (selectionPath == null || !editedNode.equals(selectionPath.getLastPathComponent())))
                success = modify();

            if (success) {
                if (selectionPath != null) {
                    cancelAllSelectionBut(tree);
                    final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath
                            .getLastPathComponent();
                    if (!node.equals(editedNode)) {
                        ParameterInATree userObj = null;
                        final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                        if (!node.isRoot()
                                && selectionPath.getPathCount() == model.getPathToRoot(node).length) {
                            userObj = (ParameterInATree) node.getUserObject();
                            final ParameterInfo info = userObj.info;
                            editedNode = node;
                            editedTree = tree;
                            edit(info);
                        } else {
                            tree.setSelectionPath(null);
                            if (cancelButton.isEnabled())
                                cancelButton.doClick();
                            resetSettings();
                            enableDisableSettings(false);
                            editedNode = null;
                            editedTree = null;
                        }

                        updateDescriptionField(userObj);
                    } else
                        updateDescriptionField();

                } else
                    updateDescriptionField();

                enableDisableParameterCombinationButtons();
            } else {
                final DefaultTreeModel model = (DefaultTreeModel) editedTree.getModel();
                final DefaultMutableTreeNode storedEditedNode = editedNode;
                editedNode = null;
                tree.setSelectionPath(null);
                editedNode = storedEditedNode;
                editedTree.setSelectionPath(new TreePath(model.getPathToRoot(editedNode)));
            }
        }
    });

    treeScrPane.setViewportView(tree);
    treeScrPane.setBorder(null);
    treeScrPane.setViewportBorder(null);
    treeScrPane.setPreferredSize(new Dimension(450, 250));

    final JButton upButton = new JButton();
    upButton.setOpaque(false);
    upButton.setRolloverEnabled(true);
    upButton.setIcon(PARAMETER_UP_ICON);
    upButton.setRolloverIcon(PARAMETER_UP_ICON_RO);
    upButton.setDisabledIcon(PARAMETER_UP_ICON_DIS);
    upButton.setBorder(null);
    upButton.setToolTipText("Move up the selected parameter");
    upButton.setActionCommand(ACTIONCOMMAND_MOVE_UP);

    final JButton downButton = new JButton();
    downButton.setOpaque(false);
    downButton.setRolloverEnabled(true);
    downButton.setIcon(PARAMETER_DOWN_ICON);
    downButton.setRolloverIcon(PARAMETER_DOWN_ICON_RO);
    downButton.setDisabledIcon(PARAMETER_DOWN_ICON_DIS);
    downButton.setBorder(null);
    downButton.setToolTipText("Move down the selected parameter");
    downButton.setActionCommand(ACTIONCOMMAND_MOVE_DOWN);

    final JPanel mainPanel = FormsUtils.build("~ f:p:g ~ p ~ r:p",
            "012||" + "333||" + "44_||" + "445||" + "446||" + "44_ f:p:g", runLabel, first ? "" : warningLabel,
            first ? warningLabel : closeButton, new FormsUtils.Separator(""), treeScrPane, upButton, downButton)
            .getPanel();

    mainPanel.setBorder(BorderFactory.createTitledBorder(""));

    final JButton addButton = new JButton();
    addButton.setOpaque(false);
    addButton.setRolloverEnabled(true);
    addButton.setIcon(PARAMETER_ADD_ICON);
    addButton.setRolloverIcon(PARAMETER_ADD_ICON_RO);
    addButton.setDisabledIcon(PARAMETER_ADD_ICON_DIS);
    addButton.setBorder(null);
    addButton.setToolTipText("Add selected parameter");
    addButton.setActionCommand(ACTIONCOMMAND_ADD_PARAM);

    final JButton removeButton = new JButton();
    removeButton.setOpaque(false);
    removeButton.setRolloverEnabled(true);
    removeButton.setIcon(PARAMETER_REMOVE_ICON);
    removeButton.setRolloverIcon(PARAMETER_REMOVE_ICON_RO);
    removeButton.setDisabledIcon(PARAMETER_REMOVE_ICON_DIS);
    removeButton.setBorder(null);
    removeButton.setToolTipText("Remove selected parameter");
    removeButton.setActionCommand(ACTIONCOMMAND_REMOVE_PARAM);

    final JPanel result = FormsUtils.build("p ~ f:p:g", "_0 f:p:g||" + "10 p ||" + "20 p||" + "_0 f:p:g",
            mainPanel, addButton, removeButton).getPanel();

    Style.registerCssClasses(result, Dashboard.CSS_CLASS_COMMON_PANEL);

    final ParameterCombinationGUI pcGUI = new ParameterCombinationGUI(tree, treeRoot, runLabel, warningLabel,
            addButton, removeButton, upButton, downButton);
    parameterTreeBranches.add(pcGUI);

    final ActionListener boxActionListener = new ActionListener() {

        //====================================================================================================
        // methods

        //----------------------------------------------------------------------------------------------------
        public void actionPerformed(final ActionEvent e) {
            final String cmd = e.getActionCommand();

            if (ACTIONCOMMAND_ADD_PARAM.equals(cmd))
                handleAddParameter(pcGUI);
            else if (ACTIONCOMMAND_REMOVE_PARAM.equals(cmd))
                handleRemoveParameter(tree);
            else if (ACTIONCOMMAND_REMOVE_BOX.equals(cmd))
                handleRemoveBox(tree);
            else if (ACTIONCOMMAND_MOVE_UP.equals(cmd))
                handleMoveUp();
            else if (ACTIONCOMMAND_MOVE_DOWN.equals(cmd))
                handleMoveDown();
        }

        //----------------------------------------------------------------------------------------------------
        private void handleAddParameter(final ParameterCombinationGUI pcGUI) {
            final Object[] selectedValues = parameterList.getSelectedValues();
            if (selectedValues != null && selectedValues.length > 0) {
                final AvailableParameter[] params = new AvailableParameter[selectedValues.length];
                System.arraycopy(selectedValues, 0, params, 0, selectedValues.length);
                addParameterToTree(params, pcGUI);
                enableDisableParameterCombinationButtons();
            }
        }

        //----------------------------------------------------------------------------------------------------
        private void handleRemoveParameter(final JTree tree) {
            final TreePath selectionPath = tree.getSelectionPath();
            if (selectionPath != null) {
                cancelButton.doClick();

                final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath
                        .getLastPathComponent();
                if (!node.isRoot()) {
                    final DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent();
                    if (parentNode.isRoot()) {
                        removeParameter(tree, node, parentNode);
                        enableDisableParameterCombinationButtons();
                    }
                }
            }
        }

        //----------------------------------------------------------------------------------------------------
        private void handleRemoveBox(final JTree tree) {
            final int answer = Utilities.askUser(dashboard, false, "Comfirmation",
                    "This operation deletes the combination.",
                    "All related parameter returns back to the list on the left side.", "Are you sure?");
            if (answer == 1) {
                final DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();

                if (tree.getSelectionCount() > 0) {
                    editedNode = null;
                    tree.setSelectionPath(null);
                    if (cancelButton.isEnabled())
                        cancelButton.doClick();
                }

                final DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();
                for (int i = 0; i < root.getChildCount(); ++i) {
                    final DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
                    removeParameter(tree, node, root);
                }

                enableDisableParameterCombinationButtons();

                parameterTreeBranches.remove(pcGUI);
                combinationsPanel.remove(result);
                combinationsPanel.revalidate();

                updateNumberOfRuns();
            }
        }

        //----------------------------------------------------------------------------------------------------
        private void removeParameter(final JTree tree, final DefaultMutableTreeNode node,
                final DefaultMutableTreeNode parentNode) {
            final ParameterInATree userObj = (ParameterInATree) node.getUserObject();
            final ParameterInfo originalInfo = findOriginalInfo(userObj.info);
            if (originalInfo != null) {
                final DefaultListModel model = (DefaultListModel) parameterList.getModel();
                model.addElement(new AvailableParameter(originalInfo, currentModelHandler.getModelClass()));
                final DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
                treeModel.removeNodeFromParent(node);
                updateNumberOfRuns();
                tree.expandPath(new TreePath(treeModel.getPathToRoot(parentNode)));
            } else
                throw new IllegalStateException(
                        "Parameter " + userObj.info.getName() + " is not found in the model.");
        }

        //----------------------------------------------------------------------------------------------------
        private void handleMoveUp() {
            final TreePath selectionPath = tree.getSelectionPath();
            if (selectionPath != null) {
                boolean success = true;
                if (editedNode != null)
                    success = modify();

                if (success) {
                    final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath
                            .getLastPathComponent();
                    final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();

                    if (parent == null || parent.getFirstChild().equals(node)) {
                        tree.setSelectionPath(null); // we need this to preserve the state of the parameter settings panel
                        tree.setSelectionPath(new TreePath(node.getPath()));
                        return;
                    }

                    final int index = parent.getIndex(node);
                    final DefaultTreeModel treemodel = (DefaultTreeModel) tree.getModel();
                    treemodel.removeNodeFromParent(node);
                    treemodel.insertNodeInto(node, parent, index - 1);
                    tree.setSelectionPath(new TreePath(node.getPath()));
                }

            }
        }

        //----------------------------------------------------------------------------------------------------
        private void handleMoveDown() {
            final TreePath selectionPath = tree.getSelectionPath();
            if (selectionPath != null) {
                boolean success = true;
                if (editedNode != null)
                    success = modify();

                if (success) {
                    final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath
                            .getLastPathComponent();
                    final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();

                    if (parent == null || parent.getLastChild().equals(node)) {
                        tree.setSelectionPath(null); // we need this to preserve the state of the parameter settings panel
                        tree.setSelectionPath(new TreePath(node.getPath()));
                        return;
                    }

                    final int index = parent.getIndex(node);
                    final DefaultTreeModel treemodel = (DefaultTreeModel) tree.getModel();
                    treemodel.removeNodeFromParent(node);
                    treemodel.insertNodeInto(node, parent, index + 1);
                    tree.setSelectionPath(new TreePath(node.getPath()));
                }
            }
        }
    };

    GUIUtils.addActionListener(boxActionListener, closeButton, upButton, downButton, addButton, removeButton);

    result.setPreferredSize(new Dimension(500, 250));
    enableDisableParameterCombinationButtons();

    Style.apply(result, dashboard.getCssStyle());

    return result;
}

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

private boolean droppingDoesntMakeSense(List<DefaultMutableTreeNode> sourceNode,
        DefaultMutableTreeNode destinationNode) {
    /**/*w  ww  .j  av a  2  s  .co  m*/
     * Doesn't make sense if: The destination node is the source node The
     * destination node is the immediate parent of the source node The
     * destination node is a child of the source node
     */
    boolean doesntMakeSense = false;
    for (DefaultMutableTreeNode node : sourceNode) {
        doesntMakeSense = ((node.equals(destinationNode))
                || ((((DefaultMutableTreeNode) node).getParent() != null)
                        && (((DefaultMutableTreeNode) node).getParent().equals(destinationNode))));
        for (int i = 0; !doesntMakeSense && i < node.getChildCount(); i++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
            if (destinationNode.equals(childNode)) {
                doesntMakeSense = true;
            } else {
                ArrayList<DefaultMutableTreeNode> nodeList = new ArrayList<DefaultMutableTreeNode>();
                nodeList.add(childNode);
                doesntMakeSense = droppingDoesntMakeSense(nodeList, destinationNode);
            }
        }
        if (doesntMakeSense) {
            break;
        }
    }
    return doesntMakeSense;
}

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

@Test
public final void testSetup() {
    isVisible = false;//from  www  .j a v  a 2s  . co  m
    /*
     * _includeCMS2Search = false; _includeCMS1Search = false;
     * _includeNoCMSOption = false;
     */
    try {
        depositPresenter.setupScreen();
        loginPresenter.login(LOGIN_NAME, LOGIN_PASSWORD);
    } catch (Exception ex) {
        fail();
    }
    assertTrue(isVisible);
    /*
     * assertTrue(_includeCMS2Search); assertTrue(_includeCMS1Search);
     * assertTrue(_includeNoCMSOption);
     */

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
    File test = new File(RESOURCES_INPUT_PATH);
    FileSystemObject fso = FileSystemObject.create("Input", test, null);
    rootNode.setUserObject(fso);
    DepositTreeModel model = new DepositTreeModel(rootNode, ETreeType.EntityTree);
    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) theFrame.treeFileSystem
            .getLastSelectedPathComponent();
    assertTrue(selectedNode == null);
    theFrame.treeFileSystem.setModel(model);
    // _presenter.selectIEFile(fso);
    depositPresenter.selectNode(fso, ETreeType.EntityTree);
    selectedNode = (DefaultMutableTreeNode) theFrame.treeFileSystem.getLastSelectedPathComponent();
    assertTrue(selectedNode.equals(rootNode));
    assertFalse(theFrame.cursorIsWaiting);
}

From source file:org.fhaes.gui.AnalysisResultsPanel.java

/**
 * Draw the analysis results table depending on the current tree-node forcing the GUI to refresh regardless
 * /*from   w ww  .j a  va2s .  co m*/
 * @param forceRefresh
 */
public void setupTable(Boolean forceRefresh) {

    cl.show(cards, RESULTSPANEL);
    MainWindow.getInstance().getReportPanel().actionResultsHelp.setEnabled(true);

    goldFishPanel.setParamsText();

    try {
        log.debug("Setting cursor to wait");
        table.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        treeResults.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

        DefaultMutableTreeNode selectednode = (DefaultMutableTreeNode) treeResults
                .getLastSelectedPathComponent();

        if (selectednode == null) {
            clearTable();
            return;
        }

        log.debug("Node selected: " + selectednode.toString());

        // If selection hasn't changed don't do anything
        if (previouslySelectedNode != null && selectednode.equals(previouslySelectedNode) && !forceRefresh) {
            log.debug("Node selection hasn't changed so not doing anything");
            return;
        }

        previouslySelectedNode = selectednode;

        if (!(selectednode instanceof FHAESResultTreeNode)) {
            clearTable();
            return;
        }

        FHAESResultTreeNode resultnode = (FHAESResultTreeNode) treeResults.getLastSelectedPathComponent();

        FHAESResult result = resultnode.getFHAESResult();
        panelResult.setBorder(new TitledBorder(null, result.getFullName(), TitledBorder.LEADING,
                TitledBorder.TOP, null, null));

        if (result.equals(FHAESResult.SEASONALITY_SUMMARY)) {

            log.debug("Seasonality summary node selected");

            if (seasonalitySummaryModel == null) {
                clearTable();
                return;
            }

            table.setModel(seasonalitySummaryModel);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.INTERVAL_SUMMARY)) {

            log.debug("Interval summary node selected");

            if (intervalsSummaryModel == null) {
                clearTable();
                return;
            }

            table.setModel(intervalsSummaryModel);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);

        }

        else if (result.equals(FHAESResult.INTERVAL_EXCEEDENCE_TABLE)) {

            log.debug("Interval exceedence node selected");

            if (intervalsExceedenceModel == null) {
                clearTable();
                return;
            }

            table.setModel(intervalsExceedenceModel);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.BINARY_MATRIX_00)) {

            if (this.bin00Model == null) {
                clearTable();
                return;
            }

            table.setModel(bin00Model);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.BINARY_MATRIX_01)) {

            if (this.bin01Model == null) {
                clearTable();
                return;
            }

            table.setModel(bin01Model);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.BINARY_MATRIX_10)) {

            if (this.bin10Model == null) {
                clearTable();
                return;
            }

            table.setModel(bin10Model);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.BINARY_MATRIX_11)) {

            if (this.bin11Model == null) {
                clearTable();
                return;
            }

            table.setModel(bin11Model);
            // table.setSortOrder(0, SortOrder.ASCENDING);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.BINARY_MATRIX_SUM)) {

            if (this.binSumModel == null) {
                clearTable();
                return;
            }
            table.setModel(binSumModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.JACCARD_SIMILARITY_MATRIX)) {

            if (this.SJACModel == null) {
                clearTable();
                return;
            }
            table.setModel(SJACModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.COHEN_SIMILARITITY_MATRIX)) {

            if (this.SCOHModel == null) {
                clearTable();
                return;
            }
            table.setModel(SCOHModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.JACCARD_SIMILARITY_MATRIX_D)) {

            if (this.DSJACModel == null) {
                clearTable();
                return;
            }
            table.setModel(DSJACModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.COHEN_SIMILARITITY_MATRIX_D)) {

            if (this.DSCOHModel == null) {
                clearTable();
                return;
            }
            table.setModel(DSCOHModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.BINARY_MATRIX_NTP)) {

            log.debug("doing NTP");
            if (this.NTPModel == null) {
                log.debug("fileNTP is null so clearing table");

                clearTable();
                return;
            }
            table.setModel(NTPModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.BINARY_MATRIX_SITE)) {

            if (this.siteSummaryModel == null) {
                clearTable();
                return;
            }
            table.setModel(siteSummaryModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        } else if (result.equals(FHAESResult.BINARY_MATRIX_TREE)) {

            if (this.treeSummaryModel == null) {
                clearTable();
                return;
            }
            table.setModel(treeSummaryModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.GENERAL_SUMMARY)) {

            if (this.generalSummaryModel == null) {
                clearTable();
                return;
            }
            table.setModel(generalSummaryModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.SINGLE_FILE_SUMMARY)) {

            if (this.singleFileSummaryModel == null) {
                clearTable();
                return;
            }
            table.setModel(singleFileSummaryModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
        }

        else if (result.equals(FHAESResult.SINGLE_EVENT_SUMMARY)) {

            if (this.singleEventSummaryModel == null) {
                clearTable();
                return;
            }
            table.setModel(singleEventSummaryModel);
            setDefaultTableAlignment(SwingConstants.RIGHT);
            setTableColumnAlignment(SwingConstants.LEFT, 2);
        }

        else {
            log.warn("Unhandled FHAESResult type");
            clearTable();
        }

        table.packAll();

        table.setColumnControlVisible(true);
        table.setAutoCreateRowSorter(true);

    } catch (Exception e) {
        log.debug("Caught exception loading table data");

    } finally {
        log.debug("Clearing cursor");

        table.setCursor(Cursor.getDefaultCursor());
        treeResults.setCursor(Cursor.getDefaultCursor());
    }

}