Skip to content

Commit b114884

Browse files
committed
Redefine PaintComponent's select method, PaintComponent are now responsible for adding to and removing from the SelectTool's list of selected components
1 parent 02d224f commit b114884

File tree

4 files changed

+101
-39
lines changed

4 files changed

+101
-39
lines changed

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
import settings.Defaults;
99

10+
/**
11+
* A data text with a point on the right
12+
* @author chenzb
13+
*
14+
*/
1015
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent {
1116

1217

src/paintcomponents/DataTextPaintComponent.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.awt.Graphics;
66
import java.awt.Graphics2D;
77

8+
import painttools.tools.SelectTool;
89
import settings.Defaults;
910

1011
/**
@@ -16,14 +17,15 @@
1617
public class DataTextPaintComponent extends TextPaintComponent {
1718

1819

19-
private RectanglePaintComponent rect;
20+
private RectanglePaintComponent rect;
2021
private Color defaultColor;
2122
private Color selectedColor;
2223

23-
public DataTextPaintComponent(String displayingText, int x, int y) {
24+
public DataTextPaintComponent(String displayingText, int x, int y) {
2425
super(displayingText, x, y);
2526
defaultColor = Defaults.sharedDefaults().defaultColorForDataInputTextfield();
2627
selectedColor = Defaults.sharedDefaults().defaultColorForSelectedDataInputTextfield();
28+
rect = new RectanglePaintComponent(x, y, 0, 0);
2729
}
2830

2931

@@ -49,10 +51,31 @@ protected void paintSelected(Graphics g) {
4951
}
5052

5153
private void updateAndPaintBoudingRectangle(Graphics g){
52-
rect = new RectanglePaintComponent(getX(), getY(), (int)this.bounds.getWidth(), (int)this.bounds.getHeight());
53-
//select rectangle according to current select status
54-
if(isSelected())rect.select(); else rect.deselect();
54+
rect.setWidth((int) bounds.getWidth());
55+
rect.setHeight((int) bounds.getHeight());
5556
rect.paint(g);
5657
}
58+
59+
@Override
60+
public void translate(int i, int j) {
61+
super.translate(i, j);
62+
this.rect.translate(i, j);
63+
}
64+
65+
@Override
66+
public void select(SelectTool selectTool) {
67+
super.select(selectTool);
68+
//pass in null to prevent current selection from being modified
69+
//only causes changes in apperance
70+
rect.select(null);
71+
}
5772

73+
@Override
74+
public void deselect(SelectTool selectTool) {
75+
// TODO Auto-generated method stub
76+
super.deselect(selectTool);
77+
rect.deselect(null);
78+
}
79+
80+
5881
}
Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package paintcomponents;
2+
23
import java.awt.Graphics;
34
import java.awt.Rectangle;
45

6+
import painttools.tools.SelectTool;
7+
8+
/**
9+
* Abstracts the behavior of a paint component.
10+
*
11+
* Override translate method if you want to customize tranlation. Override
12+
* select and deselect method to perform additional selections.
13+
*
14+
*
15+
* You should generally not override paint, toggleSelect as the default
16+
* implementation delegates to other methods that you have to.
17+
*
18+
* MAKE SURE YOU CALL SUPER when overriding non-abstract methods.
19+
*
20+
* @author chenzb
21+
*
22+
*/
523
public abstract class PaintComponent {
6-
24+
725
private int x;
826
private int y;
927
private boolean selected;
@@ -16,7 +34,8 @@ public int getX() {
1634
}
1735

1836
/**
19-
* @param x the x to set
37+
* @param x
38+
* the x to set
2039
*/
2140
public void setX(int x) {
2241
this.x = x;
@@ -30,7 +49,8 @@ public int getY() {
3049
}
3150

3251
/**
33-
* @param y the y to set
52+
* @param y
53+
* the y to set
3454
*/
3555
public void setY(int y) {
3656
this.y = y;
@@ -41,71 +61,88 @@ public PaintComponent(int x, int y) {
4161
this.x = x;
4262
this.y = y;
4363
}
44-
64+
4565
/**
4666
* Paints this component using a graphics object
67+
*
4768
* @param g
4869
*/
49-
public void paint(Graphics g){
50-
if(selected){
70+
public void paint(Graphics g) {
71+
if (selected) {
5172
paintSelected(g);
5273
} else {
5374
paintNotSelected(g);
5475
}
55-
76+
5677
}
57-
78+
5879
/**
5980
* Paint the non-select version of this paint object
81+
*
6082
* @param g
6183
*/
62-
protected abstract void paintNotSelected(Graphics g) ;
63-
84+
protected abstract void paintNotSelected(Graphics g);
6485

6586
/**
6687
* Paints the selected version of this paint object
88+
*
6789
* @param g
6890
*/
69-
protected abstract void paintSelected(Graphics g) ;
70-
91+
protected abstract void paintSelected(Graphics g);
7192

7293
/**
7394
* Set the state of this object to be selected state
95+
*
96+
* @param selectTool
97+
* TODO
7498
*/
75-
public void select(){
99+
public void select(SelectTool selectTool) {
76100
selected = true;
101+
if (selectTool != null)
102+
selectTool.getSelectedComponents().add(this);
77103
}
78-
104+
79105
/**
80106
* Set the state of this object to be unselected
107+
*
108+
* @param selectTool
109+
* TODO
81110
*/
82-
public void deselect(){
111+
public void deselect(SelectTool selectTool) {
83112
selected = false;
113+
if (selectTool != null)
114+
selectTool.getSelectedComponents().remove(this);
84115
}
85-
116+
86117
/**
87118
* toggle the selection status of this paintable object
119+
*
120+
* @param selectTool
121+
* TODO
88122
*/
89-
public void toggleSelect() {
90-
selected = !selected;
123+
public void toggleSelect(SelectTool selectTool) {
124+
if (isSelected()) {
125+
deselect(selectTool);
126+
} else {
127+
select(selectTool);
128+
}
91129
}
92-
130+
93131
/**
94132
* Check if this object is selected
95-
* @return
133+
*
134+
* @return
96135
*/
97-
public boolean isSelected(){
136+
public boolean isSelected() {
98137
return selected;
99138
}
100139

101140
public void translate(int i, int j) {
102-
this.x+=i;
103-
this.y+=j;
104-
105-
}
106-
141+
this.x += i;
142+
this.y += j;
107143

108-
public abstract boolean contains(int x2, int y2);
144+
}
109145

146+
public abstract boolean contains(int x2, int y2);
110147

111148
}

src/painttools/tools/SelectTool.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public void mouseClicked(MouseEvent e) {
5555
* @param comp
5656
*/
5757
public void selectComponent(PaintComponent comp) {
58-
comp.select();
59-
selectedComponents.add(comp);
58+
comp.select(this);
6059
for (SelectionToolListener selectionToolListener : listeners) {
6160
selectionToolListener.selectionChanged();
6261
}
@@ -70,8 +69,7 @@ public void selectComponent(PaintComponent comp) {
7069
* @param comp
7170
*/
7271
public void deselectComponent(PaintComponent comp) {
73-
comp.deselect();
74-
selectedComponents.remove(comp);
72+
comp.deselect(this);
7573
for (SelectionToolListener selectionToolListener : listeners) {
7674
selectionToolListener.selectionChanged();
7775
}
@@ -85,10 +83,9 @@ public void deselectComponent(PaintComponent comp) {
8583
*/
8684
public void clearSelection() {
8785
// remove all selection
88-
for (PaintComponent component : selectedComponents) {
89-
component.deselect();
86+
while(!selectedComponents.isEmpty()) {
87+
selectedComponents.get(0).deselect(this);
9088
}
91-
selectedComponents.clear();
9289

9390
for (SelectionToolListener selectionToolListener : listeners) {
9491
selectionToolListener.selectionChanged();

0 commit comments

Comments
 (0)