Search in sources :

Example 66 with TreeNode

use of javax.swing.tree.TreeNode in project n2a by frothga.

the class Move method apply.

public static void apply(List<String> path, int indexBefore, int indexAfter, int indexMetadata, boolean createOrder, boolean destroyOrder) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotUndoException();
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    NodeBase moveNode = (NodeBase) parent.getChildAt(indexBefore);
    model.removeNodeFromParent(moveNode);
    NodeBase metadataNode = parent.child("$metadata");
    if (createOrder) {
        if (metadataNode == null) {
            metadataNode = new NodeAnnotations((MPart) parent.source.set("$metadata", ""));
            model.insertNodeIntoUnfiltered(metadataNode, parent, indexMetadata);
        }
        NodeBase orderNode = new NodeAnnotation((MPart) metadataNode.source.set("gui.order", ""));
        model.insertNodeIntoUnfiltered(orderNode, metadataNode, metadataNode.getChildCount());
    }
    if (destroyOrder) {
        NodeBase orderNode = metadataNode.child("gui.order");
        FontMetrics fm = orderNode.getFontMetrics(tree);
        metadataNode.source.clear("gui.order");
        model.removeNodeFromParent(metadataNode.child("gui.order"));
        if (metadataNode.getChildCount() == 0) {
            parent.source.clear("$metadata");
            model.removeNodeFromParent(metadataNode);
        } else {
            metadataNode.updateTabStops(fm);
            metadataNode.allNodesChanged(model);
        }
    }
    model.insertNodeIntoUnfiltered(moveNode, parent, indexAfter);
    TreeNode[] movePath = moveNode.getPath();
    if (!destroyOrder)
        mep.panelEquations.updateOrder(movePath);
    mep.panelEquations.updateVisibility(movePath);
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) JTree(javax.swing.JTree) NodeAnnotations(gov.sandia.n2a.ui.eq.tree.NodeAnnotations) MPart(gov.sandia.n2a.eqset.MPart) NodeAnnotation(gov.sandia.n2a.ui.eq.tree.NodeAnnotation) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotUndoException(javax.swing.undo.CannotUndoException) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 67 with TreeNode

use of javax.swing.tree.TreeNode in project megameklab by MegaMek.

the class BayWeaponCriticalTree method addToBay.

/**
 * Adds an equipment mount to a bay. Changes the equipment mount's location and updates the bay's
 * weapon or ammo list if necessary.
 *
 * @param bay
 * @param eq
 */
