1 /*
2  *  CVS: $Id: PropertyComposite.java,v 1.8 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.ModifyEvent;
31import org.eclipse.swt.events.ModifyListener;
32import org.eclipse.swt.events.SelectionEvent;
33import org.eclipse.swt.events.SelectionListener;
34import org.eclipse.swt.layout.GridData;
35import org.eclipse.swt.layout.GridLayout;
36import org.eclipse.swt.widgets.Button;
37import org.eclipse.swt.widgets.Composite;
38import org.eclipse.swt.widgets.Label;
39import org.eclipse.swt.widgets.MessageBox;
40import org.eclipse.swt.widgets.Shell;
41import org.eclipse.swt.widgets.Text;
42import org.jdom.Element;
43
44/**
45 * 
46 * Created on Jul 12, 2004
47 * 
48 * 
49 * @version $Revision: 1.8 $
50 */
51
52public class PropertyComposite extends Composite {
53    protected Button useable, takeable, deleteonuse;
54
55    protected Text size;
56
57    protected List propertyElements;
58
59    protected final static String SIZE_TIP = Messages.getString("SIZE_TIP"); //$NON-NLS-1$
60
61    protected final static String USEABLE_TIP = Messages.getString("USEABLE_TIP"); //$NON-NLS-1$
62
63    protected final static String TAKEABLE_TIP = Messages.getString("TAKEABLE_TIP"); //$NON-NLS-1$
64
65    protected final static String DELETONUSE_TIP = Messages.getString("DELETEONUSE_TIP"); //$NON-NLS-1$
66
67    protected class CheckInteger implements ModifyListener {
68        public void modifyText(ModifyEvent arg0) {
69            if (size.getText().length() > 0) {
70                try {
71                    int value = Integer.parseInt(size.getText());
72                    System.err.println("value is " + value); //$NON-NLS-1$
73                } catch (NumberFormatException e) {
74                    MessageBox mb = new MessageBox(new Shell(arg0.display),
75                            SWT.ICON_ERROR | SWT.OK);
76                    mb.setText(Messages.getString("GAMEDITOR_ERROR")); //$NON-NLS-1$
77                    mb
78                            .setMessage(Messages.getString("SIZE_ERROR")); //$NON-NLS-1$
79                    mb.open();
80                    size.setText("0"); //$NON-NLS-1$
81                }
82                System.err.println("ModifyEvent: " + arg0.toString()); //$NON-NLS-1$
83            }
84        }
85
86    }
87
88    /**
89     * @param arg0
90     * @param arg1
91     */
92    public PropertyComposite(Composite arg0, int arg1) {
93        super(arg0, arg1);
94
95        //this.setText("Properties:");
96        GridData propGridData = new GridData(GridData.FILL_HORIZONTAL
97                | GridData.FILL_VERTICAL);
98        this.setLayoutData(propGridData);
99        GridLayout thisLayout = new GridLayout(3, false);
00        this.setLayout(thisLayout);
01
02        Label sizel = new Label((Composite) this, SWT.NONE);
03        sizel.setText(Messages.getString("SIZE")); //$NON-NLS-1$
04        sizel.setToolTipText(PropertyComposite.SIZE_TIP);
05        size = new Text((Composite) this, SWT.BORDER);
06        GridData sizeLayoutData = new GridData(GridData.FILL_HORIZONTAL);
07        sizeLayoutData.horizontalSpan = 2;
08        size.setLayoutData(sizeLayoutData);
09        size.setText("0"); //$NON-NLS-1$
10        size.addModifyListener(new CheckInteger());
11        size.setToolTipText(PropertyComposite.SIZE_TIP);
12
13        useable = new Button((Composite) this, SWT.CHECK);
14        useable.setToolTipText(PropertyComposite.USEABLE_TIP);
15        useable.setText(Messages.getString("USEABLE")); //$NON-NLS-1$
16        useable.addSelectionListener(new SelectionListener() {
17
18            public void widgetSelected(SelectionEvent e) {
19                if (useable.getSelection()) {
20                    deleteonuse.setEnabled(true);
21                } else {
22                    deleteonuse.setEnabled(false);
23                }
24
25            }
26
27            public void widgetDefaultSelected(SelectionEvent e) {
28                // das soll leer sein
29
30            }
31
32        });
33
34        deleteonuse = new Button((Composite) this, SWT.CHECK);
35        deleteonuse.setToolTipText(PropertyComposite.DELETONUSE_TIP);
36        deleteonuse.setText(Messages.getString("DELETE_ON_USE")); //$NON-NLS-1$
37        deleteonuse.setEnabled(false);
38        
39        takeable = new Button((Composite) this, SWT.CHECK);
40        takeable.setToolTipText(PropertyComposite.TAKEABLE_TIP);
41        takeable.setText(Messages.getString("TAKEABLE")); //$NON-NLS-1$
42
43    }
44
45    public void showProperties(List propertyElements) {
46        this.propertyElements = propertyElements;
47        if (propertyElements == null) {
48            this.clear();
49            return;
50        }
51        setEnabled(true);
52        String sizeText = getPropertyValue("size"); //$NON-NLS-1$
53        if (sizeText == null || sizeText == "") { sizeText = "0"; } //$NON-NLS-1$ //$NON-NLS-2$
54        size.setText(sizeText);
55        useable.setSelection(getBoolProperty("useable")); //$NON-NLS-1$
56        takeable.setSelection(getBoolProperty("takeable")); //$NON-NLS-1$
57        if (!getBoolProperty("useable")) { //$NON-NLS-1$
58            deleteonuse.setEnabled(false);
59        } else {
60            deleteonuse.setEnabled(true);
61            deleteonuse.setSelection(getBoolProperty("deleteonuse")); //$NON-NLS-1$
62        }
63
64    }
65
66    public String getPropertyValue(String property) {
67        for (Iterator propIter = propertyElements.iterator(); propIter
68                .hasNext();) {
69            Element element = (Element) propIter.next();
70            if (element.getAttributeValue("name").equals(property)) //$NON-NLS-1$
71                return element.getAttributeValue("value"); //$NON-NLS-1$
72        }
73        return ""; //$NON-NLS-1$
74    }
75
76    public boolean getBoolProperty(String property) {
77        String val = getPropertyValue(property);
78        if (val != null) {
79            return val.equals("true"); //$NON-NLS-1$
80        } else {
81            return false;
82        }
83    }
84
85    public List getElementList() {
86        List retlist = new ArrayList();
87        retlist.add(getButtonProperty("useable", useable)); //$NON-NLS-1$
88        retlist.add(getButtonProperty("takeable", takeable)); //$NON-NLS-1$
89        retlist.add(getButtonProperty("deleteonuse", deleteonuse)); //$NON-NLS-1$
90        Element sizeEl = new Element("property"); //$NON-NLS-1$
91        sizeEl.setAttribute("name", "size"); //$NON-NLS-1$ //$NON-NLS-2$
92        if (size.getText().length() > 0) {
93            sizeEl.setAttribute("value", size.getText()); //$NON-NLS-1$
94        } else {
95            sizeEl.setAttribute("value", "0"); //$NON-NLS-1$ //$NON-NLS-2$
96        }
97        retlist.add(sizeEl);
98        return retlist;
99    }
00
01    protected Element getButtonProperty(String name, Button button) {
02        Element retval = new Element("property"); //$NON-NLS-1$
03        retval.setAttribute("name", name); //$NON-NLS-1$
04        if (button.getSelection()) {
05            retval.setAttribute("value", "true"); //$NON-NLS-1$ //$NON-NLS-2$
06        } else {
07            retval.setAttribute("value", "false"); //$NON-NLS-1$ //$NON-NLS-2$
08        }
09        return retval;
10    }
11
12    public void clear() {
13        this.useable.setSelection(false);
14        this.takeable.setSelection(false);
15        this.deleteonuse.setSelection(false);
16        this.size.setText("0"); //$NON-NLS-1$
17        this.propertyElements = null;
18        setEnabled(false);
19    }
20
21    public void setEnabled(boolean enabled) {
22        this.useable.setEnabled(enabled);
23        this.takeable.setEnabled(enabled);
24        this.size.setEnabled(enabled);
25
26    }
27
28}