Mercurial > projects > dwt-samples
annotate examples/addressbook/DataEntryDialog.d @ 161:eb84f9418bbf
Change module names to this not have examples in the FQN.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 01 Sep 2008 22:24:27 +0200 |
parents | 9c2803aea121 |
children |
rev | line source |
---|---|
78 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2005 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
161
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
82
diff
changeset
|
11 module addressbook.DataEntryDialog; |
78 | 12 |
13 import dwt.DWT; | |
14 import dwt.events.ModifyEvent; | |
15 import dwt.events.ModifyListener; | |
16 import dwt.events.SelectionAdapter; | |
17 import dwt.events.SelectionEvent; | |
18 import dwt.layout.GridData; | |
19 import dwt.layout.GridLayout; | |
20 import dwt.widgets.Button; | |
21 import dwt.widgets.Composite; | |
22 import dwt.widgets.Display; | |
23 import dwt.widgets.Label; | |
24 import dwt.widgets.Shell; | |
25 import dwt.widgets.Text; | |
26 | |
27 import dwt.dwthelper.ResourceBundle; | |
28 import dwt.dwthelper.utils; | |
29 | |
30 /** | |
31 * DataEntryDialog class uses <code>org.eclipse.swt</code> | |
32 * libraries to implement a dialog that accepts basic personal information that | |
33 * is added to a <code>Table</code> widget or edits a <code>TableItem</code> entry | |
34 * to represent the entered data. | |
35 */ | |
36 public class DataEntryDialog { | |
37 | |
38 private static ResourceBundle resAddressBook; | |
39 | |
40 Shell shell; | |
41 char[][] values; | |
42 char[][] labels; | |
43 | |
82
9c2803aea121
Fix Addressbook example and HG ignores
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
44 public this(Shell parent, ResourceBundle bdl ) { |
78 | 45 if( resAddressBook is null ){ |
82
9c2803aea121
Fix Addressbook example and HG ignores
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
46 resAddressBook = bdl;//ResourceBundle.getBundle("examples_addressbook"); |
78 | 47 } |
48 shell = new Shell(parent, DWT.DIALOG_TRIM | DWT.PRIMARY_MODAL); | |
49 shell.setLayout(new GridLayout()); | |
50 } | |
51 | |
52 private void addTextListener(Text text) { | |
53 text.addModifyListener(new class(text) ModifyListener { | |
54 Text text; | |
55 this( Text text ){ this.text = text; } | |
56 public void modifyText(ModifyEvent e){ | |
57 Integer index = cast(Integer)(this.text.getData("index")); | |
58 values[index.intValue()] = this.text.getText(); | |
59 } | |
60 }); | |
61 } | |
62 private void createControlButtons() { | |
63 Composite composite = new Composite(shell, DWT.NONE); | |
64 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); | |
65 GridLayout layout = new GridLayout(); | |
66 layout.numColumns = 2; | |
67 composite.setLayout(layout); | |
68 | |
69 Button okButton = new Button(composite, DWT.PUSH); | |
70 okButton.setText(resAddressBook.getString("OK")); | |
71 okButton.addSelectionListener(new class() SelectionAdapter { | |
72 public void widgetSelected(SelectionEvent e) { | |
73 shell.close(); | |
74 } | |
75 }); | |
76 | |
77 Button cancelButton = new Button(composite, DWT.PUSH); | |
78 cancelButton.setText(resAddressBook.getString("Cancel")); | |
79 cancelButton.addSelectionListener(new class() SelectionAdapter { | |
80 public void widgetSelected(SelectionEvent e) { | |
81 values = null; | |
82 shell.close(); | |
83 } | |
84 }); | |
85 | |
86 shell.setDefaultButton(okButton); | |
87 } | |
88 | |
89 private void createTextWidgets() { | |
90 if (labels is null) return; | |
91 | |
92 Composite composite = new Composite(shell, DWT.NONE); | |
93 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
94 GridLayout layout= new GridLayout(); | |
95 layout.numColumns = 2; | |
96 composite.setLayout(layout); | |
97 | |
98 if (values is null) | |
99 values = new char[][labels.length]; | |
100 | |
101 for (int i = 0; i < labels.length; i++) { | |
102 Label label = new Label(composite, DWT.RIGHT); | |
103 label.setText(labels[i]); | |
104 Text text = new Text(composite, DWT.BORDER); | |
105 GridData gridData = new GridData(); | |
106 gridData.widthHint = 400; | |
107 text.setLayoutData(gridData); | |
108 if (values[i] !is null) { | |
109 text.setText(values[i]); | |
110 } | |
111 text.setData("index", new Integer(i)); | |
112 addTextListener(text); | |
113 } | |
114 } | |
115 | |
116 public char[][] getLabels() { | |
117 return labels; | |
118 } | |
119 public char[] getTitle() { | |
120 return shell.getText(); | |
121 } | |
122 /** | |
123 * Returns the contents of the <code>Text</code> widgets in the dialog in a | |
124 * <code>char[]</code> array. | |
125 * | |
126 * @return char[][] | |
127 * The contents of the text widgets of the dialog. | |
128 * May return null if all text widgets are empty. | |
129 */ | |
130 public char[][] getValues() { | |
131 return values; | |
132 } | |
133 /** | |
134 * Opens the dialog in the given state. Sets <code>Text</code> widget contents | |
135 * and dialog behaviour accordingly. | |
136 * | |
137 * @param dialogState int | |
138 * The state the dialog should be opened in. | |
139 */ | |
140 public char[][] open() { | |
141 createTextWidgets(); | |
142 createControlButtons(); | |
143 shell.pack(); | |
144 shell.open(); | |
145 Display display = shell.getDisplay(); | |
146 while(!shell.isDisposed()){ | |
147 if(!display.readAndDispatch()) | |
148 display.sleep(); | |
149 } | |
150 | |
151 return getValues(); | |
152 } | |
153 public void setLabels(char[][] labels) { | |
154 this.labels = labels; | |
155 } | |
156 public void setTitle(char[] title) { | |
157 shell.setText(title); | |
158 } | |
159 /** | |
160 * Sets the values of the <code>Text</code> widgets of the dialog to | |
161 * the values supplied in the parameter array. | |
162 * | |
163 * @param itemInfo char[][] | |
164 * The values to which the dialog contents will be set. | |
165 */ | |
166 public void setValues(char[][] itemInfo) { | |
167 if (labels is null) return; | |
168 | |
169 if (values is null) | |
170 values = new char[][labels.length]; | |
171 | |
172 int numItems = Math.min(values.length, itemInfo.length); | |
173 for(int i = 0; i < numItems; i++) { | |
174 values[i] = itemInfo[i]; | |
175 } | |
176 } | |
177 } |