1 /*
2  *  CVS: $Id: EventEditorDialog.java,v 1.10 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.Arrays;
26import java.util.Iterator;
27
28import org.eclipse.swt.SWT;
29import org.eclipse.swt.events.SelectionEvent;
30import org.eclipse.swt.events.SelectionListener;
31import org.eclipse.swt.events.ShellAdapter;
32import org.eclipse.swt.events.ShellEvent;
33import org.eclipse.swt.layout.GridData;
34import org.eclipse.swt.layout.GridLayout;
35import org.eclipse.swt.widgets.Button;
36import org.eclipse.swt.widgets.Dialog;
37import org.eclipse.swt.widgets.Display;
38import org.eclipse.swt.widgets.Event;
39import org.eclipse.swt.widgets.Group;
40import org.eclipse.swt.widgets.List;
41import org.eclipse.swt.widgets.Shell;
42import org.jdom.Element;
43import org.jzuul.engine.gui.utils.Util;
44
45/**
46 * 
47 * Created on Jul 12, 2004
48 * 
49 * 
50 * @version $Revision: 1.10 $
51 */
52public class EventEditorDialog extends Dialog {
53
54    protected String[] events = { "default", "takeup", "drop", "playerenter", "playerleave", "use", "dialog_continue", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
55            "dialog_custom_result_1", "dialog_custom_result_2", "dialog_custom_result_3", "dialog_error", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
56            "dialog_npc_give", "dialog_npc_take", "dialog_end_failure", "dialog_end_success", "use_success", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
57            "use_failure", "timer" }; //$NON-NLS-1$ //$NON-NLS-2$
58
59    protected List eventList;
60
61    protected ActionEditorComposite actionComposite;
62
63    protected String currentEvent;
64
65    protected java.util.List eventElementsList;
66
67    /**
68     * @param arg0
69     */
70    public EventEditorDialog(Shell arg0) {
71        super(arg0);
72        Arrays.sort(events);
73    }
74
75    /**
76     * @param arg0
77     * @param arg1
78     */
79    public EventEditorDialog(Shell arg0, int arg1) {
80        super(arg0, arg1);
81        Arrays.sort(events);
82    }
83
84    public void setEvents(String[] events) {
85        this.events = events;
86        
87    }
88
89    public Object open(java.util.List eventElements) {
90        eventElementsList = eventElements;
91        Shell parent = getParent();
92        final Shell shell = new Shell(parent, SWT.BORDER | SWT.MIN | SWT.RESIZE | SWT.APPLICATION_MODAL);
93        shell.setText(Messages.getString("EDIT_EVENT_ACTIONS")); //$NON-NLS-1$
94        shell.setLayout(new GridLayout(10, true));
95        shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png")); //$NON-NLS-1$
96        shell.addShellListener(new ShellAdapter() {
97
98            public void shellClosed(ShellEvent e) {
99                cleanUpChanges();
00            }
01        });
02
03        { //The list on the left side
04            eventList = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE | SWT.CHECK);
05            final GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL
06                    | GridData.GRAB_VERTICAL);
07            gridData.horizontalSpan = 3;
08            eventList.setLayoutData(gridData);
09            eventList.setItems(events);
10            eventList.addSelectionListener(new SelectionListener() {
11
12                public void widgetSelected(SelectionEvent arg0) {
13                    cleanUpChanges();
14                    currentEvent = eventList.getItem(eventList.getSelectionIndex());
15                    //actionGroup.setText("Actions for event " + currentEvent);
16                    showActionsByEvent(currentEvent);
17                }
18
19                public void widgetDefaultSelected(SelectionEvent arg0) {
20                }
21            });
22
23        }
24        { // The group with the action
25            actionComposite = new ActionEditorComposite(shell, SWT.NONE,findEvent(null));
26        }
27        { // The buttons on the bottom
28            final Group bottomGroup = new Group(shell, SWT.NONE);
29            GridData groupLD = new GridData(GridData.FILL_HORIZONTAL);
30            groupLD.heightHint = 40;
31            groupLD.horizontalSpan = 10;
32            bottomGroup.setLayoutData(groupLD);
33
34            bottomGroup.setLayout(new GridLayout(10, true));
35
36            GridData buttonData = new GridData(GridData.VERTICAL_ALIGN_END);
37            buttonData.heightHint = 30;
38            buttonData.widthHint = 80;
39
40            Button ok = new Button(bottomGroup, SWT.PUSH);
41            ok.setText(Messages.getString("OK")); //$NON-NLS-1$
42            ok.addSelectionListener(new SelectionListener() {
43
44                public void widgetSelected(SelectionEvent arg0) {
45                    cleanUpChanges();
46                    shell.dispose();
47                }
48
49                public void widgetDefaultSelected(SelectionEvent arg0) {
50                }
51            });
52            ok.setLayoutData(buttonData);
53
54        }
55        eventList.select(0);
56        eventList.notifyListeners(SWT.Selection, new Event());
57        
58        shell.setSize(620, 480);
59        shell.open();
60        Display display = parent.getDisplay();
61        while (!shell.isDisposed()) {
62            if (!display.readAndDispatch()) display.sleep();
63        }
64        return null;
65    }
66
67    public Element findEvent(String name) {
68        if (name == null) { name = events[0]; }
69        for (Iterator elementIter = eventElementsList.iterator(); elementIter.hasNext();) {
70            Element eventElement = (Element) elementIter.next();
71            if (eventElement.getAttributeValue("name").equals(name)) { return eventElement; } //$NON-NLS-1$
72        }
73        // we didn't find the element, create a new one:
74        Element newEl = new Element("event"); //$NON-NLS-1$
75        newEl.setAttribute("name", name); //$NON-NLS-1$
76        eventElementsList.add(newEl);
77        return newEl;
78    }
79
80    public void showActionsByEvent(String event) {
81        Element eventEl = findEvent(event);
82        Element actions = eventEl.getChild("actions"); //$NON-NLS-1$
83        if (actions == null) return;
84        java.util.List singleActions = actions.getChildren();
85        actionComposite.showActionElements(singleActions);
86    }
87
88    protected void cleanUpChanges() {
89        replaceActions(currentEvent, actionComposite.getElementList());
90    }
91
92    protected void replaceActions(String eventName, java.util.List actionElements) {
93        Element event = findEvent(eventName);
94        if (actionElements.isEmpty()) {
95            event.detach();
96            return;
97        }
98        
99        event.removeChildren("actions"); //$NON-NLS-1$
00        Element actions = new Element("actions"); //$NON-NLS-1$
01        actions.addContent(actionElements);
02        event.addContent(actions);
03
04        System.err.println("Added " + actionElements.size() + " Elements to event " + eventName); //$NON-NLS-1$ //$NON-NLS-2$
05    }
06
07}