Search in sources :

Example 1 with SimpleNode

use of org.eclipse.zest.layouts.exampleStructures.SimpleNode in project archi by archimatetool.

the class SimpleSWTExample method createSimpleNode.

/**
 * Creates a SimpleNode
 * @param name
 * @return SimpleNode
 */
private SimpleNode createSimpleNode(String name) {
    SimpleNode simpleNode = new SimpleNode(name);
    // an initial approximation of the width
    int w = name.length() * 8;
    simpleNode.setSizeInLayout(Math.max(w, INITIAL_NODE_WIDTH), INITIAL_NODE_HEIGHT);
    return simpleNode;
}
Also used : Point(org.eclipse.swt.graphics.Point) LayoutBendPoint(org.eclipse.zest.layouts.LayoutBendPoint) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode)

Example 2 with SimpleNode

use of org.eclipse.zest.layouts.exampleStructures.SimpleNode in project archi by archimatetool.

the class SimpleSWTExample method placeRandomly.

/**
 * Places nodes randomly on the screen *
 */
private void placeRandomly() {
    for (Iterator iter = entities.iterator(); iter.hasNext(); ) {
        SimpleNode simpleNode = (SimpleNode) iter.next();
        double x = Math.random() * INITIAL_PANEL_WIDTH - INITIAL_NODE_WIDTH;
        double y = Math.random() * INITIAL_PANEL_HEIGHT - INITIAL_NODE_HEIGHT;
        simpleNode.setLocationInLayout(x, y);
    }
}
Also used : Iterator(java.util.Iterator) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode)

Example 3 with SimpleNode

use of org.eclipse.zest.layouts.exampleStructures.SimpleNode in project archi by archimatetool.

the class SimpleSWTExample method createTreeGraph.

/**
 * @param maxLevels Max number of levels wanted in tree
 * @param maxChildren Max number of children for each node in the tree
 * @param random Whether or not to pick random number of levels (from 1 to maxLevels) and
 * random number of children (from 1 to maxChildren)
 */
private void createTreeGraph(int maxLevels, int maxChildren, boolean random) {
    entities = new ArrayList();
    relationships = new ArrayList();
    // ccallendar - testing out having 2 roots
    SimpleNode root = createSimpleNode(getNextID());
    entities.add(root);
    SimpleNode root2 = createSimpleNode(getNextID());
    entities.add(root2);
    // end
    SimpleNode currentParent = createSimpleNode(getNextID());
    entities.add(currentParent);
    // ccallendar - adding relationships from the parent to the 2 roots
    SimpleRelationship rel = new SimpleRelationship(root, currentParent, false);
    root.addRelationship(rel);
    currentParent.addRelationship(rel);
    relationships.add(rel);
    rel = new SimpleRelationship(root2, currentParent, false);
    root2.addRelationship(rel);
    currentParent.addRelationship(rel);
    relationships.add(rel);
    // end
    int levels = random ? (int) (Math.random() * maxLevels + 1) : maxLevels;
    createTreeGraphRecursive(currentParent, maxChildren, levels, 1, random);
}
Also used : ArrayList(java.util.ArrayList) SimpleRelationship(org.eclipse.zest.layouts.exampleStructures.SimpleRelationship) Point(org.eclipse.swt.graphics.Point) LayoutBendPoint(org.eclipse.zest.layouts.LayoutBendPoint) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode)

Example 4 with SimpleNode

use of org.eclipse.zest.layouts.exampleStructures.SimpleNode in project archi by archimatetool.

the class SimpleSWTExample method createMainPanel.

