001: package org.cougaar.core.qos.frame.visualizer;
002:
003: import java.awt.Dimension;
004: import java.awt.Graphics2D;
005: import java.awt.Point;
006: import java.awt.event.MouseEvent;
007: import java.awt.event.MouseListener;
008: import java.awt.event.MouseMotionListener;
009: import java.util.HashMap;
010:
011: import org.cougaar.util.log.Logger;
012: import org.cougaar.util.log.Logging;
013:
014: /**
015: * Created by IntelliJ IDEA.
016: * User: mwalczak
017: * Date: Apr 4, 2005
018: * Time: 2:10:44 PM
019: * To change this template use File | Settings | File Templates.
020: */
021: public class AnimatedCanvas extends AnimatingSurface implements
022: MouseListener, MouseMotionListener {
023: protected HashMap shapes;
024: protected boolean mouseMoveFlag = false;
025: protected Point mousePoint = null, lastPoint = null;
026: protected Point mouseStartDragPoint = null;
027: protected ShapeGraphic selectedShape, mouseOverShape;
028: protected Dimension oldSize;
029: private transient Logger log = Logging.getLogger(getClass()
030: .getName());
031:
032: //temp
033: int count = 0;
034:
035: public AnimatedCanvas() {
036: super ();
037: shapes = new HashMap();
038: selectedShape = null;
039: mouseOverShape = null;
040: addMouseListener(this );
041: addMouseMotionListener(this );
042: }
043:
044: public void reset(int w, int h) {
045: /*ShapeGraphic shape;
046: for (Iterator ii=shapes.values().iterator(); ii.hasNext();) {
047: shape = (ShapeGraphic) ii.next();
048: shape.reset(w, h);
049: }
050: */
051: oldSize = getSize();
052: }
053:
054: public void step(int w, int h) {
055: /*ShapeGraphic shape;
056: for (Iterator ii=shapes.values().iterator(); ii.hasNext();) {
057: shape = (ShapeGraphic) ii.next();
058: shape.step(w, h);
059: }
060: */
061: }
062:
063: public ShapeGraphic get(String shapeId) {
064: return (ShapeGraphic) shapes.get(shapeId);
065: }
066:
067: public void register(ShapeGraphic shape) {
068: if (shapes.get(shape.getId()) == null)
069: shapes.put(shape.getId(), shape);
070: }
071:
072: public void unregister(ShapeGraphic shape) {
073: if (shapes.get(shape.getId()) != null)
074: shapes.remove(shape.getId());
075: }
076:
077: public void render(int w, int h, Graphics2D g2) {
078: //System.out.println("SassiAnimatedSurface.render w="+w+" h="+h);
079: Dimension d = getSize();
080: if (oldSize.width != d.width || oldSize.height != d.height)
081: reset(w, h);
082: }
083:
084: public ShapeGraphic findShape(double x, double y) {
085: //ShapeGraphic shapes[] = findShapes(x,y);
086: //return (shapes != null && shapes.length > 0 ? shapes[0] : null);
087: return null;
088: }
089:
090: //////////////////////////////////////////////////////////////////////////////////
091: // mouse event handlers
092: //////////////////////////////////////////////////////////////////////////////////
093: public void mouseEntered(MouseEvent evt) {
094: }
095:
096: public void mouseExited(MouseEvent evt) {
097: }
098:
099: public void mouseClicked(MouseEvent evt) {
100: }
101:
102: public void mousePressed(MouseEvent evt) {
103: //System.out.println("AnimatedCanvas.mousePressed");
104: if (mouseMoveFlag == false) {
105: // start drag
106: mouseMoveFlag = true;
107: mousePoint = evt.getPoint();
108: mouseStartDragPoint = mousePoint;
109: if (selectedShape != null)
110: selectedShape.setSelected(false);
111: selectedShape = findShape(mousePoint.x, mousePoint.y);
112: if (selectedShape != null) {
113: if (log.isDebugEnabled())
114: log.debug("selecting = " + selectedShape);
115:
116: /*if (selectedShape instanceof ShapeContainer) {
117: String childString="";
118: ShapeGraphic ch;
119: for (Iterator ii=((ShapeContainer)selectedShape).getChildren().iterator(); ii.hasNext();) {
120: ch = (ShapeGraphic)ii.next();
121: childString=childString+ ", "+ch.getId();
122: }
123: //if (log.isDebugEnabled())
124: // log.debug("------>children:"+childString);
125: childString = "";
126: for (Iterator ii=((ShapeContainer)selectedShape).getPrototypes().iterator(); ii.hasNext();) {
127: ch = (ShapeGraphic) ii.next();
128: childString=childString+ ", "+ch.getId();
129: }
130: if (log.isDebugEnabled())
131: log.debug("------>prototypes:"+childString);
132: }
133: */
134: selectedShape.setSelected(true);
135: }
136: }
137: }
138:
139: public void mouseReleased(MouseEvent evt) {
140: mouseMoveFlag = false;
141: mousePoint = null;
142: mouseStartDragPoint = null;
143: }
144:
145: // MouseMotionListener interface
146: public void mouseDragged(MouseEvent e) {
147: if (mouseMoveFlag == true)
148: mousePoint = e.getPoint();
149: }
150:
151: public void mouseMoved(MouseEvent e) {
152: //System.out.println("mouseMoved");
153: Point mp = e.getPoint();
154: ShapeGraphic tmp = mouseOverShape;
155: mouseOverShape = findShape(mp.x, mp.y);
156: if (tmp == mouseOverShape)
157: return;
158: if (tmp != null)
159: tmp.setMouseOver(false);
160:
161: if (mouseOverShape != null)
162: mouseOverShape.setMouseOver(true);
163: }
164: }
|