Skip to content

Commit c15f729

Browse files
author
Xiangyi Gong
committed
Add interative operation component version 1
1 parent 0019350 commit c15f729

6 files changed

Lines changed: 282 additions & 2 deletions

File tree

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

src/actions/menu/ActionsMenuBar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import actions.AddDataInputBoxAction;
1414
import actions.AddHaskellComponent;
1515
import actions.AddHaskellEvaluatorComponentAction;
16+
import actions.AddInstanceMethodAction;
1617
import actions.AddLazyJavaClassAction;
1718
import actions.AddLazyJavaConstructorAction;
1819
import actions.AddLazyJavaFieldsComponentAction;
@@ -35,6 +36,7 @@
3536
import actions.ZoomOutAction;
3637
import actions.ZoomInAction;
3738
import actions.ZoomOutAction;
39+
import actions.AddInstanceOperationAction;
3840

3941
public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
4042

@@ -57,6 +59,8 @@ public ActionsMenuBar(PaintPanel panel){
5759
addAction(new AddLazyJavaConstructorAction(panel));
5860
addAction(new AddLazyJavaMethodComponentAction(panel));
5961
addAction(new AddLazyJavaFieldsComponentAction(panel));
62+
addAction(new AddInstanceOperationAction(panel));
63+
addAction(new AddInstanceMethodAction(panel));
6064

6165
//edit
6266
addAction(new EditRedoAction(panel));

src/actions/menu/ActionsMenuBarTitles.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public ActionsMenuBarTitles Java_Constructor() {
8787
append("Java Constructor");
8888
return this;
8989
}
90-
90+
9191
public ActionsMenuBarTitles Java_Method() {
9292
append("Java Method");
9393
return this;
@@ -137,5 +137,14 @@ public ActionsMenuBarTitles Zoom_Out() {
137137
append("Zoom Out");
138138
return this;
139139
}
140-
140+
141+
public ActionsMenuBarTitles Instance_Operation() {
142+
append("Instance Operation Component");
143+
return this;
144+
}
145+
146+
public ActionsMenuBarTitles Add_Instance_Method() {
147+
append("Add Instance Method");
148+
return this;
149+
}
141150
}

src/paintcomponents/java/lazy/ClassConstructorPaintComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,9 @@ public ClassConstructorPaintComponent(Element rootElement,
197197
// }
198198

199199
}
200+
201+
public Class getSelectedClass() {
202+
return displayingConstructor.getDeclaringClass();
203+
}
200204

201205
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package paintcomponents.java.lazy;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Method;
5+
6+
import org.w3c.dom.Element;
7+
8+
import paintcomponents.data.DataFromPoint;
9+
import paintcomponents.data.DataFromPointDataProvider;
10+
import paintcomponents.data.DataTextIOPaintComponent;
11+
import paintcomponents.data.DataToPoint;
12+
import painttools.tools.SelectTool;
13+
import ui.PaintPanel;
14+
import java.util.ArrayList;
15+
16+
public class InstanceOperationComponent extends DataTextIOPaintComponent
17+
implements DataFromPointDataProvider {
18+
19+
private int height;
20+
private int unitHeight;
21+
22+
private ClassConstructorPaintComponent ctorPC;
23+
private ArrayList<MethodPaintComponent> methods;
24+
25+
26+
public InstanceOperationComponent(Constructor displayingContructor,
27+
int x, int y) {
28+
super(displayingContructor.toString(), x, y);
29+
ctorPC = new ClassConstructorPaintComponent(displayingContructor, x, y);
30+
methods = new ArrayList<>();
31+
height = 0;
32+
}
33+
34+
35+
@Override
36+
public Object provideInformationToDataFromPoint(DataFromPoint dataFromPoint) {
37+
return ctorPC.provideInformationToDataFromPoint(dataFromPoint);
38+
}
39+
40+
@Override
41+
public boolean canProvideInformationToDataFromPoint(DataFromPoint dataFromPoint) {
42+
return ctorPC.canProvideInformationToDataFromPoint(dataFromPoint);
43+
}
44+
45+
46+
public void addMethodPaintComponent(Method method, PaintPanel panel) {
47+
if (height == 0) {
48+
height = this.getRowHeight() * this.getNumberOfRows();
49+
unitHeight = this.getRowHeight();
50+
}
51+
52+
MethodPaintComponent methodComp = new MethodPaintComponent(
53+
method, this.getX(), this.getY() + height);
54+
String[] rows = methodComp.getDisplayingText().split("\n");
55+
height += unitHeight * rows.length;
56+
methods.add(methodComp);
57+
panel.addPaintComponent(methodComp);
58+
panel.repaint();
59+
}
60+
61+
62+
public Class getDisplayingClass() {
63+
return ctorPC.getSelectedClass();
64+
}
65+
66+
@Override
67+
public void translate(int i, int j) {
68+
// TODO Auto-generated method stub
69+
super.translate(i, j);
70+
ctorPC.translate(i, j);
71+
methods.forEach(e -> e.translate(i, j));
72+
}
73+
74+
@Override
75+
public boolean contains(int x, int y) {
76+
// TODO Auto-generated method stub
77+
if (ctorPC.contains(x, y)) {
78+
return true;
79+
} else {
80+
for (MethodPaintComponent method : methods) {
81+
if (method.contains(x, y)) {
82+
return true;
83+
}
84+
}
85+
}
86+
return super.contains(x, y);
87+
}
88+
89+
90+
@Override
91+
public void select(SelectTool selectTool) {
92+
int x = selectTool.getLastMouseEvent().getX();
93+
int y = selectTool.getLastMouseEvent().getY();
94+
for (MethodPaintComponent method : methods) {
95+
if (method.contains(x, y)) {
96+
method.select(selectTool);
97+
}
98+
}
99+
super.select(selectTool);
100+
}
101+
102+
103+
@Override
104+
public void deselect(SelectTool selectTool) {
105+
int x = selectTool.getLastMouseEvent().getX();
106+
int y = selectTool.getLastMouseEvent().getY();
107+
for (MethodPaintComponent method : methods) {
108+
if (method.contains(x, y)) {
109+
method.select(selectTool);
110+
}
111+
}
112+
super.deselect(selectTool);
113+
}
114+
115+
116+
117+
}

0 commit comments

Comments
 (0)