1 /*
2  *  CVS: $Id: ContentsEditorDialog.java,v 1.12 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;
27
28import org.eclipse.swt.SWT;
29import org.eclipse.swt.events.SelectionEvent;
30import org.eclipse.swt.events.SelectionListener;
31import org.eclipse.swt.events.ShellAdapter;
32import org.eclipse.swt.events.ShellEvent;
33import org.eclipse.swt.layout.GridData;
34import org.eclipse.swt.layout.GridLayout;
35import org.eclipse.swt.widgets.Button;
36import org.eclipse.swt.widgets.Dialog;
37import org.eclipse.swt.widgets.Display;
38import org.eclipse.swt.widgets.Event;
39import org.eclipse.swt.widgets.Group;
40import org.eclipse.swt.widgets.List;
41import org.eclipse.swt.widgets.MessageBox;
42import org.eclipse.swt.widgets.Shell;
43import org.jdom.Element;
44import org.jzuul.engine.gui.utils.Util;
45
46/**
47 * 
48 * Created on Jul 14, 2004
49 * 
50 * 
51 * @version $Revision: 1.12 $
52 */
53public class ContentsEditorDialog extends Dialog {
54
55    Element content;
56    private List characters;
57    private List contentChars;
58    private List items;
59    private List contentItems;
60    private Button charAdd;
61    private Button charDelete;
62    private Button itemsAdd;
63    private Button itemsDelete;
64
65    /**
66     * @param arg0
67     */
68    public ContentsEditorDialog(Shell arg0) {
69        super(arg0);
70    }
71
72    /**
73     * @param arg0
74     * @param arg1
75     */
76    public ContentsEditorDialog(Shell arg0, int arg1) {
77        super(arg0, arg1);
78    }
79
80    public void open(Element elementWithContents) {
81        content = elementWithContents;
82        Shell parent = getParent();
83        final Shell shell = new Shell(parent, SWT.BORDER | SWT.MIN | SWT.RESIZE | SWT.APPLICATION_MODAL);
84        shell.setText(Messages.getString("EDIT_CONTENTS")); //$NON-NLS-1$
85        shell.setLayout(new GridLayout(1, true));
86        shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png")); //$NON-NLS-1$
87
88        shell.addShellListener(new ShellAdapter() {
89
90            public void shellClosed(ShellEvent e) {
91                cleanUpChanges();
92            }
93        });
94
95        // characters
96        Group charEditGroup = new Group(shell, SWT.NONE);
97        charEditGroup.setText(Messages.getString("CHARACTERS")); //$NON-NLS-1$
98        GridData cegData = new GridData(GridData.FILL_BOTH);
99        charEditGroup.setLayoutData(cegData);
00        charEditGroup.setLayout(new GridLayout(3, true));
01
02        characters = new List(charEditGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
03        GridData clData = new GridData(GridData.FILL_BOTH);
04        characters.setLayoutData(clData);
05        {
06        String[] items = JdomHelpers.getCharacterNames(content);
07        if (items != null) {
08            characters.setItems(items);
09            characters.select(0);
10            characters.notifyListeners(SWT.Selection, new Event());
11            } 
12        }
13        characters.addSelectionListener(new SelectionListener() {
14
15            public void widgetSelected(SelectionEvent e) {}
16
17            public void widgetDefaultSelected(SelectionEvent e) {
18                charAdd.notifyListeners(SWT.Selection, new Event());
19            }
20        });
21        Group charButtonGroup = new Group(charEditGroup, SWT.NONE);
22        GridData cbgData = new GridData(GridData.FILL_BOTH);
23        charButtonGroup.setLayoutData(cbgData);
24        charButtonGroup.setLayout(new GridLayout(1,true));
25
26        charAdd = new Button(charButtonGroup,SWT.PUSH);
27        GridData charAddDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
28        charAdd.setLayoutData(charAddDat);
29        charAdd.setText(">>"); //$NON-NLS-1$
30        charAdd.addSelectionListener(new SelectionListener() {
31
32            public void widgetSelected(SelectionEvent arg0) {
33                String[] selected = characters.getSelection();
34               
35                for (int i = 0; i < selected.length; i++) {
36                        contentChars.add(selected[i]);
37                    }
38                }
39
40            public void widgetDefaultSelected(SelectionEvent arg0) {}
41        });
42        
43        charDelete = new Button(charButtonGroup,SWT.PUSH);
44        charDelete.setText("<<"); //$NON-NLS-1$
45        GridData delDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
46        charDelete.setLayoutData(delDat);
47        charDelete.addSelectionListener(new SelectionListener() {
48
49            public void widgetSelected(SelectionEvent arg0) {
50                String[] selected = contentChars.getSelection();
51               
52                for (int i = 0; i < selected.length; i++) {
53                        contentChars.remove(selected[i]);
54                    
55                }
56            }
57
58            public void widgetDefaultSelected(SelectionEvent arg0) {}
59        });
60        
61        
62        contentChars = new List(charEditGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
63        GridData ccData = new GridData(GridData.FILL_BOTH);
64        contentChars.setLayoutData(ccData);
65        contentChars.setItems(containedChars());
66        contentChars.addSelectionListener(new SelectionListener() {
67
68            public void widgetSelected(SelectionEvent e) {}
69
70            public void widgetDefaultSelected(SelectionEvent e) {
71                charDelete.notifyListeners(SWT.Selection, new Event());
72            }
73        });
74        
75        // items
76        Group itemEditGroup = new Group(shell, SWT.NONE);
77        itemEditGroup.setText(Messages.getString("ITEMS")); //$NON-NLS-1$
78        GridData iegData = new GridData(GridData.FILL_BOTH);
79        itemEditGroup.setLayoutData(iegData);
80        itemEditGroup.setLayout(new GridLayout(3, true));
81
82        items = new List(itemEditGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
83        GridData ilData = new GridData(GridData.FILL_BOTH);
84        items.setLayoutData(ilData);
85        String[] itemNames = JdomHelpers.getItemNames(content);
86        if (itemNames != null) {
87            items.setItems(itemNames);
88            items.select(0);
89            items.notifyListeners(SWT.Selection, new Event());
90        }
91        items.addSelectionListener(new SelectionListener() {
92
93            public void widgetSelected(SelectionEvent e) {}
94
95            public void widgetDefaultSelected(SelectionEvent e) {
96                itemsAdd.notifyListeners(SWT.Selection, new Event());
97            }
98        });
99        
00        Group itemButtonGroup = new Group(itemEditGroup, SWT.NONE);
01        GridData ibgData = new GridData(GridData.FILL_BOTH);
02        itemButtonGroup.setLayoutData(ibgData);
03        itemButtonGroup.setLayout(new GridLayout(1,true));
04        
05        itemsAdd = new Button(itemButtonGroup,SWT.PUSH);
06        GridData itemsAddDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
07        itemsAdd.setLayoutData(itemsAddDat);
08        itemsAdd.setText(">>"); //$NON-NLS-1$
09        itemsAdd.addSelectionListener(new SelectionListener() {
10
11            public void widgetSelected(SelectionEvent arg0) {
12                String[] selected = items.getSelection();
13               
14                for (int i = 0; i < selected.length; i++) {
15                        contentItems.add(selected[i]);
16                    }
17            }
18
19            public void widgetDefaultSelected(SelectionEvent arg0) {}
20        });
21        
22        itemsDelete = new Button(itemButtonGroup,SWT.PUSH);
23        itemsDelete.setText("<<"); //$NON-NLS-1$
24        GridData deleDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
25        itemsDelete.setLayoutData(deleDat);
26        itemsDelete.addSelectionListener(new SelectionListener() {
27
28            public void widgetSelected(SelectionEvent arg0) {
29                String[] selected = contentItems.getSelection();
30               
31                for (int i = 0; i < selected.length; i++) {
32                       contentItems.remove(selected[i]);
33                    
34                }
35
36            }
37
38            public void widgetDefaultSelected(SelectionEvent arg0) {}
39        });
40        
41        contentItems = new List(itemEditGroup, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
42        GridData ciData = new GridData(GridData.FILL_BOTH);
43        contentItems.setLayoutData(ciData);
44        contentItems.setItems(containedItems());
45        contentItems.addSelectionListener(new SelectionListener() {
46
47            public void widgetSelected(SelectionEvent e) {}
48
49            public void widgetDefaultSelected(SelectionEvent e) {
50                itemsDelete.notifyListeners(SWT.Selection, new Event());
51            }
52        });
53        
54        Group bottomGroup = new Group(shell, SWT.NONE);
55        GridData bgData = new GridData(GridData.FILL_HORIZONTAL);
56        bottomGroup.setLayoutData(bgData);
57        bottomGroup.setLayout(new GridLayout(5,true));
58        
59        Button ok = new Button(bottomGroup, SWT.PUSH);
60        GridData okDat = new GridData(GridData.FILL_HORIZONTAL);
61        ok.setLayoutData(okDat);
62        ok.setText(Messages.getString("OK")); //$NON-NLS-1$
63        ok.addSelectionListener(new SelectionListener() {
64
65            public void widgetSelected(SelectionEvent arg0) {
66                cleanUpChanges();
67                shell.dispose();
68            }
69
70            public void widgetDefaultSelected(SelectionEvent arg0) {}
71        });
72
73        
74        
75        shell.setSize(620, 480);
76        shell.open();
77        Display display = parent.getDisplay();
78        while (!shell.isDisposed()) {
79            if (!display.readAndDispatch()) display.sleep();
80        }
81
82    }
83
84    public void cleanUpChanges() {
85        content.removeChild("contents"); //$NON-NLS-1$
86        Element contents = new Element("contents"); //$NON-NLS-1$
87        content.addContent(contents);
88        String[] items = contentItems.getItems();
89        for (int i = 0; i < items.length; i++) {
90            contents.addContent(new Element("item").setAttribute("name", items[i])); //$NON-NLS-1$ //$NON-NLS-2$
91        }
92        String[] chars = contentChars.getItems();
93        for (int i = 0; i < chars.length; i++) {
94            contents.addContent(new Element("character").setAttribute("name",chars[i])); //$NON-NLS-1$ //$NON-NLS-2$
95        }
96    }
97    
98    public String[] containedItems() {
99        Element contents = content.getChild("contents"); //$NON-NLS-1$
00        java.util.List itemNames = new ArrayList();
01        if (contents != null ) {
02            java.util.List items = contents.getChildren("item");  //$NON-NLS-1$
03            for (Iterator itemIter = items.iterator(); itemIter.hasNext();) {
04                Element itemElement = (Element) itemIter.next();
05                itemNames.add(itemElement.getAttributeValue("name")); //$NON-NLS-1$
06            }
07        }
08        String[] retval = new String[itemNames.size()];
09        itemNames.toArray(retval);
10        return retval;        
11    }
12
13    public String[] containedChars() {
14        Element contents = content.getChild("contents"); //$NON-NLS-1$
15        java.util.List charNames = new ArrayList();
16        if (contents != null ) {
17            java.util.List items = contents.getChildren("character");  //$NON-NLS-1$
18            for (Iterator itemIter = items.iterator(); itemIter.hasNext();) {
19                Element itemElement = (Element) itemIter.next();
20                charNames.add(itemElement.getAttributeValue("name")); //$NON-NLS-1$
21            }
22        }
23        String[] retval = new String[charNames.size()];
24        charNames.toArray(retval);
25        return retval;
26    }
27    
28}