Search in sources :

Example 1 with OurTree

use of edu.mit.csail.sdg.alloy4.OurTree in project org.alloytools.alloy by AlloyTools.

the class Browsable method showAsTree.

/**
 * Display this node and its subnodes as a tree; if listener!=null, it will
 * receive OurTree.Event.SELECT events when nodes are selected.
 */
public final JFrame showAsTree(Listener listener) {
    final OurTree tree = new OurTree(12) {

        private static final long serialVersionUID = 0;

        private final boolean onWindows = Util.onWindows();

        {
            do_start();
        }

        @Override
        public String convertValueToText(Object val, boolean selected, boolean expanded, boolean leaf, int row, boolean focus) {
            String c = ">";
            String x = (val instanceof Browsable) ? ((Browsable) val).getHTML() : Util.encode(String.valueOf(val));
            if (onWindows)
                c = selected ? " style=\"color:#ffffff;\">" : " style=\"color:#000000;\">";
            return "<html><span" + c + x + "</span></html>";
        }

        @Override
        public List<?> do_ask(Object parent) {
            if (parent instanceof Browsable)
                return ((Browsable) parent).getSubnodes();
            else
                return new ArrayList<Browsable>();
        }

        @Override
        public Object do_root() {
            return Browsable.this;
        }
    };
    tree.setBorder(new EmptyBorder(3, 3, 3, 3));
    final JScrollPane scr = new JScrollPane(tree, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scr.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            tree.requestFocusInWindow();
        }

        @Override
        public void focusLost(FocusEvent e) {
        }
    });
    final JFrame x = new JFrame("Parse Tree");
    x.setLayout(new BorderLayout());
    x.add(scr, BorderLayout.CENTER);
    x.pack();
    x.setSize(500, 500);
    x.setLocationRelativeTo(null);
    x.setVisible(true);
    if (listener != null)
        tree.listeners.add(listener);
    return x;
}
Also used : JScrollPane(javax.swing.JScrollPane) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) OurTree(edu.mit.csail.sdg.alloy4.OurTree) EmptyBorder(javax.swing.border.EmptyBorder) FocusListener(java.awt.event.FocusListener) FocusEvent(java.awt.event.FocusEvent)

Example 2 with OurTree

use of edu.mit.csail.sdg.alloy4.OurTree in project org.alloytools.alloy by AlloyTools.

the class VizCustomizationPanel method remakeAll.

// =============================================================================================================//
/**
 * Regenerate all the customization widgets based on the latest settings.
 */
