source: trunk/src/org/expeditee/items/MagneticConstraint/Utilities/Line.java@ 963

Last change on this file since 963 was 963, checked in by bln4, 9 years ago
File size: 3.6 KB
Line 
1package org.expeditee.items.MagneticConstraint.Utilities;
2
3import java.awt.Rectangle;
4import java.util.LinkedList;
5
6import org.expeditee.items.Item;
7
8@SuppressWarnings("serial")
9public class Line extends LinkedList<Item> {
10
11 private Line() {
12 super();
13 }
14
15 private Line(final Line toCopy) {
16 super(toCopy);
17 }
18
19 public Line prependLine(final Line line) {
20 final int x = this.getBoundingBox().x;
21 final int width = line.getBoundingBox().width;
22 this.deltaX(width);
23 line.setX(x);
24 line.setY(this.getFirst().getY());
25 return Line.getLineFromToken(line.getFirst());
26 }
27
28 public Line appendLine(final Line line) {
29 final int x = this.getBoundingBox().x;
30 final int width = this.getBoundingBox().width;
31 line.setX(x + width);
32 line.setY(this.getFirst().getY());
33 return Line.getLineFromToken(this.getFirst());
34 }
35
36 public static Line getLineFromToken(final Item token) {
37 if(token == null) return new Line();
38 else {
39 return getLineFromToken(token, new Line());
40 }
41 }
42
43 public static Line getLineContainingToken(final Item token) {
44 if(token == null) return new Line();
45 final Line ret = new Line();
46 Item current = token;
47 while(current.getMagnetizedItemLeft() != -1) {
48 current = current.getParent().getItemWithID(current.getMagnetizedItemLeft());
49 ret.addFirst(current);
50 }
51 final Line following = getLineFromToken(token, new Line());
52 ret.addAll(following);
53 return ret;
54// return getLineFromToken(current, new Line());
55 }
56
57 public static int getLineHeight(final Line line) {
58 return (int) (line.getBoundingBox().getHeight() * 1.2);
59 }
60
61 public Line setX(final int newX) {
62 int delta = newX - this.getFirst().getX();
63 deltaX(delta);
64 return this;
65 }
66
67 public Line setY(final int newY) {
68 int delta = newY - this.getFirst().getY();
69 deltaY(delta);
70 return this;
71 }
72
73 public Line deltaX(final int delta) {
74 for(final Item token : this) token.setPosition(token.getX() + delta, token.getY());
75 return this;
76 }
77
78 public Line deltaY(final int delta) {
79 for(final Item token : this) token.setPosition(token.getX(), token.getY() + delta);
80 return this;
81 }
82
83 public Rectangle getBoundingBox() {
84// final Rectangle rect = new Rectangle(this.getFirst().getX(), this.getFirst().getY(), this.getFirst().getBoundsWidth(), this.getFirst().getBoundsHeight());
85// for(final Item token : this)
86// rect.add(new Rectangle(token.getX(), token.getY(), token.getBoundsWidth(), token.getBoundsHeight()));
87// return rect;
88 final Rectangle rect = this.getFirst().getArea().getBounds();
89 for(final Item token : this)
90 rect.add(token.getArea().getBounds());
91 return rect;
92 }
93
94 public Line next() {
95 final Item lastInThisLine = this.getLast();//this.get(this.size() - 1);
96 if(lastInThisLine.getMagnetizedItemBottom() == -1) return null;
97 else return getLineFromToken(this.get(0).getParent().getItemWithID(lastInThisLine.getMagnetizedItemBottom()));
98 }
99
100 public Line last() {
101 final Item firstInThisLine = this.getFirst();//this.get(0);
102 if(firstInThisLine.getMagnetizedItemTop() == -1) return null;
103 else return getLineContainingToken(this.get(0).getParent().getItemWithID(firstInThisLine.getMagnetizedItemTop()));
104 }
105
106 private static Line getLineFromToken(final Item token, final Line acc) {
107 if(token == null) return acc;
108 if(token.getMagnetizedItemRight() == -1) {
109 acc.add(token);
110 return acc;
111 } else {
112 final Line lineSoFar = new Line(acc);
113 if(lineSoFar.contains(token)) return lineSoFar;
114 lineSoFar.add(token);
115 return getLineFromToken(token.getParent().getItemWithID(token.getMagnetizedItemRight()), lineSoFar);
116 }
117 }
118}
Note: See TracBrowser for help on using the repository browser.