001: package org.cougaar.core.qos.frame.visualizer;
002:
003: import java.awt.Dimension;
004: import java.awt.Graphics2D;
005: import java.awt.Rectangle;
006: import java.awt.Shape;
007: import java.awt.geom.Point2D;
008: import java.awt.geom.Rectangle2D;
009: import java.awt.geom.RectangularShape;
010: import java.util.ArrayList;
011: import java.util.Collection;
012: import java.util.HashSet;
013: import java.util.Iterator;
014:
015: import org.cougaar.core.qos.frame.visualizer.test.FramePredicate;
016: import org.cougaar.core.qos.frame.visualizer.util.SlotChangeListener;
017: import org.cougaar.util.log.Logger;
018: import org.cougaar.util.log.Logging;
019:
020: public class ShapeGraphic implements Cloneable {
021: protected String id, label;
022: protected LabelRenderer labelRenderer;
023: protected ShapeRenderer renderer;
024: protected RectangularShape shape, shapePrototype;
025: protected double x, y, width, height;
026: protected double originalWidth, originalHeight; // remember original size (some layout mgrs do resizeing)
027: protected boolean visible, selected, mouseOver, isPrototype;
028: protected ShapeContainer parent;
029:
030: // data
031: protected org.cougaar.core.qos.frame.Frame frame;
032: protected String frameidSlotName = "";
033: protected FramePredicate predicate;
034: protected FrameModel frameModel;
035:
036: // slot value triggers
037: protected ArrayList slotListeners;
038: // logging
039: protected Logger log = Logging.getLogger(getClass().getName());
040:
041: // for the container tree display
042: //protected ShapeGraphicNode treeNode;
043:
044: public ShapeGraphic() {
045: this (null, null);
046: }
047:
048: protected ShapeGraphic(String id, String label) {
049: this .id = id;
050: this .label = label;
051: x = y = 0d;
052: width = height = 10d;
053: visible = true;
054: selected = mouseOver = false;
055: slotListeners = new ArrayList();
056: //treeNode = null;
057: }
058:
059: /*
060: public ShapeGraphicNode getTreeNode() {
061: if (treeNode == null)
062: treeNode = new ShapeGraphicNode(this, (parent != null ? parent.getTreeNode() : null));
063: return treeNode;
064: } */
065:
066: public void setMouseOver(boolean mouseOver) {
067: this .mouseOver = mouseOver;
068: }
069:
070: public boolean isMouseOver() {
071: return mouseOver;
072: }
073:
074: public void addSlotListener(SlotChangeListener l) {
075: l.setShapeGraphic(this );
076: slotListeners.add(l);
077: }
078:
079: public Collection getSlotListeners() {
080: return slotListeners;
081: }
082:
083: public void validateListeners() {
084: SlotChangeListener sl;
085: for (Iterator ii = slotListeners.iterator(); ii.hasNext();) {
086: sl = (SlotChangeListener) ii.next();
087: sl.validate();
088: }
089: }
090:
091: public void processFrameChange(org.cougaar.core.qos.frame.Frame f,
092: org.cougaar.core.qos.frame.Frame.Change change) {
093: if (f == frame && slotListeners.size() > 0) {
094: for (Iterator ii = slotListeners.iterator(); ii.hasNext();)
095: ((SlotChangeListener) ii.next()).slotChanged(f, change);
096: }
097: }
098:
099: public void setLabelRenderer(LabelRenderer lbl) {
100: this .labelRenderer = lbl;
101: }
102:
103: public LabelRenderer getLabelRenderer() {
104: return labelRenderer;
105: }
106:
107: public void setRenderer(ShapeRenderer shapeRenderer) {
108: this .renderer = shapeRenderer;
109: }
110:
111: public String toString() {
112: return "[" + (isContainer() ? "Container" : "Component")
113: + " id=" + id + " name=" + label + " proto="
114: + isPrototype + " " + predicate + "]";
115: }
116:
117: public boolean isContainer() {
118: return false;
119: }
120:
121: public boolean isPrototype() {
122: return isPrototype;
123: }
124:
125: public void setPrototype(boolean isPrototype) {
126: this .isPrototype = isPrototype;
127: }
128:
129: public String getId() {
130: return id;
131: }
132:
133: public void setId(String id) {
134: this .id = id;
135: }
136:
137: public String getLabel() {
138: return label;
139: }
140:
141: public void setLabel(String lbl) {
142: label = lbl;
143: }
144:
145: public void setFrameIdSlotName(String slotName) {
146: frameidSlotName = slotName;
147: }
148:
149: public String getFrameIdSlotName() {
150: return frameidSlotName;
151: }
152:
153: public void setFrame(org.cougaar.core.qos.frame.Frame frame) {
154: this .frame = frame;
155: if (frame != null) {
156: this .id = (String) frame.getValue("name"); //frameidSlotName);
157: this .label = id;
158: if (log.isDebugEnabled())
159: log.debug(label + ".setFrame(): id=" + this .id + ": "
160: + this .toString());
161: frameModel.registerGraphic(frame, this );
162: } else {
163: this .id = "";
164: this .label = "";
165: }
166: }
167:
168: public void unregister() {
169: if (frame != null && frameModel != null) {
170: frameModel.unregisterGraphic(frame, this );
171: }
172: }
173:
174: public void update(FrameModel frameModel, HashSet addedDataFrames,
175: HashSet removedDataFrames, HashSet addedRelations) {
176: this .frameModel = frameModel;
177: if (frame == null && predicate != null) {
178: if (log.isDebugEnabled())
179: log.debug("ShapeGraphic.update");
180: setFrame(frameModel.findFrame(predicate)); //(addedDataFrames!=null ? frameModel.findFrame(addedDataFrames, predicate) : frameModel.findFrame(predicate)));
181: }
182: }
183:
184: /*
185: public void addedFrames(Collection newFrames) {
186: if (frame == null && predicate != null) {
187: setFrame(frameModel.findFrame(newFrames, predicate));
188: }
189: }*/
190:
191: public boolean hasFrame() {
192: return frame != null;
193: }
194:
195: public org.cougaar.core.qos.frame.Frame getFrame() {
196: if (frame != null)
197: return frame;
198: return getParentFrame();
199: }
200:
201: public void setFramePredicate(FramePredicate p) {
202: predicate = p;
203: }
204:
205: public FramePredicate getFramePredicate() {
206: return predicate;
207: }
208:
209: public void setParent(ShapeContainer container) {
210: this .parent = container;
211: /*
212: treeNode = getTreeNode();
213: if (treeNode != null) {
214: ShapeGraphicNode oldParentNode = (ShapeGraphicNode) treeNode.getParent();
215: ShapeGraphicNode newParentNode = parent.getTreeNode();
216: if (oldParentNode != null && oldParentNode != newParentNode) {
217: newParentNode.add(treeNode);
218: //if (frameModel != null)
219: // frameModel.containerTreeNodeChanged(newParentNode, treeNode);
220: }
221: } */
222: }
223:
224: public ShapeContainer getParent() {
225: return parent;
226: }
227:
228: public ShapeContainer getParentWithFrame() {
229: ShapeContainer p = parent;
230: while (p != null) {
231: if (p.frame != null)
232: return p;
233: p = p.getParent();
234: }
235: return null;
236: }
237:
238: public org.cougaar.core.qos.frame.Frame getParentFrame() {
239: ShapeContainer p = parent;
240: org.cougaar.core.qos.frame.Frame f;
241: while (p != null) {
242: f = (p != null ? p.getFrame() : null);
243: if (f != null)
244: return f;
245: p = p.getParent();
246: }
247: return null;
248: }
249:
250: public void setVisible(boolean visible) {
251: this .visible = visible;
252: }
253:
254: public void setSelected(boolean selected) {
255: this .selected = selected;
256: }
257:
258: public boolean isSelected() {
259: return selected;
260: }
261:
262: public boolean isVisible() {
263: return visible;
264: }
265:
266: public Shape getShape() {
267: if (shape == null)
268: shape = createShape();
269: return shape;
270: }
271:
272: public void setShapePrototype(RectangularShape shape) {
273: this .shapePrototype = shape;
274: if (shape != null) {
275: Rectangle2D r = shape.getFrame();
276: originalWidth = (r.getWidth() > 0 ? r.getWidth()
277: : originalWidth);
278: originalHeight = (r.getHeight() > 0 ? r.getHeight()
279: : originalHeight);
280: }
281: }
282:
283: public RectangularShape createShape() {
284: if (shapePrototype != null) {
285: RectangularShape tmp = shape;
286: shape = (RectangularShape) shapePrototype.clone();
287: if (tmp != null)
288: shape.setFrame(tmp.getFrame()); // this is a Java2D frame
289: }
290: return shape;
291: }
292:
293: public boolean contains(double mx, double my) {
294: Shape sh = getShape();
295: return (sh != null ? sh.contains(mx, my) : false);
296: }
297:
298: public ShapeGraphic find(double mx, double my) {
299: return (contains(mx, my) ? this : null);
300: }
301:
302: public ShapeGraphic find(org.cougaar.core.qos.frame.Frame f) {
303: if (frame != null && frame == f) //id != null && id.equals((String) f.getValue("name")))
304: return this ;
305: return null;
306: }
307:
308: public void reshape(double x, double y, double width, double height) {
309: this .x = x;
310: this .y = y;
311: this .width = width;
312: this .height = height;
313: if (shape != null)
314: shape.setFrame(x, y, width, height);
315: }
316:
317: public void resetSize() {
318: this .width = originalWidth;
319: this .height = originalHeight;
320: if (shape != null)
321: shape.setFrame(x, y, width, height);
322: }
323:
324: public Point2D.Double getPosition() {
325: return new Point2D.Double(x, y);
326: }
327:
328: public void draw(Graphics2D g2) {
329: if (renderer != null && visible)
330: renderer.drawShape(g2, this );
331: }
332:
333: public void drawLabel(Graphics2D g2) {
334: if (labelRenderer != null && visible)
335: labelRenderer.drawLabel(g2, this );
336: }
337:
338: public Dimension getSize() {
339: Rectangle r = getBounds();
340: return new Dimension(r.width, r.height);
341: }
342:
343: public Rectangle getBounds() {
344: return getShape().getBounds();
345: }
346:
347: public Rectangle2D getBounds2D() {
348: return getShape().getBounds2D();
349: }
350:
351: // clone thyself and assign the given frame
352: public ShapeGraphic createInstance(
353: org.cougaar.core.qos.frame.Frame frame, FrameModel fmodel) {
354: try {
355: ShapeGraphic cloned = (ShapeGraphic) this .clone();
356: cloned.frameModel = fmodel;
357: if (frame != null)
358: cloned.setFrame(frame);
359: cloned.setPrototype(false);
360: cloned.shapePrototype = (shapePrototype != null ? ((RectangularShape) shapePrototype
361: .clone())
362: : null);
363: cloned.shape = cloned.createShape();
364: org.cougaar.core.qos.frame.Frame parent = getFrame();
365: if (cloned.predicate != null && parent != null)
366: cloned.predicate = new FramePredicate(cloned.predicate,
367: (String) parent.getValue("name"));
368:
369: if (slotListeners.size() > 0) {
370: cloned.slotListeners = new ArrayList();
371: for (Iterator ii = slotListeners.iterator(); ii
372: .hasNext();) {
373: cloned.addSlotListener(((SlotChangeListener) ii
374: .next()).cloneInstance());
375: }
376: }
377: cloned.validateListeners();
378: return cloned;
379: } catch (CloneNotSupportedException ee) {
380: ee.printStackTrace();
381: }
382: return null;
383: }
384: }
|