Skip to content

Commit 491ee77

Browse files
author
Ultimate Pea
authored
Merge pull request #9 from UCSDOalads/addIOComponentsbyUltimatePea
Add io componentsby ultimate pea, Complete DataInput and DataDisplay PaintComponent
2 parents 7355018 + 0721af8 commit 491ee77

11 files changed

+305
-11
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package actions;
2+
3+
import paintcomponents.DataDisplayPaintComponent;
4+
import ui.PaintPanel;
5+
6+
public class AddDataDisplayBoxAction extends PaintAction {
7+
8+
public AddDataDisplayBoxAction(PaintPanel panel) {
9+
super(panel);
10+
}
11+
12+
@Override
13+
public boolean canPerformAction() {
14+
return true;
15+
}
16+
17+
@Override
18+
public void performAction() {
19+
DataDisplayPaintComponent comp = new DataDisplayPaintComponent("Data Display", panel.getWidth() /2, panel.getHeight()/2);
20+
panel.addPaintComponent(comp);
21+
panel.repaint();
22+
}
23+
24+
@Override
25+
public String locationString() {
26+
return "Add/Data Display";
27+
}
28+
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package actions;
2+
3+
import java.util.ArrayList;
4+
5+
import paintcomponents.DataFromPoint;
6+
import paintcomponents.DataLineSegment;
7+
import paintcomponents.DataToPoint;
8+
import paintcomponents.PaintComponent;
9+
import ui.PaintPanel;
10+
11+
public class ConstructDataLineSegmentAction extends ConstructLineSegmentAction {
12+
13+
public ConstructDataLineSegmentAction(PaintPanel panel) {
14+
super(panel);
15+
// TODO Auto-generated constructor stub
16+
}
17+
18+
@Override
19+
public boolean canPerformAction() {
20+
if( super.canPerformAction() == false) return false;
21+
//we must connect from a DataFromPoint to DataToPoint
22+
//assume ConstructLineSegment is doing correctly, there is two corrently selected points
23+
ArrayList<PaintComponent> comps = this.panel.getSelectTool().getSelectedComponents();
24+
//TODO IMPORTANT Generic Argument is erased, may cause unexpected behavior when types dont match in the future
25+
if(comps.get(0) instanceof DataFromPoint<?> && comps.get(1) instanceof DataToPoint<?>){
26+
//allow connection only when no segment has no existing connections to the data
27+
if(((DataToPoint<?>)comps.get(1)).getLineSegment() == null){
28+
return true;
29+
}
30+
}
31+
return false;
32+
}
33+
34+
@Override
35+
public void performAction() {
36+
37+
ArrayList<PaintComponent> comps = this.panel.getSelectTool().getSelectedComponents();
38+
@SuppressWarnings("rawtypes")
39+
DataLineSegment<?> seg = new DataLineSegment((DataFromPoint<?>)comps.get(0), (DataToPoint<?>)comps.get(1));
40+
addLineSegment(seg);
41+
}
42+
43+
@Override
44+
public String locationString() {
45+
// TODO Auto-generated method stub
46+
return "Data/Construct/Line Segment";
47+
}
48+
49+
}

src/actions/ConstructLineSegmentAction.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,25 @@ public void performAction() {
5959

6060
//construct line segment
6161
LineSegment lineSegment = new LineSegment((SimplePoint)(items.get(0)), (SimplePoint)(items.get(1)));
62+
63+
64+
addLineSegment(lineSegment);
65+
}
66+
/**
67+
* This method updates the panel's list of paint components and selection after a line segment is added
68+
* Subclasses should call this method to update the panel when customizing the addition of a line segment
69+
*
70+
* @param lineSegment the lineSegment to be added to the painting panel
71+
*/
72+
protected void addLineSegment(LineSegment lineSegment) {
73+
6274
//add to panel
6375
panel.addPaintComponent(lineSegment);
6476

6577
//change selection
6678
panel.getSelectTool().clearSelection();
6779
panel.getSelectTool().selectComponent(lineSegment);
6880
panel.repaint();
69-
70-
7181
}
7282

7383
@Override

src/actions/PaintAction.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,47 @@
22

33
import ui.PaintPanel;
44

5+
/**
6+
* Paint Action abstracts a particular menu action
7+
*
8+
* Override this class to create a new Action.
9+
*
10+
* Perfrom action will only be called when can perform action returns true.
11+
*
12+
* Most of the cases, you have to call panel.repaint() as the last statement of
13+
* perform action
14+
*
15+
* @author chenzb
16+
*
17+
*/
518
public abstract class PaintAction {
6-
19+
720
protected PaintPanel panel;
821

9-
public PaintAction(PaintPanel panel){
22+
public PaintAction(PaintPanel panel) {
1023
this.panel = panel;
1124
}
12-
25+
26+
/**
27+
* Whether this action can perform. Subclasses generally base the return
28+
* value on the current selection on screen
29+
* <code>panel.getSelectTool().getSelectedComponents</code>
30+
*
31+
* @return true if the action can be performed
32+
*/
1333
public abstract boolean canPerformAction();
34+
1435
/**
15-
* Performs this action
16-
* Subclassess must invoke panel.repaint if the action changes the panel
36+
* Performs this action Subclassess must invoke panel.repaint if the action
37+
* changes the panel
1738
*/
1839
public abstract void performAction();
1940

41+
/**
42+
* The location of this item in the menu bar.
43+
* For example, "File/Save As..", "File/Open Recent/Clear Menu"
44+
* @return
45+
*/
2046
public abstract String locationString();
2147

2248
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package actions;
2+
3+
import java.util.NoSuchElementException;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import paintcomponents.DataDisplayPaintComponent;
8+
import paintcomponents.DataFromPointNoDataProviderException;
9+
import paintcomponents.DataFromPointProviderCannotProvideDataException;
10+
import paintcomponents.NoConnectingLineSegmentException;
11+
import ui.PaintPanel;
12+
13+
public class UpdateDataDisplayBoxAction extends PaintAction {
14+
15+
public UpdateDataDisplayBoxAction(PaintPanel panel) {
16+
super(panel);
17+
}
18+
19+
@Override
20+
public boolean canPerformAction() {
21+
if(panel.getSelectTool().getSelectedComponents().size() == 1){
22+
if(panel.getSelectTool().getSelectedComponents().get(0) instanceof DataDisplayPaintComponent){
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
29+
@Override
30+
public void performAction() {
31+
DataDisplayPaintComponent comp = (DataDisplayPaintComponent) panel.getSelectTool().getSelectedComponents().get(0) ;
32+
try {
33+
comp.updateDisplayText();
34+
panel.repaint();
35+
} catch (NoSuchElementException | NoConnectingLineSegmentException
36+
| DataFromPointNoDataProviderException
37+
| DataFromPointProviderCannotProvideDataException e) {
38+
e.printStackTrace();
39+
JOptionPane.showMessageDialog(panel, e.toString());
40+
}
41+
42+
}
43+
44+
@Override
45+
public String locationString() {
46+
return "Data/Display Box/Update";
47+
}
48+
49+
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
import painttools.tools.SelectionToolListener;
1111
import ui.PaintPanel;
12+
import actions.AddDataDisplayBoxAction;
1213
import actions.AddDataInputBoxAction;
1314
import actions.AddTextBoxAction;
15+
import actions.ConstructDataLineSegmentAction;
1416
import actions.ConstructLineSegmentAction;
1517
import actions.GeneratePolygonSourceJava;
1618
import actions.InputDataForDataInputBoxAction;
1719
import actions.PaintAction;
20+
import actions.UpdateDataDisplayBoxAction;
1821

1922
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
2023

@@ -24,6 +27,13 @@ public ActionsMenuBar(PaintPanel panel){
2427
addAction(new AddTextBoxAction(panel));
2528
addAction(new AddDataInputBoxAction(panel));
2629
addAction(new InputDataForDataInputBoxAction(panel));
30+
31+
//data display
32+
addAction(new AddDataDisplayBoxAction(panel));
33+
addAction(new UpdateDataDisplayBoxAction(panel));
34+
35+
//data segments
36+
addAction(new ConstructDataLineSegmentAction(panel));
2737

2838
}
2939

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
62+
@Override
63+
public void select(SelectTool selectTool) {
64+
int x = selectTool.getLastMouseEvent().getX();
65+
int y = selectTool.getLastMouseEvent().getY();
66+
if (toPoint.contains(x, y)) {
67+
toPoint.select(selectTool);
68+
} else {
69+
super.select(selectTool);
70+
}
71+
72+
}
73+
74+
@Override
75+
public void deselect(SelectTool selectTool) {
76+
int x = selectTool.getLastMouseEvent().getX();
77+
int y = selectTool.getLastMouseEvent().getY();
78+
if (toPoint.contains(x, y)) {
79+
toPoint.deselect(selectTool);
80+
} else {
81+
super.deselect(selectTool);
82+
}
83+
}
84+
85+
@Override
86+
public boolean isSelected() {
87+
//if the from point is selected, this components is considered selected
88+
if(this.toPoint.isSelected()) return true;
89+
return super.isSelected();
90+
}
91+
92+
/**
93+
* Update the current display.
94+
*
95+
* This class will try to fetch the data from the toPointClass
96+
* @throws DataFromPointProviderCannotProvideDataException
97+
* @throws DataFromPointNoDataProviderException
98+
* @throws NoConnectingLineSegmentException
99+
* @throws NoSuchElementException
100+
* @see DataToPoint.fetchData for exception details
101+
*/
102+
public void updateDisplayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{
103+
this.setDisplayingText(toPoint.fetchData());
104+
}
105+
106+
107+
108+
}

src/paintcomponents/DataFromPoint.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ public void setProvider(DataFromPointDataProvider<T> provider) {
6060
this.provider = provider;
6161
}
6262

63+
6364
}

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*/
1313
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent
14-
implements DataFromPointDataProvider<String> {
14+
implements DataFromPointDataProvider<String>{
1515

1616
private static final int HORIZONTAL_OFFSET = 10;
1717
private DataFromPoint<String> fromPoint;
@@ -73,7 +73,7 @@ public String provideInformationToDataFromPoint(
7373
@Override
7474
public boolean canProvideInformationToDataFromPoint(
7575
DataFromPoint<String> dataFromPoint) {
76-
return displayingText == null;
76+
return displayingText != null;
7777
}
7878

7979
@Override
@@ -98,5 +98,15 @@ public void deselect(SelectTool selectTool) {
9898
super.deselect(selectTool);
9999
}
100100
}
101+
102+
@Override
103+
public boolean isSelected() {
104+
//if the from point is selected, this components is considered selected
105+
if(this.fromPoint.isSelected()) return true;
106+
return super.isSelected();
107+
}
108+
109+
101110

111+
102112
}

src/paintcomponents/DataLineSegment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public class DataLineSegment<T> extends LineSegment {
44

55
public DataLineSegment(DataFromPoint<T> fromPoint, DataToPoint<T> toPoint) {
66
super(fromPoint, toPoint);
7+
fromPoint.setLineSegment(this);
8+
toPoint.setLineSegment(this);
79

810
}
911

0 commit comments

Comments
 (0)