source: trunk/src/org/expeditee/items/Item.java@ 29

Last change on this file since 29 was 29, checked in by ra33, 16 years ago
File size: 33.8 KB
Line 
1package org.expeditee.items;
2
3import java.awt.Color;
4import java.awt.Cursor;
5import java.awt.Dimension;
6import java.awt.Graphics2D;
7import java.awt.Point;
8import java.awt.Polygon;
9import java.awt.Rectangle;
10import java.awt.geom.Area;
11import java.util.ArrayList;
12import java.util.ConcurrentModificationException;
13import java.util.LinkedList;
14import java.util.List;
15
16import org.expeditee.actions.Actions;
17import org.expeditee.actions.Simple;
18import org.expeditee.gui.DisplayIO;
19import org.expeditee.gui.Frame;
20import org.expeditee.gui.FrameGraphics;
21import org.expeditee.gui.FrameIO;
22import org.expeditee.gui.FrameUtils;
23import org.expeditee.io.Conversion;
24import org.expeditee.io.Logger;
25import org.expeditee.simple.Context;
26import org.expeditee.stats.AgentStats;
27
28/**
29 * Represents everything that can be drawn on the screen (text, lines, dots,
30 * images). Each specific type is a subclass of Item.
31 *
32 * @author jdm18
33 *
34 */
35public abstract class Item implements Comparable<Item>, Runnable {
36
37 public static int PERMISSION_NONE = 0;
38 public static int PERMISSION_FOLLOW_LINKS = 1;
39 public static int PERMISSION_COPY = 2;
40 public static int PERMISSION_TDFC = 3;
41 public static int PERMISSION_FULL = 4;
42
43 public static final int NEAR_DISTANCE = 15;
44
45 /**
46 * The default Color to draw highlighting in
47 */
48 public static final int DEFAULT_HIGHLIGHT_THICKNESS = 2;
49
50 public static final Color DEFAULT_HIGHLIGHT = Color.RED;
51
52 public static final Color DEPRESSED_HIGHLIGHT = Color.GREEN;
53
54 public static final Color DISCONNECT_HIGHLIGHT = Color.BLUE;
55
56 public static final Color LINK_COLOR = Color.BLACK;
57
58 public static final Color ACTION_COLOR = Color.BLACK;
59
60 public static final Color LINK_ACTION_COLOR = Color.RED;
61
62 public static final Color DEFAULT_FOREGROUND = Color.BLACK;
63
64 public static final Color DEFAULT_BACKGROUND = org.expeditee.gui.DisplayIO.DEFAULT_BACKGROUND;
65
66 /**
67 * The number of pixels highlighting should extend around Items.
68 */
69 public static final int XGRAVITY = 3;
70
71 public static final int MARGIN_RIGHT = 10;
72
73 public static final int MARGIN_LEFT = 20;
74
75 protected static final double DEFAULT_ARROWHEAD_RATIO = 0.5;
76
77 public static final Color GREEN = Color.GREEN.darker();
78
79 /**
80 * The Colors cycled through when using function keys to set the Color of
81 * this Item.
82 */
83 public static Color[] COLOR_WHEEL = { Color.BLACK, Color.RED,
84 Color.BLUE, Item.GREEN, Color.MAGENTA, Color.YELLOW.darker(),
85 DisplayIO.DEFAULT_BACKGROUND };
86
87 public static Color[] FILL_COLOR_WHEEL = { Color.BLACK, new Color(255,150,150),
88 new Color(150,150,255), new Color(150,255,150), new Color(255,150,255), new Color(255,255,100),
89 DisplayIO.DEFAULT_BACKGROUND };
90
91 public static final int UNCHANGED_CURSOR = -100;
92
93 public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
94
95 public static final int HIDDEN_CURSOR = Cursor.CUSTOM_CURSOR;
96
97 public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
98
99 public static final int CROP_CURSOR = Cursor.CROSSHAIR_CURSOR;
100
101 public static final int JUSTIFICATION_NONE = -1;
102
103 // private boolean _isValidLink = true;
104
105 public static final int JUSTIFICATION_FULL = 0;
106
107 public static final int JUSTIFICATION_CENTER = 1;
108
109 public static final int JUSTIFICATION_RIGHT = 2;
110
111 public static final int JUSTIFICATION_LEFT = 3;
112
113 public static final int POINTTYPE_SQUARE = -1;
114
115 public static final int POINTTYPE_CIRCLE = 0;
116
117 public static void DuplicateItem(Item source, Item dest) {
118 dest.setX(source.getX());
119 dest.setY(source.getY());
120
121 dest.setAction(source.getAction());
122 dest.setActionCursorEnter(source.getActionCursorEnter());
123 dest.setActionCursorLeave(source.getActionCursorLeave());
124 dest.setActionEnterFrame(source.getActionEnterFrame());
125 dest.setActionLeaveFrame(source.getActionLeaveFrame());
126 dest.setActionMark(source.getActionMark());
127 dest.setAnnotation(source.isAnnotation());
128
129 dest.setBackgroundColor(source.getBackgroundColor());
130 dest.setBottomShadowColor(source.getBottomShadowColor());
131 dest.setColor(source.getColor());
132
133 dest.setData(source.getData());
134 dest.setFillColor(source.getFillColor());
135 dest.setFillPattern(source.getFillPattern());
136
137 dest.setHighlight(source.getHighlight());
138 dest.setLink(source.getLink());
139 dest.setLinkFrameset(source.getLinkFrameset());
140 dest.setLinkMark(source.getLinkMark());
141 dest.setLinkTemplate(source.getLinkTemplate());
142
143 dest.setMaxSize(source.getMaxSize());
144 dest.setOffset(source.getOffset());
145 dest.setOwner(source.getOwner());
146 dest.setSize(source.getSize());
147 dest.setTopShadowColor(source.getTopShadowColor());
148 dest.setLinePattern(source.getLinePattern());
149
150 dest.setFloating(source.isFloating());
151 dest.setArrow(source.getArrowheadLength(), source.getArrowheadRatio());
152
153 dest.setParent(source.getParent());
154 // Add the copied item to the frame
155 if (source.getParent() != null) {
156 dest.setID(source.getParent().getNextItemID());
157 } else {
158 dest.setID(source.getID());
159 }
160 }
161
162 public static int getGravity() {
163 return org.expeditee.gui.UserSettings.Gravity;
164 }
165
166 public static boolean showLineHighlight() {
167 return org.expeditee.gui.UserSettings.LineHighlight;
168 }
169
170 public enum SelectedMode {
171 None, Enclosed, Connected, Disconnect, Normal
172 }
173
174 public void setSelectedMode(SelectedMode mode) {
175 setSelectedMode(mode, DEFAULT_HIGHLIGHT);
176 }
177
178 protected SelectedMode _mode = SelectedMode.None;
179
180 private Point _offset = new Point(0, 0);
181
182 private int _x;
183
184 private int _y;
185
186 private int _id;
187
188 private String _creationDate = null;
189
190 private boolean _linkMark = true;
191
192 private boolean _actionMark = true;
193
194 private boolean _highlight = true;
195
196 private Dimension _maxSize = null;
197
198 private String _owner = null;
199
200 private String _link = null;
201
202 private StringBuffer _data = new StringBuffer();
203
204 private List<String> _actionCursorEnter = null;
205
206 private List<String> _actionCursorLeave = null;
207
208 private List<String> _actionEnterFrame = null;
209
210 private List<String> _actionLeaveFrame = null;
211
212 public int Permission = PERMISSION_FULL;
213
214 private Color _colorFill = null;
215
216 private Color _color = null;
217
218 private Color _highlightColor = DEFAULT_HIGHLIGHT;
219
220 private Color _colorBackground = null;
221
222 private Color _colorTopShadow = null;
223
224 private Color _colorBottomShadow = null;
225
226 // the link\action circle
227 private Polygon _circle = null;
228
229 // the invalid link cross
230 private Polygon _circleCross = null;
231
232 private Frame _parent = null;
233
234 protected int _highlightThickness = 2;
235
236 // arrowhead parameters
237 private int _arrowheadLength = 0;
238
239 private double _arrowheadRatio = DEFAULT_ARROWHEAD_RATIO;
240
241 private Polygon _arrowhead = null;
242
243 // the list of lines that this point is part of.
244 private List<Line> _lines = new ArrayList<Line>();
245
246 private int[] _linePattern = null;
247
248 private boolean _floating = false;
249
250 // list of points constrained with this point
251 private List<Constraint> _constraints = null;
252
253 private LinkedList<String> _actions = null;
254
255 private String _link_frameset = null;
256
257 private String _link_template = null;
258
259 private String _fillPattern = null;
260
261 private boolean _visible = true;
262
263 private SelectedMode _lastMode = SelectedMode.None;
264
265 protected Item() {
266 _creationDate = Logger.EasyDateFormat("ddMMMyyyy:HHmm");
267 }
268
269 /**
270 * Adds an action to this Item.
271 *
272 * @param action
273 * The KMS action language to add to this Item
274 */
275 public void addAction(String action) {
276 if (action == null || action.equals("")) {
277 return;
278 }
279
280 if (_actions == null)
281 _actions = new LinkedList<String>();
282 _actions.add(action);
283 }
284
285 public void addAllConnected(List<Item> connected) {
286 if (!connected.contains(this))
287 connected.add(this);
288
289 for (Line line : getLines()) {
290 if (!connected.contains(line))
291 line.addAllConnected(connected);
292 }
293 }
294
295 /**
296 * Adds the given Constraint to this Dot
297 *
298 * @param c
299 * The Constraint to set this Dot as a member of.
300 */
301 public void addConstraint(Constraint c) {
302 if (_constraints == null)
303 _constraints = new ArrayList<Constraint>();
304
305 // do not add duplicate constraint
306 if (_constraints.contains(c))
307 return;
308
309 _constraints.add(c);
310 }
311
312 /**
313 * Adds a given line to the list of lines that this Point is an end for.
314 *
315 * @param line
316 * The Line that this Point is an end of.
317 */
318 public void addLine(Line line) {
319 if (_lines.contains(line)) {
320 return;
321 }
322
323 line.setColor(getColor());
324 line.setSize(getSize());
325 line.setLinePattern(getLinePattern());
326
327 _lines.add(line);
328 }
329
330 /**
331 * Items are sorted by their Y coordinate on the screen.
332 *
333 * @param i
334 * The Item to compare this Item to
335 * @return a negative integer, zero, or a positive integer as this object is
336 * less than, equal to, or greater than the specified object.
337 */
338 public int compareTo(Item i) {
339 return getY() - i.getY();
340 }
341
342 /**
343 * Every Item has an area around it defined by a Shape (typically a
344 * rectangle), this method returns true if the given x,y pair lies within
345 * the area and false otherwise.
346 *
347 * @param x
348 * The x coordinate to check
349 * @param y
350 * The y coordinate to check
351 * @return True if the Shape around this Item contains the given x,y pair,
352 * false otherwise.
353 */
354 public boolean contains(int x, int y) {
355 return getPolygon().contains(x, y);
356 }
357
358 /**
359 * Returns a deep copy of this Item, note: it is up to the receiver to
360 * change the Item ID etc as necessary.
361 *
362 * @return A deep copy of this Item.
363 */
364 public abstract Item copy();
365
366 public void delete() {
367 setVisible(false);
368 }
369
370 public boolean equals(Object o) {
371 if (o instanceof Item) {
372 Item i = (Item) o;
373 return i.getID() == getID()
374 && ((i.getParent() == _parent) || (i.getParent() != null && i
375 .getParent().equals(_parent)));
376
377 } else
378 return false;
379 }
380
381 final public String getAbsoluteLink() {
382 String link = getLink();
383
384 if (link == null)
385 return null;
386 //assert (_parent!= null);
387 if (_parent == null){
388 //if parent is null it is an item on the message box
389 //so it must already be absolute
390 assert(!FrameIO.isPositiveInteger(link));
391 return link;
392 }
393
394 // if its a relative link then return absolute
395 if (FrameIO.isPositiveInteger(link)) {
396 return _parent.getFramesetName()
397 + Conversion.getFrameNumber(link);
398 }
399 return link;
400 }
401
402 /**
403 * Returns a list of any action code (KMS action language) that is currently
404 * associated with this Item
405 *
406 * @return A List of action code associated with this Item, or null if none
407 * has been assigned.
408 */
409 public List<String> getAction() {
410 return _actions;
411 }
412
413 public List<String> getActionCursorEnter() {
414 return _actionCursorEnter;
415 }
416
417 public List<String> getActionCursorLeave() {
418 return _actionCursorLeave;
419 }
420
421 public List<String> getActionEnterFrame() {
422 return _actionEnterFrame;
423 }
424
425 public List<String> getActionLeaveFrame() {
426 return _actionLeaveFrame;
427 };
428
429 public boolean getActionMark() {
430 return _actionMark;
431 }
432
433 public List<Item> getAllConnected() {
434 List<Item> list = new LinkedList<Item>();
435 addAllConnected(list);
436 return list;
437 }
438
439 public Area getArea() {
440 return new Area(getPolygon());
441 }
442
443 public String getArrow() {
444 if (!hasVisibleArrow())
445 return null;
446
447 String ratio = "" + getArrowheadRatio();
448 if (ratio.length() - ratio.indexOf(".") > 2)
449 ratio = ratio.substring(0, ratio.indexOf(".") + 3);
450
451 return getArrowheadLength() + " " + ratio;
452 }
453
454 public Polygon getArrowhead() {
455 return _arrowhead;
456 }
457
458 public int getArrowheadLength() {
459 return _arrowheadLength;
460 }
461
462 public double getArrowheadRatio() {
463 return _arrowheadRatio;
464 }
465
466 public Color getBackgroundColor() {
467 return _colorBackground;
468 }
469
470 /**
471 * Returns the Color being used to shade the bottom half of this Item's
472 * border. This can be NULL if no Color is being used
473 *
474 * @return The Color displayed on the bottom\right half of this Item's
475 * border.
476 */
477 public Color getBottomShadowColor() {
478 return _colorBottomShadow;
479 }
480
481 /**
482 * Returns the height (in pixels) of this Item's surrounding area.
483 *
484 * @return The height (in pixels) of this Item's surrounding area as
485 * returned by getArea().
486 */
487 public int getBoundsHeight() {
488 return getPolygon().getBounds().height;
489 }
490
491 /**
492 * Returns the width (in pixels) of this Item's surrounding area.
493 *
494 * @return The width (in pixels) of this Item's surrounding area as returned
495 * by getArea().
496 */
497 public int getBoundsWidth() {
498 return getPolygon().getBounds().width;
499 }
500
501 protected Polygon getCircle() {
502 if (_circle == null) {
503 int points = 16;
504
505 double radians = 0.0;
506 int xPoints[] = new int[points];
507 int yPoints[] = new int[xPoints.length];
508
509 for (int i = 0; i < xPoints.length; i++) {
510 // circle looks best if these values are not related to gravity
511 xPoints[i] = (int) (3.5 * Math.cos(radians)) + 6;// (2 *
512 // GRAVITY);
513 yPoints[i] = (int) (3.5 * Math.sin(radians)) + 3;// GRAVITY;
514 radians += (2.0 * Math.PI) / xPoints.length;
515 }
516
517 _circle = new Polygon(xPoints, yPoints, xPoints.length);
518 }
519
520 return _circle;
521 }
522
523 protected Polygon getCircleCross() {
524
525 if (_circleCross == null) {
526 _circleCross = new Polygon();
527
528 Rectangle bounds = getCircle().getBounds();
529 int x1 = (int) bounds.getMinX();
530 int x2 = (int) bounds.getMaxX();
531 int y1 = (int) bounds.getMinY();
532 int y2 = (int) bounds.getMaxY();
533 int midX = ((x2 - x1) / 2) + x1;
534 int midY = ((y2 - y1) / 2) + y1;
535
536 _circleCross.addPoint(x1, y1);
537 _circleCross.addPoint(x2, y2);
538 _circleCross.addPoint(midX, midY);
539 _circleCross.addPoint(x1, y2);
540 _circleCross.addPoint(x2, y1);
541 _circleCross.addPoint(midX, midY);
542 }
543
544 return _circleCross;
545 }
546
547 public Color getColor() {
548 return _color;
549 }
550
551 public List<Item> getConnected() {
552 List<Item> conn = new LinkedList<Item>();
553 conn.add(this);
554
555 conn.addAll(getLines());
556 return conn;
557 }
558
559 public String getConstraintIDs() {
560 if (_constraints == null || _constraints.size() == 0)
561 return null;
562
563 String cons = "";
564
565 for (Constraint c : _constraints)
566 cons += c.getID() + " ";
567
568 return cons.trim();
569 }
570
571 /*
572 * public void setLinkValid(boolean val) { _isValidLink = val; }
573 */
574
575 /**
576 * Returns a List of any Constraints that this Dot is a memeber of.
577 *
578 * @return a List of Constraints that this Dot is a member of.
579 */
580 public List<Constraint> getConstraints() {
581 return _constraints;
582 }
583
584 public String getData() {
585 if (_data != null && _data.length() > 0)
586 return _data.toString();
587 return null;
588 }
589
590 public String getDateCreated() {
591 return _creationDate;
592 }
593
594 public Color getFillColor() {
595 return _colorFill;
596 }
597
598 public String getFillPattern() {
599 return _fillPattern;
600 }
601
602 public String getFirstAction() {
603 if (_actions == null)
604 return null;
605 return _actions.getFirst();
606 }
607
608 public boolean getHighlight() {
609 return _highlight;
610 }
611
612 public Color getHighlightColor() {
613 return _highlightColor;
614 }
615
616 /**
617 * Returns the ID of this Item, which must be unique for the Frame.
618 *
619 * @return The ID of this Item.
620 */
621 public int getID() {
622 return _id;
623 }
624
625 /**
626 * Returns the list of IDs of the Lines that this Dot is an end of.
627 *
628 * @return The list of Line IDs that this point is part of.
629 */
630 public String getLineIDs() {
631 String lineID = null;
632
633 if (_lines.size() > 0) {
634 lineID = "" + _lines.get(0).getID();
635
636 for (int i = 1; i < _lines.size(); i++)
637 lineID += " " + _lines.get(i).getID();
638 }
639
640 return lineID;
641 }
642
643 public int[] getLinePattern() {
644 return _linePattern;
645 }
646
647 /**
648 * Returns a list of Lines where this Dot is an end.
649 *
650 * @return A list of the Lines that this Dot is an end for or null if no
651 * Lines have been added.
652 */
653 public List<Line> getLines() {
654 return _lines;
655 }
656
657 /**
658 * Returns the name of a Frame that this Item links to, or null if this Item
659 * has no link.
660 *
661 * @return The name of a Frame that this Item links to (if any) or null if
662 * this Item does not link to anything.
663 */
664 public String getLink() {
665 return _link;
666 }
667
668 public String getLinkFrameset() {
669 return _link_frameset;
670 }
671
672 public boolean getLinkMark() {
673 return _linkMark;
674 }
675
676 public String getLinkTemplate() {
677 return _link_template;
678 }
679
680 public Dimension getMaxSize() {
681 return _maxSize;
682 }
683
684 public Point getOffset() {
685 return _offset;
686 }
687
688 public String getOwner() {
689 return _owner;
690 }
691
692 public Color getPaintBackgroundColor() {
693 if (_colorBackground == null) {
694 if (getParent() != null && getParent().getBackgroundColor() != null)
695 return getParent().getBackgroundColor();
696
697 return DEFAULT_BACKGROUND;
698 }
699
700 return _colorBackground;
701 }
702
703 /**
704 * Returns the foreground Color of this Item.
705 *
706 * @return The Color of this item (foreground)
707 */
708 public Color getPaintColor() {
709 if (_color == null) {
710 if (getParent() != null)
711 return getParent().getPaintForegroundColor();
712
713 if (DisplayIO.getCurrentFrame() == null)
714 return DEFAULT_FOREGROUND;
715
716 return DisplayIO.getCurrentFrame().getPaintForegroundColor();
717 }
718
719 return _color;
720 }
721
722 protected Color getPaintHighlightColor() {
723 if (getParent() != null
724 && getParent().getPaintBackgroundColor()
725 .equals(_highlightColor))
726 return getParent().getPaintForegroundColor();
727
728 return _highlightColor;
729 }
730
731 public Frame getParent() {
732 return _parent;
733 }
734
735 /**
736 * Returns the Shape that surrounds this Item representing this Item's
737 * 'gravity'.
738 *
739 * @return The Shape (rectangle) surrounding this Item, which represents
740 * this Items 'gravity'.
741 */
742 public abstract Polygon getPolygon();
743
744 public Point getPosition() {
745 return new Point(getX(), getY());
746 }
747
748 /**
749 * Returns the size of this Item. For Text this is the Font size, for Lines
750 * and Dots this is the thickness.
751 *
752 * @return The size of this Item.
753 */
754 public abstract int getSize();
755
756 public float getThickness() {
757 return 0;
758 }
759
760 /**
761 * Returns the Color being used to shade the top half of this Item's border.
762 * This can be NULL if no Color is being used
763 *
764 * @return The Color displayed on the top\left half of this Item's border.
765 */
766 public Color getTopShadowColor() {
767 return _colorTopShadow;
768 }
769
770 public String getTypeAndID() {
771 return "T " + getID();
772 }
773
774 public int getWidth() {
775 return 0;
776 }
777
778 /**
779 * Returns the X coordinate of this Item on the screen
780 *
781 * @return The X coordinate of this Item on the screen
782 */
783 public int getX() {
784 return _x;
785 }
786
787 /**
788 * Returns the Y coordinate of this Item on the screen
789 *
790 * @return The Y coordinate of this Item on the screen
791 */
792 public int getY() {
793 return _y;
794 }
795
796 protected boolean hasVisibleArrow() {
797 return false;
798 }
799
800 /**
801 * Checks if the given Shape intersects with the Shape around this Item.
802 * Note: Both Shape objects should be rectangles for this to work properly.
803 *
804 * @param s
805 * The Shape to check.
806 * @return True if the two Shapes overlap, False otherwise.
807 */
808 public boolean intersects(Polygon p) {
809 if (p == null)
810 return false;
811
812 // return p.getBounds().intersects(getArea().getBounds());
813 Area a = new Area(p);
814
815 a.intersect(this.getArea());
816 return !a.isEmpty();
817 }
818
819 /**
820 * Note: Pictures always return False, as they should be drawn even when no
821 * other annotation Items are.
822 *
823 * @return True if this Item is an annotation, False otherwise.
824 */
825 public abstract boolean isAnnotation();
826
827 public boolean isFloating() {
828 return _floating;
829 }
830
831 public boolean isFrameName() {
832 if (this.getParent() == null || this.getParent().getFrameNameItem() != this)
833 return false;
834 return true;
835 }
836
837 public boolean isFrameTitle() {
838 if (this.getParent() == null || this.getParent().getTitle() != this)
839 return false;
840 return true;
841 }
842
843 /**
844 * Returns True if this Item is currently highlighted.
845 *
846 * @return True if this Item is currently highlighted on the screen, False
847 * otherwise.
848 */
849 public boolean isHighlighted() {
850 return _mode != SelectedMode.None;
851 }
852
853 /**
854 * Tests if the item link is a valid framename, that is, the String must
855 * begin with a character, end with a number with 0 or more letters and
856 * numbers in between. If there is a dot in the framename all the chars
857 * after it must be digits.
858 *
859 * @return True if the given framename is proper, false otherwise.
860 */
861 public boolean isLinkValid() {
862 if (FrameIO.isPositiveInteger(getLink()))
863 return true;
864
865 if (FrameIO.isValidFrameName(getLink()))
866 return true;
867 return false;
868 }
869
870 public boolean isNear(int x, int y) {
871
872
873 int xLeft = getPolygon().getBounds().x;
874 int yTop = getPolygon().getBounds().y;
875
876 return (x > xLeft - NEAR_DISTANCE && y > yTop - NEAR_DISTANCE
877 && x < xLeft + getBoundsWidth() + NEAR_DISTANCE
878 && y < yTop + getBoundsHeight() + NEAR_DISTANCE);
879 }
880
881 /**
882 * Checks if this item is a frame title.
883 *
884 * @return true if the item is a frame title
885 */
886 /*
887 * public boolean isTitle() { // check if the title has been assigned if
888 * (getID() >= 0 && this instanceof Text) if (getX() < 200 && getY() <
889 * getSize() + system.io.KMSConversion.Y_ADJUST) return true; return false; }
890 */
891
892 public boolean isOldTag() {
893 if (this instanceof Text)
894 if (((Text) this).getText().get(0).toLowerCase().equals("@old"))
895 return true;
896 return false;
897 }
898
899 /**
900 * Merges this Item with the given Item. The merger Item should be left
901 * unchanged after this method. The merger may or may not be the same class
902 * as this Item, exact behaviour depends on the subclass, No-op is allowed.
903 *
904 * @param merger
905 * The Item to merge with
906 * @param mouseX
907 * The X coordinate of the mouse when performing this merge
908 * operation
909 * @param mouseY
910 * The Y coordinate of the mouse when performing this merge
911 * operation
912 * @return any Item that should remain on the cursor
913 */
914 public abstract Item merge(Item merger, int mouseX, int mouseY);
915
916 /**
917 * Displays this item directly on the screen. Note: All Items are
918 * responsible for their own drawing, buffering, etc.
919 *
920 * @param g
921 * The Graphics to draw this Item on.
922 */
923 public abstract void paint(Graphics2D g);
924
925 /**
926 * This method performs all the actions in an items list. If it contains a
927 * link as well the link is used as the source frame for all acitons.
928 */
929 public void performActions() {
930 Frame sourceFrame = null;
931 // if a link exists make it the source frame for this action
932 if (getLink() != null) {
933 sourceFrame = FrameUtils.getFrame(getLink());
934 }
935 // if no link exists or the link is bad then use the
936 // currently displayed frame as the source frame for the
937 // action
938 if (sourceFrame == null) {
939 sourceFrame = DisplayIO.getCurrentFrame();
940 }
941
942 for (String s : getAction()) {
943 Actions.PerformAction(sourceFrame, this, s);
944 }
945 }
946
947 public void removeAllConstraints() {
948 if (_constraints != null)
949 _constraints.clear();
950 }
951
952 /**
953 * Clears the list of Lines that this Dot is an end of. Note: This only
954 * clears this Dot's list and does not have any affect on the Lines or other
955 * Dots.
956 */
957 public void removeAllLines() {
958 _lines.clear();
959 }
960
961 /**
962 * Removes the given Constraint from the list of constraintss that this Dot
963 * is a part of.
964 *
965 * @param c
966 * The Constraint that this Dot is no longer a part of.
967 */
968 public void removeConstraint(Constraint c) {
969 _constraints.remove(c);
970 }
971
972 /**
973 * Removes the given Line from the list of lines that this Dot is an end
974 * for.
975 *
976 * @param line
977 * The Line that this Dot is no longer an end of.
978 */
979 public void removeLine(Line line) {
980 _lines.remove(line);
981 }
982
983 public void run() {
984 try {
985 AgentStats.reset();
986 FrameGraphics
987 .DisplayMessage("Running SimpleProgram...", Color.BLUE);
988 Simple.RunFrameAndReportError(this, new Context());
989 Simple.ProgramFinished();
990 FrameGraphics.DisplayMessage(AgentStats.getStats(), GREEN);
991 } catch (ConcurrentModificationException ce) {
992 ce.printStackTrace();
993 } catch (Exception e) {
994 FrameGraphics.LinkedErrorMessage(e.getMessage());
995 Simple.ProgramFinished();
996 }
997 }
998
999 /**
1000 * Check if it has a relative link if so make it absolute.
1001 *
1002 */
1003 public void setAbsoluteLink() {
1004 if (_link == null)
1005 return;
1006 // Check if all the characters are digits and hence it is a relative
1007 // link
1008 for (int i = 0; i < _link.length(); i++) {
1009 if (!Character.isDigit(_link.charAt(i)))
1010 return;
1011 }
1012
1013 // Make it an absolute link
1014 String framesetName;
1015
1016 if (_parent == null)
1017 framesetName = DisplayIO.getCurrentFrame()
1018 .getFramesetName();
1019 else
1020 framesetName = _parent.getFramesetName();
1021
1022 _link = framesetName + _link;
1023 }
1024
1025 /**
1026 * Sets any action code (KMS action language) that should be associated with
1027 * this Item Each entry in the list is one line of code
1028 *
1029 * @param actions
1030 * The lines of code to associate with this Item
1031 */
1032 public void setAction(List<String> actions) {
1033 if (actions == null || actions.size() == 0)
1034 _actions = null;
1035 else
1036 _actions = new LinkedList<String>(actions);
1037 }
1038
1039 public void setActionCursorEnter(List<String> enter) {
1040 _actionCursorEnter = enter;
1041 }
1042
1043 public void setActionCursorLeave(List<String> leave) {
1044 _actionCursorLeave = leave;
1045 }
1046
1047 public void setActionEnterFrame(List<String> enter) {
1048 _actionEnterFrame = enter;
1049 }
1050
1051 public void setActionLeaveFrame(List<String> leave) {
1052 _actionLeaveFrame = leave;
1053 }
1054
1055 public void setActionMark(boolean val) {
1056 _actionMark = val;
1057 }
1058
1059 /**
1060 * Sets whether this Item is an Annotation.
1061 *
1062 * @param val
1063 * True if this Item is an Annotation, False otherwise.
1064 */
1065 public abstract void setAnnotation(boolean val);
1066
1067 /**
1068 * Used to set this Line as an Arrow. If length and ratio are 0, no arrow is
1069 * shown.
1070 *
1071 * @param length
1072 * The how far down the shaft of the line the arrowhead should
1073 * come.
1074 * @param ratio
1075 * The ratio of the arrow's length to its width.
1076 */
1077 public void setArrow(int length, double ratio) {
1078 _arrowheadLength = length;
1079 _arrowheadRatio = ratio;
1080 updateArrowPolygon();
1081 }
1082
1083 public void setArrowhead(Polygon arrow) {
1084 _arrowhead = arrow;
1085 }
1086
1087 public void setArrowheadLength(int length) {
1088 _arrowheadLength = length;
1089 updateArrowPolygon();
1090 }
1091
1092 public void setArrowheadRatio(double ratio) {
1093 _arrowheadRatio = ratio;
1094 updateArrowPolygon();
1095 }
1096
1097 public void setBackgroundColor(Color c) {
1098 _colorBackground = c;
1099 }
1100
1101 /**
1102 * Sets the Color to use on the bottom and right sections of this Item's
1103 * border. If top is NULL, then the Item's background Color will be used.
1104 *
1105 * @param top
1106 * The Color to display in the bottom and right sections of this
1107 * Item's border.
1108 */
1109 public void setBottomShadowColor(Color bottom) {
1110 _colorBottomShadow = bottom;
1111 }
1112
1113 /**
1114 * Sets the foreground Color of this Item to the given Color.
1115 *
1116 * @param c
1117 */
1118 public void setColor(Color c) {
1119 _color = c;
1120
1121 // update the colour of any lines
1122 for (Line line : getLines())
1123 line.setColor(c);
1124 }
1125
1126 public void setConstraintIDs(String IDs) {
1127 }
1128
1129 public void setConstraints(List<Constraint> constraints) {
1130 _constraints = constraints;
1131 }
1132
1133 public void setData(String newData) {
1134 if (newData != null)
1135 _data = new StringBuffer(newData);
1136 else
1137 _data = null;
1138 }
1139
1140 /**
1141 * Sets the created date of this Frame to the given String.
1142 *
1143 * @param date
1144 * The date to use for this Frame.
1145 */
1146 public void setDateCreated(String date) {
1147 _creationDate = date;
1148 }
1149
1150 public void setFillColor(Color c) {
1151 _colorFill = c;
1152
1153 for (Line line : _lines) {
1154 Item other = line.getOppositeEnd(this);
1155 if (other.getFillColor() != c)
1156 other.setFillColor(c);
1157 }
1158 }
1159
1160 public void setFilledHighlight(boolean value) {
1161 }
1162
1163 public void setFillPattern(String patternLink) {
1164 _fillPattern = patternLink;
1165 }
1166
1167 public void setFloating(boolean val) {
1168 _floating = val;
1169 }
1170
1171 public void setHighlight(boolean val) {
1172 _highlight = val;
1173 }
1174
1175 /**
1176 * Sets the ID of this Item to the given Integer. Note: Items with ID's < 0
1177 * are not saved
1178 *
1179 * @param newID
1180 * The new ID to assign this Item.
1181 */
1182 public void setID(int newID) {
1183 _id = newID;
1184 }
1185
1186 /**
1187 * Sets the list of lines that this point is part of (may be set to null).
1188 *
1189 * @param lineID
1190 * A String of line ID numbers separated by spaces.
1191 */
1192 public void setLineIDs(String lineID) {
1193 }
1194
1195 public void setLinePattern(int[] pattern) {
1196 _linePattern = pattern;
1197
1198 for (Line line : getLines())
1199 line.setLinePattern(pattern);
1200 }
1201
1202 public void setLines(List<Line> lines) {
1203 _lines = lines;
1204
1205 for (Line line : lines)
1206 line.setLinePattern(getLinePattern());
1207 }
1208
1209 /**
1210 * Links this item to the given Frame, this may be set to null to remove a
1211 * link.
1212 *
1213 * @param frameName
1214 * The name of the Frame to link this item to.
1215 */
1216 public void setLink(String frameName) {
1217 _link = frameName;
1218 }
1219
1220 public void setLinkFrameset(String frameset) {
1221 _link_frameset = frameset;
1222 }
1223
1224 public void setLinkMark(boolean val) {
1225 _linkMark = val;
1226 }
1227
1228 public void setLinkTemplate(String template) {
1229 _link_template = template;
1230 }
1231
1232 /**
1233 * Sets the maximum coordinates on the screen that this item may occupy.
1234 * This is used by Text items to compute word-wrapping lengths.
1235 *
1236 * @param d
1237 * The Maximum size of the Frame containing this Item.
1238 */
1239 public void setMaxSize(Dimension d) {
1240 if (d != null) {
1241 _maxSize = d;
1242 updatePolygon();
1243 }
1244 }
1245
1246 public void setOffset(int x, int y) {
1247 _offset.setLocation(x, y);
1248 }
1249
1250 public void setOffset(Point p) {
1251 _offset.setLocation(p);
1252 }
1253
1254 public void setOwner(String own) {
1255 _owner = own;
1256 }
1257
1258 public void setParent(Frame frame) {
1259 _parent = frame;
1260 }
1261
1262 /**
1263 * Sets the position of this item on the screen
1264 *
1265 * @param x
1266 * The new X coordinate
1267 * @param y
1268 * The new Y coordinate
1269 */
1270 public void setPosition(int x, int y) {
1271 _x = x;
1272 _y = y;
1273
1274 updatePolygon();
1275
1276 // update the position of any dots that are constrained by this one
1277 if (_constraints != null) {
1278 for (Constraint c : _constraints) {
1279 Item other = c.getOppositeEnd(this);
1280
1281 // only set the position if the other dot is still fixed to the
1282 // frame
1283 if (!other.isFloating()) {
1284 if (c.getConstraintType() == Constraint.HORIZONTAL
1285 && other.getY() != getY())
1286 other.setY(getY());
1287
1288 if (c.getConstraintType() == Constraint.VERTICAL
1289 && other.getX() != getX())
1290 other.setX(getX());
1291 }
1292 }
1293 }
1294
1295 for (Line line : getLines())
1296 line.updatePolygon();
1297 }
1298
1299 public void setPosition(Point position) {
1300 setPosition(position.x, position.y);
1301 }
1302
1303 public void setRelativeLink() {
1304 if (_link == null)
1305 return;
1306 assert (_parent != null);
1307 // Check if the link is for the current frameset
1308 if (_parent.getFramesetName().equalsIgnoreCase(
1309 Conversion.getFrameset(_link))) {
1310 _link = "" + Conversion.getFrameNumber(_link);
1311 }
1312 }
1313
1314 /**
1315 * Sets the size of this Item. For Text this is the Font size. For Lines and
1316 * Dots this is the thickness.
1317 */
1318 public abstract void setSize(int size);
1319
1320 public void setThickness(float thick) throws UnsupportedOperationException {
1321 throw new UnsupportedOperationException(
1322 "Item type does not support thickness attribute!");
1323 }
1324
1325 /**
1326 * Sets the Color to use on the top and left sections of this Item's border.
1327 * If top is NULL, then the Item's background Color will be used.
1328 *
1329 * @param top
1330 * The Color to display in the top and left sections of this
1331 * Item's border.
1332 */
1333 public void setTopShadowColor(Color top) {
1334 _colorTopShadow = top;
1335 }
1336
1337 public void setWidth(int width) throws UnsupportedOperationException {
1338 throw new UnsupportedOperationException(
1339 "Item type does not support width attribute!");
1340 }
1341
1342 /**
1343 * Sets the position of this Item on the X axis
1344 *
1345 * @param newX
1346 * The position on the X axis to assign to this Item
1347 */
1348 public void setX(int newX) {
1349 setPosition(newX, getY());
1350 }
1351
1352 /**
1353 * Sets the position of this Item on the Y axis
1354 *
1355 * @param newY
1356 * The position on the Y axis to assign to this Item
1357 */
1358 public void setY(int newY) {
1359 setPosition(getX(), newY);
1360 }
1361
1362 /**
1363 * Paints any highlighting of this Item. This may include changing the
1364 * thickness (lines) or painting a box around the item (Text, Images). If
1365 * val is True then the Graphics Color is changed to the highlight Color, if
1366 * False then the Graphics Color is left unchanged (for clearing of
1367 * highlighting).
1368 *
1369 * @param val
1370 * True if this Item should be highlighted, false if the
1371 * highlighting is being cleared.
1372 * @return The desired mouse cursor when this Item is highlighted (negative
1373 * means no change)
1374 */
1375 public int setSelectionColor() {
1376 return setSelectionColor(DEFAULT_HIGHLIGHT);
1377 }
1378
1379 public int setSelectionColor(Color c) {
1380 _highlightThickness = DEFAULT_HIGHLIGHT_THICKNESS;
1381 if (c != null)
1382 _highlightColor = c;
1383 else
1384 _highlightColor = DEFAULT_HIGHLIGHT;
1385
1386 return Item.UNCHANGED_CURSOR;
1387 }
1388
1389 private void updateArrowPolygon() {
1390 if (getArrowheadLength() < 0 || getArrowheadRatio() < 0)
1391 _arrowhead = null;
1392 else {
1393 _arrowhead = new Polygon();
1394 _arrowhead.addPoint((int) getX(), (int) getY());
1395 _arrowhead
1396 .addPoint(
1397 (int) getX() - getArrowheadLength(),
1398 (int) (getY() - (getArrowheadLength() * getArrowheadRatio())));
1399 _arrowhead.addPoint((int) getX(), (int) getY());
1400 _arrowhead
1401 .addPoint(
1402 (int) getX() - getArrowheadLength(),
1403 (int) (getY() + (getArrowheadLength() * getArrowheadRatio())));
1404 }
1405 }
1406
1407 protected abstract void updatePolygon();
1408
1409 public void setVisible(boolean state) {
1410 this._visible = state;
1411 }
1412
1413 public boolean isVisible() {
1414 return _visible;
1415 }
1416
1417
1418
1419 // BROOK: Overrideable
1420 public void onRemovedFromFrame() {}
1421 public void onAddedToFrame(boolean isOverlayed, int overlayLevel) {}
1422 public void onParentFameHidden() {} // because of caching
1423 public void onParentFameShown(boolean isOverlayed, int overlayLevel) {} // because of caching
1424
1425 public void setSelectedMode(SelectedMode mode, Color color) {
1426 setSelectionColor(color);
1427 _lastMode = _mode;
1428 _mode = mode;
1429 }
1430
1431 public void restoreLastMode(Color selectionColor) {
1432 setSelectionColor(selectionColor);
1433 if (_mode != SelectedMode.None)
1434 return;
1435 _mode = _lastMode;
1436 _lastMode = SelectedMode.None;
1437 }
1438
1439 public SelectedMode getSelectedMode() {
1440 return _mode;
1441 }
1442}
Note: See TracBrowser for help on using the repository browser.