Search in sources :

Example 31 with NodeBase

use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.

the class ChangeVariable method apply.

public static void apply(List<String> path, String nameBefore, String nameAfter, MNode savedTree) {
    NodeBase parent = NodeBase.locateNode(path);
    if (parent == null)
        throw new CannotRedoException();
    NodeVariable nodeBefore = (NodeVariable) parent.child(nameBefore);
    if (nodeBefore == null)
        throw new CannotRedoException();
    PanelModel mep = PanelModel.instance;
    JTree tree = mep.panelEquations.tree;
    FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
    FontMetrics fm = nodeBefore.getFontMetrics(tree);
    NodeVariable nodeAfter;
    if (nameBefore.equals(nameAfter)) {
        nodeAfter = nodeBefore;
        // Same as valueAfter. Sub-tree is not relevant here.
        nodeAfter.source.set(savedTree.get());
    } else {
        // Update database
        MPart mparent = parent.source;
        mparent.clear(nameBefore);
        mparent.set(nameAfter, "").merge(savedTree);
        MPart newPart = (MPart) mparent.child(nameAfter);
        MPart oldPart = (MPart) mparent.child(nameBefore);
        // Update GUI
        nodeAfter = (NodeVariable) parent.child(nameAfter);
        if (oldPart == null) {
            if (nodeAfter == null) {
                nodeAfter = nodeBefore;
                nodeAfter.source = newPart;
            } else {
                model.removeNodeFromParent(nodeBefore);
            }
        } else {
            if (nodeAfter == null) {
                int index = parent.getIndex(nodeBefore);
                nodeAfter = new NodeVariable(newPart);
                model.insertNodeIntoUnfiltered(nodeAfter, parent, index);
            }
            nodeBefore.build();
            nodeBefore.findConnections();
            if (nodeBefore.visible(model.filterLevel))
                model.nodeStructureChanged(nodeBefore);
            else
                parent.hide(nodeBefore, model, true);
        }
    }
    nodeAfter.build();
    nodeAfter.findConnections();
    nodeAfter.updateColumnWidths(fm);
    parent.updateTabStops(fm);
    parent.allNodesChanged(model);
    TreeNode[] nodePath = nodeAfter.getPath();
    mep.panelEquations.updateOrder(nodePath);
    mep.panelEquations.updateVisibility(nodePath);
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) PanelModel(gov.sandia.n2a.ui.eq.PanelModel) JTree(javax.swing.JTree) MPart(gov.sandia.n2a.eqset.MPart) FontMetrics(java.awt.FontMetrics) TreeNode(javax.swing.tree.TreeNode) CannotRedoException(javax.swing.undo.CannotRedoException) NodeVariable(gov.sandia.n2a.ui.eq.tree.NodeVariable) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

Example 32 with NodeBase

use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.

the class EquationTreeCellEditor method getTreeCellEditorComponent.

@Override
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
    editingIcon = renderer.getIconFor((NodeBase) value, expanded, leaf);
    offset = renderer.getIconTextGap() + editingIcon.getIconWidth();
    Font font = renderer.getFontFor(editingNode);
    String text = editingNode.getText(expanded, true);
    FontMetrics fm = tree.getFontMetrics(font);
    int textWidth = fm.stringWidth(text);
    int width = tree.getWidth();
    if (editingComponent != null)
        editingContainer.remove(editingComponent);
    if (text.contains("\n") || textWidth > width || multiLineRequested) {
        editingComponent = multiLinePane;
        multiLineEditor.setText(text);
        multiLineEditor.setFont(font);
        int equals = text.indexOf('=');
        if (equals >= 0)
            multiLineEditor.setCaretPosition(equals);
        multiLineRequested = false;
    } else {
        editingComponent = oneLineEditor;
        oneLineEditor.setText(text);
        oneLineEditor.setFont(font);
    }
    editingContainer.add(editingComponent);
    undoManager.discardAllEdits();
    return editingContainer;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) FontMetrics(java.awt.FontMetrics) Font(java.awt.Font)

