Skip to content

Commit b21e587

Browse files
committed
Add Select Tool's Last Event, Update DataInputTextfieldPaintComponent to override Select method to provide point selection behaviour, Bug: the poitn cannot be deselected
1 parent 0f4a1a1 commit b21e587

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
package paintcomponents;
22

3-
import java.awt.BasicStroke;
4-
import java.awt.Color;
53
import java.awt.Graphics;
6-
import java.awt.Graphics2D;
74

8-
import settings.Defaults;
5+
import painttools.tools.SelectTool;
96

107
/**
118
* A data text with a point on the right
9+
*
1210
* @author chenzb
1311
*
1412
*/
15-
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent implements DataFromPointDataProvider<String> {
16-
13+
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent
14+
implements DataFromPointDataProvider<String> {
1715

1816
private static final int HORIZONTAL_OFFSET = 10;
1917
private DataFromPoint<String> fromPoint;
20-
18+
2119
public DataInputTextfieldPaintComponent(String displayingText, int x,
2220
int y) {
2321
super(displayingText, x, y);
2422
this.fromPoint = new DataFromPoint<>(x, y);
2523
fromPoint.setProvider(this);
2624
}
27-
25+
2826
@Override
2927
protected void paintSelected(Graphics g) {
3028
super.paintSelected(g);
3129
updateFromPointPosition();
3230
fromPoint.paint(g);
3331
}
34-
32+
3533
@Override
3634
protected void paintNotSelected(Graphics g) {
3735
super.paintNotSelected(g);
@@ -40,11 +38,13 @@ protected void paintNotSelected(Graphics g) {
4038
}
4139

4240
/**
43-
* This method will use the protected bounds, which will be updated in super.paint[Not]Selected.
44-
* Make sure you've already invoked super's paintNotSelectedMethod before invoking this one.
41+
* This method will use the protected bounds, which will be updated in
42+
* super.paint[Not]Selected. Make sure you've already invoked super's
43+
* paintNotSelectedMethod before invoking this one.
4544
*/
46-
private void updateFromPointPosition(){
47-
this.fromPoint.setX((int) (getX() + this.bounds.getWidth() + HORIZONTAL_OFFSET));
45+
private void updateFromPointPosition() {
46+
this.fromPoint.setX(
47+
(int) (getX() + this.bounds.getWidth() + HORIZONTAL_OFFSET));
4848
this.fromPoint.setY((int) (getY() + this.bounds.getHeight() / 2));
4949
}
5050

@@ -53,7 +53,7 @@ public void translate(int i, int j) {
5353
super.translate(i, j);
5454
this.fromPoint.translate(i, j);
5555
}
56-
56+
5757
@Override
5858
public boolean contains(int x, int y) {
5959

@@ -75,5 +75,28 @@ public boolean canProvideInformationToDataFromPoint(
7575
DataFromPoint<String> dataFromPoint) {
7676
return displayingText == null;
7777
}
78-
78+
79+
@Override
80+
public void select(SelectTool selectTool) {
81+
int x = selectTool.getLastMouseEvent().getX();
82+
int y = selectTool.getLastMouseEvent().getY();
83+
if (fromPoint.contains(x, y)) {
84+
fromPoint.select(selectTool);
85+
} else {
86+
super.select(selectTool);
87+
}
88+
89+
}
90+
91+
@Override
92+
public void deselect(SelectTool selectTool) {
93+
int x = selectTool.getLastMouseEvent().getX();
94+
int y = selectTool.getLastMouseEvent().getY();
95+
if (fromPoint.contains(x, y)) {
96+
fromPoint.deselect(selectTool);
97+
} else {
98+
super.deselect(selectTool);
99+
}
100+
}
101+
79102
}

src/paintcomponents/PaintComponent.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
/**
99
* Abstracts the behavior of a paint component.
1010
*
11-
* Override translate method if you want to customize tranlation. Override
12-
* select and deselect method to perform additional selections.
11+
* To add functionality to translation: Override translate method if you want to
12+
* customize tranlation.
13+
*
14+
* To add functionality to selection: Override select and deselect method to
15+
* perform additional selections.
1316
*
1417
*
1518
* You should generally not override paint, toggleSelect as the default
@@ -92,6 +95,7 @@ public void paint(Graphics g) {
9295

9396
/**
9497
* Set the state of this object to be selected state
98+
* If select tool is not null, update select tools's selection list to contain this paint component
9599
*
96100
* @param selectTool
97101
* TODO
@@ -105,6 +109,8 @@ public void select(SelectTool selectTool) {
105109
/**
106110
* Set the state of this object to be unselected
107111
*
112+
* If the select tool is not null, remove this paint component from the tool's selection list
113+
*
108114
* @param selectTool
109115
* TODO
110116
*/

src/painttools/tools/SelectTool.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public class SelectTool extends PaintTool {
1818

1919
private ArrayList<PaintComponent> selectedComponents;
2020
private ArrayList<SelectionToolListener> listeners;
21+
22+
private MouseEvent lastMouseEvent;
23+
24+
/**
25+
* @return the lastMouseEvent
26+
*/
27+
public MouseEvent getLastMouseEvent() {
28+
return lastMouseEvent;
29+
}
2130

2231
/**
2332
* adds a selection listener
@@ -95,9 +104,11 @@ public void clearSelection() {
95104

96105
@Override
97106
public void mousePressed(MouseEvent e) {
107+
lastMouseEvent = e;
98108
// TODO Auto-generated method stub
99109
// select or deselect component under cursor on mouse clicked
100110

111+
101112
PaintComponent comp = panel.componentUnderPoint(e.getX(), e.getY());
102113
if (comp != null) {
103114
if (comp.isSelected()) {
@@ -123,6 +134,7 @@ public void mousePressed(MouseEvent e) {
123134
@Override
124135
public void mouseReleased(MouseEvent e) {
125136
// select or deselect component under cursor on mouse clicked
137+
lastMouseEvent = e;
126138

127139
PaintComponent comp = panel.componentUnderPoint(e.getX(), e.getY());
128140
if (comp != null) {
@@ -163,6 +175,7 @@ public void mouseExited(MouseEvent e) {
163175
@Override
164176
public void mouseDragged(MouseEvent e) {
165177

178+
lastMouseEvent = e;
166179
// do nothing if dragged empty
167180
// TODO GROUP SELECTION WHEN CANNOT DRAG
168181
if (!canDrag)
@@ -203,6 +216,7 @@ public void mouseDragged(MouseEvent e) {
203216
@Override
204217
public void mouseMoved(MouseEvent e) {
205218

219+
lastMouseEvent = e;
206220
// shows a left arrow if cursor hits something
207221
PaintComponent comp = panel.componentUnderPoint(e.getX(), e.getY());
208222
if (comp != null) {

0 commit comments

Comments
 (0)