1
23package org.jzuul.gdk.swt;
24
25import java.text.MessageFormat;
26import java.util.ArrayList;
27import java.util.Iterator;
28
29import org.eclipse.swt.SWT;
30import org.eclipse.swt.events.SelectionAdapter;
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.Composite;
36import org.eclipse.swt.widgets.Event;
37import org.eclipse.swt.widgets.Group;
38import org.eclipse.swt.widgets.List;
39import org.eclipse.swt.widgets.Menu;
40import org.eclipse.swt.widgets.MenuItem;
41import org.eclipse.swt.widgets.MessageBox;
42import org.eclipse.swt.widgets.Shell;
43import org.jdom.Element;
44
45
46
53public class CharacterEditorComposite extends Composite {
54 private List characterList;
55 private CharacterDetailsComposite characterComp;
56 private Element gameobj;
57
58
62 public CharacterEditorComposite(Composite arg0, int arg1) {
63 super(arg0, arg1);
64 final GridLayout gridLayout = new GridLayout();
65 gridLayout.makeColumnsEqualWidth = true;
66 gridLayout.numColumns = 5;
67 this.setLayout(gridLayout);
68
69
70 {
71 final Group group_1 =
72 new Group(this, SWT.NONE);
73 group_1.setText(Messages.getString("CHARACTERS"));
75 group_1.setLayout(new GridLayout());
76 final GridData gridData =
77 new GridData(
78 GridData.HORIZONTAL_ALIGN_FILL
79 | GridData.FILL_VERTICAL);
80 gridData.horizontalSpan = 1;
81 group_1.setLayoutData(gridData);
82
83 {
84 characterList =
85 new List(
86 group_1,
87 SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
88 characterList.setLayoutData(
89 new GridData(GridData.FILL_BOTH));
90
91
92 characterList.addSelectionListener(new SelectionListener() {
93
94 public void widgetSelected(SelectionEvent e) {
95 characterComp.updateElement();
96 int index = characterList.getSelectionIndex();
97 if (index >= 0) {
98 characterComp.showCharacterElement(getCharacter(characterList.getItem(index)));
99 } else {
00 characterComp.showCharacterElement(null);
01 }
02
03 }
04
05 public void widgetDefaultSelected(SelectionEvent e) {
06 }
08
09 });
10 characterList.setToolTipText(Messages.getString("CHARACTER_TOOLTIP"));
12 {
13 Menu popupmenu = new Menu(characterList);
14 characterList.setMenu(popupmenu);
15 {
16 final MenuItem popupItem =
17 new MenuItem(popupmenu, SWT.NONE);
18 popupItem
19 .addSelectionListener(
20 new SelectionAdapter() {
21
22 public void widgetSelected(SelectionEvent e) {
23 }
25 });
26 popupItem.setText(Messages.getString("ADD")); popupItem.addSelectionListener(new SelectionListener() {
28
29 public void widgetSelected(SelectionEvent arg0) {
30 InputDialog input = new InputDialog(new Shell(arg0.display), SWT.APPLICATION_MODAL);
31 input.setMessage(Messages.getString("CHARACTER_NAME_ENTER")); String retval = input.openNoWhitespace();
33 if (retval != null && (!retval.equals(""))) { if (characterList.indexOf(retval) == -1) {
35 addCharacter(retval);
36 characterComp.enable();
37 } else {
38 MessageBox mb = new MessageBox(new Shell(arg0.display), SWT.ICON_ERROR);
39 Object[] formatArgs = {retval };
40 mb.setMessage(MessageFormat.format(Messages.getString("CHARACTER_SAME_NAME_ERROR"),formatArgs)); mb.open();
42
43 }
44 }
45 }
46
47 public void widgetDefaultSelected(SelectionEvent arg0) {
48 }
49
50 });
51 }
52 {
53 final MenuItem popupItem =
54 new MenuItem(popupmenu, SWT.NONE);
55 popupItem.setText(Messages.getString("RENAME")); popupItem.addSelectionListener(new SelectionListener() {
57
58 public void widgetSelected(SelectionEvent arg0) {
59 try{
60 String oldname = characterList.getItem(characterList.getSelectionIndex());
61 InputDialog input = new InputDialog(new Shell(arg0.display),SWT.APPLICATION_MODAL);
62 input.setMessage(Messages.getString("CHARACTER_RENAME_ENTER") + oldname + ": "); input.setDefaultValue(oldname);
64 renameCharacter(oldname,input.openNoWhitespace());
65 }
66 catch (Exception e) {
67 System.err.println("Silent Catch: \nprevented nullpointer expection (renaming null-object)\n"+e.getLocalizedMessage()); }
69 }
70
71 public void widgetDefaultSelected(SelectionEvent arg0) {
72 }
73
74 });
75 }
76 {
77 final MenuItem popupItem =
78 new MenuItem(popupmenu, SWT.NONE);
79 popupItem.setText(Messages.getString("DELETE")); popupItem.addSelectionListener(new SelectionListener() {
81
82 public void widgetSelected(SelectionEvent arg0) {
83 try{
85 deleteCharacter(characterList.getItem(characterList.getSelectionIndex()));
86 }
87 catch (Exception e) {
88 System.err.println("Silent Catch: \nprevented nullpointer expection (deleting null-object)\n"+e.getLocalizedMessage()); }
90 }
91
92 public void widgetDefaultSelected(SelectionEvent arg0) {
93 }
94
95 });
96 }
97 }
98 }
99 }
00 {
01 characterComp = new CharacterDetailsComposite(this,SWT.NULL);
02
03 GridData gridData = new GridData(GridData.FILL_BOTH);
04 gridData.horizontalSpan = 4;
05 GridLayout layout = new GridLayout();
06 layout.numColumns = 2;
07 }
08
09 }
10
11 public Element getCharacter(String name) {
12 if (gameobj != null) {
13 Element items = gameobj.getChild("characters"); if (items != null) {
15 java.util.List itemList = items.getChildren("person"); for (Iterator goIter = itemList.iterator(); goIter.hasNext();) {
17 Element element = (Element) goIter.next();
18
19 if (element.getAttributeValue("name").equals(name)) { return element;
21 }
22 }
23 }
24 }
25 return null;
26 }
27
28 public void addCharacter(String name) {
29 if (name.equals("")) return; if (gameobj != null) {
31 Element items = gameobj.getChild("characters"); if (items != null) {
33 Element newChar = new Element("person"); newChar.setAttribute("name",name); newChar.addContent(new Element("description").setText("")); items.addContent(newChar);
37 } else {
38 gameobj.addContent(new Element("characters")); addCharacter(name);
40 }
41 }
42 showCharacterList();
43 characterList.select(characterList.indexOf(name));
44 characterList.notifyListeners(SWT.Selection, new Event());
45 }
46
47 public void renameCharacter(String oldname, String newname) {
48 if (newname.equals("")) return; Element c = getCharacter(oldname);
50 c.setAttribute("name", newname); JdomHelpers.deepObjectRename(oldname, newname);
52 showCharacterList();
53 }
54
55 public void deleteCharacter(String name) {
56 java.util.List newList = new ArrayList();
57 if (gameobj != null) {
58 Element items = gameobj.getChild("characters"); if (items != null) {
60 java.util.List itemList = items.getChildren("person"); for (Iterator goIter = itemList.iterator(); goIter.hasNext();) {
62 Element element = (Element) goIter.next();
63
64 if (!element.getAttributeValue("name").equals(name)) { newList.add(element);
66 }
67 }
68 }
69 items.removeChildren("person"); items.addContent(newList);
71 }
72 JdomHelpers.deepObjectDelete(name);
73 showCharacterList();
74 }
75
76 protected void showCharacterList() {
77 String[] chars = getCharacters();
78 if (chars != null) {
79 characterList.setItems(chars);
80 characterList.select(characterList.getTopIndex());
81 characterList.notifyListeners(SWT.Selection, new Event());
82 } else {
83 characterList.removeAll();
84 characterComp.clean();
85 }
86 }
87
88 protected String[] getCharacters() {
89 String[] retval = null;
90 if (gameobj != null) {
91 Element items = gameobj.getChild("characters"); if (items != null) {
93 java.util.List itemList = items.getChildren("person"); retval = new String[itemList.size()];
95 int count = 0;
96 for (Iterator goIter = itemList.iterator(); goIter.hasNext();) {
97 Element element = (Element) goIter.next();
98 retval[count++] = element.getAttributeValue("name"); }
00 }
01 }
02 return retval;
03 }
04
05 public void showCharacters(Element gameobjects) {
06 this.gameobj = gameobjects;
07 this.showCharacterList();
08 }
09
10 public void updateData() {
11 characterList.notifyListeners(SWT.Selection, new Event());
12 }
13
14}
15