1
23package org.jzuul.gdk.swt;
24
25import java.util.ArrayList;
26import java.util.Iterator;
27import java.util.List;
28
29import org.eclipse.swt.SWT;
30import org.eclipse.swt.events.ModifyEvent;
31import org.eclipse.swt.events.ModifyListener;
32import org.eclipse.swt.events.SelectionEvent;
33import org.eclipse.swt.events.SelectionListener;
34import org.eclipse.swt.layout.GridData;
35import org.eclipse.swt.layout.GridLayout;
36import org.eclipse.swt.widgets.Button;
37import org.eclipse.swt.widgets.Composite;
38import org.eclipse.swt.widgets.Label;
39import org.eclipse.swt.widgets.MessageBox;
40import org.eclipse.swt.widgets.Shell;
41import org.eclipse.swt.widgets.Text;
42import org.jdom.Element;
43
44
51
52public class PropertyComposite extends Composite {
53 protected Button useable, takeable, deleteonuse;
54
55 protected Text size;
56
57 protected List propertyElements;
58
59 protected final static String SIZE_TIP = Messages.getString("SIZE_TIP");
61 protected final static String USEABLE_TIP = Messages.getString("USEABLE_TIP");
63 protected final static String TAKEABLE_TIP = Messages.getString("TAKEABLE_TIP");
65 protected final static String DELETONUSE_TIP = Messages.getString("DELETEONUSE_TIP");
67 protected class CheckInteger implements ModifyListener {
68 public void modifyText(ModifyEvent arg0) {
69 if (size.getText().length() > 0) {
70 try {
71 int value = Integer.parseInt(size.getText());
72 System.err.println("value is " + value); } catch (NumberFormatException e) {
74 MessageBox mb = new MessageBox(new Shell(arg0.display),
75 SWT.ICON_ERROR | SWT.OK);
76 mb.setText(Messages.getString("GAMEDITOR_ERROR")); mb
78 .setMessage(Messages.getString("SIZE_ERROR")); mb.open();
80 size.setText("0"); }
82 System.err.println("ModifyEvent: " + arg0.toString()); }
84 }
85
86 }
87
88
92 public PropertyComposite(Composite arg0, int arg1) {
93 super(arg0, arg1);
94
95 GridData propGridData = new GridData(GridData.FILL_HORIZONTAL
97 | GridData.FILL_VERTICAL);
98 this.setLayoutData(propGridData);
99 GridLayout thisLayout = new GridLayout(3, false);
00 this.setLayout(thisLayout);
01
02 Label sizel = new Label((Composite) this, SWT.NONE);
03 sizel.setText(Messages.getString("SIZE")); sizel.setToolTipText(PropertyComposite.SIZE_TIP);
05 size = new Text((Composite) this, SWT.BORDER);
06 GridData sizeLayoutData = new GridData(GridData.FILL_HORIZONTAL);
07 sizeLayoutData.horizontalSpan = 2;
08 size.setLayoutData(sizeLayoutData);
09 size.setText("0"); size.addModifyListener(new CheckInteger());
11 size.setToolTipText(PropertyComposite.SIZE_TIP);
12
13 useable = new Button((Composite) this, SWT.CHECK);
14 useable.setToolTipText(PropertyComposite.USEABLE_TIP);
15 useable.setText(Messages.getString("USEABLE")); useable.addSelectionListener(new SelectionListener() {
17
18 public void widgetSelected(SelectionEvent e) {
19 if (useable.getSelection()) {
20 deleteonuse.setEnabled(true);
21 } else {
22 deleteonuse.setEnabled(false);
23 }
24
25 }
26
27 public void widgetDefaultSelected(SelectionEvent e) {
28
30 }
31
32 });
33
34 deleteonuse = new Button((Composite) this, SWT.CHECK);
35 deleteonuse.setToolTipText(PropertyComposite.DELETONUSE_TIP);
36 deleteonuse.setText(Messages.getString("DELETE_ON_USE")); deleteonuse.setEnabled(false);
38
39 takeable = new Button((Composite) this, SWT.CHECK);
40 takeable.setToolTipText(PropertyComposite.TAKEABLE_TIP);
41 takeable.setText(Messages.getString("TAKEABLE"));
43 }
44
45 public void showProperties(List propertyElements) {
46 this.propertyElements = propertyElements;
47 if (propertyElements == null) {
48 this.clear();
49 return;
50 }
51 setEnabled(true);
52 String sizeText = getPropertyValue("size"); if (sizeText == null || sizeText == "") { sizeText = "0"; } size.setText(sizeText);
55 useable.setSelection(getBoolProperty("useable")); takeable.setSelection(getBoolProperty("takeable")); if (!getBoolProperty("useable")) { deleteonuse.setEnabled(false);
59 } else {
60 deleteonuse.setEnabled(true);
61 deleteonuse.setSelection(getBoolProperty("deleteonuse")); }
63
64 }
65
66 public String getPropertyValue(String property) {
67 for (Iterator propIter = propertyElements.iterator(); propIter
68 .hasNext();) {
69 Element element = (Element) propIter.next();
70 if (element.getAttributeValue("name").equals(property)) return element.getAttributeValue("value"); }
73 return ""; }
75
76 public boolean getBoolProperty(String property) {
77 String val = getPropertyValue(property);
78 if (val != null) {
79 return val.equals("true"); } else {
81 return false;
82 }
83 }
84
85 public List getElementList() {
86 List retlist = new ArrayList();
87 retlist.add(getButtonProperty("useable", useable)); retlist.add(getButtonProperty("takeable", takeable)); retlist.add(getButtonProperty("deleteonuse", deleteonuse)); Element sizeEl = new Element("property"); sizeEl.setAttribute("name", "size"); if (size.getText().length() > 0) {
93 sizeEl.setAttribute("value", size.getText()); } else {
95 sizeEl.setAttribute("value", "0"); }
97 retlist.add(sizeEl);
98 return retlist;
99 }
00
01 protected Element getButtonProperty(String name, Button button) {
02 Element retval = new Element("property"); retval.setAttribute("name", name); if (button.getSelection()) {
05 retval.setAttribute("value", "true"); } else {
07 retval.setAttribute("value", "false"); }
09 return retval;
10 }
11
12 public void clear() {
13 this.useable.setSelection(false);
14 this.takeable.setSelection(false);
15 this.deleteonuse.setSelection(false);
16 this.size.setText("0"); this.propertyElements = null;
18 setEnabled(false);
19 }
20
21 public void setEnabled(boolean enabled) {
22 this.useable.setEnabled(enabled);
23 this.takeable.setEnabled(enabled);
24 this.size.setEnabled(enabled);
25
26 }
27
28}