1 /*
2  *  CVS: $Id: CombinationEditorDialog.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.Iterator;
26
27import org.eclipse.swt.SWT;
28import org.eclipse.swt.events.SelectionEvent;
29import org.eclipse.swt.events.SelectionListener;
30import org.eclipse.swt.events.ShellAdapter;
31import org.eclipse.swt.events.ShellEvent;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Button;
35import org.eclipse.swt.widgets.Dialog;
36import org.eclipse.swt.widgets.Display;
37import org.eclipse.swt.widgets.Event;
38import org.eclipse.swt.widgets.Group;
39import org.eclipse.swt.widgets.List;
40import org.eclipse.swt.widgets.Shell;
41import org.jdom.Element;
42import org.jzuul.engine.gui.utils.Util;
43
44
45
46
47/**
48 * 
49 * Created on Jul 14, 2004
50 * 
51 * 
52 * @version $Revision: 1.9 $
53 */
54public class CombinationEditorDialog extends Dialog {
55    Element itemElement;
56    List itemList;    
57    ActionEditorComposite actionGroup;
58    String currentWithObject;
59    
60    /**
61     * @param arg0
62     */
63    public CombinationEditorDialog(Shell arg0) {
64        super(arg0);
65    }
66
67    /**
68     * @param arg0
69     * @param arg1
70     */
71    public CombinationEditorDialog(Shell arg0, int arg1) {
72        super(arg0, arg1);
73    }
74    
75    public void open(Element currentItem, String[] itemNames) {
76        itemElement = currentItem;
77        
78        Shell parent = getParent();
79        final Shell shell = new Shell(parent, SWT.BORDER | SWT.MIN  | SWT.RESIZE | SWT.APPLICATION_MODAL);
80        shell.setText(Messages.getString("EDIT_COMBINATIONS")); //$NON-NLS-1$
81        shell.setLayout(new GridLayout(10,true));
82        shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png"));                 //$NON-NLS-1$
83        shell.addShellListener(new ShellAdapter() {
84            public void shellClosed(ShellEvent e) {
85                cleanUpChanges();
86            }
87        });
88        
89            { //The list on the left side
90                itemList = new List(shell, SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE | SWT.CHECK);
91                final GridData gridData =
92                    new GridData(GridData.HORIZONTAL_ALIGN_FILL |
93                                 GridData.VERTICAL_ALIGN_FILL |
94                                 GridData.GRAB_VERTICAL );
95                gridData.horizontalSpan = 3;
96                itemList.setLayoutData(gridData);
97                for (int i = 0; i < itemNames.length; i++) {
98                    if (!itemNames[i].equals(itemElement.getAttributeValue("name"))) { //$NON-NLS-1$
99                        itemList.add(itemNames[i]);
00                    }
01                }
02                
03                itemList.addSelectionListener(new SelectionListener() {
04
05                    public void widgetSelected(SelectionEvent arg0) {
06                        cleanUpChanges();
07                        currentWithObject = itemList.getItem(itemList.getSelectionIndex());
08                      //  actionGroup.setText("Combinations for " + itemElement.getAttributeValue("name") + " with "  + currentWithObject);
09                        showCombinationsWithObject(currentWithObject);
10                    }
11
12                    public void widgetDefaultSelected(SelectionEvent arg0) {
13                    }
14                });
15                
16            }
17            { // The group with the action
18                actionGroup = new ActionEditorComposite(shell, SWT.NONE , itemElement);
19                }
20            { // The buttons on the bottom
21                final Group bottomGroup = new Group(shell, SWT.NONE);
22                GridData groupLD = new GridData(GridData.FILL_HORIZONTAL);
23                groupLD.heightHint = 40;
24                groupLD.horizontalSpan = 10;
25                bottomGroup.setLayoutData(groupLD);
26                
27                bottomGroup.setLayout(new GridLayout(10,true));
28                
29                GridData buttonData = new GridData(GridData.VERTICAL_ALIGN_END);
30                buttonData.heightHint = 30;
31                buttonData.widthHint = 80;
32                
33                Button ok = new Button(bottomGroup, SWT.PUSH);
34                ok.setText(Messages.getString("OK")); //$NON-NLS-1$
35                ok.addSelectionListener(new SelectionListener() {
36
37                    public void widgetSelected(SelectionEvent arg0) {
38                        cleanUpChanges();
39                        shell.dispose();
40                    }
41
42                    public void widgetDefaultSelected(SelectionEvent arg0) {}
43                });
44                ok.setLayoutData(buttonData);
45                
46             }
47        
48        shell.setSize(620,480);
49        if (itemList.getItemCount() > 0) {
50            itemList.select(0);
51            itemList.notifyListeners(SWT.Selection, new Event());
52        }
53        
54        shell.open();
55        Display display = parent.getDisplay();
56        while (!shell.isDisposed()) {
57                 if (!display.readAndDispatch()) display.sleep();
58        }
59        
60    }
61    
62    protected void cleanUpChanges() {
63        replaceActionList(currentWithObject,actionGroup.getElementList());
64    }
65    
66    protected void showCombinationsWithObject(String withObjectName) {
67        actionGroup.showActionElements(getActionsForWithItem(withObjectName));
68    }
69    
70    protected Element getWithObject(String withObjectName) {
71        if (withObjectName == null) return null;
72        Element combinations = itemElement.getChild("combinations"); //$NON-NLS-1$
73        
74        if (combinations != null) {
75            java.util.List withObjects = combinations.getChildren("with-object"); //$NON-NLS-1$
76            for (Iterator withObjIter = withObjects.iterator(); withObjIter.hasNext();) {
77                Element element = (Element) withObjIter.next();
78                if (element.getAttributeValue("name").equals(withObjectName)) { //$NON-NLS-1$
79                    return element;
80                }
81            }
82            Element newEl = new Element("with-object"); //$NON-NLS-1$
83            newEl.setAttribute("name", withObjectName); //$NON-NLS-1$
84            combinations.addContent(newEl);
85            return newEl;
86        } else {
87            itemElement.addContent(new Element("combinations")); //$NON-NLS-1$
88            return getWithObject(withObjectName);
89        }
90        
91    }
92
93    protected java.util.List getActionsForWithItem(String withObjectName) {
94        Element withObject = getWithObject(withObjectName);
95        Element actions = withObject.getChild("actions"); //$NON-NLS-1$
96        if (actions == null) return null;
97        return actions.getChildren(); 
98    }
99    
00    protected void replaceActionList(String currentWithObject, java.util.List actionList) {
01        Element withObj = getWithObject(currentWithObject);
02        if (withObj == null) return;
03        withObj.removeChild("actions"); //$NON-NLS-1$
04        Element newActions = new Element("actions"); //$NON-NLS-1$
05        newActions.addContent(actionList);
06        withObj.addContent(newActions);
07    }
08    
09}
10