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

Last change on this file since 966 was 966, checked in by bln4, 9 years ago
File size: 3.7 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 final Item potentialStart = current.getParent().getItemWithID(current.getMagnetizedItemLeft());
49 if(potentialStart == null) break;
50 current = potentialStart;
51 ret.addFirst(current);
52 }
53 final Line following = getLineFromToken(token, new Line());
54 ret.addAll(following);
55 return ret;
56// return getLineFromToken(current, new Line());
57 }
58
59 public static int getLineHeight(final Line line) {
60 return (int) (line.getBoundingBox().getHeight() * 1.2);
61 }
62
63 public Line setX(final int newX) {
64 int delta = newX - this.getFirst().getX();
65 deltaX(delta);
66 return this;
67 }
68
69 public Line setY(final int newY) {
70 int delta = newY - this.getFirst().getY();
71 deltaY(delta);
72 return this;
73 }
74
75 public Line deltaX(final int delta) {
76 for(final Item token : this) token.setPosition(token.getX() + delta, token.getY());
77 return this;
78 }
79
80 public Line deltaY(final int delta) {
81 for(final Item token : this) token.setPosition(token.getX(), token.getY() + delta);
82 return this;
83 }
84
85 public Rectangle getBoundingBox() {
86// final Rectangle rect = new Rectangle(this.getFirst().getX(), this.getFirst().getY(), this.getFirst().getBoundsWidth(), this.getFirst().getBoundsHeight());
87// for(final Item token : this)
88// rect.add(new Rectangle(token.getX(), token.getY(), token.getBoundsWidth(), token.getBoundsHeight()));
89// return rect;
90 final Rectangle rect = this.getFirst().getArea().getBounds();
91 for(final Item token : this)
92 rect.add(token.getArea().getBounds());
93 return rect;
94 }
95
96 public Line next() {
97 final Item lastInThisLine = this.getLast();//this.get(this.size() - 1);
98 if(lastInThisLine.getMagnetizedItemBottom() == -1) return null;
99 else return getLineFromToken(this.get(0).getParent().getItemWithID(lastInThisLine.getMagnetizedItemBottom()));
100 }
101
102 public Line last() {
103 final Item firstInThisLine = this.getFirst();//this.get(0);
104 if(firstInThisLine.getMagnetizedItemTop() == -1) return null;
105 else return getLineContainingToken(this.get(0).getParent().getItemWithID(firstInThisLine.getMagnetizedItemTop()));
106 }
107
108 private static Line getLineFromToken(final Item token, final Line acc) {
109 if(token == null) return acc;
110 if(token.getMagnetizedItemRight() == -1) {
111 acc.add(token);
112 return acc;
113 } else {
114 final Line lineSoFar = new Line(acc);
115 if(lineSoFar.contains(token)) return lineSoFar;
116 lineSoFar.add(token);
117 return getLineFromToken(token.getParent().getItemWithID(token.getMagnetizedItemRight()), lineSoFar);
118 }
119 }
120}
Note: See TracBrowser for help on using the repository browser.