1 /*
2  *  CVS: $Id: CharacterDetailsComposite.java,v 1.11 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.ArrayList;
26import java.util.Iterator;
27import java.util.List;
28
29import org.eclipse.swt.SWT;
30import org.eclipse.swt.events.SelectionEvent;
31import org.eclipse.swt.events.SelectionListener;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Button;
35import org.eclipse.swt.widgets.Combo;
36import org.eclipse.swt.widgets.Composite;
37import org.eclipse.swt.widgets.Group;
38import org.eclipse.swt.widgets.Label;
39import org.eclipse.swt.widgets.Shell;
40import org.eclipse.swt.widgets.Text;
41import org.jdom.Content;
42import org.jdom.Element;
43import org.jdom.Parent;
44
45/**
46 * 
47 * Created on Jul 12, 2004
48 * 
49 * 
50 * @version $Revision: 1.11 $
51 */
52public class CharacterDetailsComposite extends Composite {
53
54    Text description;
55
56    Combo action;
57
58    List actions; // Element lists;
59
60    Element character;
61
62    PropertyComposite properties;
63
64    private Button bt1;
65
66    private Button bt2;
67
68    /**
69     * @param arg0
70     * @param arg1
71     */
72    public CharacterDetailsComposite(Composite arg0, int arg1) {
73        super(arg0, arg1);
74
75        actions = new ArrayList();
76        character = new Element("person"); //$NON-NLS-1$
77
78        // layout the Composite
79        GridData layoutGridData = new GridData(GridData.FILL_BOTH);
80        layoutGridData.horizontalSpan = 4;
81        this.setLayoutData(layoutGridData);
82        GridLayout layout = new GridLayout(1, false);
83        this.setLayout(layout);
84
85        // layout the main group
86        final Group group = new Group(this, SWT.NONE);
87        GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
88        group.setLayoutData(gridData);
89        GridLayout groupLayout = new GridLayout(2, false);
90        group.setLayout(groupLayout);
91
92        new Label((Composite) group, SWT.NONE).setText(Messages.getString("DESCRIPTION_COLON")); //$NON-NLS-1$
93        description = new Text((Composite) group, SWT.BORDER);
94        description.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
95
96        properties = new PropertyComposite((Composite) this, SWT.NONE);
97
98        // layout the button group
99
00        final Group buttonGroup = new Group(this, SWT.NONE);
01        GridData buttonGridData = new GridData(GridData.FILL_HORIZONTAL);
02        buttonGridData.horizontalSpan = 1;
03        buttonGroup.setLayoutData(buttonGridData);
04        GridLayout buttonGroupLayout = new GridLayout(2, false);
05        buttonGroup.setLayout(buttonGroupLayout);
06
07        bt1 = new Button((Composite) buttonGroup, SWT.NONE);
08        bt1.setText(Messages.getString("EDIT_EVENTS")); //$NON-NLS-1$
09        bt1.addSelectionListener(new SelectionListener() {
10
11            public void widgetSelected(SelectionEvent arg0) {
12                EventEditorDialog ed = new EventEditorDialog(new Shell(arg0.display));
13                ed.open(character.getChildren("event")); //$NON-NLS-1$
14            }
15
16            public void widgetDefaultSelected(SelectionEvent arg0) {
17            }
18        });
19
20        bt2 = new Button((Composite) buttonGroup, SWT.PUSH);
21        bt2.setText(Messages.getString("EDIT_DIALOGS")); //$NON-NLS-1$
22        bt2.addSelectionListener(new SelectionListener() {
23
24            public void widgetSelected(SelectionEvent arg0) {
25                DialogEditorDialog ed = new DialogEditorDialog(new Shell(arg0.display));
26                ed.open(getDialogForCharacter());
27                
28            }
29
30            public void widgetDefaultSelected(SelectionEvent arg0) {
31            }
32        });
33        group.layout();
34    }
35
36    public void showCharacterElement(Element character) {
37        if (character != null) {
38            this.enable();
39            this.character = character;
40            String desc = character.getChildText("description"); //$NON-NLS-1$
41            if (desc != null && !desc.equals("")) { //$NON-NLS-1$
42                description.setText(desc);
43            } else {
44                description.setText(""); //$NON-NLS-1$
45                description.setFocus();
46            }
47            description.setSelection(description.getText().length());
48            properties.showProperties(character.getChildren("property")); //$NON-NLS-1$
49
50        } else {
51            this.clean();
52        }
53    }
54
55    public void updateElement() {
56        if (character != null) {
57            enable();
58            Element e = character.getChild("description"); //$NON-NLS-1$
59            if (e != null) {
60                e.setText(description.getText());
61            } else {
62                character.addContent(new Element("description").setText(description.getText())); //$NON-NLS-1$
63            }
64            character.removeChildren("property"); //$NON-NLS-1$
65            character.addContent(properties.getElementList());
66        } else {
67            disable();
68        }
69    }
70
71    protected Element getDialogForCharacter() {
72        String name = character.getAttributeValue("name"); //$NON-NLS-1$
73        if (name == null) return null;
74        Parent gamefile = character;
75        
76        List contents = JdomHelpers.getFirstLevelElements(character);
77        Element dialogs = null;
78        for (Iterator contentIter = contents.iterator(); contentIter.hasNext();) {
79            Content jdomContent = (Content) contentIter.next();
80            if (jdomContent instanceof Element) {
81                Element jdomElement = (Element) jdomContent;
82                if (jdomElement.getName().equals("dialogs")) { //$NON-NLS-1$
83                    dialogs = jdomElement;
84                    break;
85                }
86            }
87        }
88        if (dialogs != null) {
89            System.err.println("Dialog != null"); //$NON-NLS-1$
90            List dialogOwner = dialogs.getChildren("dialog-owner"); //$NON-NLS-1$
91            for (Iterator dialogOwnerIter = dialogOwner.iterator(); dialogOwnerIter.hasNext();) {
92                Element ownerElement = (Element) dialogOwnerIter.next();
93                if (ownerElement.getAttributeValue("name").equals(name)) { return ownerElement; } //$NON-NLS-1$
94            }
95        }
96        Element newEl = new Element("dialog-owner"); //$NON-NLS-1$
97        newEl.setAttribute("name", name); //$NON-NLS-1$
98       
99        dialogs.addContent(newEl);
00       
01        return newEl;
02    }
03
04    protected void clean() {
05        this.description.setText(""); //$NON-NLS-1$
06        if (this.action != null) this.action.select(0);
07        this.actions = null;
08        this.character = null;
09        this.properties.showProperties(null);
10        disable();
11    }
12    
13    public void enable() {
14        setEnabled(true);
15    }
16    
17    public void disable() {
18        setEnabled(false);
19    }
20    
21    public void setEnabled(boolean enabled) {
22        description.setEnabled(enabled);
23        if (action != null) {
24            action.setEnabled(enabled);
25        }
26        properties.setEnabled(enabled);
27        bt1.setEnabled(enabled);
28        bt2.setEnabled(enabled);
29        properties.setEnabled(enabled);
30    }
31
32}