001: package org.cougaar.core.qos.frame.visualizer;
002:
003: import org.cougaar.util.log.Logger;
004: import org.cougaar.util.log.Logging;
005: import org.cougaar.core.qos.frame.visualizer.util.Vec2d;
006:
007: import java.awt.geom.Point2D;
008: import java.awt.geom.Rectangle2D;
009: import java.awt.*;
010:
011: /**
012: * Created by IntelliJ IDEA.
013: * User: mwalczak
014: * Date: Apr 4, 2005
015: * Time: 3:26:27 PM
016: * To change this template use File | Settings | File Templates.
017: */
018: public class Transition {
019: protected Point2D.Double fromPos;
020: protected Point2D.Double toPos;
021: protected ShapeGraphic shape;
022: protected ShapeContainer fromContainer, toContainer;
023: protected boolean firstStep, lastStep;
024: //double increment = 30d;
025: protected double angle, speed = 30d, tLength, xoff, yoff;
026:
027: protected Vec2d startVec, endVec, transVec;
028:
029: protected transient Logger log = Logging.getLogger(getClass()
030: .getName());
031:
032: public Transition(ShapeGraphic shape, ShapeContainer fromContainer,
033: ShapeContainer toContainer) {
034: this .shape = shape;
035: this .fromContainer = fromContainer;
036: this .toContainer = toContainer;
037: this .fromPos = (fromContainer != null ? fromContainer
038: .getNextInsertPosition() : null);
039: this .toPos = (toContainer != null ? toContainer
040: .getNextInsertPosition() : null);
041: firstStep = true;
042: lastStep = false;
043: }
044:
045: public boolean isFinished() {
046: return lastStep;
047: }
048:
049: // radians to degrees
050: public static float r2d(float radians) {
051: return ((radians / (float) (2.0 * Math.PI)) * 360.0f) % 360;
052: }
053:
054: // degree to radians
055: public static float d2r(float angle) {
056: return ((float) (angle * 2.0 * Math.PI)) / 360.0f;
057: }
058:
059: public boolean step() {
060: Rectangle2D r = shape.getBounds2D();
061: double tx = r.getX(), ty = r.getY();
062: this .fromPos = fromContainer.getNextInsertPosition();
063: this .toPos = toContainer.getNextInsertPosition();
064:
065: //double xoff=0d, yoff=0d;
066:
067: if (firstStep) {
068: if (fromContainer != null) {
069: fromContainer.remove(shape);
070: shape.resetSize();
071: }
072:
073: if (!Display.ENABLE_ANIMATION || fromPos == null) {
074: lastStep = true;
075: toContainer.add(shape);
076: return true;
077: }
078:
079: firstStep = false;
080: shape.reshape(fromPos.x, fromPos.y, r.getWidth(), r
081: .getHeight());
082:
083: startVec = new Vec2d(fromPos.x, fromPos.y);
084: endVec = new Vec2d(toPos.x, toPos.y);
085:
086: transVec = endVec.minus(startVec);
087: tLength = transVec.length();
088:
089: startVec.normalize();
090: endVec.normalize();
091: transVec.normalize();
092:
093: double d = (transVec.getY() / (transVec.getX() == 0d ? 0.01
094: : transVec.getX()));
095: angle = Math.atan(d);
096: xoff = (transVec.getX() < 0 ? -1d : 1d) * speed
097: * Math.cos(angle);
098: yoff = (transVec.getX() < 0 ? -1d : 1d) * speed
099: * Math.sin(angle);
100:
101: if (log.isDebugEnabled())
102: log.debug("starting transition, shape='"
103: + shape.getId() + "' from '"
104: + fromContainer.getId() + "' to '"
105: + toContainer.getId() + "'start x=" + fromPos.x
106: + " y=" + fromPos.y + " end x=" + toPos.x
107: + " y=" + toPos.y + " length=" + tLength
108: + " angle = " + angle);
109: }
110:
111: //Rectangle2D bounds = shape.getShape().getBounds2D();
112: if (((int) tx == (int) toPos.x && (int) ty == (int) toPos.y)
113: || toContainer.contains(tx, ty)) {
114: lastStep = true;
115: } else {
116: Vec2d t = new Vec2d((tx - fromPos.x), (ty - fromPos.y));
117: double length = t.length();
118: if (log.isDebugEnabled())
119: log.debug("current vector length=" + length
120: + " target length=" + tLength);
121:
122: if (length >= tLength)
123: lastStep = true;
124: }
125: if (lastStep) {
126: toContainer.add(shape);
127: // temp hack to get the slot listeners to update the values
128: //shape.validateListeners();
129: return true;
130: }
131:
132: tx += xoff;
133: ty += yoff;
134:
135: //if (log.isDebugEnabled())
136: // log.debug("transition= tx"+tx+" ty="+ty);
137:
138: /*
139: if ((int) tx < (int) toPos.x) tx+=increment;//tx++;
140: else if ((int) tx > (int) toPos.x) tx-=increment;//tx--;
141: else if ((int) tx == (int) toPos.x || toContainer.contains(tx,ty)) tx = toPos.x;
142:
143: if ((int) ty < (int) toPos.y) ty+=increment;//ty++;
144: else if ((int) ty > (int) toPos.y) ty-=increment;//ty--;
145: else if ((int) ty == (int) toPos.y || toContainer.contains(tx,ty)) ty = toPos.y;
146: */
147: shape.reshape(tx, ty, r.getWidth(), r.getHeight());
148: return false;
149: }
150:
151: public void draw(Graphics2D g2) {
152: shape.draw(g2);
153: shape.drawLabel(g2);
154: }
155:
156: public String toString() {
157: String from = (fromContainer != null ? fromContainer.id
158: : "null");
159: String to = (toContainer != null ? toContainer.id : "null");
160: return "Transition: move '" + shape.id + "' from '" + from
161: + "' to '" + to + "'";
162: }
163: }
|