Example usage for javax.swing.tree DefaultMutableTreeNode getIndex

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

Introduction

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

Prototype

public int getIndex(TreeNode aChild) 

Source Link

Document

Returns the index of the specified child in this node's child array.

Usage

From source file:lcmc.gui.resources.ServiceInfo.java

/** Puts a resource up in a group. */
void upResource(final Host dcHost, final boolean testOnly) {
    final GroupInfo gi = groupInfo;
    final DefaultMutableTreeNode giNode = gi.getNode();
    if (giNode == null) {
        return;/*from w w  w  .j  a  v  a  2s . co  m*/
    }
    final DefaultMutableTreeNode node = getNode();
    if (node == null) {
        return;
    }
    final int index = giNode.getIndex(node);
    if (index > 0) {
        @SuppressWarnings("unchecked")
        final Enumeration<DefaultMutableTreeNode> e = giNode.children();
        final List<String> newOrder = new ArrayList<String>();
        while (e.hasMoreElements()) {
            final DefaultMutableTreeNode n = e.nextElement();
            final ServiceInfo child = (ServiceInfo) n.getUserObject();
            newOrder.add(child.getHeartbeatId(testOnly));
        }
        final String el = newOrder.remove(index);
        newOrder.add(index - 1, el);
        if (!testOnly) {
            setUpdated(true);
        }
        gi.applyWhole(dcHost, false, newOrder, testOnly);
    }
}

From source file:lcmc.gui.resources.ServiceInfo.java

/** Puts a resource down in a group. */
void downResource(final Host dcHost, final boolean testOnly) {
    final GroupInfo gi = groupInfo;
    final DefaultMutableTreeNode giNode = gi.getNode();
    if (giNode == null) {
        return;//from   w w w.j  a  v  a  2 s.c  om
    }
    final DefaultMutableTreeNode node = getNode();
    if (node == null) {
        return;
    }
    final int index = giNode.getIndex(node);
    if (index < giNode.getChildCount() - 1) {
        @SuppressWarnings("unchecked")
        final Enumeration<DefaultMutableTreeNode> e = giNode.children();
        final List<String> newOrder = new ArrayList<String>();
        while (e.hasMoreElements()) {
            final DefaultMutableTreeNode n = e.nextElement();
            final ServiceInfo child = (ServiceInfo) n.getUserObject();
            newOrder.add(child.getHeartbeatId(testOnly));
        }
        final String el = newOrder.remove(index);
        newOrder.add(index + 1, el);
        if (!testOnly) {
            setUpdated(true);
        }
        gi.applyWhole(dcHost, false, newOrder, testOnly);
    }
}

From source file:lcmc.gui.resources.ServiceInfo.java

/**
 * Returns list of items for service popup menu with actions that can
 * be executed on the heartbeat services.
 *///from   w  ww. j  a  v  a  2  s . c  o  m
