1 /*
2  *  CVS: $Id: GDKMainWindow.java,v 1.49 2004/07/25 21:40:55 marcus Exp $
3  * 
4  *  This file is part of JZuul.
5  *
6  *  JZuul 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 *  JZuul 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 */
23
24package org.jzuul.gdk.swt;
25
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.FileNotFoundException;
29import java.io.FileOutputStream;
30import java.io.FileReader;
31import java.io.FileWriter;
32import java.io.IOException;
33import java.io.InputStreamReader;
34import java.io.OutputStream;
35import java.io.OutputStreamWriter;
36import java.nio.CharBuffer;
37import java.nio.charset.Charset;
38import java.util.zip.ZipEntry;
39import java.util.zip.ZipOutputStream;
40
41import org.eclipse.swt.SWT;
42import org.eclipse.swt.events.SelectionEvent;
43import org.eclipse.swt.events.SelectionListener;
44import org.eclipse.swt.events.ShellAdapter;
45import org.eclipse.swt.events.ShellEvent;
46import org.eclipse.swt.layout.FillLayout;
47import org.eclipse.swt.layout.GridLayout;
48import org.eclipse.swt.widgets.Combo;
49import org.eclipse.swt.widgets.Display;
50import org.eclipse.swt.widgets.Event;
51import org.eclipse.swt.widgets.FileDialog;
52import org.eclipse.swt.widgets.Menu;
53import org.eclipse.swt.widgets.MenuItem;
54import org.eclipse.swt.widgets.MessageBox;
55import org.eclipse.swt.widgets.Shell;
56import org.eclipse.swt.widgets.TabFolder;
57import org.eclipse.swt.widgets.TabItem;
58import org.jdom.DocType;
59import org.jdom.Document;
60import org.jdom.Element;
61import org.jdom.JDOMException;
62import org.jdom.input.SAXBuilder;
63import org.jdom.output.Format;
64import org.jdom.output.XMLOutputter;
65import org.jzuul.engine.CommandList;
66import org.jzuul.engine.Engine;
67import org.jzuul.engine.gui.SwtGui;
68import org.jzuul.engine.gui.utils.Util;
69
70//TODO Deploy
71
72/**
73 * THIS IS EARLY BETA CODE... DON'T TRY THIS AT HOME :-D
74 * 
75 * 
76 * @version $Revision: 1.49 $
77 *  
78 */
79public class GDKMainWindow {
80
81    private Element data;
82
83    private Element dataClone;
84
85    private Shell shell;
86
87    /**
88     * <code>appName</code> definiert den Namen der Application, welcher in
89     * der Titelleiste angezeigt wird
90     */
91    protected final String appName = Messages.getString("GDK_NAME"); //$NON-NLS-1$
92
93    private MenuItem saveMenuItem;
94
95    private Menu menubar;
96
97    private TabFolder tabFolder;
98
99    private Display display;
00
01    private String filename;
02
03    private MapEditorComposite mapEditor;
04
05    private PlayerEditorComposite playerComposite;
06
07    private CharacterEditorComposite characterComposite;
08
09    private ItemEditorComposite itemComposite;
10
11    private RunValues runValues;
12
13    /**
14     * Constructor for Mainwindow
15     *  
16     */
17    public GDKMainWindow() {
18        runValues = new RunValues();
19    }
20
21    /**
22     * erstellt die Gui des GDK
23     * 
24     * @param display1
25     */
26    public void createGui(Display display1) {
27        display = display1;
28        shell = new Shell(SWT.BORDER | SWT.MIN  | SWT.RESIZE | SWT.MAX);
29        FillLayout lay = new FillLayout(SWT.VERTICAL);
30        shell.addShellListener(new ShellAdapter() {
31
32            public void shellClosed(ShellEvent e) {
33                exit();
34            }
35        });
36        shell
37                .setImage(Util.getImagefromResource(display,
38                        "etc/artwork/jz.png")); //$NON-NLS-1$
39        shell.setLayout(lay);
40
41        shell.setText(appName);
42        {
43            menubar = new Menu(shell, SWT.BAR);
44            shell.setMenuBar(menubar);
45            {
46                final MenuItem fileMenu = new MenuItem(menubar, SWT.CASCADE);
47                fileMenu.setText(Messages.getString("FILE")); //$NON-NLS-1$
48                {
49                    Menu popupmenu = new Menu(fileMenu);
50                    fileMenu.setMenu(popupmenu);
51                    {
52                        final MenuItem newMenuItem = new MenuItem(popupmenu,
53                                SWT.CASCADE);
54                        newMenuItem.setText(Messages.getString("NEW")); //$NON-NLS-1$
55                        newMenuItem.setAccelerator(SWT.CONTROL | 'n');
56                        newMenuItem
57                                .addSelectionListener(new SelectionListener() {
58
59                                    public void widgetDefaultSelected(
60                                            SelectionEvent e) {
61                                        // das soll leer sein
62                                    }
63
64                                    public void widgetSelected(SelectionEvent e) {
65                                        newFile();
66                                    }
67
68                                });
69                    }
70                    {
71                        final MenuItem openMenuItem = new MenuItem(popupmenu,
72                                SWT.NONE);
73                        openMenuItem.setText(Messages.getString("OPEN")); //$NON-NLS-1$
74                        openMenuItem.setAccelerator(SWT.CONTROL | 'o');
75                        openMenuItem
76                                .addSelectionListener(new SelectionListener() {
77
78                                    public void widgetDefaultSelected(
79                                            SelectionEvent e) {
80                                        //das soll leer sein
81                                    }
82
83                                    public void widgetSelected(SelectionEvent e) {
84                                        FileDialog foo = new FileDialog(shell,
85                                                SWT.OPEN);
86                                        foo.setText(Messages.getString("LOAD")); //$NON-NLS-1$
87                                        String[] bar = new String[1];
88                                        bar[0] = "*.xml"; //$NON-NLS-1$
89                                        foo.setFilterExtensions(bar);
90                                        String file;
91                                        file = foo.open();
92                                        if (file != null)
93                                            filename = file;
94                                        openFile(file);
95
96                                    }
97
98                                });
99                    }
00                    {
01                        saveMenuItem = new MenuItem(popupmenu, SWT.NONE);
02                        saveMenuItem.setText(Messages.getString("SAVE")); //$NON-NLS-1$
03                        saveMenuItem.setAccelerator(SWT.CONTROL | 's');
04                        saveMenuItem
05                                .addSelectionListener(new SelectionListener() {
06
07                                    public void widgetDefaultSelected(
08                                            SelectionEvent e) {
09                                        //das soll leer sein
10                                    }
11
12                                    public void widgetSelected(SelectionEvent e) {
13                                        saveFile();
14                                    }
15
16                                });
17
18                    }
19                    {
20                        saveMenuItem = new MenuItem(popupmenu, SWT.NONE);
21                        saveMenuItem.setText(Messages.getString("SAVE_AS")); //$NON-NLS-1$
22                        //saveMenuItem.setAccelerator(SWT.CONTROL | 's');
23                        saveMenuItem
24                                .addSelectionListener(new SelectionListener() {
25
26                                    public void widgetDefaultSelected(
27                                            SelectionEvent e) {
28                                        //das soll leer sein
29                                    }
30
31                                    public void widgetSelected(SelectionEvent e) {
32                                        filename = null;
33                                        saveFile();
34                                    }
35
36                                });
37
38                    }
39                    {
40                        final MenuItem separater = new MenuItem(popupmenu,
41                                SWT.SEPARATOR);
42                    }
43                    {
44                        final MenuItem quitMenuItem = new MenuItem(popupmenu,
45                                SWT.NONE);
46                        quitMenuItem.setText(Messages.getString("QUIT")); //$NON-NLS-1$
47                        quitMenuItem.setAccelerator(SWT.CONTROL | 'q');
48                        quitMenuItem
49                                .addSelectionListener(new SelectionListener() {
50
51                                    public void widgetDefaultSelected(
52                                            SelectionEvent e) {
53                                        //das soll leer sein
54                                    }
55
56                                    public void widgetSelected(SelectionEvent e) {
57                                        exit();
58                                    }
59
60                                });
61
62                    }
63                }
64            }
65            {
66                final MenuItem gameMenu = new MenuItem(menubar, SWT.CASCADE);
67                gameMenu.setText(Messages.getString("GAME")); //$NON-NLS-1$
68                {
69                    Menu popupmenu = new Menu(gameMenu);
70                    gameMenu.setMenu(popupmenu);
71                    {
72                        final MenuItem newMenuItem = new MenuItem(popupmenu,
73                                SWT.NONE);
74                        newMenuItem.setText(Messages.getString("DESCRIPTION")); //$NON-NLS-1$
75                        newMenuItem.setAccelerator(SWT.CONTROL | 'd');
76                        newMenuItem
77                                .addSelectionListener(new SelectionListener() {
78
79                                    public void widgetDefaultSelected(
80                                            SelectionEvent e) {
81                                        //fpp
82                                    }
83
84                                    public void widgetSelected(SelectionEvent e) {
85                                        InputDialog id = new InputDialog(
86                                                new Shell(e.display), SWT.NONE);
87                                        id.setStyle(SWT.MULTI | SWT.WRAP
88                                                | SWT.V_SCROLL);
89                                        id
90                                                .setMessage(Messages.getString("ENTER_DESCRIPTION")); //$NON-NLS-1$
91                                        id.setDefaultValue(data
92                                                .getChildText("description")); //$NON-NLS-1$
93                                        String retval = (String) id.open();
94                                        if (retval != null) {
95                                            Element des = data
96                                                    .getChild("description"); //$NON-NLS-1$
97                                            if (!(des == null)) {
98                                                des.detach();
99                                            }
00                                            data.addContent(new Element(
01                                                    "description") //$NON-NLS-1$
02                                                    .setText(retval));
03                                        }
04
05                                    }
06
07                                });
08
09                    }
10                    {
11                        final MenuItem newMenuItem = new MenuItem(popupmenu,
12                                SWT.NONE);
13                        newMenuItem.setText(Messages.getString("RUN")); //$NON-NLS-1$
14                        newMenuItem.setAccelerator(SWT.F9);
15                        newMenuItem
16                                .addSelectionListener(new SelectionListener() {
17
18                                    public void widgetDefaultSelected(
19                                            SelectionEvent e) {
20                                        //das soll leer sein
21                                    }
22
23                                    public void widgetSelected(SelectionEvent e) {
24                                        updateAllData();
25                                        if (mapEditor.verifyAllMaps(false)) {
26                                            if (changed() || (filename == null)) {
27                                                if (askChanged() == SWT.YES)
28                                                    saveFile();
29                                            }
30                                            try {
31                                                Engine engine = new Engine(
32                                                        filename, CommandList
33                                                                .defaultList(),
34                                                        new SwtGui(e.display),
35                                                        runValues.threadedNPCs,
36                                                        runValues.numOfPlayers);
37                                                engine.run(false);
38                                            } catch (NullPointerException npe) {
39                                                System.err
40                                                        .println("jzuul engine died with nullpointerexception"); //$NON-NLS-1$
41                                                npe.printStackTrace();
42                                            } catch (Exception ex) {
43                                                ex.printStackTrace();
44                                            }
45                                        }
46
47                                    }
48
49                                });
50
51                    }
52                    {
53                        final MenuItem newMenuItem = new MenuItem(popupmenu,
54                                SWT.NONE);
55                        newMenuItem.setText(Messages.getString("RUN_DOTS")); //$NON-NLS-1$
56                        //newMenuItem.setEnabled(false); //FIXME crashes
57                        // sometimes
58                        newMenuItem.setAccelerator(SWT.F11);
59                        newMenuItem
60                                .addSelectionListener(new SelectionListener() {
61
62                                    public void widgetDefaultSelected(
63                                            SelectionEvent e) {
64                                        //das soll leer sein
65                                    }
66
67                                    public void widgetSelected(SelectionEvent e) {
68                                        updateAllData();
69                                        if (mapEditor.verifyAllMaps(false)) {
70                                            if (changed() || (filename == null)) {
71                                                if (askChanged() == SWT.YES)
72                                                    saveFile();
73                                            }
74                                            RunDialog id = new RunDialog(shell,
75                                                    SWT.NONE);
76                                            id.setRunValues(runValues);
77                                            RunValues tmpValues = id.open();
78                                            id = null;
79                                            System.gc();
80                                            if (tmpValues != null) {
81                                                runValues = tmpValues;
82                                                System.err
83                                                        .println("Map filename: " //$NON-NLS-1$
84                                                                + filename);
85                                                try {
86                                                    Engine engine = new Engine(
87                                                            filename,
88                                                            CommandList
89                                                                    .defaultList(),
90                                                            new SwtGui(
91                                                                    e.display),
92                                                            runValues.threadedNPCs,
93                                                            runValues.numOfPlayers);
94                                                    engine.run(runValues.askPlayerName);
95                                                    System.gc();
96                                                } catch (NullPointerException npe) {
97                                                    System.err
98                                                            .println("Jzuul engine died with nullpointexception"); //$NON-NLS-1$
99                                                    npe.printStackTrace();
00                                                } catch (Exception ex) {
01                                                    ex.printStackTrace();
02                                                }
03                                            }
04
05                                        }
06                                    }
07
08                                });
09
10                    }
11
12                
13                {
14                    final MenuItem newMenuItem = new MenuItem(popupmenu,
15                            SWT.NONE);
16                    newMenuItem.setText(Messages.getString("DEPLOY")); //$NON-NLS-1$
17                    newMenuItem.setAccelerator(SWT.CONTROL | 'e');
18                    newMenuItem
19                            .addSelectionListener(new SelectionListener() {
20
21                                public void widgetDefaultSelected(
22                                        SelectionEvent e) {
23                                    //fpp
24                                }
25
26                                public void widgetSelected(SelectionEvent e) {
27                                    deploy();
28                                    
29                                }
30
31                            });
32                    newMenuItem.setEnabled(false);
33                }
34            }
35            }
36
37            {
38                final MenuItem debugMenu = new MenuItem(menubar, SWT.CASCADE);
39                debugMenu.setText(Messages.getString("DEBUG")); //$NON-NLS-1$
40                debugMenu.setEnabled(false);
41                
42                {
43                    Menu popupmenu = new Menu(debugMenu);
44                    debugMenu.setMenu(popupmenu);
45                    {
46                        final MenuItem newMenuItem = new MenuItem(popupmenu,
47                                SWT.NONE);
48                        newMenuItem.setText(Messages.getString("CHECK_CHANGED")); //$NON-NLS-1$
49                        newMenuItem.setAccelerator(SWT.CONTROL | 'c');
50                        newMenuItem
51                                .addSelectionListener(new SelectionListener() {
52
53                                    public void widgetDefaultSelected(
54                                            SelectionEvent e) {
55                                        //fpp
56                                    }
57
58                                    public void widgetSelected(SelectionEvent e) {
59                                        MessageBox mb = new MessageBox(
60                                                new Shell(e.display),
61                                                SWT.APPLICATION_MODAL);
62                                        mb.setMessage("Check changed: " //$NON-NLS-1$
63                                                + changed());
64                                        mb.open();
65                                    }
66
67                                });
68
69                    }
70                    debugMenu.dispose();
71                }
72            }
73
74            {
75                final MenuItem helpMenu = new MenuItem(menubar, SWT.CASCADE);
76                helpMenu.setText(Messages.getString("HELP")); //$NON-NLS-1$
77                {
78                    Menu popupmenu = new Menu(helpMenu);
79                    helpMenu.setMenu(popupmenu);
80                    {
81                        final MenuItem cutMenuItem = new MenuItem(popupmenu,
82                                SWT.NONE);
83                        cutMenuItem.setText(Messages.getString("ABOUT")); //$NON-NLS-1$
84                        cutMenuItem.setAccelerator(SWT.CONTROL | 'a');
85                        cutMenuItem
86                                .addSelectionListener(new SelectionListener() {
87
88                                    public void widgetDefaultSelected(
89                                            SelectionEvent e) {
90                                        //das soll leer sein
91                                    }
92
93                                    public void widgetSelected(SelectionEvent e) {
94                                        MessageBox mb = new MessageBox(shell,
95                                                SWT.ICON_INFORMATION | SWT.OK);
96                                        mb.setText(Messages.getString("ABOUT_TEXT")); //$NON-NLS-1$
97                                        mb
98                                                .setMessage(Messages.getString("ABOUT_START")  //$NON-NLS-1$
99                                                        + " $Revision: 1.49 $\n\n" //$NON-NLS-1$
00                                                        + "2004 by\n" //$NON-NLS-1$
01                                                        + "\tMarcus Thiesen (marcus@jzuul.org)\n" //$NON-NLS-1$
02                                                        + "\tDaniel Lehmann (leh@jzuul.org)\n\n" //$NON-NLS-1$
03                                                        + Messages.getString("TRANSLATION_CREDIT")  //$NON-NLS-1$
04                                                        + Messages.getString("GPL_1")  //$NON-NLS-1$
05                                                        + Messages.getString("GPL_2")  //$NON-NLS-1$
06                                                        + "www.jzuul.org"); //$NON-NLS-1$
07                                        mb.open();
08                                    }
09
10                                });
11
12                    }
13                    {
14                        final MenuItem helpMenuItem = new MenuItem(popupmenu,
15                                SWT.NONE);
16                        helpMenuItem.setText(Messages.getString("HELP_MENU")); //$NON-NLS-1$
17                        helpMenuItem.setAccelerator(SWT.CONTROL | 'h');
18                        helpMenuItem.setAccelerator(SWT.F1);
19                        helpMenuItem
20                                .addSelectionListener(new SelectionListener() {
21
22                                    public void widgetDefaultSelected(
23                                            SelectionEvent e) {
24                                        //das soll leer sein
25                                    }
26
27                                    public void widgetSelected(SelectionEvent e) {
28                                        try {
29                                        HelpViewerDialog d = new HelpViewerDialog(new Shell(e.display));
30                                        d.open();
31                                        
32                                        } catch (org.eclipse.swt.SWTError ex) {
33                                            MessageBox mv = new MessageBox(new Shell(e.display), SWT.ICON_ERROR);
34                                            String message = Messages.getString("BROWSER_ERROR");  //$NON-NLS-1$
35                                            if (SWT.getPlatform().equals("gtk")) { //$NON-NLS-1$
36                                                message += Messages.getString("MOZILLA_WARNING_1"); //$NON-NLS-1$
37                                                message += Messages.getString("MOZILLA_WARNING_2"); //$NON-NLS-1$
38                                            } 
39                                            message += Messages.getString("ERROR_WAS"); //$NON-NLS-1$
40                                            mv.setMessage(message + ex.getMessage());
41                                            mv.open();
42                                        }
43                                        
44                                    }
45
46                                });
47
48                    }
49                }
50            }
51
52        }
53        {
54            tabFolder = new TabFolder(shell, SWT.BORDER);
55
56            tabFolder.addSelectionListener(new SelectionListener() {
57
58                public void widgetSelected(SelectionEvent arg0) {
59                    updateAllData();
60                }
61
62                public void widgetDefaultSelected(SelectionEvent arg0) {
63                }
64            });
65
66            tabFolder.setLayout(new GridLayout());
67            {
68                final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
69                tabItem.setToolTipText(Messages.getString("MAP_TOOLTIP")); //$NON-NLS-1$
70                tabItem.setText(Messages.getString("MAP")); //$NON-NLS-1$
71                {
72                    mapEditor = new MapEditorComposite(tabFolder, SWT.NONE);
73                    tabItem.setControl(mapEditor);
74                }
75
76            }
77            {
78                final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
79                tabItem.setToolTipText(Messages.getString("CHARACTER_TAB_TOOLTIP")); //$NON-NLS-1$
80                tabItem.setText(Messages.getString("CHARACTERS")); //$NON-NLS-1$
81                {
82                    characterComposite = new CharacterEditorComposite(
83                            tabFolder, SWT.NONE);
84                    tabItem.setControl(characterComposite);
85                }
86            }
87            {
88                final TabItem itemsTab = new TabItem(tabFolder, SWT.NONE);
89                itemsTab.setToolTipText(Messages.getString("ITEM_TOOLTIP")); //$NON-NLS-1$
90                itemsTab.setText(Messages.getString("ITEMS")); //$NON-NLS-1$
91                {
92                    itemComposite = new ItemEditorComposite(tabFolder, SWT.NONE);
93                    itemsTab.setControl(itemComposite);
94
95                }
96            }
97            {
98                final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
99                tabItem.setToolTipText(Messages.getString("PLAYER_TOOLTIP")); //$NON-NLS-1$
00                tabItem.setText(Messages.getString("PLAYER")); //$NON-NLS-1$
01                {
02                    playerComposite = new PlayerEditorComposite(tabFolder,
03                            SWT.NONE);
04                    tabItem.setControl(playerComposite);
05                }
06
07            }
08
09        }
10
11        shell.pack();
12        shell.setSize(800, 600);
13        Util.centerWindow(shell);
14
15    }
16
17    /**
18     * parsed das zu öffnende File ins DOM
19     * 
20     * @param filename
21     *            des zu öffnenden Files
22     */
23    public void openFile(String filename) {
24        if (filename == null)
25            return;
26        if (changed()) {
27            if (askChanged() == SWT.YES)
28                saveFileDialog();
29        }
30        this.filename = filename;
31
32        SAXBuilder builder = new SAXBuilder();
33
34        try {
35            FileInputStream is = new FileInputStream(new File(filename));
36            InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8")); //$NON-NLS-1$
37            
38            Document doc = builder.build(reader);
39            Element el = doc.getRootElement();
40            System.err.println("Element Name " + el.getName()); //$NON-NLS-1$
41            if (el.getName().equals("gamefile")) { //$NON-NLS-1$
42                this.setData(el);
43            } else {
44                MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR);
45                mb.setMessage(Messages.getString("FILE_ERROR")); //$NON-NLS-1$
46                mb.open();
47                newFile();
48            }
49            
50            //this.data.detach();
51        } catch (JDOMException e) {
52            MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR);
53            mb.setMessage(Messages.getString("PARSE_ERROR_MSG") + filename + "\n" + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
54            mb.open();
55        } catch (IOException e) {
56            MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR);
57            mb.setMessage(Messages.getString("IO_ERROR_MSG") + filename + "\n" + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
58            mb.open();
59        }
60        shell.setText(appName + " - " + filename); //$NON-NLS-1$
61        showMap();
62        showItems();
63        showCharacter();
64        showPlayer();
65    }
66
67    /**
68     * schreibt den DOM-Tree in ein File
69     * 
70     * @param filename
71     *            des zu speichernden Files
72     */
73    public void saveFile() {
74        if (this.filename == null) {
75            this.filename = saveFileDialog();
76            if (filename == null)
77                return;
78        }
79
80        DocType type = new DocType("gamefile", //$NON-NLS-1$
81                "file:org/jzuul/engine/dtd/gamefile.dtd"); //$NON-NLS-1$
82        data.detach();
83
84        Document doc = new Document(data, type);
85
86        try {
87            FileOutputStream out = new FileOutputStream(new File(filename),false);
88            OutputStreamWriter ow = new OutputStreamWriter(out, Charset.forName("UTF-8")); //$NON-NLS-1$
89            
90            Format format = Format.getPrettyFormat();
91            format.setEncoding("UTF-8"); //$NON-NLS-1$
92            XMLOutputter fmt = new XMLOutputter(format);
93            
94            fmt.output(doc, ow);
95            ow.flush();
96            ow.close();
97        } catch (IOException e) {
98            e.printStackTrace();
99            MessageBox mb = new MessageBox(this.shell, SWT.ICON_ERROR);
00            mb.setMessage(e.getMessage());
01            mb.open();
02        }
03        this.setData(doc.getRootElement());
04    }
05
06    public void open(Display display) {
07        shell.open();
08        if (this.data == null) {
09            newFile();
10        }
11        while (!shell.isDisposed()) {
12            if (!display.readAndDispatch())
13                display.sleep();
14        }
15    }
16
17    public String saveFileDialog() {
18        FileDialog foo = new FileDialog(shell, SWT.SAVE);
19        foo.setText(Messages.getString("SAVE_DOT")); //$NON-NLS-1$
20        String[] bar = new String[1];
21        bar[0] = "*.xml"; //$NON-NLS-1$
22        foo.setFilterExtensions(bar);
23        String filename = foo.open();
24        if (filename != null) {
25            shell.setText(appName + " - " + filename); //$NON-NLS-1$
26            this.filename = "/" + filename; //$NON-NLS-1$
27
28        }
29        return filename;
30    }
31
32    protected int askChanged() {
33        return askChanged(SWT.YES | SWT.NO | SWT.CANCEL);
34    }
35    
36    protected int askChanged(int flags) {
37        MessageBox mb = new MessageBox(shell, SWT.ICON_QUESTION | flags);
38        mb
39                .setMessage(Messages.getString("SAVE_QUESTION")); //$NON-NLS-1$
40        mb.setText(Messages.getString("GDK_QUESTION")); //$NON-NLS-1$
41        return mb.open();
42    }
43
44    protected void exit() {
45        if (!changed()) {
46            shell.dispose();
47        } else {
48            switch (askChanged(SWT.YES | SWT.NO)) {
49            case SWT.YES:
50                saveFileDialog();
51            // intended fallthrough
52            case SWT.NO:
53                shell.dispose();
54                break;
55            case SWT.CANCEL: // do nothing
56                break;
57            }
58        }
59    }
60
61    protected void newFile() {
62        if (changed()) {
63            if (askChanged() == SWT.YES)
64                saveFileDialog();
65        }
66        shell.setText(appName + Messages.getString("UNTITLED")); //$NON-NLS-1$
67        filename = null;
68
69        Element freshFile = new Element("gamefile"); //$NON-NLS-1$
70        freshFile.addContent(new Element("description")); //$NON-NLS-1$
71        freshFile
72                .addContent(new Element("map").setAttribute("name", "default")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
73        freshFile.addContent(new Element("player")); //$NON-NLS-1$
74        freshFile.addContent(new Element("dialogs")); //$NON-NLS-1$
75        freshFile.addContent(new Element("gameobjects")); //$NON-NLS-1$
76        freshFile.getChild("gameobjects").addContent(new Element("characters")); //$NON-NLS-1$ //$NON-NLS-2$
77        freshFile.getChild("gameobjects").addContent(new Element("items")); //$NON-NLS-1$ //$NON-NLS-2$
78
79        setData(freshFile);
80
81        showItems();
82        showCharacter();
83        showMap();
84        showPlayer();
85
86    }
87
88    public void close() {
89        display.syncExec(new Runnable() {
90
91            public void run() {
92
93                Engine.debug("Shell & display are being closed", 5); //$NON-NLS-1$
94                shell.dispose();
95                display.dispose();
96            }
97        });
98
99    }
00
01    public void showMap() {
02        mapEditor.setMaps(data.getChildren("map")); //$NON-NLS-1$
03        mapEditor.initalSelect();
04    }
05
06    public void showPlayer() {
07        playerComposite.showPlayer(data.getChild("player")); //$NON-NLS-1$
08    }
09
10    public void showCharacter() {
11        characterComposite.showCharacters(data.getChild("gameobjects")); //$NON-NLS-1$
12    }
13
14    public void showItems() {
15        itemComposite.showItems(data.getChild("gameobjects")); //$NON-NLS-1$
16    }
17
18    public boolean changed() {
19        return !JdomHelpers.equals(this.data, dataClone);
20    }
21
22    public void setData(Element newData) {
23        System.err.println("Set data called"); //$NON-NLS-1$
24        dataClone = (Element) newData.clone();
25        this.data = newData;
26        JdomHelpers.setRoot(newData);
27    }
28
29    public void updateAllData() {
30        if (mapEditor != null)
31            mapEditor.updateData();
32        if (playerComposite != null)
33            playerComposite.updateData();
34        if (characterComposite != null)
35            characterComposite.updateData();
36        if (itemComposite != null)
37            itemComposite.updateData();
38    }
39    
40    public void deploy() {
41        updateAllData();
42        if (mapEditor.verifyAllMaps(false)) {
43            if (changed() || (filename == null)) {
44                if (askChanged() == SWT.YES)
45                    saveFile();
46            }
47        InputDialog in = new InputDialog(shell);
48        in.setMessage(Messages.getString("ENTER_GAME_NAME")); //$NON-NLS-1$
49        String name = in.openNoWhitespace();
50        if (name != null) {
51            
52            try {
53                FileOutputStream fo = new FileOutputStream(new File(name + ".zip"),false); //$NON-NLS-1$
54                ZipOutputStream zip = new ZipOutputStream(fo);
55                
56                copyFileToStream(filename, zip);
57            
58            
59                zip.close();
60            } catch (FileNotFoundException e) {
61                MessageBox mb = new MessageBox(shell);
62                mb.setMessage("An error occured creating " + name + ".zip\n" + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
63                mb.open();
64            }  catch (IOException e) {
65                MessageBox mb = new MessageBox(shell);
66                mb.setMessage("An I/O error occured creating " + name + ".zip\n" + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
67                mb.open();
68            }
69        
70            
71            }
72        
73        }
74        
75        
76        
77    }
78    
79    public void copyFileToStream(String filename, ZipOutputStream zip) {
80            byte[] buf = new byte[1024];
81        
82            FileInputStream in;
83            try {
84                in = new FileInputStream(filename);
85         
86            // Add ZIP entry to output stream.
87            zip.putNextEntry(new ZipEntry(filename));
88    
89            // Transfer bytes from the file to the ZIP file
90            int len;
91            while ((len = in.read(buf)) > 0) {
92                zip.write(buf, 0, len);
93            }
94    
95            // Complete the entry
96            zip.closeEntry();
97            } catch (FileNotFoundException e) {
98                e.printStackTrace();
99            } catch (IOException e) {
00                e.printStackTrace();
01            }
02        
03    }
04    
05    public static void saveSelectFirst(Combo box) {
06        if (box != null) {
07            if (box.getItemCount() > 0) {
08                box.select(0);
09                box.notifyListeners(SWT.Selection, new Event());
10            }
11            
12        }
13    }
14    
15}
16
17class RunValues {
18    boolean threadedNPCs;
19    boolean askPlayerName;
20    int numOfPlayers;
21
22    public RunValues() {
23        numOfPlayers = 1;
24        threadedNPCs = false;
25        askPlayerName = false;
26    }
27
28    public RunValues(boolean threadedNPC, int numberOfPlayers, boolean askPlayerName) {
29        numOfPlayers = numberOfPlayers;
30        threadedNPCs = threadedNPC;
31        this.askPlayerName = askPlayerName;
32    }
33}