Skip to content

Commit a082739

Browse files
authored
Merge pull request #33 from UCSDOalads/MYCBranch
Add and remove functions for annotations
2 parents 82e7903 + e953fa9 commit a082739

9 files changed

Lines changed: 169 additions & 6 deletions
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 javax.swing.JOptionPane;
6+
7+
import actions.menu.ActionsMenuBarTitles;
8+
import paintcomponents.PaintComponent;
9+
import paintcomponents.annotations.TextAnnotation;
10+
import paintcomponents.data.DataTextPaintComponent;
11+
import ui.PaintPanel;
12+
13+
public class AddAnnotationAction extends PaintAction{
14+
15+
public AddAnnotationAction(PaintPanel panel) {
16+
super(panel);
17+
}
18+
19+
@Override
20+
public boolean canPerformAction() {
21+
//get selected components
22+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
23+
24+
if(items.size() != 1){
25+
return false;
26+
}
27+
if(!(items.get(0) instanceof DataTextPaintComponent)){
28+
return false;
29+
}
30+
return true;
31+
}
32+
33+
@Override
34+
public void performAction() {
35+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
36+
String annotations = JOptionPane
37+
.showInputDialog("Please specify the annotation of the component");
38+
new TextAnnotation(items.get(0), annotations);
39+
40+
panel.repaint();
41+
}
42+
43+
@Override
44+
public String locationString() {
45+
return ActionsMenuBarTitles.Data().Annotations().Add().toString();
46+
}
47+
48+
49+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package actions;
2+
3+
import java.util.ArrayList;
4+
5+
import actions.menu.ActionsMenuBarTitles;
6+
import paintcomponents.PaintComponent;
7+
import ui.PaintPanel;
8+
9+
public class RemoveAnnotationAction extends PaintAction{
10+
11+
public RemoveAnnotationAction(PaintPanel panel) {
12+
super(panel);
13+
}
14+
15+
@Override
16+
public boolean canPerformAction() {
17+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
18+
19+
if(items.size() != 1){
20+
return false;
21+
}
22+
if(items.get(0).getOptionalAnnotation() == null){
23+
return false;
24+
}
25+
return true;
26+
}
27+
28+
@Override
29+
public void performAction() {
30+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
31+
items.get(0).setOptionalAnnotation(null);
32+
panel.repaint();
33+
}
34+
35+
@Override
36+
public String locationString() {
37+
return ActionsMenuBarTitles.Data().Annotations().Remove().toString();
38+
}
39+
40+
}

src/actions/menu/ActionsMenuBar.java

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

1010
import painttools.tools.SelectionToolListener;
1111
import ui.PaintPanel;
12+
import actions.AddAnnotationAction;
1213
import actions.AddDataDisplayBoxAction;
1314
import actions.AddDataInputBoxAction;
1415
import actions.AddHaskellComponent;
@@ -27,6 +28,7 @@
2728
import actions.GeneratePolygonSourceJava;
2829
import actions.InputDataForDataInputBoxAction;
2930
import actions.PaintAction;
31+
import actions.RemoveAnnotationAction;
3032
import actions.RemovePaintComponent;
3133
import actions.UpdateDataDisplayBoxAction;
3234
import actions.singleinstanceoperations.SetPointSizeOperation;
@@ -79,6 +81,9 @@ public ActionsMenuBar(PaintPanel panel){
7981
addAction(new UpdateFontSizeOperation(panel));
8082
addAction(new SetPointSizeOperation(panel));
8183

84+
// add data annotation
85+
addAction(new AddAnnotationAction(panel));
86+
addAction(new RemoveAnnotationAction(panel));
8287
}
8388

8489
private void addAction(PaintAction action) {

src/actions/menu/ActionsMenuBarTitles.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,9 @@ public ActionsMenuBarTitles Zoom_Out() {
138138
return this;
139139
}
140140

141+
public ActionsMenuBarTitles Annotations() {
142+
append("Annotations");
143+
return this;
144+
}
145+
141146
}

src/paintcomponents/PaintComponent.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.w3c.dom.Node;
88
import org.w3c.dom.NodeList;
99

10+
import paintcomponents.annotations.PaintComponentAnnotation;
1011
import painttools.tools.SelectTool;
1112
import ui.PaintPanel;
1213

@@ -38,6 +39,7 @@ public abstract class PaintComponent {
3839
static private long UNIQUE_ID = 0;
3940
long uid = ++UNIQUE_ID;
4041

42+
private PaintComponentAnnotation optionalAnnotation;
4143
/**
4244
* Get a Unique ID of this component. IDs resets to zero when JVM starts;
4345
*
@@ -95,6 +97,10 @@ public void paint(Graphics g) {
9597
paintNotSelected(g);
9698
}
9799

100+
//paint annotation
101+
if(optionalAnnotation != null){
102+
optionalAnnotation.paint(g);
103+
}
98104
}
99105

100106
/**
@@ -166,7 +172,11 @@ public boolean isSelected() {
166172
public void translate(int i, int j) {
167173
this.x += i;
168174
this.y += j;
169-
175+
176+
//if attached component is not null, translate it as well
177+
if(optionalAnnotation != null){
178+
optionalAnnotation.translate(i, j);
179+
}
170180
}
171181

172182
public abstract boolean contains(int x2, int y2);
@@ -220,4 +230,18 @@ public PaintComponent(Element rootElement, PaintPanel panel) {
220230
this.x = Integer.parseInt(pos.getElementsByTagName("xcoordinate").item(0).getTextContent());
221231
this.y = Integer.parseInt(pos.getElementsByTagName("ycoordinate").item(0).getTextContent());
222232
}
233+
234+
/**
235+
* @return the optionalAnnotation
236+
*/
237+
public PaintComponentAnnotation getOptionalAnnotation() {
238+
return optionalAnnotation;
239+
}
240+
241+
/**
242+
* @param optionalAnnotation the optionalAnnotation to set
243+
*/
244+
public void setOptionalAnnotation(PaintComponentAnnotation optionalAnnotation) {
245+
this.optionalAnnotation = optionalAnnotation;
246+
}
223247
}

src/paintcomponents/TextPaintComponent.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ protected void paintNotSelected(Graphics g) {
103103
updateBoundsAndDrawString(g);
104104
}
105105
private void updateBoundsAndDrawString(Graphics g) {
106+
updateBounds(g);
107+
drawString(g);
108+
109+
}
110+
111+
/**
112+
* Update bounds updates the width and row height of this text paint component
113+
* @param g Graphics Object
114+
*/
115+
public void updateBounds(Graphics g) {
106116
//reset bounds to begin calculation
107117
width = 0;
108118
//derive row Height
@@ -120,6 +130,20 @@ private void updateBoundsAndDrawString(Graphics g) {
120130
if( curStringWidth > width){
121131
width = curStringWidth;
122132
}
133+
}
134+
}
135+
136+
/**
137+
* Draws the string, do not update bounds
138+
* @param g
139+
*/
140+
private void drawString(Graphics g){
141+
142+
//get number of rows
143+
String[] rows = this.displayingText.split("\n");
144+
145+
for (int i = 0; i < rows.length; i++) {
146+
String string = rows[i];
123147

124148
g.drawString(string, getX(), (int) (getY() + rowHeight * (i + 1)));
125149
}

src/paintcomponents/annotations/PaintComponentAnnotation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public abstract class PaintComponentAnnotation extends PaintComponent{
1818
public PaintComponentAnnotation(PaintComponent attachedComponent) {
1919
super(attachedComponent.getX(), attachedComponent.getY());
2020
this.setAttachedComponent(attachedComponent);
21+
attachedComponent.setOptionalAnnotation(this);
2122
}
2223

2324
/**

src/paintcomponents/annotations/TextAnnotation.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,45 @@
33
import java.awt.Graphics;
44

55
import paintcomponents.PaintComponent;
6+
import paintcomponents.TextPaintComponent;
67

78
public class TextAnnotation extends PaintComponentAnnotation {
89

9-
public TextAnnotation(PaintComponent attachedComponent) {
10+
private TextPaintComponent textPaintComponent;
11+
12+
13+
public TextAnnotation(PaintComponent attachedComponent, String displayingText) {
1014
super(attachedComponent);
15+
this.textPaintComponent = new TextPaintComponent(displayingText, attachedComponent.getX(),
16+
attachedComponent.getY());
17+
1118
}
1219

1320
@Override
1421
protected void paintNotSelected(Graphics g) {
1522
// TODO Auto-generated method stub
16-
23+
textPaintComponent.updateBounds(g);
24+
textPaintComponent.setY(getAttachedComponent().getY() - textPaintComponent.getRowHeight());
25+
textPaintComponent.paint(g);
1726
}
1827

1928
@Override
2029
protected void paintSelected(Graphics g) {
2130
// TODO Auto-generated method stub
22-
31+
textPaintComponent.updateBounds(g);
32+
textPaintComponent.setY(getAttachedComponent().getY() - textPaintComponent.getRowHeight());
33+
textPaintComponent.paint(g);
2334
}
2435

2536
@Override
2637
public boolean contains(int x2, int y2) {
2738
// TODO Auto-generated method stub
28-
return false;
39+
return textPaintComponent.contains(x2, y2);
2940
}
3041

42+
public void translate(int x, int y){
43+
textPaintComponent.translate(x, y);
44+
}
45+
46+
3147
}

src/ui/PaintPanel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ public void showCursor() {
176176

177177
public void addPaintComponent(PaintComponent comp) {
178178
components.add(comp);
179-
180179
}
181180

182181
public ArrayList<PaintComponent> getPaintComponents() {

0 commit comments

Comments
 (0)