@Override
public List<UpdatableItem> createPopup() {
    final List<UpdatableItem> items = new ArrayList<UpdatableItem>();
    final boolean testOnly = false;
    final CloneInfo ci = getCloneInfo();
    if (ci == null) {
        addDependencyMenuItems(items, false, testOnly);
    }
    /* start resource */
    final MyMenuItem startMenuItem = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.StartResource"),
            START_ICON, ClusterBrowser.STARTING_PTEST_TOOLTIP, new AccessMode(ConfigData.AccessType.OP, false),
            new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public final String enablePredicate() {
            if (getBrowser().clStatusFailed()) {
                return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
            } else if (isStarted(testOnly)) {
                return Tools.getString("ServiceInfo.AlreadyStarted");
            } else {
                return getService().isAvailableWithText();
            }
        }

        @Override
        public void action() {
            hidePopup();
            startResource(getBrowser().getDCHost(), testOnly);
        }
    };
    final ClusterBrowser.ClMenuItemCallback startItemCallback = getBrowser().new ClMenuItemCallback(
            startMenuItem, null) {
        @Override
        public void action(final Host dcHost) {
            startResource(dcHost, true); /* testOnly */
        }
    };
    addMouseOverListener(startMenuItem, startItemCallback);
    items.add((UpdatableItem) startMenuItem);

    /* stop resource */
    final MyMenuItem stopMenuItem = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.StopResource"), STOP_ICON,
            ClusterBrowser.STARTING_PTEST_TOOLTIP, new AccessMode(ConfigData.AccessType.OP, false),
            new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public String enablePredicate() {
            if (getBrowser().clStatusFailed()) {
                return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
            } else if (isStopped(testOnly)) {
                return Tools.getString("ServiceInfo.AlreadyStopped");
            } else {
                return getService().isAvailableWithText();
            }
        }

        @Override
        public void action() {
            hidePopup();
            stopResource(getBrowser().getDCHost(), testOnly);
        }
    };
    final ClusterBrowser.ClMenuItemCallback stopItemCallback = getBrowser().new ClMenuItemCallback(stopMenuItem,
            null) {
        @Override
        public void action(final Host dcHost) {
            stopResource(dcHost, true); /* testOnly */
        }
    };
    addMouseOverListener(stopMenuItem, stopItemCallback);
    items.add((UpdatableItem) stopMenuItem);

    /* up group resource */
    final MyMenuItem upMenuItem = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.UpResource"), GROUP_UP_ICON,
            ClusterBrowser.STARTING_PTEST_TOOLTIP, new AccessMode(ConfigData.AccessType.OP, false),
            new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean visiblePredicate() {
            return groupInfo != null;
        }

        @Override
        public String enablePredicate() {
            if (getResource().isNew()) {
                return IS_NEW_STRING;
            }
            final GroupInfo gi = groupInfo;
            if (gi == null) {
                return "no";
            }
            if (getBrowser().clStatusFailed()) {
                return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
            }
            final DefaultMutableTreeNode giNode = gi.getNode();
            if (giNode == null) {
                return "no";
            }
            final DefaultMutableTreeNode node = getNode();
            if (node == null) {
                return "no";
            }
            final int index = giNode.getIndex(node);
            if (index == 0) {
                return "already up";
            }
            return null;
        }

        @Override
        public void action() {
            hidePopup();
            upResource(getBrowser().getDCHost(), testOnly);
        }
    };
    final ClusterBrowser.ClMenuItemCallback upItemCallback = getBrowser().new ClMenuItemCallback(upMenuItem,
            null) {
        @Override
        public void action(final Host dcHost) {
            upResource(dcHost, true); /* testOnly */
        }
    };
    addMouseOverListener(upMenuItem, upItemCallback);
    items.add((UpdatableItem) upMenuItem);

    /* down group resource */
    final MyMenuItem downMenuItem = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.DownResource"),
            GROUP_DOWN_ICON, ClusterBrowser.STARTING_PTEST_TOOLTIP,
            new AccessMode(ConfigData.AccessType.OP, false), new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean visiblePredicate() {
            return groupInfo != null;
        }

        @Override
        public String enablePredicate() {
            if (getResource().isNew()) {
                return IS_NEW_STRING;
            }
            final GroupInfo gi = groupInfo;
            if (gi == null) {
                return "no";
            }
            if (getBrowser().clStatusFailed()) {
                return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
            }
            final DefaultMutableTreeNode giNode = gi.getNode();
            if (giNode == null) {
                return "no";
            }
            final DefaultMutableTreeNode node = getNode();
            if (node == null) {
                return "no";
            }
            final int index = giNode.getIndex(node);
            if (index >= giNode.getChildCount() - 1) {
                return "already down";
            }
            return null;
        }

        @Override
        public void action() {
            hidePopup();
            downResource(getBrowser().getDCHost(), testOnly);
        }
    };
    final ClusterBrowser.ClMenuItemCallback downItemCallback = getBrowser().new ClMenuItemCallback(downMenuItem,
            null) {
        @Override
        public void action(final Host dcHost) {
            downResource(dcHost, true); /* testOnly */
        }
    };
    addMouseOverListener(downMenuItem, downItemCallback);
    items.add((UpdatableItem) downMenuItem);

    /* clean up resource */
    final MyMenuItem cleanupMenuItem = new MyMenuItem(
            Tools.getString("ClusterBrowser.Hb.CleanUpFailedResource"), SERVICE_RUNNING_ICON,
            ClusterBrowser.STARTING_PTEST_TOOLTIP,

            Tools.getString("ClusterBrowser.Hb.CleanUpResource"), SERVICE_RUNNING_ICON,
            ClusterBrowser.STARTING_PTEST_TOOLTIP, new AccessMode(ConfigData.AccessType.OP, false),
            new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean predicate() {
            return getService().isAvailable() && isOneFailed(testOnly);
        }

        @Override
        public String enablePredicate() {
            if (getBrowser().clStatusFailed()) {
                return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
            } else if (!isOneFailedCount(testOnly)) {
                return "no fail count";
            } else {
                return getService().isAvailableWithText();
            }
        }

        @Override
        public void action() {
            hidePopup();
            cleanupResource(getBrowser().getDCHost(), testOnly);
        }
    };
    /* cleanup ignores CIB_file */
    items.add((UpdatableItem) cleanupMenuItem);

    /* manage resource */
    final MyMenuItem manageMenuItem = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.ManageResource"),
            MANAGE_BY_CRM_ICON, ClusterBrowser.STARTING_PTEST_TOOLTIP,

            Tools.getString("ClusterBrowser.Hb.UnmanageResource"), UNMANAGE_BY_CRM_ICON,
            ClusterBrowser.STARTING_PTEST_TOOLTIP,

            new AccessMode(ConfigData.AccessType.OP, false), new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public boolean predicate() {
            return !isManaged(testOnly);
        }

        @Override
        public String enablePredicate() {
            if (getBrowser().clStatusFailed()) {
                return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
            } else {
                return getService().isAvailableWithText();
            }
        }

        @Override
        public void action() {
            hidePopup();
            if (this.getText().equals(Tools.getString("ClusterBrowser.Hb.ManageResource"))) {
                setManaged(true, getBrowser().getDCHost(), testOnly);
            } else {
                setManaged(false, getBrowser().getDCHost(), testOnly);
            }
        }
    };
    final ClusterBrowser.ClMenuItemCallback manageItemCallback = getBrowser().new ClMenuItemCallback(
            manageMenuItem, null) {
        @Override
        public void action(final Host dcHost) {
            setManaged(!isManaged(false), dcHost, true); /* testOnly */
        }
    };
    addMouseOverListener(manageMenuItem, manageItemCallback);
    items.add((UpdatableItem) manageMenuItem);
    addMigrateMenuItems(items);
    if (ci == null) {
        /* remove service */
        final MyMenuItem removeMenuItem = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.RemoveService"),
                ClusterBrowser.REMOVE_ICON, ClusterBrowser.STARTING_PTEST_TOOLTIP,
                new AccessMode(ConfigData.AccessType.ADMIN, false),
                new AccessMode(ConfigData.AccessType.OP, false)) {
            private static final long serialVersionUID = 1L;

            @Override
            public String enablePredicate() {
                if (getService().isNew()) {
                    return null;
                }
                if (getBrowser().clStatusFailed()) {
                    return ClusterBrowser.UNKNOWN_CLUSTER_STATUS_STRING;
                } else if (getService().isRemoved()) {
                    return IS_BEING_REMOVED_STRING;
                } else if (isRunning(testOnly) && !Tools.getConfigData().isAdvancedMode()) {
                    return "cannot remove running resource<br>" + "(advanced mode only)";
                }
                if (groupInfo == null) {
                    return null;
                }
                final ClusterStatus cs = getBrowser().getClusterStatus();
                final List<String> gr = cs.getGroupResources(groupInfo.getHeartbeatId(testOnly), testOnly);

                if (gr != null && gr.size() > 1) {
                    return null;
                } else {
                    return "you can remove the group";
                }
            }

            @Override
            public void action() {
                hidePopup();
                if (getService().isOrphaned()) {
                    cleanupResource(getBrowser().getDCHost(), testOnly);
                } else {
                    removeMyself(false);
                }
                getBrowser().getCRMGraph().repaint();
            }
        };
        final ServiceInfo thisClass = this;
        final ClusterBrowser.ClMenuItemCallback removeItemCallback = getBrowser().new ClMenuItemCallback(
                removeMenuItem, null) {
            @Override
            public final boolean isEnabled() {
                return super.isEnabled() && !getService().isNew();
            }

            @Override
            public final void action(final Host dcHost) {
                removeMyselfNoConfirm(dcHost, true); /* test only */
            }
        };
        addMouseOverListener(removeMenuItem, removeItemCallback);
        items.add((UpdatableItem) removeMenuItem);
    }
    /* view log */
    final MyMenuItem viewLogMenu = new MyMenuItem(Tools.getString("ClusterBrowser.Hb.ViewServiceLog"),
            LOGFILE_ICON, null, new AccessMode(ConfigData.AccessType.RO, false),
            new AccessMode(ConfigData.AccessType.RO, false)) {

        private static final long serialVersionUID = 1L;

        @Override
        public String enablePredicate() {
            if (getService().isNew()) {
                return IS_NEW_STRING;
            } else {
                return null;
            }
        }

        @Override
        public void action() {
            hidePopup();
            ServiceLogs l = new ServiceLogs(getBrowser().getCluster(), getNameForLog(),
                    getService().getHeartbeatId());
            l.showDialog();
        }
    };
    items.add(viewLogMenu);
    /* more migrate options */
    final MyMenu migrateSubmenu = new MyMenu(Tools.getString("ClusterBrowser.MigrateSubmenu"),
            new AccessMode(ConfigData.AccessType.OP, false), new AccessMode(ConfigData.AccessType.OP, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public String enablePredicate() {
            return null; //TODO: enable only if it has items
        }
    };
    items.add(migrateSubmenu);
    addMoreMigrateMenuItems(migrateSubmenu);

    /* config files */
    final MyMenu filesSubmenu = new MyMenu(Tools.getString("ClusterBrowser.FilesSubmenu"),
            new AccessMode(ConfigData.AccessType.ADMIN, false),
            new AccessMode(ConfigData.AccessType.ADMIN, false)) {
        private static final long serialVersionUID = 1L;

        @Override
        public String enablePredicate() {
            return null; //TODO: enable only if it has items
        }

        @Override
        public void update() {
            super.update();
            final MyMenu self = this;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    removeAll();
                    addFilesMenuItems(self);
                }
            });
        }
    };
    items.add(filesSubmenu);
    return items;
}

