Skip to content

Commit 265c190

Browse files
author
Xiangyi Gong
committed
add interactive instance ctor and method v4
1 parent 4a0e2bc commit 265c190

File tree

6 files changed

+147
-21
lines changed

6 files changed

+147
-21
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package actions;
2+
3+
import actions.menu.ActionsMenuBarTitles;
4+
import paintcomponents.java.interactive.InstanceOperationComponent;
5+
import ui.PaintPanel;
6+
7+
public class ExecuteInstanceConstructorAction extends PaintAction {
8+
9+
public ExecuteInstanceConstructorAction(PaintPanel panel) {
10+
super(panel);
11+
}
12+
13+
@Override
14+
public boolean canPerformAction() {
15+
if (panel.getSelectTool().getSelectedComponents().size() != 1) {
16+
return false;
17+
}
18+
if (panel.getSelectTool().getSelectedComponents()
19+
.get(0) instanceof InstanceOperationComponent) {
20+
return true;
21+
}
22+
return false;
23+
}
24+
25+
@Override
26+
public void performAction() {
27+
InstanceOperationComponent insComp =
28+
(InstanceOperationComponent)panel.getSelectTool().
29+
getSelectedComponents().get(0);
30+
insComp.executeConstructor();
31+
}
32+
33+
@Override
34+
public String locationString() {
35+
// TODO Auto-generated method stub
36+
return ActionsMenuBarTitles.Lazy().Add().Execute_Instance_Constructor().toString();
37+
}
38+
39+
40+
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package actions;
2+
3+
import actions.menu.ActionsMenuBarTitles;
4+
import paintcomponents.java.interactive.MethodPaintComponent;
5+
import ui.PaintPanel;
6+
7+
public class ExecuteInstanceMethodAction extends PaintAction {
8+
9+
public ExecuteInstanceMethodAction(PaintPanel panel) {
10+
super(panel);
11+
}
12+
13+
@Override
14+
public boolean canPerformAction() {
15+
if (panel.getSelectTool().getSelectedComponents().size() != 1) {
16+
return false;
17+
}
18+
if (panel.getSelectTool().getSelectedComponents()
19+
.get(0) instanceof MethodPaintComponent) {
20+
return true;
21+
}
22+
return false;
23+
}
24+
25+
@Override
26+
public void performAction() {
27+
MethodPaintComponent methodComp =
28+
(MethodPaintComponent)panel.getSelectTool().
29+
getSelectedComponents().get(0);
30+
methodComp.executeMethod();
31+
}
32+
33+
@Override
34+
public String locationString() {
35+
// TODO Auto-generated method stub
36+
return ActionsMenuBarTitles.Lazy().Add().Execute_Instance_Method().toString();
37+
}
38+
39+
40+
41+
}

src/actions/menu/ActionsMenuBar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import actions.AddInstanceOperationAction;
4444
import actions.AddInteractiveConstructorAction;
4545
import actions.AddInteractiveJavaMethodComponentAction;
46+
import actions.ExecuteInstanceConstructorAction;
47+
import actions.ExecuteInstanceMethodAction;
4648

4749
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
4850

@@ -67,6 +69,8 @@ public ActionsMenuBar(PaintPanel panel){
6769
addAction(new AddLazyJavaFieldsComponentAction(panel));
6870
addAction(new AddInstanceOperationAction(panel));
6971
addAction(new AddInstanceMethodAction(panel));
72+
addAction(new ExecuteInstanceConstructorAction(panel));
73+
addAction(new ExecuteInstanceMethodAction(panel));
7074

7175
//interactive
7276
addAction(new AddInteractiveConstructorAction(panel));

src/actions/menu/ActionsMenuBarTitles.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ public ActionsMenuBarTitles Add_Instance_Method() {
151151
return this;
152152
}
153153

154-
154+
public ActionsMenuBarTitles Execute_Instance_Constructor() {
155+
append("Execute Constructor");
156+
return this;
157+
}
158+
159+
public ActionsMenuBarTitles Execute_Instance_Method() {
160+
append("Execute Method");
161+
return this;
162+
}
155163

156164
public ActionsMenuBarTitles Annotations() {
157165
append("Annotations");

src/paintcomponents/java/interactive/InstanceOperationComponent.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
import java.lang.reflect.Constructor;
44
import java.lang.reflect.Method;
55
import java.util.ArrayList;
6+
import java.util.NoSuchElementException;
7+
8+
import javax.swing.JOptionPane;
69

710
import org.w3c.dom.Element;
811

12+
import paintcomponents.NoConnectingLineSegmentException;
913
import paintcomponents.data.DataFromPoint;
1014
import paintcomponents.data.DataFromPointDataProvider;
15+
import paintcomponents.data.DataFromPointNoDataProviderException;
16+
import paintcomponents.data.DataFromPointProviderCannotProvideDataException;
1117
import paintcomponents.data.DataTextIOPaintComponent;
18+
import paintcomponents.data.DataToPoint;
1219
import painttools.tools.SelectTool;
1320
import typesystem.JavaType;
1421
import ui.PaintPanel;
@@ -19,7 +26,6 @@ public class InstanceOperationComponent extends DataTextIOPaintComponent
1926
private int height;
2027
private int unitHeight;
2128

22-
//private ClassConstructorPaintComponent ctorPC;
2329
private ArrayList<MethodPaintComponent> methods;
2430

2531
private Constructor displayingConstructor;
@@ -92,22 +98,13 @@ public Class getDisplayingClass() {
9298
public void translate(int i, int j) {
9399
// TODO Auto-generated method stub
94100
super.translate(i, j);
95-
//ctorPC.translate(i, j);
96101
methods.forEach(e -> e.translate(i, j));
97102
}
98103

99104
@Override
100105
public boolean contains(int x, int y) {
101106
// TODO Auto-generated method stub
102-
// if (ctorPC.contains(x, y)) {
103-
// return true;
104-
// } else {
105-
// for (MethodPaintComponent method : methods) {
106-
// if (method.contains(x, y)) {
107-
// return true;
108-
// }
109-
// }
110-
// }
107+
111108
for (MethodPaintComponent method : methods) {
112109
if (method.contains(x, y)) {
113110
return true;
@@ -121,10 +118,7 @@ public boolean contains(int x, int y) {
121118
public void select(SelectTool selectTool) {
122119
int x = selectTool.getLastMouseEvent().getX();
123120
int y = selectTool.getLastMouseEvent().getY();
124-
// if (ctorPC.contains(x, y)) {
125-
// ctorPC.select(selectTool);
126-
// return;
127-
// }
121+
128122
for (MethodPaintComponent method : methods) {
129123
if (method.contains(x, y)) {
130124
method.select(selectTool);
@@ -139,16 +133,31 @@ public void select(SelectTool selectTool) {
139133
public void deselect(SelectTool selectTool) {
140134
int x = selectTool.getLastMouseEvent().getX();
141135
int y = selectTool.getLastMouseEvent().getY();
142-
// if (ctorPC.contains(x, y)) {
143-
// ctorPC.deselect(selectTool);
144-
// return;
145-
// }
136+
146137
for (MethodPaintComponent method : methods) {
147138
if (method.contains(x, y)) {
148139
method.select(selectTool);
149140
}
150141
}
151142
super.deselect(selectTool);
152143
}
144+
145+
public void executeConstructor() {
146+
147+
try {
148+
Object[] initargs = new Object[this.getToPoints().size()];
149+
for (int i = 0; i < initargs.length; i ++) {
150+
DataFromPoint fromP =
151+
this.getToPoints().get(i).getLineSegment().getFromPoint();
152+
initargs[i] = fromP.getProvider().provideInformationToDataFromPoint(fromP);
153+
}
154+
instance = this.displayingConstructor.newInstance(initargs);
155+
methods.forEach(e -> e.setInstance(instance));
156+
System.out.println("new instance: " + instance);
157+
} catch (Exception e) {
158+
JOptionPane.showMessageDialog(null, "Not Valid Parameters");
159+
e.printStackTrace();
160+
}
161+
}
153162

154163
}

src/paintcomponents/java/interactive/MethodPaintComponent.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public MethodPaintComponent(Method displayingMethod, Object instance, int x, int
4141
this.displayingMethod = displayingMethod;
4242
this.instance = instance;
4343
init();
44-
System.out.println("interactive method");
4544
}
4645

4746
/*
@@ -150,4 +149,28 @@ public Object getInstance(){
150149
return instance;
151150
}
152151

152+
public void setInstance(Object instance) {
153+
this.instance = instance;
154+
}
155+
156+
public void executeMethod() {
157+
try {
158+
if (instance == null) {
159+
JOptionPane.showMessageDialog(null, "Please initiate an instance");
160+
return;
161+
}
162+
Object[] initargs = new Object[this.getToPoints().size()];
163+
for (int i = 0; i < initargs.length; i ++) {
164+
DataFromPoint fromP =
165+
this.getToPoints().get(i).getLineSegment().getFromPoint();
166+
initargs[i] = fromP.getProvider().provideInformationToDataFromPoint(fromP);
167+
}
168+
Object returnVal = this.displayingMethod.invoke(instance, initargs);
169+
System.out.println("method return val: " + returnVal);
170+
} catch (Exception e) {
171+
JOptionPane.showMessageDialog(null, "Error Occurs");
172+
e.printStackTrace();
173+
}
174+
}
175+
153176
}

0 commit comments

Comments
 (0)