Skip to content

Commit c1be514

Browse files
authored
Merge pull request #41 from UCSDOalads/MYCBranch
add set annotation font size function
2 parents 49744e5 + ba932c2 commit c1be514

8 files changed

Lines changed: 180 additions & 45 deletions

src/actions/AddAnnotationAction.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,48 @@
55
import javax.swing.JOptionPane;
66

77
import actions.menu.ActionsMenuBarTitles;
8+
import actions.singleinstanceoperations.SingleInstanceOperation;
89
import paintcomponents.PaintComponent;
910
import paintcomponents.annotations.TextAnnotation;
1011
import paintcomponents.data.DataTextPaintComponent;
1112
import ui.PaintPanel;
1213

13-
public class AddAnnotationAction extends PaintAction{
14-
14+
/**
15+
* add the annotation to a component
16+
*
17+
* @author muchi
18+
*
19+
*/
20+
public class AddAnnotationAction extends SingleInstanceOperation<PaintComponent>{
21+
22+
/**
23+
* ctor
24+
* @param panel the panel
25+
*/
1526
public AddAnnotationAction(PaintPanel panel) {
1627
super(panel);
1728
}
1829

30+
/**
31+
* @return the location of the button
32+
*/
1933
@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;
34+
public String locationString() {
35+
return ActionsMenuBarTitles.Data().Annotations().Add().toString();
3136
}
3237

3338
@Override
34-
public void performAction() {
35-
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
39+
protected void performActionOnInstance(PaintComponent instance) {
40+
// TODO Auto-generated method stub
3641
String annotations = JOptionPane
3742
.showInputDialog("Please specify the annotation of the component");
38-
new TextAnnotation(items.get(0), annotations);
43+
new TextAnnotation(instance, annotations);
3944

40-
panel.repaint();
4145
}
4246

4347
@Override
44-
public String locationString() {
45-
return ActionsMenuBarTitles.Data().Annotations().Add().toString();
48+
protected Class<PaintComponent> getGenericClassType() {
49+
return PaintComponent.class;
4650
}
4751

4852

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package actions;
2+
3+
import java.util.ArrayList;
4+
5+
import paintcomponents.PaintComponent;
6+
import paintcomponents.TextPaintComponent;
7+
import ui.PaintPanel;
8+
import ui.general.InputManager;
9+
import ui.general.InputManagerDelegate;
10+
import actions.menu.ActionsMenuBarTitles;
11+
import actions.singleinstanceoperations.SingleInstanceOperation;
12+
import paintcomponents.annotations.*;
13+
14+
/**
15+
* edit the size of the component
16+
*
17+
* @author muchi
18+
*
19+
*/
20+
public class EditAnnotationSizeAction extends SingleInstanceOperation<PaintComponent>{
21+
22+
/**
23+
* ctor
24+
* @param panel the panel of the component
25+
*/
26+
public EditAnnotationSizeAction(PaintPanel panel) {
27+
super(panel);
28+
}
29+
30+
/**
31+
* the action can only be performed when there is only one component selected and the component has a annotation
32+
*
33+
* @return true if action can be performed, otherwise false
34+
*/
35+
@Override
36+
public boolean canPerformAction(){
37+
if(super.canPerformAction() == false){ return false;}
38+
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
39+
40+
// check if the annotation of the component is not null
41+
if(items.get(0).getOptionalAnnotation() == null){
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
48+
@Override
49+
protected void performActionOnInstance(PaintComponent instance) {
50+
InputManager.sharedInstance().askForFloat(panel, new InputManagerDelegate<Float>() {
51+
52+
@Override
53+
public void didFinishInput(Float input) {
54+
instance.getOptionalAnnotation().setFontSize(input);
55+
panel.repaint();
56+
}
57+
});
58+
59+
}
60+
61+
@Override
62+
protected Class<PaintComponent> getGenericClassType() {
63+
// TODO Auto-generated method stub
64+
return PaintComponent.class;
65+
}
66+
67+
/**
68+
* @return the location of the button
69+
*/
70+
@Override
71+
public String locationString() {
72+
return ActionsMenuBarTitles.Edit().Annotation_Font_Size().toString();
73+
}
74+
75+
}

src/actions/RemoveAnnotationAction.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,63 @@
33
import java.util.ArrayList;
44

55
import actions.menu.ActionsMenuBarTitles;
6+
import actions.singleinstanceoperations.SingleInstanceOperation;
67
import paintcomponents.PaintComponent;
78
import ui.PaintPanel;
89

9-
public class RemoveAnnotationAction extends PaintAction{
10+
/**
11+
* remove the annotation of the component
12+
*
13+
* @author muchi
14+
*
15+
*/
16+
public class RemoveAnnotationAction extends SingleInstanceOperation<PaintComponent>{
1017

18+
/**
19+
* ctor
20+
* @param panel the panel of the component
21+
*/
1122
public RemoveAnnotationAction(PaintPanel panel) {
1223
super(panel);
1324
}
1425

26+
/**
27+
* the perform only can be performed when only one component is selected and the component has an annotation
28+
*
29+
* @return true if the action can be performed, otherwise false
30+
*/
1531
@Override
1632
public boolean canPerformAction() {
33+
if(super.canPerformAction() == false){ return false;}
1734
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
1835

19-
if(items.size() != 1){
20-
return false;
21-
}
36+
// check if the component has an annotation
2237
if(items.get(0).getOptionalAnnotation() == null){
2338
return false;
2439
}
2540
return true;
2641
}
2742

43+
/**
44+
* @return the location of the button
45+
*/
2846
@Override
29-
public void performAction() {
30-
ArrayList<PaintComponent> items = panel.getSelectTool().getSelectedComponents();
31-
items.get(0).setOptionalAnnotation(null);
32-
panel.repaint();
47+
public String locationString() {
48+
return ActionsMenuBarTitles.Data().Annotations().Remove().toString();
3349
}
3450

3551
@Override
36-
public String locationString() {
37-
return ActionsMenuBarTitles.Data().Annotations().Remove().toString();
52+
protected void performActionOnInstance(PaintComponent instance) {
53+
// TODO Auto-generated method stub
54+
instance.setOptionalAnnotation(null);
55+
}
56+
57+
@Override
58+
protected Class<PaintComponent> getGenericClassType() {
59+
// TODO Auto-generated method stub
60+
return PaintComponent.class;
3861
}
3962

63+
64+
4065
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import actions.AddTextBoxAction;
2323
import actions.ConstructDataLineSegmentAction;
2424
import actions.ConstructLineSegmentAction;
25+
import actions.EditAnnotationSizeAction;
2526
import actions.EditRedoAction;
2627
import actions.EditUndoAction;
2728
import actions.FileOpen;
@@ -88,6 +89,8 @@ public ActionsMenuBar(PaintPanel panel){
8889
// add data annotation
8990
addAction(new AddAnnotationAction(panel));
9091
addAction(new RemoveAnnotationAction(panel));
92+
93+
addAction(new EditAnnotationSizeAction(panel));
9194
}
9295

9396
private void addAction(PaintAction action) {

src/actions/menu/ActionsMenuBarTitles.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,9 @@ public ActionsMenuBarTitles Add_Instance_Method() {
153153
append("Add Instance Method");
154154
return this;
155155
}
156+
157+
public ActionsMenuBarTitles Annotation_Font_Size() {
158+
append("Annotation Font Size");
159+
return this;
160+
}
156161
}

src/paintcomponents/TextPaintComponent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public TextPaintComponent(Element rootElement, PaintPanel panel) {
9696

9797
@Override
9898
protected void paintNotSelected(Graphics g) {
99-
g.setFont(g.getFont().deriveFont(fontSize));
10099
g.setColor(defaultTextColor);
101100

102101
//draw string starts from bottom left corner, shift to top left
@@ -113,6 +112,8 @@ private void updateBoundsAndDrawString(Graphics g) {
113112
* @param g Graphics Object
114113
*/
115114
public void updateBounds(Graphics g) {
115+
g.setFont(g.getFont().deriveFont(fontSize));
116+
116117
//reset bounds to begin calculation
117118
width = 0;
118119
//derive row Height
@@ -155,7 +156,6 @@ private void drawString(Graphics g){
155156

156157
@Override
157158
protected void paintSelected(Graphics g) {
158-
g.setFont(g.getFont().deriveFont(fontSize));
159159
g.setColor(selectedTextColor);
160160

161161
//draw string starts from bottom left corner, shift to top left

src/paintcomponents/annotations/PaintComponentAnnotation.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ public void setAttachedComponent(PaintComponent attachedComponent) {
3535
this.attachedComponent = attachedComponent;
3636
}
3737

38-
39-
40-
41-
42-
43-
44-
45-
38+
/**
39+
* set the font size of the annotation
40+
*
41+
* @param input the new size
42+
*/
43+
abstract public void setFontSize(float input);
4644
}

src/paintcomponents/annotations/TextAnnotation.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@
55
import paintcomponents.PaintComponent;
66
import paintcomponents.TextPaintComponent;
77

8+
/**
9+
* the text annotation of the component
10+
* @author muchi
11+
*
12+
*/
813
public class TextAnnotation extends PaintComponentAnnotation {
914

1015
private TextPaintComponent textPaintComponent;
1116

12-
17+
/**
18+
* create the annotation of a component with information
19+
*
20+
* @param attachedComponent the component attached to the annotation
21+
* @param displayingText the information to be displayed
22+
*/
1323
public TextAnnotation(PaintComponent attachedComponent, String displayingText) {
1424
super(attachedComponent);
1525
this.textPaintComponent = new TextPaintComponent(displayingText, attachedComponent.getX(),
16-
attachedComponent.getY());
17-
26+
attachedComponent.getY());
1827
}
1928

2029
@Override
2130
protected void paintNotSelected(Graphics g) {
22-
// TODO Auto-generated method stub
2331
textPaintComponent.updateBounds(g);
2432
textPaintComponent.setY(getAttachedComponent().getY() - textPaintComponent.getRowHeight());
2533
textPaintComponent.paint(g);
@@ -33,15 +41,32 @@ protected void paintSelected(Graphics g) {
3341
textPaintComponent.paint(g);
3442
}
3543

44+
/**
45+
* @return true if the given position is contained, otherwise false
46+
*/
3647
@Override
3748
public boolean contains(int x2, int y2) {
3849
// TODO Auto-generated method stub
3950
return textPaintComponent.contains(x2, y2);
4051
}
4152

53+
/**
54+
* move the text
55+
*
56+
* @param x the movement on x-position
57+
* @param y the movement on y-position
58+
*/
4259
public void translate(int x, int y){
4360
textPaintComponent.translate(x, y);
4461
}
45-
46-
62+
63+
/**
64+
* set the font size of the given annotation
65+
*
66+
* @param input the size
67+
*/
68+
@Override
69+
public void setFontSize(float input) {
70+
textPaintComponent.setFontSize(input);
71+
}
4772
}

0 commit comments

Comments
 (0)