1 /*
2  *  CVS: $Id: InputDialog.java,v 1.17 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 org.eclipse.swt.SWT;
26import org.eclipse.swt.events.KeyEvent;
27import org.eclipse.swt.events.KeyListener;
28import org.eclipse.swt.events.SelectionEvent;
29import org.eclipse.swt.events.SelectionListener;
30import org.eclipse.swt.layout.GridData;
31import org.eclipse.swt.layout.GridLayout;
32import org.eclipse.swt.widgets.Button;
33import org.eclipse.swt.widgets.Composite;
34import org.eclipse.swt.widgets.Dialog;
35import org.eclipse.swt.widgets.Display;
36import org.eclipse.swt.widgets.Event;
37import org.eclipse.swt.widgets.Label;
38import org.eclipse.swt.widgets.MessageBox;
39import org.eclipse.swt.widgets.Shell;
40import org.eclipse.swt.widgets.Text;
41import org.jzuul.engine.gui.utils.Util;
42
43public class InputDialog extends Dialog {
44    Object result;
45    Label message;    
46    Text input;
47    String msgtext=""; //$NON-NLS-1$
48    String def=""; //$NON-NLS-1$
49    int style = SWT.BORDER;
50    Button ok;
51    
52    public InputDialog (Shell parent, int style) {
53            super (parent, style);
54    }
55    public InputDialog (Shell parent) {
56            this (parent, 0); // your default style bits go here (not the Shell's style bits)
57    }
58    public String open () {
59            Shell parent = getParent();
60            final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
61            shell.setText(getText());
62            shell.setSize(400,150);
63            shell.setImage(Util.getImagefromResource(parent.getDisplay(),"etc/artwork/jz.png")); //$NON-NLS-1$
64            shell.setLayout(new GridLayout(2,true));
65            
66            String text = ""; //$NON-NLS-1$
67            
68            message = new Label((Composite)shell,SWT.NONE);
69            message.setText(msgtext);
70            
71            
72            
73            input = new Text((Composite)shell,style);
74            input.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
75            input.setText("\n\n\n"); //$NON-NLS-1$
76            input.addKeyListener(new KeyListener() {
77
78                public void keyPressed(KeyEvent arg0) {}
79
80                public void keyReleased(KeyEvent arg0) {
81                    if (arg0.keyCode == SWT.CR && (style & SWT.MULTI)==0) {
82                        ok.notifyListeners(SWT.Selection, new Event());
83                    }
84
85                }
86            });
87            
88            Composite buttonComp = new Composite((Composite)shell,SWT.NONE);
89            GridData buttonCompDat = new GridData(GridData.FILL_HORIZONTAL);
90            buttonCompDat.horizontalSpan = 2;
91            buttonComp.setLayoutData(buttonCompDat);
92            
93            buttonComp.setLayout(new GridLayout(2,true));
94            
95            ok = new Button(buttonComp,SWT.NONE);
96            ok.setText(Messages.getString("OK")); //$NON-NLS-1$
97            ok.addSelectionListener(new SelectionListener() {
98
99                public void widgetSelected(SelectionEvent arg0) {
00                    if (input.getText().length() > 0) {
01                        shell.dispose();
02                    }
03                }
04
05                public void widgetDefaultSelected(SelectionEvent arg0) {
06                }
07            });
08            GridData okDat = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
09            okDat.widthHint = 80;
10            ok.setLayoutData(okDat);
11            
12            Button cancel = new Button(buttonComp,SWT.NONE);
13            cancel.setText(Messages.getString("CANCEL")); //$NON-NLS-1$
14            cancel.addSelectionListener(new SelectionListener() {
15
16                public void widgetSelected(SelectionEvent arg0) {
17                    input.setText(""); //$NON-NLS-1$
18                    shell.dispose();
19
20                }
21
22                public void widgetDefaultSelected(SelectionEvent arg0) {}
23            });
24            GridData cancelDat = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
25            cancelDat.widthHint = 80;
26            cancel.setLayoutData(cancelDat);
27            
28                shell.pack();
29                input.setText(def);
30
31            shell.open();
32            Display display = parent.getDisplay();
33            input.setFocus();
34            while (!shell.isDisposed()) {
35                    text =  input.getText();
36                    if (!display.readAndDispatch()) display.sleep();
37            }
38            if (text.matches("\\s*")) { //$NON-NLS-1$
39                return null;
40            } else {
41                return text;
42            }
43    }
44    
45    public void setMessage(String message) {
46        msgtext = message;
47    }
48    
49    public void setDefaultValue(String value) {
50        def = value;
51    }
52    
53    public void setStyle(int style) {
54        this.style = style;
55    }
56    
57    public String openNoWhitespace() {
58        String retVal = open();
59        if (retVal == null) return null;
60        String newVal = retVal.replaceAll("\\s+","").toLowerCase(); //$NON-NLS-1$ //$NON-NLS-2$
61        if (retVal.length() != newVal.length()) {
62            MessageBox mb = new MessageBox(this.getParent(),SWT.ICON_INFORMATION);
63        
64            mb.setMessage(Messages.getString("WHITESPACE_TRUNC")); //$NON-NLS-1$
65            mb.open();
66        }
67        return newVal;
68    }
69    
70 
71}
72