source: trunk/org/expeditee/items/Item.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

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