Example 33 with NodeBase

use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.

the class EquationTreeCellEditor method isCellEditable.

/**
 *        Indicate whether the current cell may be edited.
 *        Apparently, this method is called in only two situations:
 *        1) The left or right (but not center or scroll wheel) mouse button has been clicked.
 *           In this case, event is the MouseEvent. For a double-click, the first and second presses
 *           are delivered as separate events (with click count set to 2 on the second press).
 *        2) startEditingAtPath() was called, and JTree wants to verify that editing is permitted.
 *           In this case, event is null.
 */
@Override
public boolean isCellEditable(EventObject event) {
    if (event != null) {
        Object source = event.getSource();
        if (!(source instanceof JTree))
            return false;
        if (source != tree) {
            // Allow us to change trees,
            setTree((JTree) source);
            // but still avoid immediate edit.
            return false;
        }
        if (event instanceof MouseEvent) {
            MouseEvent me = (MouseEvent) event;
            final TreePath path = tree.getPathForLocation(me.getX(), me.getY());
            if (path != null) {
                if (me.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(me) && path.equals(lastPath)) {
                    EventQueue.invokeLater(new Runnable() {

                        public void run() {
                            tree.startEditingAtPath(path);
                        }
                    });
                }
            }
        }
        return false;
    }
    if (lastPath == null)
        return false;
    Object o = lastPath.getLastPathComponent();
    if (!(o instanceof NodeBase))
        return false;
    editingNode = (NodeBase) o;
    if (!editingNode.allowEdit())
        return false;
    return true;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase) JTree(javax.swing.JTree) MouseEvent(java.awt.event.MouseEvent) TreePath(javax.swing.tree.TreePath) EventObject(java.util.EventObject)

Example 34 with NodeBase

use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.

the class FilteredTreeModel method isLeaf.

public boolean isLeaf(Object node) {
    // Don't bother with getAllowsChildren()
    NodeBase p = (NodeBase) node;
    List<Integer> filtered = p.getFiltered();
    if (filtered == null)
        return p.isLeaf();
    return filtered.size() == 0;
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase)

Example 35 with NodeBase

use of gov.sandia.n2a.ui.eq.tree.NodeBase in project n2a by frothga.

the class FilteredTreeModel method getChildCount.

public int getChildCount(Object parent) {
    NodeBase p = (NodeBase) parent;
    List<Integer> filtered = p.getFiltered();
    if (filtered == null)
        return p.getChildCount();
    return filtered.size();
}
Also used : NodeBase(gov.sandia.n2a.ui.eq.tree.NodeBase)

Aggregations

NodeBase (gov.sandia.n2a.ui.eq.tree.NodeBase)39 MPart (gov.sandia.n2a.eqset.MPart)20 JTree (javax.swing.JTree)19 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)18 PanelModel (gov.sandia.n2a.ui.eq.PanelModel)15 TreeNode (javax.swing.tree.TreeNode)15 FontMetrics (java.awt.FontMetrics)12 CannotRedoException (javax.swing.undo.CannotRedoException)12 NodePart (gov.sandia.n2a.ui.eq.tree.NodePart)11 CannotUndoException (javax.swing.undo.CannotUndoException)8 TreePath (javax.swing.tree.TreePath)7 NodeVariable (gov.sandia.n2a.ui.eq.tree.NodeVariable)5 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)4 PanelEquationTree (gov.sandia.n2a.ui.eq.PanelEquationTree)4 NodeAnnotation (gov.sandia.n2a.ui.eq.tree.NodeAnnotation)4 NodeEquation (gov.sandia.n2a.ui.eq.tree.NodeEquation)3 NodeAnnotations (gov.sandia.n2a.ui.eq.tree.NodeAnnotations)2 NodeContainer (gov.sandia.n2a.ui.eq.tree.NodeContainer)2 NodeReference (gov.sandia.n2a.ui.eq.tree.NodeReference)2 Font (java.awt.Font)2