Skip to content

Commit 631972c

Browse files
committed
allow specifying names of components
1 parent f68cd56 commit 631972c

10 files changed

Lines changed: 393 additions & 214 deletions

src/script/ComponentMap.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package script;
2+
3+
import java.util.HashMap;
4+
5+
import paintcomponents.PaintComponent;
6+
7+
public class ComponentMap {
8+
public static HashMap<String, PaintComponent> map = new HashMap<String, PaintComponent>();
9+
}

src/script/Interpreter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import paintcomponents.data.DataDisplayPaintComponent;
77
import actions.edit.undoredo.SharedUndoRedoActionManager;
88
import actions.edit.undoredo.UndoRedoableInterface;
9+
import paintcomponents.*;
910

1011
/**
1112
* Interpret and execute scripts
1213
* @author Xiaoquan Jiang
1314
*/
1415
public class Interpreter {
1516

17+
private static final String SELECT = "select";
1618
private static final String ZOOM = "zoom";
1719
private static final String REMOVE = "remove";
1820
private static final String UPDATE = "update";
@@ -21,8 +23,8 @@ public class Interpreter {
2123
private static final String EDIT = "edit";
2224
private static final String CONSTRUCT = "construct";
2325
private static final String ADD = "add";
24-
private PaintPanel panel;
25-
26+
protected PaintPanel panel;
27+
2628
public Interpreter(PaintPanel panel) {
2729
this.panel = panel;
2830
}
@@ -62,6 +64,10 @@ public void interpreteLine(String script) throws ExecutionErrorException {
6264
new InterpreterRemoveAction(tokenizer, panel);
6365
break;
6466

67+
case SELECT:
68+
new InterpreterSelectAction(tokenizer, panel);
69+
break;
70+
6571
default:
6672
throw new ExecutionErrorException("invalid script");
6773
}

src/script/InterpreterAddActions.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package script;
22

33
import javax.swing.JOptionPane;
4-
54
import paintcomponents.TextPaintComponent;
65
import actions.edit.undoredo.SharedUndoRedoActionManager;
76
import actions.edit.undoredo.UndoRedoableInterface;
87
import ui.PaintPanel;
8+
import paintcomponents.*;
99

1010
/**
1111
* Interpret and execute 'add' scripts
@@ -17,10 +17,13 @@ public class InterpreterAddActions {
1717
private static final String LAZY = "lazy";
1818
private static final String HASKELL = "haskell";
1919
private static final String DATA = "data";
20+
private PaintPanel panel;
21+
private PaintComponent component;
2022

2123
public InterpreterAddActions(Tokenizer tokenizer, PaintPanel panel)
2224
throws ExecutionErrorException {
23-
25+
this.panel = panel;
26+
2427
if (tokenizer.hasNext()) {
2528
switch (tokenizer.next()) {
2629
case DATA:
@@ -36,7 +39,7 @@ public InterpreterAddActions(Tokenizer tokenizer, PaintPanel panel)
3639
break;
3740

3841
case TEXT_BOX:
39-
performAddTextBox(panel);
42+
component = performAddTextBox();
4043
break;
4144

4245
default:
@@ -45,9 +48,14 @@ public InterpreterAddActions(Tokenizer tokenizer, PaintPanel panel)
4548
} else {
4649
throw new ExecutionErrorException("incomplete script");
4750
}
51+
52+
// name and store the component added
53+
if (tokenizer.hasNext() && component != null) {
54+
ComponentMap.map.put(tokenizer.next(), component);
55+
}
4856
}
4957

50-
private void performAddTextBox(PaintPanel panel) {
58+
private PaintComponent performAddTextBox() {
5159
String s = JOptionPane.showInputDialog("Please enter the text to display");
5260
TextPaintComponent comp = new TextPaintComponent(s, panel.getWidth() / 2,
5361
panel.getHeight() / 2);
@@ -69,5 +77,6 @@ public void redoAction() {
6977
}
7078
});
7179
panel.repaint();
80+
return comp;
7281
}
7382
}

src/script/InterpreterAddData.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package script;
22

3+
import paintcomponents.PaintComponent;
34
import paintcomponents.data.DataDisplayPaintComponent;
45
import paintcomponents.data.DataInputTextfieldPaintComponent;
56
import ui.PaintPanel;
7+
68
import actions.edit.undoredo.SharedUndoRedoActionManager;
79
import actions.edit.undoredo.UndoRedoableInterface;
810

@@ -14,20 +16,21 @@ public class InterpreterAddData {
1416
private static final String INPUT_BOX = "inputBox";
1517
private static final String DISPLAY_BOX = "displayBox";
1618
private PaintPanel panel;
19+
private PaintComponent component;
1720

1821
public InterpreterAddData(Tokenizer tokenizer, PaintPanel panel)
1922
throws ExecutionErrorException {
2023

2124
this.panel = panel;
22-
25+
2326
if (tokenizer.hasNext()) {
2427
switch (tokenizer.next()) {
2528
case DISPLAY_BOX:
26-
performAddDisplayBoxAction();
29+
component = performAddDisplayBoxAction();
2730
break;
2831

2932
case INPUT_BOX:
30-
performAddInputBoxAction();
33+
component = performAddInputBoxAction();
3134
break;
3235

3336
default:
@@ -36,9 +39,14 @@ public InterpreterAddData(Tokenizer tokenizer, PaintPanel panel)
3639
} else {
3740
throw new ExecutionErrorException("incomplete script");
3841
}
42+
43+
// name and store the component added
44+
if (tokenizer.hasNext() && component != null) {
45+
ComponentMap.map.put(tokenizer.next(), component);
46+
}
3947
}
4048

41-
private void performAddDisplayBoxAction() {
49+
private PaintComponent performAddDisplayBoxAction() {
4250
DataDisplayPaintComponent comp = new DataDisplayPaintComponent(
4351
"Data Display", panel.getWidth() / 2, panel.getHeight() / 2);
4452
panel.addPaintComponent(comp);
@@ -60,9 +68,10 @@ public void redoAction() {
6068
}
6169
});
6270
panel.repaint();
71+
return comp;
6372
}
6473

65-
private void performAddInputBoxAction() {
74+
private PaintComponent performAddInputBoxAction() {
6675
DataInputTextfieldPaintComponent comp = new DataInputTextfieldPaintComponent(
6776
"Data Input", panel.getWidth() / 2, panel.getHeight() / 2);
6877
panel.addPaintComponent(comp);
@@ -85,6 +94,6 @@ public void redoAction() {
8594
}
8695
});
8796
panel.repaint();
97+
return comp;
8898
}
89-
9099
}

0 commit comments

Comments
 (0)