1
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=""; String def=""; 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); }
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")); shell.setLayout(new GridLayout(2,true));
65
66 String text = "";
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"); 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")); 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")); cancel.addSelectionListener(new SelectionListener() {
15
16 public void widgetSelected(SelectionEvent arg0) {
17 input.setText(""); 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*")) { 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(); if (retVal.length() != newVal.length()) {
62 MessageBox mb = new MessageBox(this.getParent(),SWT.ICON_INFORMATION);
63
64 mb.setMessage(Messages.getString("WHITESPACE_TRUNC")); mb.open();
66 }
67 return newVal;
68 }
69
70
71}
72