1 /*
2  *  CVS: $Id: ItemDetailsComposite.java,v 1.9 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.Arrays;
27import java.util.Iterator;
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.Composite;
36import org.eclipse.swt.widgets.Group;
37import org.eclipse.swt.widgets.Label;
38import org.eclipse.swt.widgets.MessageBox;
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.9 $
51 */
52public class ItemDetailsComposite extends Composite {
53
54    protected Text description;
55
56    protected PropertyComposite properties;
57
58    protected Element item;
59
60    private Button bt2;
61
62    private Button bt1;
63
64    /**
65     * @param arg0
66     * @param arg1
67     */
68    public ItemDetailsComposite(Composite arg0, int arg1) {
69        super(arg0, arg1);
70
71        // layout the Composite
72        GridData layoutGridData = new GridData(GridData.FILL_BOTH);
73        layoutGridData.horizontalSpan = 4;
74        this.setLayoutData(layoutGridData);
75        GridLayout layout = new GridLayout(1, false);
76        this.setLayout(layout);
77
78        // layout the main group
79        final Group group = new Group(this, SWT.NONE);
80        GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
81        group.setLayoutData(gridData);
82        GridLayout groupLayout = new GridLayout(2, false);
83        group.setLayout(groupLayout);
84
85        new Label((Composite) group, SWT.NONE).setText(Messages.getString("DESCRIPTION_COLON")); //$NON-NLS-1$
86        description = new Text((Composite) group, SWT.BORDER);
87        description.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88
89        properties = new PropertyComposite((Composite) this, SWT.NONE);
90
91        // layout the button group
92
93        final Group buttonGroup = new Group(this, SWT.NONE);
94        GridData buttonGridData = new GridData(GridData.FILL_HORIZONTAL);
95        buttonGridData.horizontalSpan = 1;
96        buttonGroup.setLayoutData(buttonGridData);
97        GridLayout buttonGroupLayout = new GridLayout(2, false);
98        buttonGroup.setLayout(buttonGroupLayout);
99
00        bt2 = new Button((Composite) buttonGroup, SWT.NONE);
01        bt2.setText(Messages.getString("EDIT_EVENTS")); //$NON-NLS-1$
02        bt2.addSelectionListener(new SelectionListener() {
03
04            public void widgetSelected(SelectionEvent arg0) {
05                java.util.List events = item.getChildren("event"); //$NON-NLS-1$
06                EventEditorDialog ed = new EventEditorDialog(new Shell(arg0.display));
07                ed.open(events);
08
09            }
10
11            public void widgetDefaultSelected(SelectionEvent arg0) {}
12        });
13
14        bt1 = new Button((Composite) buttonGroup, SWT.NONE);
15        bt1.setText(Messages.getString("EDIT_COMBINATIONS")); //$NON-NLS-1$
16        bt1.addSelectionListener(new SelectionListener() {
17
18            public void widgetSelected(SelectionEvent arg0) {
19                String[] items = getItemNames();
20                if (items != null && items.length > 1) {
21                CombinationEditorDialog ce = new CombinationEditorDialog(new Shell(arg0.display));
22                ce.open(item, items);
23                } else {
24                    MessageBox mb = new MessageBox(new Shell(arg0.display), SWT.ICON_ERROR);
25                    mb.setMessage(Messages.getString("COMB_ERROR_MSG")); //$NON-NLS-1$
26                    mb.open();
27                    return;
28                }
29                
30            }
31
32            public void widgetDefaultSelected(SelectionEvent arg0) {}
33        });
34
35        group.layout();
36    }
37
38    public void showItemElement(Element item) {
39        cleanUpChanges();
40        this.item = item;
41        if (item == null) {
42            this.clear();
43            this.disable();
44            return;
45        }
46        this.enable();
47        if (item.getChildText("description") != null && !item.getChildText("description").equals("")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
48            description.setText(item.getChildText("description")); //$NON-NLS-1$
49        } else {
50            description.setText(""); //$NON-NLS-1$
51            description.setFocus();
52        }
53        properties.showProperties(item.getChildren("property")); //$NON-NLS-1$
54        
55    }
56
57    public void cleanUpChanges() {
58        if (item == null) return;
59        item.removeChildren("property"); //$NON-NLS-1$
60        item.addContent(properties.getElementList());
61        Element descr = item.getChild("description"); //$NON-NLS-1$
62        descr.setText(description.getText());
63
64    }
65
66    protected String[] getItemNames() {
67        if (item == null) return null;
68        java.util.List names = new ArrayList();
69
70        Parent p = item.getParent();
71        java.util.List contents = p.getContent();
72
73        for (Iterator contentIter = contents.iterator(); contentIter.hasNext();) {
74            Content contentElement = (Content) contentIter.next();
75            if (contentElement instanceof Element) {
76                String name = ((Element) contentElement).getAttributeValue("name"); //$NON-NLS-1$
77                System.err.println("Found new Element: " + name); //$NON-NLS-1$
78                names.add(name);
79            }
80        }
81        String[] retval = new String[names.size()];
82        names.toArray(retval);
83        Arrays.sort(retval);
84        return retval;
85    }
86
87    protected void clear() {
88        description.setText(""); //$NON-NLS-1$
89        item = null;
90        properties.showProperties(null);
91    }
92    
93    public void disable() {
94        setEnabled(false);    
95    }
96    
97    public void enable() {
98        setEnabled(true);
99    }
00    
01    public void setEnabled(boolean enabled) {
02        bt1.setEnabled(enabled);
03        bt2.setEnabled(enabled);
04        description.setEnabled(enabled);
05    }
06
07}