public void addToBay(@Nullable Mounted bay, Mounted eq) {
    // Check that we have a bay
    if ((null == bay) || !canTakeEquipment(bay, eq)) {
        if (eq.getType() instanceof WeaponType) {
            EquipmentType bayType = ((WeaponType) eq.getType()).getBayType();
            addToNewBay(bayType, eq);
        } else {
            addToLocation(eq);
        }
        return;
    } else if (eq.getType() instanceof MiscType) {
        if (eq.getType().hasFlag(MiscType.F_ARTEMIS) || eq.getType().hasFlag(MiscType.F_ARTEMIS_PROTO) || eq.getType().hasFlag(MiscType.F_ARTEMIS_V)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
                final WeaponType wtype = (WeaponType) weapon.getType();
                if ((weapon.getLinkedBy() == null) && ((wtype.getAmmoType() == AmmoType.T_LRM) || (wtype.getAmmoType() == AmmoType.T_SRM) || (wtype.getAmmoType() == AmmoType.T_MML) || (wtype.getAmmoType() == AmmoType.T_NLRM))) {
                    moveToArc(eq);
                    eq.setLinked(weapon);
                    break;
                }
            }
        } else if (eq.getType().hasFlag(MiscType.F_APOLLO) || eq.getType().hasFlag(MiscType.F_PPC_CAPACITOR)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
                if (weapon.getLinkedBy() == null) {
                    moveToArc(eq);
                    eq.setLinked(weapon);
                    break;
                }
            }
        }
    } else {
        // there.
        if (eq.getType() instanceof AmmoType) {
            Optional<Mounted> addMount = bay.getBayAmmo().stream().map(n -> eSource.getEntity().getEquipment(n)).filter(m -> m.getType() == eq.getType()).findFirst();
            if (addMount.isPresent()) {
                addMount.get().setShotsLeft(addMount.get().getBaseShotsLeft() + eq.getBaseShotsLeft());
                UnitUtil.removeMounted(eSource.getEntity(), eq);
                refresh.refreshEquipment();
                refresh.refreshBuild();
                refresh.refreshPreview();
                refresh.refreshStatus();
                refresh.refreshSummary();
                return;
            }
        }
        EquipmentNode bayNode = null;
        for (Enumeration<?> e = ((TreeNode) model.getRoot()).children(); e.hasMoreElements(); ) {
            final EquipmentNode node = (EquipmentNode) e.nextElement();
            if (node.getMounted() == bay) {
                bayNode = node;
                break;
            }
        }
        if (null != bayNode) {
            moveToArc(eq);
            EquipmentNode eqNode = new EquipmentNode(eq);
            model.insertNodeInto(eqNode, bayNode, bayNode.getChildCount());
            eqNode.setParent(bayNode);
            if (eq.getType() instanceof WeaponType) {
                bay.addWeaponToBay(eSource.getEntity().getEquipmentNum(eq));
                if (eq.getLinkedBy() != null) {
                    moveToArc(eq.getLinkedBy());
                }
            } else if (eq.getType() instanceof AmmoType) {
                bay.addAmmoToBay(eSource.getEntity().getEquipmentNum(eq));
            }
        } else {
            // $NON-NLS-1$
            MegaMekLab.getLogger().log(// $NON-NLS-1$
            BayWeaponCriticalTree.class, // $NON-NLS-1$
            "addToBay(Mounted,Mounted)", LogLevel.DEBUG, // $NON-NLS-1$
            bay.getName() + "[" + eSource.getEntity().getEquipmentNum(bay) + "] not found in " + // $NON-NLS-1$
            getLocationName());
        }
    }
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : AmmoType(megamek.common.AmmoType) Color(java.awt.Color) Enumeration(java.util.Enumeration) EntitySource(megameklab.com.ui.EntitySource) Vector(java.util.Vector) MouseAdapter(java.awt.event.MouseAdapter) SmallCraft(megamek.common.SmallCraft) TestAero(megamek.common.verifier.TestAero) MouseListener(java.awt.event.MouseListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) ToolTipManager(javax.swing.ToolTipManager) TreePath(javax.swing.tree.TreePath) LocationFullException(megamek.common.LocationFullException) Set(java.util.Set) JMenu(javax.swing.JMenu) Component(java.awt.Component) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Dimension(java.awt.Dimension) List(java.util.List) WeaponType(megamek.common.WeaponType) Graphics(java.awt.Graphics) MiscType(megamek.common.MiscType) Optional(java.util.Optional) Entity(megamek.common.Entity) RefreshListener(megameklab.com.util.RefreshListener) InputEvent(java.awt.event.InputEvent) Rectangle(java.awt.Rectangle) LogLevel(megamek.common.logging.LogLevel) TreeNode(javax.swing.tree.TreeNode) ArrayList(java.util.ArrayList) PPCBayWeapon(megamek.common.weapons.bayweapons.PPCBayWeapon) HashSet(java.util.HashSet) EquipmentType(megamek.common.EquipmentType) JMenuItem(javax.swing.JMenuItem) MegaMekLab(megameklab.com.MegaMekLab) CConfig(megameklab.com.util.CConfig) Mounted(megamek.common.Mounted) UnitUtil(megameklab.com.util.UnitUtil) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) BayWeapon(megamek.common.weapons.bayweapons.BayWeapon) AmmoType(megamek.common.AmmoType) JPopupMenu(javax.swing.JPopupMenu) Nullable(megamek.common.annotations.Nullable) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) JTree(javax.swing.JTree) MouseEvent(java.awt.event.MouseEvent) JLabel(javax.swing.JLabel) StringJoiner(java.util.StringJoiner) PPCWeapon(megamek.common.weapons.ppc.PPCWeapon) Collections(java.util.Collections) Enumeration(java.util.Enumeration) Optional(java.util.Optional) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) MiscType(megamek.common.MiscType) EquipmentType(megamek.common.EquipmentType)

