Skip to content

Commit 001a581

Browse files
authored
Merge pull request #43 from UCSDOalads/addTESTbylhwlyd1
Add interactive evaluate actions
2 parents c1be514 + 30cd5e0 commit 001a581

7 files changed

Lines changed: 252 additions & 7 deletions
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package actions;
2+
3+
import java.lang.reflect.Constructor;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import actions.edit.undoredo.SharedUndoRedoActionManager;
8+
import actions.edit.undoredo.UndoRedoableInterface;
9+
import actions.menu.ActionsMenuBarTitles;
10+
import paintcomponents.java.interactive.ClassConstructorPaintComponent;
11+
import paintcomponents.java.lazy.ClassPaintComponent;
12+
import ui.PaintPanel;
13+
14+
public class AddInteractiveConstructorAction extends PaintAction {
15+
16+
public AddInteractiveConstructorAction(PaintPanel panel) {
17+
super(panel);
18+
}
19+
20+
@Override
21+
public boolean canPerformAction() {
22+
if (panel.getSelectTool().getSelectedComponents().size() != 1) {
23+
return false;
24+
}
25+
if (panel.getSelectTool().getSelectedComponents()
26+
.get(0) instanceof ClassPaintComponent) {
27+
return true;
28+
}
29+
return false;
30+
}
31+
32+
@Override
33+
public void performAction() {
34+
ClassPaintComponent comp = (ClassPaintComponent) panel.getSelectTool()
35+
.getSelectedComponents().get(0);
36+
Constructor[] cons = comp.getDisplayingClass().getConstructors();
37+
38+
int desiaredConstructorIndex = Integer
39+
.parseInt(JOptionPane.showInputDialog(
40+
"Please enter the index of the constructor you would like to use: \n\n\n"
41+
+ getConstructorsSelectionUI(cons)));
42+
ClassConstructorPaintComponent consComp = new ClassConstructorPaintComponent(
43+
cons[desiaredConstructorIndex], panel.getWidth() / 2,
44+
panel.getHeight() / 2);
45+
panel.addPaintComponent(consComp);
46+
// add action to undo redo manager
47+
SharedUndoRedoActionManager.getSharedInstance().pushUndoableAction(new UndoRedoableInterface() {
48+
49+
@Override
50+
public void undoAction() {
51+
consComp.remove(panel);
52+
panel.repaint();
53+
}
54+
55+
@Override
56+
public void redoAction() {
57+
panel.addPaintComponent(consComp);
58+
panel.repaint();
59+
}
60+
});
61+
panel.repaint();
62+
}
63+
64+
public String getConstructorsSelectionUI(Constructor[] cons) {
65+
StringBuilder builder = new StringBuilder();
66+
for (int i = 0; i < cons.length; i++) {
67+
Constructor constructor = cons[i];
68+
builder.append(i + " : " + constructor.toString() + "\n");
69+
}
70+
return builder.toString();
71+
72+
}
73+
74+
@Override
75+
public String locationString() {
76+
return ActionsMenuBarTitles.Developer("Interactive/Constructor").toString();
77+
}
78+
79+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package actions;
2+
3+
import java.lang.reflect.Method;
4+
5+
import javax.swing.JOptionPane;
6+
7+
import actions.edit.undoredo.SharedUndoRedoActionManager;
8+
import actions.edit.undoredo.UndoRedoableInterface;
9+
import actions.menu.ActionsMenuBarTitles;
10+
11+
import paintcomponents.java.interactive.ClassConstructorPaintComponent;
12+
import paintcomponents.java.interactive.MethodPaintComponent;
13+
import ui.PaintPanel;
14+
15+
public class AddInteractiveJavaMethodComponentAction extends PaintAction {
16+
17+
public AddInteractiveJavaMethodComponentAction(PaintPanel panel) {
18+
super(panel);
19+
}
20+
21+
//TODO
22+
//NOTE: I am copying from Constructor, consider refinement
23+
24+
@Override
25+
public boolean canPerformAction() {
26+
if (panel.getSelectTool().getSelectedComponents().size() != 1) {
27+
return false;
28+
}
29+
if (panel.getSelectTool().getSelectedComponents()
30+
.get(0) instanceof ClassConstructorPaintComponent) {
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
37+
@Override
38+
public void performAction() {
39+
ClassConstructorPaintComponent comp = (ClassConstructorPaintComponent) panel.getSelectTool()
40+
.getSelectedComponents().get(0);
41+
Method[] methods = comp.getDisplayingClass().getMethods();
42+
43+
44+
45+
46+
int desiaredConstructorIndex = Integer
47+
.parseInt(JOptionPane.showInputDialog(
48+
"Please enter the index of the constructor you would like to use: \n\n\n"
49+
+ getMethodsSelectionUI(methods)));
50+
MethodPaintComponent methodComp = new MethodPaintComponent(
51+
methods[desiaredConstructorIndex], comp.getInstance(), panel.getWidth() / 2,
52+
panel.getHeight() / 2);
53+
panel.addPaintComponent(methodComp);
54+
// add action to undo redo manager
55+
SharedUndoRedoActionManager.getSharedInstance().pushUndoableAction(new UndoRedoableInterface() {
56+
57+
@Override
58+
public void undoAction() {
59+
methodComp.remove(panel);
60+
panel.repaint();
61+
}
62+
63+
@Override
64+
public void redoAction() {
65+
panel.addPaintComponent(methodComp);
66+
panel.repaint();
67+
}
68+
});
69+
panel.repaint();
70+
}
71+
public String getMethodsSelectionUI(Method[] methods) {
72+
StringBuilder builder = new StringBuilder();
73+
for (int i = 0; i < methods.length; i++) {
74+
Method constructor = methods[i];
75+
builder.append(i + " : " + constructor.toString() + "\n");
76+
}
77+
return builder.toString();
78+
79+
}
80+
@Override
81+
public String locationString() {
82+
return ActionsMenuBarTitles.Developer("Interactive/Method").toString();
83+
}
84+
85+
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@
3333
import actions.RemoveAnnotationAction;
3434
import actions.RemovePaintComponent;
3535
import actions.UpdateDataDisplayBoxAction;
36+
import actions.singleinstanceoperations.ExecuteJavaInteractiveConstructor;
37+
import actions.singleinstanceoperations.ExecuteJavaInteractiveMethod;
3638
import actions.singleinstanceoperations.SetPointSizeOperation;
3739
import actions.singleinstanceoperations.UpdateFontSizeOperation;
3840
import actions.ZoomInAction;
3941
import actions.ZoomOutAction;
4042
import actions.ZoomInAction;
4143
import actions.ZoomOutAction;
4244
import actions.AddInstanceOperationAction;
45+
import actions.AddInteractiveConstructorAction;
46+
import actions.AddInteractiveJavaMethodComponentAction;
4347

4448
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
4549

@@ -65,6 +69,12 @@ public ActionsMenuBar(PaintPanel panel){
6569
addAction(new AddInstanceOperationAction(panel));
6670
addAction(new AddInstanceMethodAction(panel));
6771

72+
//interactive
73+
addAction(new AddInteractiveConstructorAction(panel));
74+
addAction(new AddInteractiveJavaMethodComponentAction(panel));
75+
addAction(new ExecuteJavaInteractiveConstructor(panel));
76+
addAction(new ExecuteJavaInteractiveMethod(panel));
77+
6878
//edit
6979
addAction(new EditRedoAction(panel));
7080
addAction(new EditUndoAction(panel));
@@ -75,6 +85,8 @@ public ActionsMenuBar(PaintPanel panel){
7585
addAction(new AddHaskellComponent(panel));
7686
addAction(new AddHaskellEvaluatorComponentAction(panel));
7787

88+
89+
7890
//file
7991
addAction(new FileSaveAs(panel));
8092
addAction(new FileOpen(panel));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package actions.singleinstanceoperations;
2+
3+
import actions.menu.ActionsMenuBarTitles;
4+
import paintcomponents.java.interactive.ClassConstructorPaintComponent;
5+
import ui.PaintPanel;
6+
7+
public class ExecuteJavaInteractiveConstructor extends SingleInstanceOperation<ClassConstructorPaintComponent> {
8+
9+
public ExecuteJavaInteractiveConstructor(PaintPanel panel) {
10+
super(panel);
11+
// TODO Auto-generated constructor stub
12+
}
13+
14+
@Override
15+
protected void performActionOnInstance(ClassConstructorPaintComponent instance) {
16+
17+
instance.evaluate();
18+
}
19+
20+
@Override
21+
protected Class<ClassConstructorPaintComponent> getGenericClassType() {
22+
return ClassConstructorPaintComponent.class;
23+
}
24+
25+
@Override
26+
public String locationString() {
27+
// TODO Auto-generated method stub
28+
return ActionsMenuBarTitles.Developer("Interactive/Constructor Evaluate").toString();
29+
}
30+
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package actions.singleinstanceoperations;
2+
3+
import actions.menu.ActionsMenuBarTitles;
4+
import paintcomponents.java.interactive.ClassConstructorPaintComponent;
5+
import paintcomponents.java.interactive.MethodPaintComponent;
6+
import ui.PaintPanel;
7+
8+
public class ExecuteJavaInteractiveMethod extends SingleInstanceOperation<MethodPaintComponent>{
9+
10+
public ExecuteJavaInteractiveMethod(PaintPanel panel) {
11+
super(panel);
12+
}
13+
14+
@Override
15+
protected void performActionOnInstance(MethodPaintComponent instance) {
16+
// TODO Auto-generated method stub
17+
instance.evaluate();
18+
}
19+
20+
@Override
21+
protected Class<MethodPaintComponent> getGenericClassType() {
22+
// TODO Auto-generated method stub
23+
return MethodPaintComponent.class;
24+
}
25+
26+
@Override
27+
public String locationString() {
28+
// TODO Auto-generated method stub
29+
return ActionsMenuBarTitles.Developer("Interactive/Method Evaluate").toString();
30+
}
31+
32+
}

src/paintcomponents/java/interactive/ClassConstructorPaintComponent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private void init() {
6868
/**
6969
* Calculate the input data and store it.
7070
*/
71-
public void evaluate(DataFromPoint dataFromPoint){
71+
public void evaluate(){
7272
// prepare argument list
7373
ArrayList<DataToPoint> toPoints = getToPoints();
7474
Object[] args = new Object[toPoints.size()];
@@ -117,4 +117,12 @@ public boolean canProvideInformationToDataFromPoint(
117117
return instance != null;
118118
}
119119

120+
public Class getDisplayingClass() {
121+
return displayingConstructor.getDeclaringClass();
122+
}
123+
124+
public Object getInstance() {
125+
return instance;
126+
}
127+
120128
}

src/paintcomponents/java/interactive/MethodPaintComponent.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public MethodPaintComponent(Method displayingMethod, Object instance, int x, int
5555

5656
private void init() {
5757

58-
// line 0 is signature
59-
addToPoint(1, new JavaType(this.displayingMethod.getDeclaringClass()));
6058
// parameters take place from line 2 to length+1
6159
Class[] paramTypes = displayingMethod.getParameterTypes();
6260
for (int i = 0; i < paramTypes.length ; i++) {
@@ -83,7 +81,7 @@ private void init() {
8381
/**
8482
* Calculate the input data and store it.
8583
*/
86-
public void evaluate(DataFromPoint dataFromPoint){
84+
public void evaluate(){
8785

8886
// prepare argument list
8987
ArrayList<DataToPoint> toPoints = getToPoints();
@@ -92,11 +90,11 @@ public void evaluate(DataFromPoint dataFromPoint){
9290

9391

9492
// args takes toPoint 1 to size
95-
Object[] args = new Object[toPoints.size() - 1];
93+
Object[] args = new Object[toPoints.size()];
9694

9795
//Get the input data from each the input points
98-
for (int i = 0; i < toPoints.size() - 1; i++) {
99-
DataToPoint toPoint = toPoints.get(i+1);
96+
for (int i = 0; i < toPoints.size() ; i++) {
97+
DataToPoint toPoint = toPoints.get(i);
10098
try {
10199
args[i] = toPoint.fetchData();
102100
} catch (NoSuchElementException | NoConnectingLineSegmentException

0 commit comments

Comments
 (0)