private void createMainPanel() {
    mainComposite = new Canvas(mainShell, SWT.NO_BACKGROUND);
    GridData mainGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, true, true);
    mainGridData.widthHint = INITIAL_PANEL_WIDTH;
    mainGridData.heightHint = INITIAL_PANEL_HEIGHT;
    mainComposite.setLayoutData(mainGridData);
    mainComposite.addPaintListener(new GraphPaintListener());
    mainComposite.setBackground(new Color(255, 255, 255));
    mainComposite.setLayout(null);
    mainComposite.addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            if (selectedEntity == null) {
                // Nothing selected, lets use a mouse hover
                SimpleNode oldEntity = hoverEntity;
                hoverEntity = null;
                for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity == null; ) {
                    SimpleNode entity = (SimpleNode) iter.next();
                    double x = entity.getX();
                    double y = entity.getY();
                    double w = entity.getWidth();
                    double h = entity.getHeight();
                    Rectangle rect = new Rectangle((int) x, (int) y, (int) w, (int) h);
                    if (rect.contains(e.x, e.y)) {
                        hoverEntity = entity;
                        hoverEntity.ignoreInLayout(true);
                        hoverEntity.setSelected();
                        break;
                    }
                }
                if (oldEntity != null && oldEntity != hoverEntity) {
                    oldEntity.ignoreInLayout(false);
                    oldEntity.setUnSelected();
                }
            }
        }
    });
    mainComposite.addMouseListener(new MouseListener() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
        }

        @Override
        public void mouseDown(MouseEvent e) {
            selectedEntity = null;
            hoverEntity = null;
            for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity == null; ) {
                SimpleNode entity = (SimpleNode) iter.next();
                double x = entity.getX();
                double y = entity.getY();
                double w = entity.getWidth();
                double h = entity.getHeight();
                Rectangle rect = new Rectangle((int) x, (int) y, (int) w, (int) h);
                if (rect.contains(e.x, e.y)) {
                    selectedEntity = entity;
                }
            }
            if (selectedEntity != null) {
                mouseDownPoint = new Point(e.x, e.y);
                selectedEntityPositionAtMouseDown = new Point((int) selectedEntity.getX(), (int) selectedEntity.getY());
                selectedEntity.ignoreInLayout(true);
                selectedEntity.setSelected();
            } else {
                mouseDownPoint = null;
                selectedEntityPositionAtMouseDown = null;
            }
        }

        @Override
        public void mouseUp(MouseEvent e) {
            if (selectedEntity != null) {
                selectedEntity.ignoreInLayout(false);
                selectedEntity.setUnSelected();
                List relatedNodes = selectedEntity.getRelatedEntities();
                for (Iterator iter = relatedNodes.iterator(); iter.hasNext(); ) {
                    SimpleNode element = (SimpleNode) iter.next();
                    element.setUnSelected();
                }
                SimpleRelationship[] rels = selectedEntity.getRelationships();
                for (int i = 0; i < rels.length; i++) {
                    rels[i].resetLineWidth();
                }
            }
            selectedEntity = null;
            mouseDownPoint = null;
            selectedEntityPositionAtMouseDown = null;
        }
    });
    // stops the algorithm when the window is closed
    mainComposite.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            if (currentLayoutAlgorithm != null) {
                currentLayoutAlgorithm.stop();
            }
        }
    });
    mainComposite.addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            if (selectedEntity != null && mouseDownPoint != null) {
                double dx = e.x - mouseDownPoint.x;
                double dy = e.y - mouseDownPoint.y;
                selectedEntity.setLocation(selectedEntityPositionAtMouseDown.x + dx, selectedEntityPositionAtMouseDown.y + dy);
                mainComposite.redraw();
            }
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) MouseEvent(org.eclipse.swt.events.MouseEvent) Canvas(org.eclipse.swt.widgets.Canvas) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) LayoutBendPoint(org.eclipse.zest.layouts.LayoutBendPoint) DisposeEvent(org.eclipse.swt.events.DisposeEvent) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode) MouseMoveListener(org.eclipse.swt.events.MouseMoveListener) MouseListener(org.eclipse.swt.events.MouseListener) GridData(org.eclipse.swt.layout.GridData) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with SimpleNode

use of org.eclipse.zest.layouts.exampleStructures.SimpleNode in project archi by archimatetool.

the class SimpleSwingExample method placeRandomly.

/**
 * Places nodes randomly on the screen *
 */
private void placeRandomly() {
    for (Iterator iter = entities.iterator(); iter.hasNext(); ) {
        SimpleNode simpleNode = (SimpleNode) iter.next();
        double x = Math.random() * INITIAL_PANEL_WIDTH - INITIAL_NODE_WIDTH;
        double y = Math.random() * INITIAL_PANEL_HEIGHT - INITIAL_NODE_HEIGHT;
        simpleNode.setLocationInLayout(x, y);
        simpleNode.setSizeInLayout(INITIAL_NODE_WIDTH, INITIAL_NODE_HEIGHT);
    }
}
Also used : Iterator(java.util.Iterator) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode)

Aggregations

SimpleNode (org.eclipse.zest.layouts.exampleStructures.SimpleNode)7 LayoutBendPoint (org.eclipse.zest.layouts.LayoutBendPoint)5 Iterator (java.util.Iterator)4 Point (org.eclipse.swt.graphics.Point)4 ArrayList (java.util.ArrayList)2 SimpleRelationship (org.eclipse.zest.layouts.exampleStructures.SimpleRelationship)2 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 MouseMotionListener (java.awt.event.MouseMotionListener)1 Rectangle2D (java.awt.geom.Rectangle2D)1 List (java.util.List)1 JScrollPane (javax.swing.JScrollPane)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 MouseEvent (org.eclipse.swt.events.MouseEvent)1 MouseListener (org.eclipse.swt.events.MouseListener)1 MouseMoveListener (org.eclipse.swt.events.MouseMoveListener)1 Color (org.eclipse.swt.graphics.Color)1