Skip to content

Commit 825010a

Browse files
author
Xiangyi Gong
committed
Merge branch 'addTESTbylhwlyd1' of https://github.com/UCSDOalads/JavaSketchPad into XYGBranch
2 parents c462625 + 30cd5e0 commit 825010a

7 files changed

Lines changed: 265 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
@@ -32,13 +32,17 @@
3232
import actions.RemoveAnnotationAction;
3333
import actions.RemovePaintComponent;
3434
import actions.UpdateDataDisplayBoxAction;
35+
import actions.singleinstanceoperations.ExecuteJavaInteractiveConstructor;
36+
import actions.singleinstanceoperations.ExecuteJavaInteractiveMethod;
3537
import actions.singleinstanceoperations.SetPointSizeOperation;
3638
import actions.singleinstanceoperations.UpdateFontSizeOperation;
3739
import actions.ZoomInAction;
3840
import actions.ZoomOutAction;
3941
import actions.ZoomInAction;
4042
import actions.ZoomOutAction;
4143
import actions.AddInstanceOperationAction;
44+
import actions.AddInteractiveConstructorAction;
45+
import actions.AddInteractiveJavaMethodComponentAction;
4246

4347
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
4448

@@ -64,6 +68,12 @@ public ActionsMenuBar(PaintPanel panel){
6468
addAction(new AddInstanceOperationAction(panel));
6569
addAction(new AddInstanceMethodAction(panel));
6670

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

87+
88+
7789
//file
7890
addAction(new FileSaveAs(panel));
7991
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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
import typesystem.JavaType;
2323
import ui.PaintPanel;
2424

25+
/*
26+
* This is the interactive constructor paint component, must call evaluate
27+
* to update the instance this component manipulates before use the output.
28+
*/
2529
public class ClassConstructorPaintComponent extends DataTextIOPaintComponent
2630
implements DataFromPointDataProvider {
2731

2832
private Constructor displayingConstructor;
33+
//Store a reference to the object this component operates.
2934
private Object instance;
3035

3136

@@ -63,7 +68,7 @@ private void init() {
6368
/**
6469
* Calculate the input data and store it.
6570
*/
66-
public void evaluate(DataFromPoint dataFromPoint){
71+
public void evaluate(){
6772
// prepare argument list
6873
ArrayList<DataToPoint> toPoints = getToPoints();
6974
Object[] args = new Object[toPoints.size()];
@@ -116,4 +121,12 @@ public Class getSelectedClass() {
116121
return displayingConstructor.getDeclaringClass();
117122
}
118123

124+
public Class getDisplayingClass() {
125+
return displayingConstructor.getDeclaringClass();
126+
}
127+
128+
public Object getInstance() {
129+
return instance;
130+
}
131+
119132
}

src/paintcomponents/java/interactive/MethodPaintComponent.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222
import typesystem.JavaType;
2323
import ui.PaintPanel;
2424

25+
/*
26+
* This is the interactive method paint component, must call the evaluate
27+
* method to calculate the data inside before trying to get data from this
28+
* component.
29+
*/
2530
public class MethodPaintComponent extends DataTextIOPaintComponent
2631
implements DataFromPointDataProvider {
2732

2833
private Method displayingMethod;
34+
//The data stored
2935
private Object returnVal;
36+
//Store an instance of the object this method manipulates
3037
private Object instance;
3138

3239
public MethodPaintComponent(Method displayingMethod, Object instance, int x, int y) {
@@ -49,8 +56,6 @@ public MethodPaintComponent(Method displayingMethod, Object instance, int x, int
4956

5057
private void init() {
5158

52-
// line 0 is signature
53-
addToPoint(1, new JavaType(this.displayingMethod.getDeclaringClass()));
5459
// parameters take place from line 2 to length+1
5560
Class[] paramTypes = displayingMethod.getParameterTypes();
5661
for (int i = 0; i < paramTypes.length ; i++) {
@@ -77,7 +82,7 @@ private void init() {
7782
/**
7883
* Calculate the input data and store it.
7984
*/
80-
public void evaluate(DataFromPoint dataFromPoint){
85+
public void evaluate(){
8186

8287
// prepare argument list
8388
ArrayList<DataToPoint> toPoints = getToPoints();
@@ -86,10 +91,11 @@ public void evaluate(DataFromPoint dataFromPoint){
8691

8792

8893
// args takes toPoint 1 to size
89-
Object[] args = new Object[toPoints.size() - 1];
94+
Object[] args = new Object[toPoints.size()];
9095

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

0 commit comments

Comments
 (0)