1 /*
2  *  CVS: $Id: ActionEditorComposite.java,v 1.11 2004/07/25 21:40:56 marcus Exp $
3  * 
4  *  This file is part of zuul.
5  *
6  *  zuul is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10 *
11 *  zuul is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with zuul; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 * 
20 *  Copyrigth 2004 by marcus, leh
21 * 
22 */
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/**
47 * 
48 * Created on Jul 14, 2004
49 * 
50 * 
51 * @version $Revision: 1.11 $
52 */
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")) { //$NON-NLS-1$ //$NON-NLS-2$
85                        makeTextInput();
86                    }
87                    if (t.equals("delete-item") || t.equals("room-item") || (t.equals("inventory-item"))) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
88                        makeItemInput();
89                    }
90                    if (t.equals("action")) { //$NON-NLS-1$
91                        makeActionInput();
92                    }
93                    if (t.equals("target")) { //$NON-NLS-1$
94                        makeTargetInput();
95                    }
96                    if (t.equals("random-success")) { //$NON-NLS-1$
97                        makeNoInput();
98                    }
99                    if (t.equals("alter-item")) { //$NON-NLS-1$
00                        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("")) { //$NON-NLS-1$
26                    if (cv.equals("action")) { //$NON-NLS-1$
27                        newEl.setAttribute("type", cv); //$NON-NLS-1$
28                    } else if (value2 != null) {
29                        newEl.setAttribute("property", cv); //$NON-NLS-1$
30                        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(""))) { //$NON-NLS-1$
41                    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"))); //$NON-NLS-1$
63                ((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"))); //$NON-NLS-1$
73                }
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")); //$NON-NLS-1$
01            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"); //$NON-NLS-1$
36            actionCombo.add("leaveRoom"); //$NON-NLS-1$
37            actionCombo.add("deleteItem"); //$NON-NLS-1$
38            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")); //$NON-NLS-1$
48            value = bt;
49            bt.addSelectionListener(new SelectionListener() {
50
51                public void widgetSelected(SelectionEvent e) {
52                    if (actionElement == null) {
53                        actionElement = new Element("target"); //$NON-NLS-1$
54                        actionElement.addContent(new Element("description")); //$NON-NLS-1$
55                    } else {
56                        actionElement.detach();
57                    }
58                    TargetEditor ed = new TargetEditor(new Shell(e.display), SWT.NONE);
59                    Element foo = new Element("actions"); //$NON-NLS-1$
60                    foo.addContent(actionElement);
61                    ed.open(foo);
62                    actionElement = foo.getChild("target"); //$NON-NLS-1$
63                }
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"); //$NON-NLS-1$
85            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", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
10            "alter-item", "delete-item", "random-success" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
11
12    protected Stack groups;
13
14    private Button more;
15
16    private Button less;
17
18    private Element element;
19
20    /**
21     * @param arg0
22     * @param arg1
23     */
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")); //$NON-NLS-1$
38
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")); //$NON-NLS-1$
58        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        /*
78         * an action group is a combo box combined with a label to specify an
79         * action
80         */
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}