1
23package org.jzuul.gdk.swt;
24
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.Iterator;
28import java.util.Stack;
29
30import org.eclipse.swt.SWT;
31import org.eclipse.swt.events.SelectionEvent;
32import org.eclipse.swt.events.SelectionListener;
33import org.eclipse.swt.layout.GridData;
34import org.eclipse.swt.layout.GridLayout;
35import org.eclipse.swt.widgets.Button;
36import org.eclipse.swt.widgets.Combo;
37import org.eclipse.swt.widgets.Composite;
38import org.eclipse.swt.widgets.Control;
39import org.eclipse.swt.widgets.Event;
40import org.eclipse.swt.widgets.Group;
41import org.eclipse.swt.widgets.Shell;
42import org.eclipse.swt.widgets.Text;
43import org.eclipse.swt.widgets.Widget;
44import org.jdom.Element;
45
46
53public class ActionEditorComposite extends Composite {
54
55 protected class ActionGroup {
56
57 protected String eventName;
58
59 protected Combo action;
60
61 protected Widget value;
62
63 protected Widget value2;
64
65 protected Group actionGroup;
66
67 protected Button delete;
68
69 protected Element actionElement;
70
71 public ActionGroup(Combo actionCombo, Widget valueText, Group actionGroupGroup) {
72 this.action = actionCombo;
73 this.value = valueText;
74 this.actionGroup = actionGroupGroup;
75
76 actionCombo.addSelectionListener(new SelectionListener() {
77
78 public void widgetSelected(SelectionEvent e) {
79 String t = action.getText();
80 if (value2 != null) {
81 value2.dispose();
82 value2 = null;
83 }
84 if (t.equals("player-say") || t.equals("npc-say")) { makeTextInput();
86 }
87 if (t.equals("delete-item") || t.equals("room-item") || (t.equals("inventory-item"))) { makeItemInput();
89 }
90 if (t.equals("action")) { makeActionInput();
92 }
93 if (t.equals("target")) { makeTargetInput();
95 }
96 if (t.equals("random-success")) { makeNoInput();
98 }
99 if (t.equals("alter-item")) { makeAlterItemInput();
01 }
02 actionGroup.layout();
03 actionGroup.redraw();
04 }
05
06 public void widgetDefaultSelected(SelectionEvent e) {
07 }
08 });
09 }
10
11 public Element toXMLElement() {
12 if (actionElement != null) {
13 java.util.List targetChilds = actionElement.getChildren();
14 if ( (targetChilds != null) && (targetChilds.size() > 0)) {
15 return actionElement;
16 } else {
17 return null;
18 }
19 }
20
21 Element newEl = new Element(action.getText());
22
23 if (value instanceof Combo) {
24 String cv = ((Combo) value).getText();
25 if (!cv.equals("")) { if (cv.equals("action")) { newEl.setAttribute("type", cv); } else if (value2 != null) {
29 newEl.setAttribute("property", cv); newEl.setText(((Text) value2).getText());
31 } else {
32 newEl.setText(cv);
33 }
34 } else {
35 return null;
36 }
37 }
38 if (value instanceof Text) {
39 String text = ((Text) value).getText();
40 if (text != null && (!text.equals(""))) { newEl.setText(text);
42 } else {
43 return null;
44 }
45 }
46 return newEl;
47 }
48
49 public void delete() {
50 Composite parent = actionGroup.getParent();
51 actionGroup.dispose();
52 parent.layout();
53 parent.redraw();
54 }
55
56 public void fill(Element el) {
57 String tag = el.getName();
58 action.select(action.indexOf(tag));
59 action.notifyListeners(SWT.Selection, new Event());
60
61 if (value2 != null) {
62 ((Combo) value).select(((Combo) value).indexOf(el.getAttributeValue("property"))); ((Text) value2).setText(el.getText());
64 return;
65 }
66
67 if (value instanceof Combo) {
68 String val = el.getText();
69 if (val != null) {
70 ((Combo) value).select(((Combo) value).indexOf(val));
71 } else {
72 ((Combo) value).select(((Combo) value).indexOf(el.getAttributeValue("type"))); }
74 return;
75 }
76 if (value instanceof Text) {
77 Text t = (Text) value;
78 t.setText(el.getText());
79 return;
80 }
81 if (value instanceof Button) {
82 actionElement = el;
83 return;
84 }
85
86 }
87
88 public void cleanWidgets() {
89 if (this.value != null) {
90 value.dispose();
91 }
92 delete.dispose();
93 value = null;
94 delete = null;
95 actionElement = null;
96 }
97
98 public void createDeleteButton() {
99 Button db = new Button(this.actionGroup, SWT.PUSH);
00 db.setText(Messages.getString("DELETE")); db.addSelectionListener(new SelectionListener() {
02
03 public void widgetSelected(SelectionEvent e) {
04 groups.remove(ActionGroup.this);
05 delete();
06 }
07
08 public void widgetDefaultSelected(SelectionEvent e) {
09 }
10 });
11 this.delete = db;
12 }
13
14 public void makeItemInput() {
15 cleanWidgets();
16 Combo itemCombo = new Combo(actionGroup, SWT.READ_ONLY);
17 itemCombo.setItems(JdomHelpers.getItemNames(element));
18 GDKMainWindow.saveSelectFirst(itemCombo);
19 value = itemCombo;
20 valueLayoutData();
21 createDeleteButton();
22 }
23
24 public void makeTextInput() {
25 cleanWidgets();
26 Text textBox = new Text(actionGroup, SWT.BORDER);
27 value = textBox;
28 valueLayoutData();
29 createDeleteButton();
30 }
31
32 public void makeActionInput() {
33 cleanWidgets();
34 Combo actionCombo = new Combo(actionGroup, SWT.READ_ONLY);
35 actionCombo.add("moveRandom"); actionCombo.add("leaveRoom"); actionCombo.add("deleteItem"); GDKMainWindow.saveSelectFirst(actionCombo);
39 value = actionCombo;
40 valueLayoutData();
41 createDeleteButton();
42 }
43
44 public void makeTargetInput() {
45 cleanWidgets();
46 Button bt = new Button(actionGroup, SWT.PUSH);
47 bt.setText(Messages.getString("EDIT_TARGETS")); value = bt;
49 bt.addSelectionListener(new SelectionListener() {
50
51 public void widgetSelected(SelectionEvent e) {
52 if (actionElement == null) {
53 actionElement = new Element("target"); actionElement.addContent(new Element("description")); } else {
56 actionElement.detach();
57 }
58 TargetEditor ed = new TargetEditor(new Shell(e.display), SWT.NONE);
59 Element foo = new Element("actions"); foo.addContent(actionElement);
61 ed.open(foo);
62 actionElement = foo.getChild("target"); }
64
65 public void widgetDefaultSelected(SelectionEvent e) {
66 }
67 });
68
69 valueLayoutData();
70 createDeleteButton();
71 }
72
73 public void makeNoInput() {
74 cleanWidgets();
75
76 createDeleteButton();
77 }
78
79 public void makeAlterItemInput() {
80 cleanWidgets();
81 Combo itemCombo = new Combo(actionGroup, SWT.READ_ONLY);
82 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
83 itemCombo.setLayoutData(gd);
84 itemCombo.add("description"); GDKMainWindow.saveSelectFirst(itemCombo);
86 value = itemCombo;
87
88 Text newDesc = new Text(actionGroup, SWT.BORDER);
89 GridData pcgd = new GridData(GridData.FILL_HORIZONTAL);
90 newDesc.setLayoutData(pcgd);
91 value2 = newDesc;
92
93 createDeleteButton();
94 }
95
96 public void valueLayoutData() {
97 if (value != null && value instanceof Control) {
98 GridData valDat = new GridData(GridData.FILL_HORIZONTAL);
99 valDat.horizontalSpan = 2;
00 ((Control) value).setLayoutData(valDat);
01
02 }
03 }
04
05
06
07 }
08
09 protected final String[] actions = { "target", "action", "player-say", "npc-say", "inventory-item", "room-item", "alter-item", "delete-item", "random-success" };
12 protected Stack groups;
13
14 private Button more;
15
16 private Button less;
17
18 private Element element;
19
20
24 public ActionEditorComposite(Composite arg0, int arg1, Element elementWithActions) {
25 super(arg0, arg1);
26 Arrays.sort(actions);
27 groups = new Stack();
28 this.element = elementWithActions;
29
30 GridData layoutData = new GridData(GridData.FILL_BOTH);
31 layoutData.horizontalSpan = 7;
32 this.setLayoutData(layoutData);
33
34 this.setLayout(new GridLayout(2, false));
35
36 more = new Button(this, SWT.PUSH);
37 more.setText(Messages.getString("MORE"));
39 GridData buttondat = new GridData();
40 buttondat.heightHint = 30;
41 buttondat.widthHint = 80;
42 more.setLayoutData(buttondat);
43
44 more.addSelectionListener(new SelectionListener() {
45
46 public void widgetSelected(SelectionEvent arg0) {
47 generateActionGroup();
48 layout();
49 redraw();
50 }
51
52 public void widgetDefaultSelected(SelectionEvent arg0) {
53 }
54 });
55
56 less = new Button(this, SWT.PUSH);
57 less.setText(Messages.getString("LESS")); GridData buttondat2 = new GridData();
59 buttondat2.heightHint = 30;
60 buttondat2.widthHint = 80;
61 less.setLayoutData(buttondat2);
62 less.addSelectionListener(new SelectionListener() {
63
64 public void widgetSelected(SelectionEvent arg0) {
65 if (groups.isEmpty()) return;
66 ActionGroup g = ((ActionGroup) groups.pop());
67 g.delete();
68 }
69
70 public void widgetDefaultSelected(SelectionEvent arg0) {
71 }
72 });
73
74 }
75
76 protected void generateActionGroup() {
77
81 final Group actionGroup = new Group(this, SWT.NONE);
82
83 GridData groupData = new GridData(GridData.FILL_HORIZONTAL);
84 groupData.horizontalSpan = 2;
85 actionGroup.setLayoutData(groupData);
86
87 actionGroup.setLayout(new GridLayout(4, false));
88
89 Combo combo = new Combo(actionGroup, SWT.READ_ONLY);
90 GridData comboDat = new GridData();
91 comboDat.horizontalSpan = 1;
92 combo.setLayoutData(comboDat);
93 combo.setItems(actions);
94
95 Text text = new Text(actionGroup, SWT.BORDER);
96 GridData textDat = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
97 textDat.horizontalSpan = 2;
98 text.setLayoutData(textDat);
99
00 ActionGroup newGroup = new ActionGroup(combo, text, actionGroup);
01 newGroup.createDeleteButton();
02 combo.select(0);
03 combo.notifyListeners(SWT.Selection, new Event());
04 groups.push(newGroup);
05
06 }
07
08 public void showActionElements(java.util.List singleActions) {
09 if (singleActions == null) return;
10 for (Iterator actionIter = singleActions.iterator(); actionIter.hasNext();) {
11 Element action = (Element) actionIter.next();
12 generateActionGroup();
13 ActionGroup last = (ActionGroup) groups.peek();
14 last.fill(action);
15 }
16 this.layout();
17 this.redraw();
18
19 }
20
21 public java.util.List getElementList() {
22 java.util.List newList = new ArrayList();
23 for (Iterator actionGroupIter = groups.iterator(); actionGroupIter.hasNext();) {
24 ActionGroup ag = (ActionGroup) actionGroupIter.next();
25 Element newEl = ag.toXMLElement();
26 if (newEl != null) {
27 newEl.detach();
28 newList.add(newEl);
29 }
30 ag.delete();
31 actionGroupIter.remove();
32 }
33 return newList;
34
35 }
36
37 public void setEnabled(boolean enable) {
38 more.setEnabled(enable);
39 less.setEnabled(enable);
40 }
41
42}