Skip to content

Commit 9a4ceb5

Browse files
committed
Add Data Display Paint Component
1 parent fd8a659 commit 9a4ceb5

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package paintcomponents;
2+
3+
import java.awt.Graphics;
4+
import java.util.NoSuchElementException;
5+
6+
import painttools.tools.SelectTool;
7+
8+
/**
9+
* The data display paint component displays the data with a asoociated DataToPoint
10+
* @author chenzb
11+
*
12+
*/
13+
//TODO THIS class is a copy of DataInputTextfieldPaintComponent class, please consider abstraction
14+
public class DataDisplayPaintComponent extends DataTextPaintComponent {
15+
16+
private static final int HORIZONTAL_OFFSET = 10;
17+
private DataToPoint<String> toPoint;
18+
19+
public DataDisplayPaintComponent(String displayingText, int x, int y) {
20+
super(displayingText, x, y);
21+
this.toPoint = new DataToPoint<>(x, y);
22+
}
23+
24+
@Override
25+
protected void paintSelected(Graphics g) {
26+
super.paintSelected(g);
27+
updateFromPointPosition();
28+
toPoint.paint(g);
29+
}
30+
31+
@Override
32+
protected void paintNotSelected(Graphics g) {
33+
super.paintNotSelected(g);
34+
updateFromPointPosition();
35+
toPoint.paint(g);
36+
}
37+
38+
/**
39+
* This method will use the protected bounds, which will be updated in
40+
* super.paint[Not]Selected. Make sure you've already invoked super's
41+
* paintNotSelectedMethod before invoking this one.
42+
*/
43+
private void updateFromPointPosition() {
44+
this.toPoint.setX(
45+
(int) (getX() - HORIZONTAL_OFFSET));
46+
this.toPoint.setY((int) (getY() + this.bounds.getHeight() / 2));
47+
}
48+
49+
@Override
50+
public void translate(int i, int j) {
51+
super.translate(i, j);
52+
this.toPoint.translate(i, j);
53+
}
54+
55+
@Override
56+
public boolean contains(int x, int y) {
57+
58+
return toPoint.contains(x, y) || super.contains(x, y);
59+
}
60+
61+
public void inputData(String s) {
62+
this.setDisplayingText(s);
63+
}
64+
65+
66+
@Override
67+
public void select(SelectTool selectTool) {
68+
int x = selectTool.getLastMouseEvent().getX();
69+
int y = selectTool.getLastMouseEvent().getY();
70+
if (toPoint.contains(x, y)) {
71+
toPoint.select(selectTool);
72+
} else {
73+
super.select(selectTool);
74+
}
75+
76+
}
77+
78+
@Override
79+
public void deselect(SelectTool selectTool) {
80+
int x = selectTool.getLastMouseEvent().getX();
81+
int y = selectTool.getLastMouseEvent().getY();
82+
if (toPoint.contains(x, y)) {
83+
toPoint.deselect(selectTool);
84+
} else {
85+
super.deselect(selectTool);
86+
}
87+
}
88+
89+
@Override
90+
public boolean isSelected() {
91+
//if the from point is selected, this components is considered selected
92+
if(this.toPoint.isSelected()) return true;
93+
return super.isSelected();
94+
}
95+
96+
/**
97+
* Update the current display.
98+
*
99+
* This class will try to fetch the data from the toPointClass
100+
* @throws DataFromPointProviderCannotProvideDataException
101+
* @throws DataFromPointNoDataProviderException
102+
* @throws NoConnectingLineSegmentException
103+
* @throws NoSuchElementException
104+
* @see DataToPoint.fetchData for exception details
105+
*/
106+
public void displayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{
107+
this.setDisplayingText(toPoint.fetchData());
108+
}
109+
110+
111+
112+
}

0 commit comments

Comments
 (0)