1 /*
2  *  CVS: $Id: RoomDetailsComposite.java,v 1.13 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 org.eclipse.swt.SWT;
26import org.eclipse.swt.events.SelectionEvent;
27import org.eclipse.swt.events.SelectionListener;
28import org.eclipse.swt.layout.GridData;
29import org.eclipse.swt.layout.GridLayout;
30import org.eclipse.swt.widgets.Button;
31import org.eclipse.swt.widgets.Combo;
32import org.eclipse.swt.widgets.Composite;
33import org.eclipse.swt.widgets.Event;
34import org.eclipse.swt.widgets.FileDialog;
35import org.eclipse.swt.widgets.Group;
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.13 $
47 */
48public class RoomDetailsComposite extends Composite {
49
50    protected RoomPropertyComposite roomProperties;
51
52    protected Element room;
53
54    private Text description;
55
56    private Text image;
57
58    private Combo type;
59
60    private Button findButton;
61
62    private String map;
63
64    /**
65     * @param arg0
66     * @param arg1
67     */
68    public RoomDetailsComposite(Composite arg0, int arg1) {
69        super(arg0, arg1);
70        GridData myDat = new GridData(GridData.FILL_BOTH);
71        myDat.horizontalSpan = 4;
72        this.setLayoutData(myDat);
73        this.setLayout(new GridLayout(1, false));
74
75        // first group, description and image
76        Group roomProperty = new Group(this, SWT.NONE);
77        GridData rpDat = new GridData(GridData.FILL_HORIZONTAL);
78        rpDat.horizontalSpan = 1;
79        roomProperty.setLayoutData(rpDat);
80        roomProperty.setLayout(new GridLayout(4, false));
81
82        Label dLabel = new Label(roomProperty, SWT.NONE);
83        GridData dlData = new GridData(GridData.HORIZONTAL_ALIGN_END);
84        dlData.horizontalSpan = 1;
85        dLabel.setLayoutData(dlData);
86        dLabel.setText(Messages.getString("ROOM_DESCRIPTION")); //$NON-NLS-1$
87
88        description = new Text(roomProperty, SWT.BORDER);
89        GridData dData = new GridData(GridData.FILL_HORIZONTAL);
90        dData.horizontalSpan = 3;
91        description.setLayoutData(dData);
92
93        Label iLabel = new Label(roomProperty, SWT.NONE);
94        GridData ilData = new GridData(GridData.HORIZONTAL_ALIGN_END);
95        ilData.horizontalSpan = 1;
96        iLabel.setLayoutData(ilData);
97        iLabel.setText(Messages.getString("ROOM_IMAGE")); //$NON-NLS-1$
98
99        image = new Text(roomProperty, SWT.BORDER);
00        GridData iData = new GridData(GridData.FILL_HORIZONTAL);
01        iData.horizontalSpan = 2;
02        image.setLayoutData(iData);
03
04        findButton = new Button(roomProperty, SWT.PUSH);
05        GridData fbData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
06        fbData.horizontalSpan = 1;
07        findButton.setLayoutData(fbData);
08        findButton.setText(Messages.getString("SELECT")); //$NON-NLS-1$
09        findButton.addSelectionListener(new SelectionListener() {
10
11            public void widgetSelected(SelectionEvent arg0) {
12                FileDialog fd = new FileDialog(new Shell(arg0.display), SWT.OPEN);
13                fd.setText(Messages.getString("PICK_IMAGE")); //$NON-NLS-1$
14                fd.setFileName(image.getText());
15                String newName = fd.open();
16                if (newName != null) {
17                    image.setText(newName);
18                }
19
20            }
21
22            public void widgetDefaultSelected(SelectionEvent arg0) {
23            }
24        });
25
26        Label tLabel = new Label(roomProperty, SWT.NONE);
27        GridData tlData = new GridData(GridData.HORIZONTAL_ALIGN_END);
28        tlData.horizontalSpan = 1;
29        tLabel.setLayoutData(tlData);
30        tLabel.setText(Messages.getString("TYPE_ROOM")); //$NON-NLS-1$
31
32        type = new Combo(roomProperty, SWT.READ_ONLY);
33        GridData tcData = new GridData(GridData.FILL_HORIZONTAL);
34        tcData.horizontalSpan = 1;
35        type.setLayoutData(tcData);
36        type.add("room"); //$NON-NLS-1$
37        type.add("transitionroom"); //$NON-NLS-1$
38        type.add("beamroom"); //$NON-NLS-1$
39        type.select(0);
40        type.addSelectionListener(new SelectionListener() {
41
42            public void widgetSelected(SelectionEvent arg0) {
43                if (roomProperties != null) roomProperties.dispose();
44                if (type.getText().equals("room")) { //$NON-NLS-1$
45                    roomProperties = new DefaultRoomPropertyComposite((Composite) RoomDetailsComposite.this,
46                            SWT.NONE, room, map);
47                    if (room != null) {
48                        room.setName("room"); //$NON-NLS-1$
49                        room.removeAttribute("final"); //$NON-NLS-1$
50                        room.removeAttribute("target"); //$NON-NLS-1$
51                        room.setAttribute("class", "org.jzuul.engine.rooms.Room"); //$NON-NLS-1$ //$NON-NLS-2$
52                    }
53                }
54                if (type.getText().equals("transitionroom")) { //$NON-NLS-1$
55                    roomProperties = new TransitionRoomPropertyComposite((Composite) RoomDetailsComposite.this,
56                            SWT.NONE, room, map);
57                    if (room != null) {
58                        room.removeAttribute("class"); //$NON-NLS-1$
59                        room.setName("transitionroom"); //$NON-NLS-1$
60                    }
61                }
62                if (type.getText().equals("beamroom")) { //$NON-NLS-1$
63                    roomProperties = new BeamRoomPropertyComposite((Composite) RoomDetailsComposite.this, SWT.NONE,
64                            room, map);
65                    if (room != null) {
66                        room.setName("room"); //$NON-NLS-1$
67                        room.removeAttribute("final"); //$NON-NLS-1$
68                        room.removeAttribute("target"); //$NON-NLS-1$
69                        room.setAttribute("class", "org.jzuul.engine.rooms.BeamRoom"); //$NON-NLS-1$ //$NON-NLS-2$
70                    }
71                }
72                setEnabled(!type.getText().equals("transitionroom")); //$NON-NLS-1$
73                if (type.getText().equals("transitionroom")) { //$NON-NLS-1$
74                    type.setEnabled(true);
75                }
76                RoomDetailsComposite.this.layout();
77                RoomDetailsComposite.this.redraw();
78
79            }
80
81            public void widgetDefaultSelected(SelectionEvent arg0) {
82            }
83        });
84    }
85
86    protected void showRoomDetails(Element roomElement, String mapName) {
87        this.cleanUpData();
88        this.map = mapName;
89        this.room = roomElement;
90        String desc = roomElement.getChildText("description"); //$NON-NLS-1$
91        if (desc != null && !desc.equals("")) { //$NON-NLS-1$
92            this.description.setText(desc);
93        } else {
94            this.description.setText(""); //$NON-NLS-1$
95            this.description.setFocus();
96        }
97        Element imageEl = roomElement.getChild("image"); //$NON-NLS-1$
98        if (imageEl != null) {
99            this.image.setText(imageEl.getAttributeValue("file")); //$NON-NLS-1$
00        } else {
01            this.image.setText(""); //$NON-NLS-1$
02        }
03        String typeName = getRoomType(roomElement);
04        setEnabled(!typeName.equals("transitionroom")); //$NON-NLS-1$
05        type.select(type.indexOf(typeName));
06        type.notifyListeners(SWT.Selection, new Event());
07        type.setEnabled(true);
08        roomProperties.showRoomProperties();
09    }
10
11    protected String getRoomType(Element roomElement) {
12        if (roomElement.getName().equals("transitionroom")) { return "transitionroom"; } //$NON-NLS-1$ //$NON-NLS-2$
13        String roomClass = roomElement.getAttributeValue("class"); //$NON-NLS-1$
14        if (roomClass == null) return "room"; //$NON-NLS-1$
15        roomClass = roomClass.replaceAll(".+\\.", ""); //$NON-NLS-1$ //$NON-NLS-2$
16        System.err.println("Room class is " + roomClass); //$NON-NLS-1$
17        return roomClass.toLowerCase();
18    }
19
20    protected void clear() {
21        setEnabled(false);
22        if (roomProperties != null) {
23            roomProperties.dispose();
24            roomProperties = null;
25        }
26        
27        room = null;
28        description.setText(""); //$NON-NLS-1$
29        image.setText(""); //$NON-NLS-1$
30        type.select(0);
31
32    }
33
34    protected void cleanUpData() {
35        if (room != null) {
36            room.removeChildren("description"); //$NON-NLS-1$
37            if (!description.getText().equals("")) { //$NON-NLS-1$
38                room.addContent(new Element("description").setText(description.getText())); //$NON-NLS-1$
39            }
40
41            room.removeChildren("image"); //$NON-NLS-1$
42            if (!image.getText().equals("")) { //$NON-NLS-1$
43                room.addContent(new Element("image").setAttribute("file", image.getText())); //$NON-NLS-1$ //$NON-NLS-2$
44            }
45
46          
47        }
48
49    }
50
51    public void setEnabled(boolean enabled) {
52        description.setEnabled(enabled);
53        image.setEnabled(enabled);
54        type.setEnabled(enabled);
55        findButton.setEnabled(enabled);
56    }
57
58    public void enable() {
59        setEnabled(true);
60    }
61
62    public void disable() {
63        setEnabled(false);
64    }
65
66}