public void remakeAll() {
    // Make the tree
    final OurTree tree = new OurTree(12) {

        private static final long serialVersionUID = 0;

        private final AlloyModel old = vizState.getOriginalModel(), now = vizState.getCurrentModel();

        private final boolean hidePrivate = vizState.hidePrivate(), hideMeta = vizState.hideMeta();

        {
            do_start();
            setRootVisible(false);
            setShowsRootHandles(false);
            listeners.add(new Listener() {

                @Override
                public Object do_action(Object sender, Event event) {
                    return null;
                }

                @Override
                public Object do_action(Object sender, Event event, Object arg) {
                    zoom(arg);
                    return null;
                }
            });
        }

        @Override
        public String convertValueToText(Object value, boolean sel, boolean expand, boolean leaf, int i, boolean focus) {
            if (value == GENERAL)
                return "<html><b>general graph settings</b></html>";
            if (value == NODES)
                return "<html><b>types and sets</b></html>";
            if (value == EDGES)
                return "<html><b>relations</b></html>";
            if (value instanceof AlloyType) {
                AlloyType x = (AlloyType) value;
                if (vizState.getCurrentModel().hasType(x))
                    return "<html><b>sig</b> " + typename(x) + "</html>";
                return "<html><b>sig</b> " + typename(x) + " <font color=\"#808080\">(projected)</font></html>";
            }
            if (value instanceof AlloySet)
                return "<html><b>set</b> " + ((AlloySet) value).getName() + "</html>";
            if (value instanceof AlloyRelation)
                return value.toString();
            else
                return "";
        }

        @Override
        public List<?> do_ask(Object parent) {
            ArrayList<Object> ans = new ArrayList<Object>();
            if (parent == ROOT) {
                ans.add(GENERAL);
                ans.add(NODES);
                ans.add(EDGES);
            } else if (parent == NODES) {
                ans.add(AlloyType.UNIV);
            } else if (parent == EDGES) {
                for (AlloyRelation rel : vizState.getCurrentModel().getRelations()) if (!(hidePrivate && rel.isPrivate) && !(hideMeta && rel.isMeta))
                    ans.add(rel);
            } else if (parent instanceof AlloyType) {
                AlloyType type = (AlloyType) parent;
                for (AlloySet s : now.getSets()) if (!(hidePrivate && s.isPrivate) && !(hideMeta && s.isMeta) && s.getType().equals(type))
                    ans.add(s);
                if (!type.isEnum)
                    for (AlloyType t : old.getDirectSubTypes(type)) if (!(hidePrivate && t.isPrivate) && !(hideMeta && t.isMeta))
                        if (now.hasType(t) || vizState.canProject(t))
                            ans.add(t);
            }
            return ans;
        }

        @Override
        public boolean do_isDouble(Object object) {
            return object == NODES || object == EDGES;
        }

        @Override
        public Object do_root() {
            return ROOT;
        }
    };
    // Pre-expand the entire tree.
    TreePath last = null;
    for (int i = 0; i < tree.getRowCount(); i++) {
        tree.expandRow(i);
        if (lastElement != null && last == null) {
            last = tree.getPathForRow(i);
            if (lastElement != last.getLastPathComponent())
                last = null;
        }
    }
    // Show the current element if found, else show the GENERAL OPTIONS
    if (last != null) {
        zoom(lastElement);
    } else {
        last = tree.getPathForRow(0);
        zoom(GENERAL);
    }
    tree.scrollPathToVisible(last);
    tree.setSelectionPath(last);
    JScrollPane scroll = OurUtil.scrollpane(tree, Color.BLACK, Color.WHITE, new OurBorder(false, false, false, Util.onMac()));
    scroll.setAlignmentX(0f);
    scroll.getVerticalScrollBar().setUnitIncrement(50);
    removeAll();
    add(zoomPane);
    add(scroll);
    validate();
}
Also used : JScrollPane(javax.swing.JScrollPane) ActionListener(java.awt.event.ActionListener) ChangeListener(javax.swing.event.ChangeListener) Listener(edu.mit.csail.sdg.alloy4.Listener) OurBorder(edu.mit.csail.sdg.alloy4.OurBorder) ArrayList(java.util.ArrayList) OurTree(edu.mit.csail.sdg.alloy4.OurTree) TreePath(javax.swing.tree.TreePath) ChangeEvent(javax.swing.event.ChangeEvent) KeyEvent(java.awt.event.KeyEvent) ActionEvent(java.awt.event.ActionEvent) MouseEvent(java.awt.event.MouseEvent) FocusEvent(java.awt.event.FocusEvent)

Example 3 with OurTree

use of edu.mit.csail.sdg.alloy4.OurTree in project org.alloytools.alloy by AlloyTools.

the class SimpleGUI method do_action.

/**
 * {@inheritDoc}
 */
@Override
public Object do_action(Object sender, Event e, Object arg) {
    if (sender instanceof OurTree && e == Event.CLICK && arg instanceof Browsable) {
        Pos p = ((Browsable) arg).pos();
        if (p == Pos.UNKNOWN)
            p = ((Browsable) arg).span();
        text.shade(p);
    }
    return true;
}
Also used : Browsable(edu.mit.csail.sdg.ast.Browsable) Pos(edu.mit.csail.sdg.alloy4.Pos) OurTree(edu.mit.csail.sdg.alloy4.OurTree)

Aggregations

OurTree (edu.mit.csail.sdg.alloy4.OurTree)3 FocusEvent (java.awt.event.FocusEvent)2 JScrollPane (javax.swing.JScrollPane)2 Listener (edu.mit.csail.sdg.alloy4.Listener)1 OurBorder (edu.mit.csail.sdg.alloy4.OurBorder)1 Pos (edu.mit.csail.sdg.alloy4.Pos)1 Browsable (edu.mit.csail.sdg.ast.Browsable)1 BorderLayout (java.awt.BorderLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FocusListener (java.awt.event.FocusListener)1 KeyEvent (java.awt.event.KeyEvent)1 MouseEvent (java.awt.event.MouseEvent)1 ArrayList (java.util.ArrayList)1 JFrame (javax.swing.JFrame)1 EmptyBorder (javax.swing.border.EmptyBorder)1 ChangeEvent (javax.swing.event.ChangeEvent)1 ChangeListener (javax.swing.event.ChangeListener)1 TreePath (javax.swing.tree.TreePath)1