1 /*
2  *  CVS: $Id: DialogEditorDialog.java,v 1.14 2004/07/26 10:09:11 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.text.MessageFormat;
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.Iterator;
29import java.util.Stack;
30
31import org.eclipse.swt.SWT;
32import org.eclipse.swt.events.SelectionEvent;
33import org.eclipse.swt.events.SelectionListener;
34import org.eclipse.swt.events.ShellAdapter;
35import org.eclipse.swt.events.ShellEvent;
36import org.eclipse.swt.layout.FillLayout;
37import org.eclipse.swt.layout.GridData;
38import org.eclipse.swt.layout.GridLayout;
39import org.eclipse.swt.widgets.Button;
40import org.eclipse.swt.widgets.Combo;
41import org.eclipse.swt.widgets.Composite;
42import org.eclipse.swt.widgets.Dialog;
43import org.eclipse.swt.widgets.Display;
44import org.eclipse.swt.widgets.Event;
45import org.eclipse.swt.widgets.Group;
46import org.eclipse.swt.widgets.Label;
47import org.eclipse.swt.widgets.List;
48import org.eclipse.swt.widgets.Menu;
49import org.eclipse.swt.widgets.MenuItem;
50import org.eclipse.swt.widgets.MessageBox;
51import org.eclipse.swt.widgets.Shell;
52import org.eclipse.swt.widgets.Text;
53import org.jdom.Content;
54import org.jdom.Element;
55import org.jdom.Parent;
56import org.jzuul.engine.gui.utils.Util;
57
58//TODO References not yet implemented
59
60/**
61 * 
62 * Created on Jul 12, 2004
63 * 
64 * 
65 * @version $Revision: 1.14 $
66 */
67public class DialogEditorDialog extends Dialog {
68
69    protected class DialogObjectElement {
70
71        private String dialogno, phase;
72
73        private Text player, npc;
74
75        private Combo type;
76
77        private Label id;
78
79        private Group group;
80
81        private Combo nextPhase;
82
83        public DialogObjectElement(String dialogno, String phase, Label id, Combo type, Combo nextPhase, Text player,
84                Text npc, Group group) {
85            this.dialogno = dialogno;
86            this.phase = phase;
87            this.id = id;
88            this.type = type;
89            this.player = player;
90            this.npc = npc;
91            this.group = group;
92            this.nextPhase = nextPhase;
93        }
94
95        public void delete() {
96            Composite c = group.getParent();
97            group.dispose();
98            c.layout();
99            c.redraw();
00        }
01
02        public Element toXMLElement() {
03            Element newEl = new Element("object"); //$NON-NLS-1$
04            newEl.setAttribute("id", getId().toString()); //$NON-NLS-1$
05            newEl.setAttribute("type", type.getText()); //$NON-NLS-1$
06            String nextphase = nextPhase.getText();
07            if (!(nextphase == null || nextphase.equals(""))) { //$NON-NLS-1$
08                newEl.setAttribute("nextphase",nextphase ); //$NON-NLS-1$
09            }
10            newEl.addContent(new Element("say").setText(player.getText())); //$NON-NLS-1$
11            newEl.addContent(new Element("reply").setText(npc.getText())); //$NON-NLS-1$
12            return newEl;
13        }
14
15        public void fill(Element dialogObjectElement) {
16            freeIds.push(this.getId());
17            id.setText("ID: " + dialogObjectElement.getAttributeValue("id")); //$NON-NLS-1$ //$NON-NLS-2$
18            type.select(type.indexOf(dialogObjectElement.getAttributeValue("type"))); //$NON-NLS-1$
19            if (type.getText().equals("dialog_continue")) { //$NON-NLS-1$
20                nextPhase.select(nextPhase.indexOf(dialogObjectElement.getAttributeValue("nextphase"))); //$NON-NLS-1$
21                nextPhase.setVisible(true);
22            }
23            player.setText(dialogObjectElement.getChildText("say")); //$NON-NLS-1$
24            npc.setText(dialogObjectElement.getChildText("reply")); //$NON-NLS-1$
25        }
26
27        public Integer getId() {
28            String idString = id.getText();
29            idString = idString.replaceAll("ID: ", ""); //$NON-NLS-1$ //$NON-NLS-2$
30            return Integer.valueOf(idString);
31        }
32
33    }
34
35    protected final String[] dialogTypes = { "dialog_continue", "dialog_custom_result_1", "dialog_custom_result_2", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
36            "dialog_custom_result_3", "dialog_error", "dialog_npc_give", "dialog_npc_take", "dialog_end_failure", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
37            "dialog_end_success" }; //$NON-NLS-1$
38
39    protected List dialogList;
40
41    protected List phaseList;
42
43    protected Combo itemCombo;
44
45    protected Group dialogGroup;
46
47    protected java.util.List dialogs;
48
49    protected Stack dialogObjects;
50
51    protected Element dialogOwnerElement;
52
53    protected int globalMaxId;
54
55    protected Stack freeIds;
56
57    private Button lessButton;
58
59    private Button moreButton;
60
61    /**
62     * @param arg0
63     */
64    public DialogEditorDialog(Shell arg0) {
65        super(arg0);
66        dialogs = new ArrayList();
67        freeIds = new Stack();
68        dialogObjects = new Stack();
69        Arrays.sort(dialogTypes);
70    }
71
72    /**
73     * @param arg0
74     * @param arg1
75     */
76    public DialogEditorDialog(Shell arg0, int arg1) {
77        super(arg0, arg1);
78        dialogs = new ArrayList();
79        dialogObjects = new Stack();
80        freeIds = new Stack();
81        Arrays.sort(dialogTypes);
82    }
83
84    public Object open(Element dialogOwnerElement) {
85
86        this.dialogOwnerElement = dialogOwnerElement;
87        if (dialogOwnerElement == null) {
88            dialogOwnerElement = new Element("dialogs"); //$NON-NLS-1$
89        }
90        findMaxId();
91        Shell parent = getParent();
92        final Shell shell = new Shell(parent, SWT.BORDER | SWT.MIN | SWT.RESIZE | SWT.APPLICATION_MODAL | SWT.MAX);
93        Object[] formatArgs = { dialogOwnerElement.getAttributeValue("name")};
94        shell.setText(MessageFormat.format(Messages.getString("EDIT_DIALOGS_FOR"), formatArgs) ); //$NON-NLS-1$ //$NON-NLS-2$
95        shell.setLayout(new GridLayout(10, true));
96        shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png")); //$NON-NLS-1$
97        shell.addShellListener(new ShellAdapter() {
98
99            public void shellClosed(ShellEvent e) {
00                cleanUpChanges();
01            }
02        });
03
04        { //The dialog List
05            Group dialogListGroup = new Group(shell, SWT.NONE);
06            final GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL
07                    | GridData.GRAB_VERTICAL);
08            gridData.horizontalSpan = 3;
09            dialogListGroup.setLayoutData(gridData);
10            dialogListGroup.setText(Messages.getString("DIALOG_NR")); //$NON-NLS-1$
11            dialogListGroup.setLayout(new GridLayout(1, false));
12
13            dialogList = new List(dialogListGroup, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE | SWT.CHECK);
14            GridData dialogListData = new GridData(GridData.FILL_BOTH);
15            dialogList.setLayoutData(dialogListData);
16
17            dialogList.addSelectionListener(new SelectionListener() {
18
19                public void widgetSelected(SelectionEvent arg0) {
20                    cleanUpChanges();
21                    showPhases(dialogList.getSelectionIndex() + 1);
22                }
23
24                public void widgetDefaultSelected(SelectionEvent arg0) {
25                }
26            });
27
28            Menu dialogPopup = new Menu(dialogList);
29            dialogList.setMenu(dialogPopup);
30            MenuItem add = new MenuItem(dialogPopup, SWT.NONE);
31            add.setText(Messages.getString("ADD")); //$NON-NLS-1$
32            add.addSelectionListener(new SelectionListener() {
33
34                public void widgetSelected(SelectionEvent arg0) {
35                    Element newEl = new Element("dialog"); //$NON-NLS-1$
36                    DialogEditorDialog.this.dialogOwnerElement.addContent(newEl);
37                    dialogs.add(newEl);
38                    showDialogs();
39                    dialogList.select(dialogList.getItemCount() - 1);
40                    dialogList.notifyListeners(SWT.Selection, new Event());
41                }
42
43                public void widgetDefaultSelected(SelectionEvent arg0) {
44                }
45            });
46
47            MenuItem delete = new MenuItem(dialogPopup, SWT.NONE);
48            delete.setText(Messages.getString("DELETE")); //$NON-NLS-1$
49            delete.addSelectionListener(new SelectionListener() {
50
51                public void widgetSelected(SelectionEvent arg0) {
52                    if (dialogList.getSelectionIndex() > -1) {
53                    deleteDialog(dialogList.getSelectionIndex() + 1);
54                    }
55                }
56
57                public void widgetDefaultSelected(SelectionEvent arg0) {
58                }
59            });
60
61            // The precondition:
62            Group preconditionGroup = new Group(dialogListGroup, SWT.NONE);
63            preconditionGroup.setLayout(new GridLayout(1, false));
64            preconditionGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
65
66            Button edit = new Button(preconditionGroup, SWT.PUSH);
67            GridData editData = new GridData(GridData.FILL_HORIZONTAL);
68            edit.setLayoutData(editData);
69
70            edit.setText(Messages.getString("EDIT_PRECONDITIONS")); //$NON-NLS-1$
71            edit.addSelectionListener(new SelectionListener() {
72
73                public void widgetSelected(SelectionEvent arg0) {
74                    PreconditionEditorDialog ed = new PreconditionEditorDialog(new Shell(arg0.display), SWT.NONE);
75                    ed.open(getDialogByNr(dialogList.getSelectionIndex() + 1));
76                }
77
78                public void widgetDefaultSelected(SelectionEvent arg0) {}
79            });
80
81        }
82        { //The phase List
83            Group phaseListGroup = new Group(shell, SWT.NONE);
84            final GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL
85                    | GridData.GRAB_VERTICAL);
86            gridData.horizontalSpan = 2;
87            phaseListGroup.setLayoutData(gridData);
88            phaseListGroup.setText(Messages.getString("DIALOG_PHASE")); //$NON-NLS-1$
89            phaseListGroup.setLayout(new FillLayout());
90
91            phaseList = new List(phaseListGroup, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE | SWT.CHECK);
92
93            phaseList.addSelectionListener(new SelectionListener() {
94
95                public void widgetSelected(SelectionEvent arg0) {
96                    cleanUpChanges();
97                    int index = phaseList.getSelectionIndex();
98                    if ( (index > 0) && (index < phaseList.getItemCount())) {
99                        System.err.println("Index is " + index); //$NON-NLS-1$
00                        String phaseNo = phaseList.getItem(index);
01                        dialogGroup.setText(Messages.getString("PHASE") + phaseNo); //$NON-NLS-1$
02                        showDialogObject(dialogList.getSelectionIndex() + 1, phaseList.getSelectionIndex() + 1);
03                    }
04                }
05
06                public void widgetDefaultSelected(SelectionEvent arg0) {
07                }
08            });
09
10            Menu phasePopup = new Menu(phaseList);
11            phaseList.setMenu(phasePopup);
12            MenuItem add = new MenuItem(phasePopup, SWT.NONE);
13            add.setText(Messages.getString("ADD")); //$NON-NLS-1$
14            add.addSelectionListener(new SelectionListener() {
15
16                public void widgetSelected(SelectionEvent arg0) {
17                    int dialogNr = dialogList.getSelectionIndex() + 1;
18                    Element currentDialog = getDialogByNr(dialogNr);
19                    currentDialog.addContent(new Element("phase")); //$NON-NLS-1$
20                    showPhases(dialogNr);
21                    phaseList.select(phaseList.getItemCount() - 1);
22                    phaseList.notifyListeners(SWT.Selection, new Event());
23                }
24
25                public void widgetDefaultSelected(SelectionEvent arg0) {
26                }
27            });
28
29            MenuItem delete = new MenuItem(phasePopup, SWT.NONE);
30            delete.setText(Messages.getString("DELETE")); //$NON-NLS-1$
31            delete.addSelectionListener(new SelectionListener() {
32
33                public void widgetSelected(SelectionEvent arg0) {
34                    deletePhase(phaseList.getSelectionIndex() + 1);
35                }
36
37                public void widgetDefaultSelected(SelectionEvent arg0) {
38                }
39            });
40
41        }
42        { // The group with the action
43            dialogGroup = new Group(shell, SWT.V_SCROLL);
44            GridData layoutData = new GridData(GridData.FILL_BOTH);
45            layoutData.horizontalSpan = 5;
46            dialogGroup.setLayoutData(layoutData);
47
48            dialogGroup.setText(Messages.getString("DIALOGS")); //$NON-NLS-1$
49            dialogGroup.setLayout(new GridLayout(2, false));
50
51            moreButton = new Button(dialogGroup, SWT.PUSH);
52            moreButton.setText(Messages.getString("MORE")); //$NON-NLS-1$
53
54            GridData buttondat = new GridData();
55            buttondat.heightHint = 30;
56            buttondat.widthHint = 80;
57            moreButton.setLayoutData(buttondat);
58
59            moreButton.addSelectionListener(new SelectionListener() {
60
61                public void widgetSelected(SelectionEvent arg0) {
62                    generateDialogGroup(dialogGroup);
63                    dialogGroup.layout();
64                    dialogGroup.redraw();
65                }
66
67                public void widgetDefaultSelected(SelectionEvent arg0) {
68                }
69            });
70
71            lessButton = new Button(dialogGroup, SWT.PUSH);
72            lessButton.setText(Messages.getString("LESS")); //$NON-NLS-1$
73            GridData buttondat2 = new GridData();
74            buttondat2.heightHint = 30;
75            buttondat2.widthHint = 80;
76            lessButton.setLayoutData(buttondat2);
77            lessButton.addSelectionListener(new SelectionListener() {
78
79                public void widgetSelected(SelectionEvent arg0) {
80                    if (dialogObjects.isEmpty()) return;
81                    DialogObjectElement toDelete = ((DialogObjectElement) dialogObjects.pop());
82                    freeIds.push(toDelete.getId());
83                    toDelete.delete();
84                }
85
86                public void widgetDefaultSelected(SelectionEvent arg0) {
87                }
88            });
89
90        }
91        { // The buttons on the bottom
92            final Group bottomGroup = new Group(shell, SWT.NONE);
93            GridData groupLD = new GridData(GridData.FILL_HORIZONTAL);
94            groupLD.heightHint = 40;
95            groupLD.horizontalSpan = 10;
96            bottomGroup.setLayoutData(groupLD);
97
98            bottomGroup.setLayout(new GridLayout(10, true));
99
00            GridData buttonData = new GridData(GridData.VERTICAL_ALIGN_END);
01            buttonData.heightHint = 30;
02            buttonData.widthHint = 80;
03
04            Button ok = new Button(bottomGroup, SWT.PUSH);
05            ok.setText(Messages.getString("OK")); //$NON-NLS-1$
06            ok.addSelectionListener(new SelectionListener() {
07
08                public void widgetSelected(SelectionEvent arg0) {
09                    cleanUpChanges();
10                    shell.dispose();
11                }
12
13                public void widgetDefaultSelected(SelectionEvent arg0) {
14                }
15            });
16
17            ok.setLayoutData(buttonData);
18
19        }
20        showDialogs();
21        String dialogNo = getDialogNo();
22        if (dialogNo != null) {
23            showPhases(dialogNo);
24        } else {
25            disableButtons();
26        }
27
28        shell.setSize(900, 600);
29        shell.open();
30
31        Display display = parent.getDisplay();
32        while (!shell.isDisposed()) {
33            if (!display.readAndDispatch()) display.sleep();
34        }
35        return null;
36    }
37
38    protected void generateDialogGroup(Group group) {
39        /*
40         * an action group is a combo box combined with a label to specify an
41         * action
42         */
43        final Group dialogObjectGroup = new Group(group, SWT.NONE);
44        dialogObjectGroup.setLayout(new GridLayout(4, false));
45
46        GridData groupData = new GridData(GridData.FILL_HORIZONTAL);
47        groupData.horizontalSpan = 2;
48        dialogObjectGroup.setLayoutData(groupData);
49
50        // first row
51        Label idLabel = new Label(dialogObjectGroup, SWT.NONE);
52        idLabel.setText("ID: " + getNextId()); //$NON-NLS-1$
53
54        final Combo combo = new Combo(dialogObjectGroup, SWT.NONE);
55        GridData comboDat = new GridData(GridData.FILL_HORIZONTAL);
56        comboDat.horizontalSpan = 1;
57        combo.setLayoutData(comboDat);
58        combo.setItems(dialogTypes);
59        combo.select(combo.indexOf("dialog_end_failure")); //$NON-NLS-1$
60
61        final Combo nextPhase = new Combo(dialogObjectGroup, SWT.NONE);
62        GridData nextPhaseDat = new GridData(GridData.FILL_HORIZONTAL);
63        nextPhaseDat.horizontalSpan = 1;
64        nextPhase.setLayoutData(nextPhaseDat);
65        nextPhase.setVisible(false);
66        nextPhase.add("0"); //$NON-NLS-1$
67        nextPhase.setItems(phaseList.getItems());
68        nextPhase.select(nextPhase.indexOf("0")); //$NON-NLS-1$
69        nextPhase.notifyListeners(SWT.Selection, new Event());
70        
71        combo.addSelectionListener(new SelectionListener() {
72
73            public void widgetSelected(SelectionEvent arg0) {
74                if (combo.getText().equals("dialog_continue")) { //$NON-NLS-1$
75                    nextPhase.setVisible(true);
76                } else {
77                    nextPhase.setVisible(false);
78                }
79            }
80
81            public void widgetDefaultSelected(SelectionEvent arg0) {
82            }
83        });
84
85        Button delete = new Button(dialogObjectGroup, SWT.PUSH);
86        GridData deleteData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
87        deleteData.horizontalSpan = 1;
88        delete.setLayoutData(deleteData);
89        delete.setText("Delete"); //$NON-NLS-1$
90        delete.addSelectionListener(new SelectionListener() {
91
92            public void widgetSelected(SelectionEvent arg0) {
93                for (Iterator groupIter = dialogObjects.iterator(); groupIter.hasNext();) {
94                    DialogObjectElement element = (DialogObjectElement) groupIter.next();
95                    if (element.group.equals(dialogObjectGroup)) {
96                        freeIds.push(element.getId());
97                        element.delete();
98                        groupIter.remove();
99                    }
00                }
01            }
02
03            public void widgetDefaultSelected(SelectionEvent arg0) {
04            }
05        });
06
07        //second row
08        Label playerLabel = new Label(dialogObjectGroup, SWT.NONE);
09        playerLabel.setText(Messages.getString("PLAYER_SAY")); //$NON-NLS-1$
10
11        Text playerText = new Text(dialogObjectGroup, SWT.BORDER);
12        GridData textDat = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
13        textDat.horizontalSpan = 3;
14        playerText.setLayoutData(textDat);
15
16        Label npcLabel = new Label(dialogObjectGroup, SWT.NONE);
17        npcLabel.setText(Messages.getString("NPC")); //$NON-NLS-1$
18
19        Text npcText = new Text(dialogObjectGroup, SWT.BORDER);
20        GridData npcDat = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
21        npcDat.horizontalSpan = 3;
22        npcText.setLayoutData(npcDat);
23
24        dialogObjects.push(new DialogObjectElement(getDialogNo(), getPhaseNo(), idLabel, combo, nextPhase, playerText,
25                npcText, dialogObjectGroup));
26    }
27
28    protected void cleanUpChanges() {
29        String dialogNo, phaseNo;
30        dialogNo = phaseNo = null;
31        java.util.List objects = new ArrayList();
32        for (Iterator iter = dialogObjects.iterator(); iter.hasNext();) {
33            DialogObjectElement element = (DialogObjectElement) iter.next();
34            dialogNo = element.dialogno;
35            phaseNo = element.phase;
36            objects.add(element.toXMLElement());
37            element.delete();
38            iter.remove();
39        }
40        setObjects(dialogNo, phaseNo, objects);
41    }
42
43    protected void setObjects(String dialogNo, String phaseNo, java.util.List objects) {
44        if (dialogNo == null || phaseNo == null) return;
45        Element phase = getPhaseByNr(Integer.parseInt(dialogNo), Integer.parseInt(phaseNo));
46        phase.removeContent();
47        phase.addContent(objects);
48    }
49
50    protected void showPhases(String dialogNr) {
51        showPhases(Integer.valueOf(dialogNr).intValue());
52    }
53
54    protected void showPhases(int dialogNr) {
55        Element dialog = getDialogByNr(dialogNr);
56        phaseList.removeAll();
57        if (dialog != null) {
58            java.util.List phases = dialog.getChildren("phase"); //$NON-NLS-1$
59            if (phases == null || phases.size() == 0) {
60                disableButtons();
61            } else {
62                enableButtons();
63                for (int i = 1; i <= phases.size(); i++) {
64                    phaseList.add(String.valueOf(i));
65                }
66            }
67        } else {
68            disableButtons();
69        }
70    }
71
72    protected void showDialogs() {
73        int dialogCount = dialogOwnerElement.getChildren().size();
74        dialogList.removeAll();
75        if (dialogCount == 0) {
76            phaseList.setEnabled(false);
77            phaseList.redraw();
78        } else {
79            phaseList.setEnabled(true);
80            phaseList.redraw();
81            System.err.println("phaseList.redraw()"); //$NON-NLS-1$
82            for (int i = 1; i <= dialogOwnerElement.getChildren().size(); i++) {
83                dialogList.add(String.valueOf(i));
84            }
85        }
86    }
87
88    protected Element getDialogByNr(int nr) {
89        if (dialogOwnerElement != null) {
90            nr--;
91            java.util.List dialogs = dialogOwnerElement.getChildren("dialog"); //$NON-NLS-1$
92            if (dialogs != null && nr >= 0) {
93                if (nr < dialogs.size()) return (Element) dialogs.get(nr);
94            }
95        }
96        Element newEl = new Element("dialog"); //$NON-NLS-1$
97        dialogOwnerElement.addContent(newEl);
98        return newEl;
99
00    }
01
02    protected Element getDialogByNr(String nr) {
03        return getDialogByNr(Integer.valueOf(nr).intValue());
04    }
05
06    protected Element getPhaseByNr(int dialog, int phase) {
07        Element dialogElement = getDialogByNr(dialog);
08        phase--;
09        java.util.List phases = dialogElement.getChildren("phase"); //$NON-NLS-1$
10        if (phases != null) {
11            System.err.println("size: " + phases.size() + " phase: " + phase); //$NON-NLS-1$ //$NON-NLS-2$
12            if ((phases.size() > phase) && (phase > -1)) return (Element) phases.get(phase);
13        }
14        Element newEl = new Element("phase"); //$NON-NLS-1$
15        dialogElement.addContent(newEl);
16        return newEl;
17    }
18
19    protected void showDialogObject(int dialog, int phase) {
20        enableButtons();
21        Element phaseEl = getPhaseByNr(dialog, phase);
22        java.util.List objects = phaseEl.getChildren("object"); //$NON-NLS-1$
23        for (Iterator objIter = objects.iterator(); objIter.hasNext();) {
24            Element element = (Element) objIter.next();
25            generateDialogGroup(dialogGroup);
26            ((DialogObjectElement) dialogObjects.peek()).fill(element);
27        }
28        dialogGroup.layout();
29        dialogGroup.redraw();
30    }
31
32    protected void findMaxId() {
33        globalMaxId = 0;
34        if (dialogOwnerElement == null) return;
35        java.util.List dialogs = dialogOwnerElement.getChildren("dialog"); //$NON-NLS-1$
36        for (Iterator dialogIter = dialogs.iterator(); dialogIter.hasNext();) {
37            Element dialogElement = (Element) dialogIter.next();
38            java.util.List phases = dialogElement.getChildren("phase"); //$NON-NLS-1$
39            for (Iterator phaseIter = phases.iterator(); phaseIter.hasNext();) {
40                Element phaseElement = (Element) phaseIter.next();
41                java.util.List objects = phaseElement.getChildren();
42                for (Iterator objIter = objects.iterator(); objIter.hasNext();) {
43                    Element element = (Element) objIter.next();
44                    if (Integer.parseInt(element.getAttributeValue("id")) > globalMaxId) { //$NON-NLS-1$
45                        globalMaxId = Integer.parseInt(element.getAttributeValue("id")); //$NON-NLS-1$
46                    }
47
48                }
49            }
50        }
51        globalMaxId++;
52    }
53
54    protected int getNextId() {
55        if (!freeIds.isEmpty()) {
56            return ((Integer) freeIds.pop()).intValue();
57        } else {
58            return globalMaxId++;
59        }
60
61    }
62
63    protected String getDialogNo() {
64        int index = this.dialogList.getSelectionIndex();
65        if (index >= 0) { return this.dialogList.getItem(index); }
66        return null;
67    }
68
69    protected String getPhaseNo() {
70        int index = this.phaseList.getSelectionIndex();
71        if (index >= 0) { return this.phaseList.getItem(index); }
72        return null;
73    }
74
75    protected void deletePhase(int phaseNo) {
76        int dialogNo = Integer.valueOf(getDialogNo()).intValue();
77        Element phase = getPhaseByNr(dialogNo, phaseNo);
78        resetPhaseReferer(phaseNo);
79        phaseList.setSelection(phaseList.getTopIndex());
80        phaseList.notifyListeners(SWT.Selection, new Event());
81        java.util.List ids = getIds(phase);
82        phase.detach();
83        addToFree(ids);
84        showPhases(dialogNo);
85    }
86
87    protected void deleteDialog(int dialogNo) {
88        Element dialog = getDialogByNr(dialogNo);
89        dialogList.setSelection(dialogList.getTopIndex());
90        dialogList.notifyListeners(SWT.Selection, new Event());
91        dialog.detach();
92        showDialogs();
93    }
94
95    protected String[] getItemNames() {
96        Parent gamefile = dialogOwnerElement.getParent().getParent();
97        java.util.List contents = gamefile.getContent();
98        Element gameobjects = null;
99        java.util.List retval = new ArrayList();
00        for (Iterator contentIter = contents.iterator(); contentIter.hasNext();) {
01            Content jdomContent = (Content) contentIter.next();
02            if (jdomContent instanceof Element) {
03                Element jdomElement = (Element) jdomContent;
04                if (jdomElement.getName().equals("gameobjects")) { //$NON-NLS-1$
05                    gameobjects = jdomElement;
06                    break;
07                }
08            } else {
09                System.err.println("not an Element: " + jdomContent.getValue() + "(" + jdomContent.getClass() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
10            }
11        }
12        if (gameobjects != null) {
13            Element items = gameobjects.getChild("items"); //$NON-NLS-1$
14            if (items != null) {
15                java.util.List gameitems = items.getChildren("gameitem"); //$NON-NLS-1$
16                for (Iterator gameItemIter = gameitems.iterator(); gameItemIter.hasNext();) {
17                    Element gi = (Element) gameItemIter.next();
18                    retval.add(gi.getAttributeValue("name")); //$NON-NLS-1$
19                }
20            }
21        }
22
23        String[] names = new String[retval.size()];
24        retval.toArray(names);
25        return names;
26    }
27
28    public void setButtonsEnabled(boolean enabled) {
29        if (moreButton != null) moreButton.setEnabled(enabled);
30        if (lessButton != null) lessButton.setEnabled(enabled);
31    }
32
33    public void enableButtons() {
34        setButtonsEnabled(true);
35    }
36
37    public void disableButtons() {
38        setButtonsEnabled(false);
39    }
40
41    public java.util.List getIds(Element phase) {
42        java.util.List objects = phase.getChildren("object"); //$NON-NLS-1$
43        java.util.List retval = new ArrayList();
44        for (Iterator objIter = objects.iterator(); objIter.hasNext();) {
45            Element element = (Element) objIter.next();
46            retval.add(element.getAttributeValue("id")); //$NON-NLS-1$
47        }
48        return retval;
49    }
50
51    public void resetPhaseReferer(int phase) {
52        int count = 0;
53
54        Element dialog = getDialogByNr(getDialogNo());
55
56        java.util.List phases = dialog.getChildren("phase"); //$NON-NLS-1$
57        for (Iterator phaseIter = phases.iterator(); phaseIter.hasNext();) {
58            Element phaseElement = (Element) phaseIter.next();
59            java.util.List objects = phaseElement.getChildren("object"); //$NON-NLS-1$
60            for (Iterator objectIter = objects.iterator(); objectIter.hasNext();) {
61                Element objectElement = (Element) objectIter.next();
62                String nextphase = objectElement.getAttributeValue("nextphase"); //$NON-NLS-1$
63                if (nextphase != null) {
64                    if (nextphase.equals(String.valueOf(phase))) {
65                        objectElement.setAttribute("nextphase", "0"); //$NON-NLS-1$ //$NON-NLS-2$
66                        objectElement.setAttribute("type", "dialog_end_failure"); //$NON-NLS-1$ //$NON-NLS-2$
67                        count++;
68                    }
69                }
70            }
71        }
72
73        if (count > 0) {
74            MessageBox mb = new MessageBox(this.getParent(), SWT.ICON_INFORMATION);
75            Object[] formatArgs = { new Integer(count) };
76            mb.setMessage(MessageFormat.format(Messages.getString("DIALOG_INTEGRITY_MSG"),formatArgs));
77            mb.open();
78        }
79
80    }
81
82    public void addToFree(java.util.List ids) {
83        for (Iterator idsIter = ids.iterator(); idsIter.hasNext();) {
84            String strId = (String) idsIter.next();
85            freeIds.push(Integer.valueOf(strId));
86        }
87    }
88
89}