From source file:org.kuali.test.ui.components.repositorytree.RepositoryTree.java

private void moveSuiteTest(DefaultMutableTreeNode suiteTestTargetNode, SuiteTest suiteTest) {
    if (suiteTestTargetNode != null) {
        DefaultMutableTreeNode pnode = (DefaultMutableTreeNode) suiteTestTargetNode.getParent();

        if (pnode != null) {
            DefaultMutableTreeNode nodeToMove = findSuiteTestNode(suiteTest);

            if ((nodeToMove != null) && (nodeToMove != suiteTestTargetNode)) {
                TestSuite testSuite = (TestSuite) pnode.getUserObject();

                SuiteTest[] tests = testSuite.getSuiteTests().getSuiteTestArray();

                int pos1 = Utils.getSuiteTestArrayIndex(tests, suiteTest);
                int pos2 = Utils.getSuiteTestArrayIndex(tests, (SuiteTest) suiteTestTargetNode.getUserObject());
                ;/*w ww  .  j a va  2s  . c  o m*/

                if ((pos1 > -1) && (pos2 > -1) && (pos1 != pos2)) {
                    SuiteTest save = (SuiteTest) tests[pos1].copy();

                    if (pos1 > pos2) {
                        for (int i = pos2; i < pos1; ++i) {
                            tests[i + 1].set(tests[i]);
                        }
                    } else if (pos1 < pos2) {
                        for (int i = pos1; i < pos2; ++i) {
                            tests[i].set(tests[i + 1]);
                        }
                    }

                    tests[pos2].set(save);

                    getModel().removeNodeFromParent(nodeToMove);
                    getModel().insertNodeInto(nodeToMove, pnode, pnode.getIndex(suiteTestTargetNode));

                    int indx = 1;
                    for (SuiteTest t : testSuite.getSuiteTests().getSuiteTestArray()) {
                        t.setIndex(indx++);
                    }

                    getMainframe().getSaveConfigurationButton().setEnabled(true);
                    getMainframe().getSaveConfigurationMenuItem().setEnabled(true);
                }
            }
        }
    }
}

From source file:org.kuali.test.ui.components.repositorytree.RepositoryTree.java

/**
 *
 * @param platform//from ww w  . j a va 2s. c o  m
 */
public void refreshPlatformNode(Platform platform) {
    DefaultMutableTreeNode node = findPlatformNodeByName(platform.getName());

    if (node != null) {
        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
        int indx = parent.getIndex(node);
        getModel().removeNodeFromParent(node);
        getModel().insertNodeInto(new RepositoryNode(getConfiguration(), platform), parent, indx);
        getModel().reload(parent);
    }
}