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