1 /*
2  *  CVS: $Id: DefaultRoomPropertyComposite.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.Iterator;
26import java.util.List;
27
28import org.eclipse.swt.SWT;
29import org.eclipse.swt.events.SelectionEvent;
30import org.eclipse.swt.events.SelectionListener;
31import org.eclipse.swt.layout.GridData;
32import org.eclipse.swt.layout.GridLayout;
33import org.eclipse.swt.widgets.Button;
34import org.eclipse.swt.widgets.Combo;
35import org.eclipse.swt.widgets.Composite;
36import org.eclipse.swt.widgets.Label;
37import org.eclipse.swt.widgets.Shell;
38import org.eclipse.swt.widgets.Text;
39import org.jdom.Element;
40
41/**
42 * 
43 * Created on Jul 14, 2004
44 * 
45 * 
46 * @version $Revision: 1.10 $
47 */
48
49public class DefaultRoomPropertyComposite extends RoomPropertyComposite {
50    Combo northRoomCombo, eastRoomCombo, southRoomCombo, westRoomCombo,
51            upRoomCombo, downRoomCombo;
52
53    Text northRoomDescription, eastRoomDescription, southRoomDescription,
54            westRoomDescription, upRoomDescription, downRoomDescription;
55
56    Element room;
57
58    String map;
59
60    /**
61     * @param arg0
62     * @param arg1
63     */
64    public DefaultRoomPropertyComposite(Composite arg0, int arg1,
65            Element roomElement, String mapName) {
66        super(arg0, arg1);
67        room = roomElement;
68        map = mapName;
69        // second group: ways and views
70        GridData wvDat = new GridData(GridData.FILL_BOTH);
71        wvDat.verticalAlignment = GridData.VERTICAL_ALIGN_CENTER;
72        this.setLayoutData(wvDat);
73        this.setLayout(new GridLayout(5, false));
74
75        //north
76        {
77            Label nLabel = new Label(this, SWT.NONE);
78            nLabel.setText(Messages.getString("NORTH")); //$NON-NLS-1$
79
80            northRoomCombo = new Combo(this, SWT.READ_ONLY);
81            GridData nrcData = new GridData(GridData.FILL_HORIZONTAL);
82            nrcData.horizontalSpan = 2;
83            northRoomCombo.setLayoutData(nrcData);
84
85            northRoomDescription = new Text(this, SWT.BORDER);
86            GridData nrdData = new GridData(GridData.FILL_HORIZONTAL);
87            nrdData.horizontalSpan = 2;
88            northRoomDescription.setLayoutData(nrdData);
89            northRoomDescription.setToolTipText(Messages.getString("VIEW_TOOLTIP")); //$NON-NLS-1$
90        }
91        //east
92        {
93            Label eLabel = new Label(this, SWT.NONE);
94            eLabel.setText(Messages.getString("EAST")); //$NON-NLS-1$
95
96            eastRoomCombo = new Combo(this, SWT.READ_ONLY);
97            GridData ercData = new GridData(GridData.FILL_HORIZONTAL);
98            ercData.horizontalSpan = 2;
99            eastRoomCombo.setLayoutData(ercData);
00
01            eastRoomDescription = new Text(this, SWT.BORDER);
02            GridData erdData = new GridData(GridData.FILL_HORIZONTAL);
03            erdData.horizontalSpan = 2;
04            eastRoomDescription.setLayoutData(erdData);
05            eastRoomDescription.setToolTipText(Messages.getString("VIEW_TOOLTIP")); //$NON-NLS-1$
06        }
07        //south
08        {
09            Label sLabel = new Label(this, SWT.NONE);
10            sLabel.setText(Messages.getString("SOUTH")); //$NON-NLS-1$
11
12            southRoomCombo = new Combo(this, SWT.READ_ONLY);
13            GridData srcData = new GridData(GridData.FILL_HORIZONTAL);
14            srcData.horizontalSpan = 2;
15            southRoomCombo.setLayoutData(srcData);
16
17            southRoomDescription = new Text(this, SWT.BORDER);
18            GridData srdData = new GridData(GridData.FILL_HORIZONTAL);
19            srdData.horizontalSpan = 2;
20            southRoomDescription.setLayoutData(srdData);
21            southRoomDescription.setToolTipText(Messages.getString("VIEW_TOOLTIP")); //$NON-NLS-1$
22        }
23        //west
24        {
25            Label wLabel = new Label(this, SWT.NONE);
26            wLabel.setText(Messages.getString("WEST")); //$NON-NLS-1$
27
28            westRoomCombo = new Combo(this, SWT.READ_ONLY);
29            GridData wrcData = new GridData(GridData.FILL_HORIZONTAL);
30            wrcData.horizontalSpan = 2;
31            westRoomCombo.setLayoutData(wrcData);
32
33            westRoomDescription = new Text(this, SWT.BORDER);
34            GridData wrdData = new GridData(GridData.FILL_HORIZONTAL);
35            wrdData.horizontalSpan = 2;
36            westRoomDescription.setLayoutData(wrdData);
37            westRoomDescription.setToolTipText(Messages.getString("VIEW_TOOLTIP")); //$NON-NLS-1$
38        }
39        //up
40        {
41            Label uLabel = new Label(this, SWT.NONE);
42            uLabel.setText(Messages.getString("UP")); //$NON-NLS-1$
43
44            upRoomCombo = new Combo(this, SWT.READ_ONLY);
45            GridData upcData = new GridData(GridData.FILL_HORIZONTAL);
46            upcData.horizontalSpan = 2;
47            upRoomCombo.setLayoutData(upcData);
48
49            upRoomDescription = new Text(this, SWT.BORDER);
50            GridData updData = new GridData(GridData.FILL_HORIZONTAL);
51            updData.horizontalSpan = 2;
52            upRoomDescription.setLayoutData(updData);
53            upRoomDescription.setToolTipText(Messages.getString("VIEW_TOOLTIP")); //$NON-NLS-1$
54        }
55        //down
56        {
57            Label dLabel = new Label(this, SWT.NONE);
58            dLabel.setText(Messages.getString("DOWN")); //$NON-NLS-1$
59
60            downRoomCombo = new Combo(this, SWT.READ_ONLY);
61            GridData docData = new GridData(GridData.FILL_HORIZONTAL);
62            docData.horizontalSpan = 2;
63            downRoomCombo.setLayoutData(docData);
64
65            downRoomDescription = new Text(this, SWT.BORDER);
66            GridData dodData = new GridData(GridData.FILL_HORIZONTAL);
67            dodData.horizontalSpan = 2;
68            downRoomDescription.setLayoutData(dodData);
69            downRoomDescription.setToolTipText(Messages.getString("VIEW_TOOLTIP")); //$NON-NLS-1$
70        }
71
72        Button contents = new Button(this, SWT.PUSH);
73        contents.setText(Messages.getString("EDIT_CONTENTS")); //$NON-NLS-1$
74        contents.addSelectionListener(new SelectionListener() {
75
76            public void widgetSelected(SelectionEvent arg0) {
77                ContentsEditorDialog ed = new ContentsEditorDialog(new Shell(
78                        arg0.display), SWT.NONE);
79                ed.open(room);
80            }
81
82            public void widgetDefaultSelected(SelectionEvent arg0) {}
83        });
84
85        Button events = new Button(this, SWT.PUSH);
86        events.setText(Messages.getString("EDIT_EVENTS")); //$NON-NLS-1$
87        events.addSelectionListener(new SelectionListener() {
88
89            public void widgetSelected(SelectionEvent arg0) {
90                String[] roomEvents = { "playerenter", "playerleave", "timer" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
91                EventEditorDialog ed = new EventEditorDialog(new Shell(
92                        arg0.display), SWT.NONE);
93                ed.setEvents(roomEvents);
94                ed.open(room.getChildren("event")); //$NON-NLS-1$
95
96            }
97
98            public void widgetDefaultSelected(SelectionEvent arg0) {}
99        });
00
01    }
02
03    public void showRoomProperties() {
04        String[] rooms = JdomHelpers.getRoomListForMap(room, map);
05        
06        northRoomCombo.setItems(rooms);
07        northRoomCombo.add(""); //$NON-NLS-1$
08        northRoomCombo.remove(room.getAttributeValue("name")); //$NON-NLS-1$
09        
10        eastRoomCombo.setItems(rooms);
11        eastRoomCombo.add(""); //$NON-NLS-1$
12        eastRoomCombo.remove(room.getAttributeValue("name")); //$NON-NLS-1$
13                
14        southRoomCombo.setItems(rooms);
15        southRoomCombo.add(""); //$NON-NLS-1$
16        southRoomCombo.remove(room.getAttributeValue("name")); //$NON-NLS-1$
17        
18        westRoomCombo.setItems(rooms);
19        westRoomCombo.add(""); //$NON-NLS-1$
20        westRoomCombo.remove(room.getAttributeValue("name")); //$NON-NLS-1$
21        
22        upRoomCombo.setItems(rooms);
23        upRoomCombo.add(""); //$NON-NLS-1$
24        upRoomCombo.remove(room.getAttributeValue("name")); //$NON-NLS-1$
25        
26        downRoomCombo.setItems(rooms);
27        downRoomCombo.add(""); //$NON-NLS-1$
28        downRoomCombo.remove(room.getAttributeValue("name")); //$NON-NLS-1$
29        
30        Element description = room.getChild("views"); //$NON-NLS-1$
31        if (description != null) {
32            List views = description.getChildren("view"); //$NON-NLS-1$
33            for (Iterator viewIter = views.iterator(); viewIter.hasNext();) {
34                Element viewElement = (Element) viewIter.next();
35                if (viewElement.getAttributeValue("direction").equals("north")) { //$NON-NLS-1$ //$NON-NLS-2$
36                    northRoomDescription.setText(viewElement.getText());
37                }
38                if (viewElement.getAttributeValue("direction").equals("east")) { //$NON-NLS-1$ //$NON-NLS-2$
39                    eastRoomDescription.setText(viewElement.getText());
40                }
41                if (viewElement.getAttributeValue("direction").equals("south")) { //$NON-NLS-1$ //$NON-NLS-2$
42                    southRoomDescription.setText(viewElement.getText());
43                }
44                if (viewElement.getAttributeValue("direction").equals("west")) { //$NON-NLS-1$ //$NON-NLS-2$
45                    westRoomDescription.setText(viewElement.getText());
46                }
47                if (viewElement.getAttributeValue("direction").equals("above")) { //$NON-NLS-1$ //$NON-NLS-2$
48                    upRoomDescription.setText(viewElement.getText());
49                }
50                if (viewElement.getAttributeValue("direction").equals("below")) { //$NON-NLS-1$ //$NON-NLS-2$
51                    downRoomDescription.setText(viewElement.getText());
52                }
53            }
54
55        }
56        Element ways = room.getChild("ways"); //$NON-NLS-1$
57        if (ways != null) {
58            List wayList = ways.getChildren("way"); //$NON-NLS-1$
59            for (Iterator wayIter = wayList.iterator(); wayIter.hasNext();) {
60                Element wayElement = (Element) wayIter.next();
61                if (wayElement.getAttributeValue("direction").equals("north")) { //$NON-NLS-1$ //$NON-NLS-2$
62                    northRoomCombo.select(northRoomCombo.indexOf(wayElement
63                            .getAttributeValue("room"))); //$NON-NLS-1$
64                }
65                if (wayElement.getAttributeValue("direction").equals("east")) { //$NON-NLS-1$ //$NON-NLS-2$
66                    eastRoomCombo.select(eastRoomCombo.indexOf(wayElement
67                            .getAttributeValue("room"))); //$NON-NLS-1$
68                }
69                if (wayElement.getAttributeValue("direction").equals("south")) { //$NON-NLS-1$ //$NON-NLS-2$
70                    southRoomCombo.select(southRoomCombo.indexOf(wayElement
71                            .getAttributeValue("room"))); //$NON-NLS-1$
72                }
73                if (wayElement.getAttributeValue("direction").equals("west")) { //$NON-NLS-1$ //$NON-NLS-2$
74                    westRoomCombo.select(westRoomCombo.indexOf(wayElement
75                            .getAttributeValue("room"))); //$NON-NLS-1$
76                }
77                if (wayElement.getAttributeValue("direction").equals("above")) { //$NON-NLS-1$ //$NON-NLS-2$
78                    upRoomCombo.select(upRoomCombo.indexOf(wayElement
79                            .getAttributeValue("room"))); //$NON-NLS-1$
80                }
81                if (wayElement.getAttributeValue("direction").equals("below")) { //$NON-NLS-1$ //$NON-NLS-2$
82                    downRoomCombo.select(downRoomCombo.indexOf(wayElement
83                            .getAttributeValue("room"))); //$NON-NLS-1$
84                }
85            }
86
87        }
88    }
89
90    public void clear() {
91        northRoomCombo.removeAll();
92        northRoomDescription.setText(""); //$NON-NLS-1$
93
94        eastRoomCombo.removeAll();
95        eastRoomDescription.setText(""); //$NON-NLS-1$
96
97        southRoomCombo.removeAll();
98        southRoomDescription.setText(""); //$NON-NLS-1$
99
00        westRoomCombo.removeAll();
01        westRoomDescription.setText(""); //$NON-NLS-1$
02
03        upRoomCombo.removeAll();
04        upRoomDescription.setText(""); //$NON-NLS-1$
05
06        downRoomCombo.removeAll();
07        downRoomDescription.setText(""); //$NON-NLS-1$
08
09        room = null;
10    }
11
12    public void dispose() {
13        if (room != null) {
14
15            Element views = new Element("views"); //$NON-NLS-1$
16            Element ways = new Element("ways"); //$NON-NLS-1$
17
18            if (!northRoomCombo.getText().equals("")) { //$NON-NLS-1$
19                Element way = new Element("way"); //$NON-NLS-1$
20                way.setAttribute("direction", "north"); //$NON-NLS-1$ //$NON-NLS-2$
21                way.setAttribute("room", northRoomCombo.getText()); //$NON-NLS-1$
22                ways.addContent(way);
23            }
24            if (!northRoomDescription.getText().equals("")) { //$NON-NLS-1$
25                Element view = new Element("view"); //$NON-NLS-1$
26                view.setAttribute("direction", "north"); //$NON-NLS-1$ //$NON-NLS-2$
27                view.setText(northRoomDescription.getText());
28                views.addContent(view);
29            }
30
31            if (!eastRoomCombo.getText().equals("")) { //$NON-NLS-1$
32                Element way = new Element("way"); //$NON-NLS-1$
33                way.setAttribute("direction", "east"); //$NON-NLS-1$ //$NON-NLS-2$
34                way.setAttribute("room", eastRoomCombo.getText()); //$NON-NLS-1$
35                ways.addContent(way);
36            }
37            if (!eastRoomDescription.getText().equals("")) { //$NON-NLS-1$
38                Element view = new Element("view"); //$NON-NLS-1$
39                view.setAttribute("direction", "east"); //$NON-NLS-1$ //$NON-NLS-2$
40                view.setText(eastRoomDescription.getText());
41                views.addContent(view);
42            }
43
44            if (!southRoomCombo.getText().equals("")) { //$NON-NLS-1$
45                Element way = new Element("way"); //$NON-NLS-1$
46                way.setAttribute("direction", "south"); //$NON-NLS-1$ //$NON-NLS-2$
47                way.setAttribute("room", southRoomCombo.getText()); //$NON-NLS-1$
48                ways.addContent(way);
49            }
50            if (!southRoomDescription.getText().equals("")) { //$NON-NLS-1$
51                Element view = new Element("view"); //$NON-NLS-1$
52                view.setAttribute("direction", "south"); //$NON-NLS-1$ //$NON-NLS-2$
53                view.setText(southRoomDescription.getText());
54                views.addContent(view);
55            }
56
57            if (!westRoomCombo.getText().equals("")) { //$NON-NLS-1$
58                Element way = new Element("way"); //$NON-NLS-1$
59                way.setAttribute("direction", "west"); //$NON-NLS-1$ //$NON-NLS-2$
60                way.setAttribute("room", westRoomCombo.getText()); //$NON-NLS-1$
61                ways.addContent(way);
62            }
63            if (!westRoomDescription.getText().equals("")) { //$NON-NLS-1$
64                Element view = new Element("view"); //$NON-NLS-1$
65                view.setAttribute("direction", "west"); //$NON-NLS-1$ //$NON-NLS-2$
66                view.setText(westRoomDescription.getText());
67                views.addContent(view);
68            }
69            if (!upRoomCombo.getText().equals("")) { //$NON-NLS-1$
70                Element way = new Element("way"); //$NON-NLS-1$
71                way.setAttribute("direction", "above"); //$NON-NLS-1$ //$NON-NLS-2$
72                way.setAttribute("room", upRoomCombo.getText()); //$NON-NLS-1$
73                ways.addContent(way);
74            }
75            if (!upRoomDescription.getText().equals("")) { //$NON-NLS-1$
76                Element view = new Element("view"); //$NON-NLS-1$
77                view.setAttribute("direction", "above"); //$NON-NLS-1$ //$NON-NLS-2$
78                view.setText(upRoomDescription.getText());
79                views.addContent(view);
80            }
81            if (!downRoomCombo.getText().equals("")) { //$NON-NLS-1$
82                Element way = new Element("way"); //$NON-NLS-1$
83                way.setAttribute("direction", "below"); //$NON-NLS-1$ //$NON-NLS-2$
84                way.setAttribute("room", downRoomCombo.getText()); //$NON-NLS-1$
85                ways.addContent(way);
86            }
87            if (!downRoomDescription.getText().equals("")) { //$NON-NLS-1$
88                Element view = new Element("view"); //$NON-NLS-1$
89                view.setAttribute("direction", "below"); //$NON-NLS-1$ //$NON-NLS-2$
90                view.setText(downRoomDescription.getText());
91                views.addContent(view);
92            }
93
94            room.removeChildren("views"); //$NON-NLS-1$
95            room.removeChildren("ways"); //$NON-NLS-1$
96
97            if (ways.getChildren().size() > 0) {
98                room.addContent(ways);
99            }
00            if (views.getChildren().size() > 0) {
01                room.addContent(views);
02            }
03        }
04
05        super.dispose();
06    }
07
08}