1 /*
2  *  CVS: $Id: ItemEditorComposite.java,v 1.14 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.text.MessageFormat;
26import java.util.Iterator;
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.Composite;
34import org.eclipse.swt.widgets.Event;
35import org.eclipse.swt.widgets.Group;
36import org.eclipse.swt.widgets.List;
37import org.eclipse.swt.widgets.Menu;
38import org.eclipse.swt.widgets.MenuItem;
39import org.eclipse.swt.widgets.MessageBox;
40import org.eclipse.swt.widgets.Shell;
41import org.jdom.Element;
42
43
44/**
45  * 
46 * Created on Jul 15, 2004
47 * 
48 * 
49 * @version $Revision: 1.14 $
50 */
51public class ItemEditorComposite extends Composite {
52    private List itemsList;
53    private ItemDetailsComposite itemComp;
54    private Element gameobj;
55
56
57    
58    /**
59     * @param arg0
60     * @param arg1
61     */
62    public ItemEditorComposite(Composite arg0, int arg1) {
63        super(arg0, arg1);
64        final GridLayout gridLayout = new GridLayout();
65
66        gridLayout.makeColumnsEqualWidth = true;
67        gridLayout.numColumns = 5;
68        this.setLayout(gridLayout);
69    
70        {
71            final Group group_1 = new Group(this, SWT.NONE);
72            group_1.setText(Messages.getString("ITEMS")); //$NON-NLS-1$
73
74            group_1.setLayout(new GridLayout());
75            group_1.setLayoutData(
76                new GridData(
77                    GridData.HORIZONTAL_ALIGN_FILL
78                        | GridData.VERTICAL_ALIGN_FILL));
79
80            {
81                itemsList =
82                    new List(
83                        group_1,
84                        SWT.BORDER
85                            | SWT.H_SCROLL
86                            | SWT.V_SCROLL
87                            | SWT.PUSH);
88                itemsList.setLayoutData(
89                    new GridData(GridData.FILL_BOTH));
90    
91                itemsList
92                .addSelectionListener(new SelectionListener() {
93
94                    public void widgetSelected(SelectionEvent e) {
95                        int index = itemsList.getSelectionIndex();
96                        if (index >= 0) { 
97                            itemComp.showItemElement(getItem(itemsList.getItem(index)));
98                        } else {
99                            itemComp.showItemElement(null);
00                        }
01                    }
02
03
04                    public void widgetDefaultSelected(SelectionEvent e) {
05                        // Doppelklick
06                    }
07
08                });
09                itemsList.setToolTipText(Messages.getString("ITEMS_TOOLTIP")); //$NON-NLS-1$
10
11                {
12                    Menu popupmenu = new Menu(itemsList);
13                    itemsList.setMenu(popupmenu);
14                    {
15                        final MenuItem popupItem =
16                            new MenuItem(popupmenu, SWT.NONE);
17                        popupItem.setText(Messages.getString("ADD")); //$NON-NLS-1$
18                        popupItem.addSelectionListener(new SelectionListener() {
19
20                            public void widgetSelected(SelectionEvent arg0) {
21                                InputDialog id = new InputDialog(new Shell(arg0.display));
22                                id.setMessage(Messages.getString("ENTER_ITEM_NAME")); //$NON-NLS-1$
23                                String result = id.openNoWhitespace();
24                                if (result != null && (!result.equals(""))) { //$NON-NLS-1$
25                                    if (itemsList.indexOf(result) == -1) {
26                                    addItem(result);
27                                    itemComp.enable();
28                                    } else {
29                                        Object[] formatArgs = { result};
30                                        MessageBox mb = new MessageBox(new Shell(arg0.display), SWT.ICON_ERROR);
31                                        mb.setMessage(MessageFormat.format(Messages.getString("ITEM_SAME_NAME_ERROR"), formatArgs)); //$NON-NLS-1$
32                                        mb.open();
33                                    }
34                                }
35                            }
36
37                            public void widgetDefaultSelected(SelectionEvent arg0) {}
38                        });
39                    }
40                    {
41                        final MenuItem popupItem =
42                            new MenuItem(popupmenu, SWT.NONE);
43                        popupItem.setText(Messages.getString("RENAME")); //$NON-NLS-1$
44                        popupItem.addSelectionListener(new SelectionListener() {
45
46                            public void widgetSelected(SelectionEvent arg0) {
47                                InputDialog id = new InputDialog(new Shell(arg0.display));
48                                if (id != null && itemsList.getSelectionIndex() > -1) {
49                                    String oldname = itemsList.getItem(itemsList.getSelectionIndex());
50                                    id.setMessage(Messages.getString("ENTER_ITEM_RENAME") + oldname); //$NON-NLS-1$
51                                    id.setDefaultValue(oldname);
52                                    renameItem(oldname,id.openNoWhitespace());
53                                }
54                            }
55
56                            public void widgetDefaultSelected(SelectionEvent arg0) {}
57                        });
58                    }
59                    {
60                        final MenuItem popupItem =
61                            new MenuItem(popupmenu, SWT.NONE);
62                        popupItem.setText(Messages.getString("DELETE")); //$NON-NLS-1$
63                        popupItem.addSelectionListener(new SelectionListener() {
64
65                            public void widgetSelected(SelectionEvent arg0) {
66                                try{
67                                String name = itemsList.getItem(itemsList.getSelectionIndex());
68                                //TODO add are U sure
69                                deleteItem(name);
70                                }catch (Exception e) {
71                                    // System.err.println("Silent Catch: \nprevented nullpointer expection (deleting null-object)\n"+e.getLocalizedMessage());
72                                }
73
74                            }
75
76                            public void widgetDefaultSelected(SelectionEvent arg0) {}
77                        });
78                    }
79                }
80            }
81        }
82        {
83            itemComp = new ItemDetailsComposite(this,SWT.NULL);
84            GridData gridData = new GridData(GridData.FILL_BOTH);
85            gridData.horizontalSpan = 4;
86            GridLayout layout = new GridLayout();
87            layout.numColumns = 2;
88        }
89    }
90    
91    
92
93    public Element getItem(String name) {
94        if (gameobj != null) {
95            Element items = gameobj.getChild("items"); //$NON-NLS-1$
96            if (items != null) {
97                java.util.List itemList = items.getChildren("gameitem"); //$NON-NLS-1$
98                for (Iterator goIter = itemList.iterator(); goIter.hasNext();) {
99                    Element element = (Element) goIter.next();
00                    
01                    if (element.getAttributeValue("name").equals(name)) { //$NON-NLS-1$
02                        return element;
03                    }
04                }
05            }
06        }
07        return null;
08    }
09    
10    public void addItem(String name) {
11        Element newItem = new Element("gameitem"); //$NON-NLS-1$
12        newItem.setAttribute("name", name); //$NON-NLS-1$
13        newItem.addContent(new Element("description")); //$NON-NLS-1$
14        Element items = gameobj.getChild("items"); //$NON-NLS-1$
15        items.addContent(newItem);
16        showItemList();
17        itemComp.enable();
18        itemsList.select(itemsList.indexOf(name));
19        itemsList.notifyListeners(SWT.Selection, new Event());
20    }
21    
22    public void renameItem(String oldname, String newname) {
23        Element item = getItem(oldname);
24        item.setAttribute("name",newname); //$NON-NLS-1$
25        JdomHelpers.deepObjectRename(oldname, newname);
26        showItemList();
27    }
28    
29    public void deleteItem(String name) {
30        Element item = getItem(name);
31        item.detach();
32        JdomHelpers.deepObjectDelete(name);
33        showItemList();
34    }
35
36    public void showItems(Element gameobjects) {
37        this.gameobj = gameobjects;
38        showItemList();
39    }
40    
41
42    protected void showItemList() {
43        String[] items = getItems();
44        if (items != null) {
45            itemsList.setItems(items);
46            itemsList.select(itemsList.getTopIndex());
47            itemsList.notifyListeners(SWT.Selection, new Event());
48        } else {
49            itemComp.clear();
50            itemsList.removeAll();
51        }
52    }
53
54    protected String[] getItems() {
55        String[] retval = null;
56        if (gameobj != null) {
57            Element items = gameobj.getChild("items"); //$NON-NLS-1$
58            if (items != null) {
59                java.util.List itemList = items.getChildren("gameitem"); //$NON-NLS-1$
60                retval = new String[itemList.size()];
61                int count = 0;
62                for (Iterator goIter = itemList.iterator(); goIter.hasNext();) {
63                    Element element = (Element) goIter.next();
64                    retval[count++] = element.getAttributeValue("name"); //$NON-NLS-1$
65                }
66            }
67        }
68
69        return retval;
70    }
71    
72    public void updateData() {
73        this.itemsList.notifyListeners(SWT.Selection, new Event());
74    }
75
76}
77