1
23package org.jzuul.gdk.swt;
24
25import java.util.Iterator;
26
27import org.eclipse.swt.SWT;
28import org.eclipse.swt.events.SelectionEvent;
29import org.eclipse.swt.events.SelectionListener;
30import org.eclipse.swt.events.ShellAdapter;
31import org.eclipse.swt.events.ShellEvent;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Button;
35import org.eclipse.swt.widgets.Dialog;
36import org.eclipse.swt.widgets.Display;
37import org.eclipse.swt.widgets.Event;
38import org.eclipse.swt.widgets.Group;
39import org.eclipse.swt.widgets.List;
40import org.eclipse.swt.widgets.Shell;
41import org.jdom.Element;
42import org.jzuul.engine.gui.utils.Util;
43
44
45
46
47
54public class CombinationEditorDialog extends Dialog {
55 Element itemElement;
56 List itemList;
57 ActionEditorComposite actionGroup;
58 String currentWithObject;
59
60
63 public CombinationEditorDialog(Shell arg0) {
64 super(arg0);
65 }
66
67
71 public CombinationEditorDialog(Shell arg0, int arg1) {
72 super(arg0, arg1);
73 }
74
75 public void open(Element currentItem, String[] itemNames) {
76 itemElement = currentItem;
77
78 Shell parent = getParent();
79 final Shell shell = new Shell(parent, SWT.BORDER | SWT.MIN | SWT.RESIZE | SWT.APPLICATION_MODAL);
80 shell.setText(Messages.getString("EDIT_COMBINATIONS")); shell.setLayout(new GridLayout(10,true));
82 shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png")); shell.addShellListener(new ShellAdapter() {
84 public void shellClosed(ShellEvent e) {
85 cleanUpChanges();
86 }
87 });
88
89 { itemList = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE | SWT.CHECK);
91 final GridData gridData =
92 new GridData(GridData.HORIZONTAL_ALIGN_FILL |
93 GridData.VERTICAL_ALIGN_FILL |
94 GridData.GRAB_VERTICAL );
95 gridData.horizontalSpan = 3;
96 itemList.setLayoutData(gridData);
97 for (int i = 0; i < itemNames.length; i++) {
98 if (!itemNames[i].equals(itemElement.getAttributeValue("name"))) { itemList.add(itemNames[i]);
00 }
01 }
02
03 itemList.addSelectionListener(new SelectionListener() {
04
05 public void widgetSelected(SelectionEvent arg0) {
06 cleanUpChanges();
07 currentWithObject = itemList.getItem(itemList.getSelectionIndex());
08 showCombinationsWithObject(currentWithObject);
10 }
11
12 public void widgetDefaultSelected(SelectionEvent arg0) {
13 }
14 });
15
16 }
17 { actionGroup = new ActionEditorComposite(shell, SWT.NONE , itemElement);
19 }
20 { final Group bottomGroup = new Group(shell, SWT.NONE);
22 GridData groupLD = new GridData(GridData.FILL_HORIZONTAL);
23 groupLD.heightHint = 40;
24 groupLD.horizontalSpan = 10;
25 bottomGroup.setLayoutData(groupLD);
26
27 bottomGroup.setLayout(new GridLayout(10,true));
28
29 GridData buttonData = new GridData(GridData.VERTICAL_ALIGN_END);
30 buttonData.heightHint = 30;
31 buttonData.widthHint = 80;
32
33 Button ok = new Button(bottomGroup, SWT.PUSH);
34 ok.setText(Messages.getString("OK")); ok.addSelectionListener(new SelectionListener() {
36
37 public void widgetSelected(SelectionEvent arg0) {
38 cleanUpChanges();
39 shell.dispose();
40 }
41
42 public void widgetDefaultSelected(SelectionEvent arg0) {}
43 });
44 ok.setLayoutData(buttonData);
45
46 }
47
48 shell.setSize(620,480);
49 if (itemList.getItemCount() > 0) {
50 itemList.select(0);
51 itemList.notifyListeners(SWT.Selection, new Event());
52 }
53
54 shell.open();
55 Display display = parent.getDisplay();
56 while (!shell.isDisposed()) {
57 if (!display.readAndDispatch()) display.sleep();
58 }
59
60 }
61
62 protected void cleanUpChanges() {
63 replaceActionList(currentWithObject,actionGroup.getElementList());
64 }
65
66 protected void showCombinationsWithObject(String withObjectName) {
67 actionGroup.showActionElements(getActionsForWithItem(withObjectName));
68 }
69
70 protected Element getWithObject(String withObjectName) {
71 if (withObjectName == null) return null;
72 Element combinations = itemElement.getChild("combinations");
74 if (combinations != null) {
75 java.util.List withObjects = combinations.getChildren("with-object"); for (Iterator withObjIter = withObjects.iterator(); withObjIter.hasNext();) {
77 Element element = (Element) withObjIter.next();
78 if (element.getAttributeValue("name").equals(withObjectName)) { return element;
80 }
81 }
82 Element newEl = new Element("with-object"); newEl.setAttribute("name", withObjectName); combinations.addContent(newEl);
85 return newEl;
86 } else {
87 itemElement.addContent(new Element("combinations")); return getWithObject(withObjectName);
89 }
90
91 }
92
93 protected java.util.List getActionsForWithItem(String withObjectName) {
94 Element withObject = getWithObject(withObjectName);
95 Element actions = withObject.getChild("actions"); if (actions == null) return null;
97 return actions.getChildren();
98 }
99
00 protected void replaceActionList(String currentWithObject, java.util.List actionList) {
01 Element withObj = getWithObject(currentWithObject);
02 if (withObj == null) return;
03 withObj.removeChild("actions"); Element newActions = new Element("actions"); newActions.addContent(actionList);
06 withObj.addContent(newActions);
07 }
08
09}
10