1 /*
2  *  CVS: $Id: PreconditionEditorDialog.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
25
26
27import java.util.ArrayList;
28import java.util.Iterator;
29
30import org.eclipse.swt.SWT;
31import org.eclipse.swt.events.SelectionEvent;
32import org.eclipse.swt.events.SelectionListener;
33import org.eclipse.swt.events.ShellAdapter;
34import org.eclipse.swt.events.ShellEvent;
35import org.eclipse.swt.layout.GridData;
36import org.eclipse.swt.layout.GridLayout;
37import org.eclipse.swt.widgets.Button;
38import org.eclipse.swt.widgets.Dialog;
39import org.eclipse.swt.widgets.Display;
40import org.eclipse.swt.widgets.Event;
41import org.eclipse.swt.widgets.Group;
42import org.eclipse.swt.widgets.List;
43import org.eclipse.swt.widgets.MessageBox;
44import org.eclipse.swt.widgets.Shell;
45import org.jdom.Element;
46import org.jzuul.engine.gui.utils.Util;
47
48
49/**
50 * 
51 * Created on Jul 14, 2004
52 * 
53 * 
54 * @version $Revision: 1.12 $
55 */
56public class PreconditionEditorDialog extends Dialog {
57    Element element;
58    List itemsList, preconditionList;
59    private Button add;
60    private Button delete;
61    
62    
63    /**
64     * @param arg0
65     */
66    public PreconditionEditorDialog(Shell arg0) {
67        super(arg0);
68    }
69
70    /**
71     * @param arg0
72     * @param arg1
73     */
74    public PreconditionEditorDialog(Shell arg0, int arg1) {
75        super(arg0, arg1);
76    }
77    
78    public void open(Element withPreconditions) {
79        this.element = withPreconditions;
80        Shell parent = getParent();
81        final Shell shell = new Shell(parent, SWT.BORDER | SWT.MIN | SWT.RESIZE | SWT.APPLICATION_MODAL);
82        shell.setText(Messages.getString("EDIT_PRECONDITIONS")); //$NON-NLS-1$
83        shell.setLayout(new GridLayout(3, true));
84        shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png")); //$NON-NLS-1$
85        shell.addShellListener(new ShellAdapter() {
86
87            public void shellClosed(ShellEvent e) {
88                cleanUpChanges();
89            }
90        });
91
92        //left hand items list
93        itemsList = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
94        GridData ilData = new GridData(GridData.FILL_BOTH);
95        ilData.horizontalSpan = 1;
96        itemsList.setLayoutData(ilData);
97        String[] itemNames =JdomHelpers.getItemNames(element);
98        if (itemNames != null && (itemNames.length > 0)) {
99            itemsList.setItems(itemNames);
00        } else {
01            MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR);
02            mb.setMessage(Messages.getString("PRECON_ERROR")); //$NON-NLS-1$
03            mb.open();
04            shell.dispose();
05            return;
06        }
07        itemsList.addSelectionListener(new SelectionListener() {
08
09            public void widgetSelected(SelectionEvent e) {}
10
11            public void widgetDefaultSelected(SelectionEvent e) {
12                add.notifyListeners(SWT.Selection, new Event());
13            }
14        });
15        
16        Group buttonGroup = new Group(shell,SWT.NONE);
17        GridData bgData = new GridData(GridData.FILL_BOTH);
18        bgData.horizontalSpan = 1;
19        buttonGroup.setLayoutData(bgData);
20        buttonGroup.setLayout(new GridLayout(1,true));
21        
22        add = new Button(buttonGroup,SWT.PUSH);
23        GridData addDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
24        add.setLayoutData(addDat);
25        add.setText(">>"); //$NON-NLS-1$
26        add.addSelectionListener(new SelectionListener() {
27
28            public void widgetSelected(SelectionEvent arg0) {
29                String[] selected = itemsList.getSelection();
30                
31                for (int i = 0; i < selected.length; i++) {
32                    preconditionList.add(selected[i]);
33               
34                }
35            }
36
37            public void widgetDefaultSelected(SelectionEvent arg0) {}
38        });
39        
40        delete = new Button(buttonGroup,SWT.PUSH);
41        delete.setText("<<"); //$NON-NLS-1$
42        GridData delDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
43        delete.setLayoutData(delDat);
44        delete.addSelectionListener(new SelectionListener() {
45
46            public void widgetSelected(SelectionEvent arg0) {
47                String[] selected = preconditionList.getSelection();
48                
49                for (int i = 0; i < selected.length; i++) {
50                    
51                        preconditionList.remove(selected[i]);
52                    
53                }
54
55            }
56
57            public void widgetDefaultSelected(SelectionEvent arg0) {}
58        });
59        
60        Button ok = new Button(buttonGroup, SWT.PUSH);
61        ok.setText(Messages.getString("OK")); //$NON-NLS-1$
62        GridData okDat = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
63        ok.setLayoutData(okDat);
64        ok.addSelectionListener(new SelectionListener() {
65
66            public void widgetSelected(SelectionEvent arg0) {
67                cleanUpChanges();
68                shell.dispose();
69
70            }
71
72            public void widgetDefaultSelected(SelectionEvent arg0) {}
73        });
74        
75        
76        preconditionList = new List(shell,SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
77        GridData plData = new GridData(GridData.FILL_BOTH);
78        plData.horizontalSpan = 1;
79        preconditionList.setLayoutData(plData);
80        String[] precons = getPreconditionItemsNames();
81        preconditionList.setItems(precons);
82        preconditionList.addSelectionListener(new SelectionListener() {
83
84            public void widgetSelected(SelectionEvent e) {}
85
86            public void widgetDefaultSelected(SelectionEvent e) {
87                delete.notifyListeners(SWT.Selection, new Event());
88            }
89        });
90        
91        shell.setSize(400,300);
92        shell.open();
93
94        Display display = parent.getDisplay();
95        while (!shell.isDisposed()) {
96            if (!display.readAndDispatch()) display.sleep();
97        }
98        
99    }
00
01    protected String[] getPreconditionItemsNames() {
02        java.util.List precons = element.getChildren("precondition"); //$NON-NLS-1$
03        java.util.List preconItemNames = new ArrayList();
04        for (Iterator preconIter = precons.iterator(); preconIter.hasNext();) {
05            Element preconElement = (Element) preconIter.next();
06            preconItemNames.add(preconElement.getAttributeValue("item")); //$NON-NLS-1$
07        }
08        String[] retval = new String[preconItemNames.size()];
09        preconItemNames.toArray(retval);
10        return retval;
11        
12    }
13
14    protected void cleanUpChanges() {
15    element.removeChildren("precondition"); //$NON-NLS-1$
16    String[] preconItems = preconditionList.getItems();
17    for (int i = 0; i < preconItems.length; i++) {
18        element.addContent(new Element("precondition").setAttribute("item",preconItems[i])); //$NON-NLS-1$ //$NON-NLS-2$
19    }
20    }
21    
22}
23