1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/visidor/src/visidor/Viewer.java Sun May 24 14:14:25 2009 +0200
1.3 @@ -0,0 +1,139 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +
1.9 +package visidor;
1.10 +
1.11 +import java.awt.Color;
1.12 +import java.awt.Point;
1.13 +import java.awt.Rectangle;
1.14 +import javax.swing.JFrame;
1.15 +import org.netbeans.api.visual.action.ActionFactory;
1.16 +import org.netbeans.api.visual.action.MoveProvider;
1.17 +import org.netbeans.api.visual.anchor.AnchorFactory;
1.18 +import org.netbeans.api.visual.border.BorderFactory;
1.19 +import org.netbeans.api.visual.widget.ConnectionWidget;
1.20 +import org.netbeans.api.visual.widget.ImageWidget;
1.21 +import org.netbeans.api.visual.widget.LabelWidget;
1.22 +import org.netbeans.api.visual.widget.LayerWidget;
1.23 +import org.netbeans.api.visual.widget.Scene;
1.24 +import org.netbeans.api.visual.widget.Widget;
1.25 +
1.26 +/**
1.27 + *
1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.29 + */
1.30 +public class Viewer implements MoveProvider {
1.31 +
1.32 + /**
1.33 + * @param args the command line arguments
1.34 + */
1.35 + public static void main(String[] args) {
1.36 + Scene scene = new Scene();
1.37 +// Scene layerBoard = scene;
1.38 + final LayerWidget layerLines = new LayerWidget(scene);
1.39 + scene.addChild(layerLines);
1.40 + final LayerWidget layerBoard = new LayerWidget(scene);
1.41 + layerLines.addChild(layerBoard);
1.42 + final int WIDTH = 50;
1.43 + final int HEIGHT = 50;
1.44 +// conn.setSourceAnchor(ahoj);
1.45 + Widget[][] fields = new Widget[10][];
1.46 + for (int i = 0; i < fields.length; i++) {
1.47 + fields[i] = new Widget[10];
1.48 + for (int j = 0; j < fields[i].length; j++) {
1.49 + LabelWidget w = new LabelWidget(scene);
1.50 + w.setPreferredBounds(new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT));
1.51 + layerBoard.addChild(w);
1.52 + fields[i][j] = w;
1.53 + }
1.54 + }
1.55 + for (int i = 0; i < fields.length; i++) {
1.56 + for (int j = 0; j < fields[i].length; j++) {
1.57 + if (i > 0) {
1.58 + ConnectionWidget horiz = new ConnectionWidget(scene);
1.59 + horiz.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i - 1][j]));
1.60 + horiz.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
1.61 + scene.addChild(horiz);
1.62 + }
1.63 + if (j > 0) {
1.64 + ConnectionWidget vert = new ConnectionWidget(scene);
1.65 + vert.setSourceAnchor(AnchorFactory.createCenterAnchor(fields[i][j - 1]));
1.66 + vert.setTargetAnchor(AnchorFactory.createCenterAnchor(fields[i][j]));
1.67 + scene.addChild(vert);
1.68 + }
1.69 + }
1.70 + }
1.71 +
1.72 + ImageWidget horizontalWall = new ImageWidget(scene);
1.73 + horizontalWall.setPreferredBounds(new Rectangle(550, 300, 100, 10));
1.74 + horizontalWall.setBackground(Color.BLACK);
1.75 + horizontalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(true)));
1.76 + horizontalWall.setForeground(Color.BLACK);
1.77 + horizontalWall.setBorder(BorderFactory.createLineBorder());
1.78 + layerBoard.addChild(horizontalWall);
1.79 +
1.80 + ImageWidget verticalWall = new ImageWidget(scene);
1.81 + verticalWall.setPreferredBounds(new Rectangle(600, 150, 10, 100));
1.82 + verticalWall.setBackground(Color.BLACK);
1.83 + verticalWall.getActions().addAction(ActionFactory.createMoveAction(null, new Viewer(false)));
1.84 + verticalWall.setBorder(BorderFactory.createLineBorder());
1.85 + layerBoard.addChild(verticalWall);
1.86 +
1.87 + JFrame f = new JFrame();
1.88 + f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1.89 +
1.90 + f.add(scene.createView());
1.91 + f.pack();
1.92 + f.setVisible(true);
1.93 + }
1.94 +
1.95 + final boolean horizontal;
1.96 +
1.97 + Point orig;
1.98 +
1.99 + private Viewer(boolean horizontal) {
1.100 + this.horizontal = horizontal;
1.101 + }
1.102 + public void movementStarted(Widget widget) {
1.103 + System.err.println("started: " + widget.getBounds());
1.104 + }
1.105 +
1.106 + public void movementFinished(Widget widget) {
1.107 + System.err.println("finished: " + widget.getLocation());
1.108 + }
1.109 +
1.110 + public Point getOriginalLocation(Widget widget) {
1.111 + orig = new Point(widget.getBounds().x, widget.getBounds().y);
1.112 + System.err.println("orig: " + orig);
1.113 + return orig;
1.114 + }
1.115 +
1.116 + public void setNewLocation(Widget widget, Point location) {
1.117 + widget.setPreferredBounds(new Rectangle(
1.118 + round(location.x, true),
1.119 + round(location.y, false),
1.120 + horizontal ? 100 : 10,
1.121 + horizontal ? 10 : 100)
1.122 + );
1.123 + }
1.124 +
1.125 + int round(int p, boolean cmpHori) {
1.126 +// p = horizontal ? orig.x + p: orig.y + p;
1.127 + int onboard = p / 50;
1.128 + System.err.println(cmpHori ? "hori: " + onboard : " vert: " + onboard);
1.129 + if (onboard < 0) {
1.130 + onboard = 0;
1.131 + }
1.132 + if (onboard >= 9) {
1.133 + return p;
1.134 + }
1.135 + int real = 25 + onboard * 50;
1.136 + if (horizontal != cmpHori) {
1.137 + return real - 5;
1.138 + } else {
1.139 + return real;
1.140 + }
1.141 + }
1.142 +}