Example 68 with TreeNode

use of javax.swing.tree.TreeNode in project JWildfire by thargor6.

the class FlamePropertiesTreeService method selectPropertyPath.

public void selectPropertyPath(JTree pTree, FlamePropertyPath pPath) {
    if (pPath != null) {
        TreeNode selNode = null;
        TreeNode root = (TreeNode) pTree.getModel().getRoot();
        List<String> pathNodes = pPath.getPathComponents();
        // skip root
        if (pathNodes.size() > 0 && root != null && root.getChildCount() == 1) {
            root = root.getChildAt(0);
            int d = 0;
            while (d < pathNodes.size()) {
                boolean found = false;
                for (int i = 0; i < root.getChildCount(); i++) {
                    TreeNode node = root.getChildAt(i);
                    if (node instanceof FlamePropertiesTreeNode) {
                        FlamePropertiesTreeNode<?> fNode = (FlamePropertiesTreeNode<?>) node;
                        if (pathNodes.get(d).equals(fNode.getUserObject())) {
                            root = node;
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    break;
                } else if (d == pathNodes.size() - 1) {
                    selNode = root;
                }
                d++;
            }
            if (selNode != null) {
                TreeNode[] nodes = ((DefaultTreeModel) pTree.getModel()).getPathToRoot(selNode);
                TreePath path = new TreePath(nodes);
                pTree.expandPath(path);
                pTree.setSelectionPath(path);
            }
        }
    }
}
Also used : TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Example 69 with TreeNode

use of javax.swing.tree.TreeNode in project checkstyle-idea by jshiell.

the class CheckStyleToolWindowPanel method expandNode.

/**
 * Expand the given tree to the given level, starting from the given node
 * and path.
 *
 * @param tree  The tree to be expanded
 * @param node  The node to start from
 * @param path  The path to start from
 * @param level The number of levels to expand to
 */
private static void expandNode(final JTree tree, final TreeNode node, final TreePath path, final int level) {
    if (level <= 0) {
        return;
    }
    tree.expandPath(path);
    for (int i = 0; i < node.getChildCount(); ++i) {
        final TreeNode childNode = node.getChildAt(i);
        expandNode(tree, childNode, path.pathByAddingChild(childNode), level - 1);
    }
}
Also used : TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 70 with TreeNode

use of javax.swing.tree.TreeNode in project sldeditor by robward-scisys.

the class ExpressionPanelv2 method dataApplied.

/**
 * Data applied.
 */
@Override
public void dataApplied() {
    btnOk.setEnabled(true);
    if (selectedNode != null) {
        DefaultMutableTreeNode node = null;
        if (selectedNode.isLeaf()) {
            node = (DefaultMutableTreeNode) selectedNode.getParent();
            if (node == null) {
                node = selectedNode;
            }
        } else {
            node = selectedNode;
        }
        TreeNode[] tmpNode = node.getPath();
        // This notifies the listeners and changes the GUI
        model.reload();
        tree.expandPath(new TreePath(tmpNode));
        displayResult();
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Aggregations

TreeNode (javax.swing.tree.TreeNode)313 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)146 TreePath (javax.swing.tree.TreePath)125 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)31 Enumeration (java.util.Enumeration)30 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)28 MPart (gov.sandia.n2a.eqset.MPart)27 ArrayList (java.util.ArrayList)27 NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)25 JTree (javax.swing.JTree)23 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)23 Nullable (org.jetbrains.annotations.Nullable)23 MutableTreeNode (javax.swing.tree.MutableTreeNode)19 CannotRedoException (javax.swing.undo.CannotRedoException)18 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)16 CannotUndoException (javax.swing.undo.CannotUndoException)14 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)12 MouseEvent (java.awt.event.MouseEvent)11 NotNull (org.jetbrains.annotations.NotNull)11 PanelEquations (gov.sandia.n2a.ui.eq.PanelEquations)10