1 package org.xpcards.dialogs;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.custom.StyledText;
5 import org.eclipse.swt.events.SelectionAdapter;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.events.VerifyEvent;
8 import org.eclipse.swt.events.VerifyListener;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Combo;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.swt.widgets.Text;
18 import org.xpcards.model.Story;
19
20 public class StoryDialog {
21
22 public static int OK_RESULT = 1;
23 public static int CANCEL_RESULT = -1;
24
25 Story story;
26 int result = CANCEL_RESULT;
27 Shell shell;
28
29 StyledText description;
30 StyledText validationCtiteria;
31 Text text;
32 Combo priority;
33 Combo risk;
34 Text storyPoints;
35 intArray priorities;
36 intArray risks;
37
38 public StoryDialog(Shell parent, int[] priorities, int[] risks) {
39 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
40 shell.setLayout(new GridLayout());
41 this.priorities = new intArray(priorities);
42 this.risks = new intArray(risks);
43 }
44
45 private void createControlButtons() {
46 Composite composite = new Composite(shell, SWT.NULL);
47 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
48 GridLayout layout = new GridLayout();
49 layout.numColumns = 2;
50 composite.setLayout(layout);
51
52 Button okButton = new Button(composite, SWT.PUSH);
53
54 // TODO OK Button I18n
55 okButton.setText("OK");
56 okButton.addSelectionListener(new SelectionAdapter() {
57 public void widgetSelected(SelectionEvent e) {
58 result = commit();
59 shell.close();
60 }
61 });
62
63 Button cancelButton = new Button(composite, SWT.PUSH);
64 // TODO Concel I18n
65 cancelButton.setText("Cancel");
66 cancelButton.addSelectionListener(new SelectionAdapter() {
67 public void widgetSelected(SelectionEvent e) {
68 result = rollback();
69 shell.close();
70 }
71 });
72
73 shell.setDefaultButton(okButton);
74 }
75
76 private void createTextWidgets() {
77 Composite composite = new Composite(shell, SWT.NULL);
78 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
79 GridLayout layout = new GridLayout();
80 layout.numColumns = 2;
81 composite.setLayout(layout);
82
83 Label label = new Label(composite, SWT.RIGHT);
84 label.setText("Name");
85 text = new Text(composite, SWT.BORDER);
86 GridData gridData = new GridData();
87 gridData.widthHint = 300;
88 text.setLayoutData(gridData);
89
90 if (story != null) {
91 text.setText(story.getName());
92 }
93
94 label = new Label(composite, SWT.RIGHT);
95 label.setText("Description");
96 gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
97 label.setLayoutData(gridData);
98
99 description =
100 new StyledText(
101 composite,
102 SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.WRAP);
103
104 gridData = new GridData(GridData.FILL_HORIZONTAL);
105 gridData.heightHint = 70;
106 description.setLayoutData(gridData);
107
108 if (story != null) {
109 description.setText(story.getDescription());
110 }
111
112 // Validation criteria
113
114 label = new Label(composite, SWT.RIGHT);
115 label.setText("Validation Criteria");
116 gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
117 label.setLayoutData(gridData);
118
119 validationCtiteria =
120 new StyledText(
121 composite,
122 SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.WRAP);
123
124 gridData = new GridData(GridData.FILL_HORIZONTAL);
125 gridData.heightHint = 70;
126 validationCtiteria.setLayoutData(gridData);
127
128 if (story != null) {
129 validationCtiteria.setText(story.getCompletionCriteria());
130 }
131
132 Composite footerComposite = new Composite(shell, SWT.NULL);
133 footerComposite.setLayoutData(
134 new GridData(GridData.FILL_HORIZONTAL | GridData.CENTER));
135
136 GridLayout gl = new GridLayout();
137 gl.numColumns = 6;
138
139 footerComposite.setLayout(gl);
140
141 label = new Label(footerComposite, SWT.RIGHT);
142 label.setText("Priority");
143
144 GridData data = new GridData();
145 data.horizontalIndent = 20;
146 label.setLayoutData(data);
147
148 priority = new Combo(footerComposite, SWT.READ_ONLY);
149
150 priority.setItems(priorities.getAsStringArray());
151
152 data = new GridData();
153 data.widthHint = 15;
154 priority.setLayoutData(data);
155
156 if (getStory() != null) {
157 priority.setText(priorities.convertValue(getStory().getPriority()));
158 // priority.select(getStory().getPriority());
159 } else {
160 // priority.select(0);
161 priority.setText(priority.getItem(0));
162 }
163
164 // priority.setLayoutData(new RowData(15, SWT.DEFAULT));
165
166 label = new Label(footerComposite, SWT.RIGHT);
167 label.setText("Risk");
168
169 data = new GridData();
170 data.horizontalIndent = 20;
171 label.setLayoutData(data);
172
173 risk = new Combo(footerComposite, SWT.READ_ONLY);
174 risk.setItems(risks.getAsStringArray());
175 risk.setSize(15, SWT.DEFAULT);
176 if (getStory() != null) {
177 risk.setText(risks.convertValue(getStory().getRisk()));
178 } else {
179 risk.setText(risk.getItem(0));
180 }
181
182 data = new GridData();
183 data.widthHint = 15;
184 risk.setLayoutData(data);
185
186 label = new Label(footerComposite, SWT.RIGHT);
187 label.setText("Story points");
188
189 data = new GridData();
190 data.horizontalIndent = 20;
191 label.setLayoutData(data);
192
193 storyPoints = new Text(footerComposite, SWT.BORDER);
194
195 if (getStory() != null) {
196 storyPoints.setText("" + getStory().getStoryPoints());
197 } else {
198 storyPoints.setText("1");
199 }
200
201 storyPoints.addVerifyListener(new VerifyListener() {
202
203 public void verifyText(VerifyEvent e) {
204 if (!Character.isDigit(e.character)
205 && !(Character.getType(e.character) == 15)) {
206 e.doit = false;
207 }
208 }
209
210 });
211
212 data = new GridData();
213 data.widthHint = 15;
214 storyPoints.setLayoutData(data);
215
216 }
217
218 public String getTitle() {
219 return shell.getText();
220 }
221
222 public void setTitle(String title) {
223 shell.setText(title);
224 }
225
226 public int open() {
227 createTextWidgets();
228 createControlButtons();
229 result = CANCEL_RESULT;
230 shell.pack();
231 shell.open();
232 Display display = shell.getDisplay();
233 while (!shell.isDisposed()) {
234 if (!display.readAndDispatch())
235 display.sleep();
236 }
237
238 return result;
239 }
240
241 private int commit() {
242 if (text.getText() != null) {
243 if (story == null) {
244 story = new Story();
245 }
246 story.setName(text.getText());
247
248 if (description.getText() != null) {
249 story.setDescription(description.getText());
250 }
251
252 if (validationCtiteria.getText() != null) {
253 story.setCompletionCriteria(validationCtiteria.getText());
254 }
255
256 if (priority.getSelectionIndex() >= 0) {
257 story.setPriority(
258 priorities.getElementAt(priority.getSelectionIndex()));
259 } else {
260 story.setPriority(0);
261 }
262
263 if (risk.getSelectionIndex() >= 0) {
264 story.setRisk(risks.getElementAt(risk.getSelectionIndex()));
265 } else {
266 story.setRisk(0);
267 }
268
269 if (storyPoints.getText() != null
270 && storyPoints.getText().length() > 0) {
271 story.setStoryPoints(Integer.parseInt(storyPoints.getText()));
272 } else {
273 story.setStoryPoints(1);
274 }
275
276 return OK_RESULT;
277 }
278 return CANCEL_RESULT;
279 }
280
281 private int rollback() {
282 return CANCEL_RESULT;
283 }
284
285 public static void main(String[] args) {
286 Display display = new Display();
287 Shell shell = new Shell(display);
288
289 StoryDialog d =
290 new StoryDialog(
291 shell,
292 new int[] { 1, 2, 3, 4 },
293 new int[] { 0, 1, 2, 3 });
294
295 d.setTitle("Test story");
296 int result = d.open();
297
298 System.err.println("Result is :" + result);
299 System.err.println("Story is:" + d.getStory());
300
301 }
302
303 /***
304 * @return
305 */
306 public Story getStory() {
307 return story;
308 }
309
310 /***
311 * @param story
312 */
313 public void setStory(Story story) {
314 this.story = story;
315 }
316
317 }
318
319 class intArray {
320
321 private int[] source = null;
322
323 intArray(int[] source) {
324 this.source = source;
325 }
326
327 String[] getAsStringArray() {
328 String[] s = new String[source.length];
329 for (int i = 0; i < source.length; i++) {
330 s[i] = "" + source[i];
331 }
332 return s;
333 }
334
335 int getElementAt(int index) {
336 return source[index];
337 }
338
339 String convertValue(int value) {
340 return "" + convertValueToInt(value);
341 }
342
343 int convertValueToInt(int value) {
344 // Assume array sorted acceding
345 for (int i = 0; source != null && i < source.length; i++) {
346 if (source[i] >= value || i == source.length - 1) {
347 return source[i];
348 }
349 }
350 return 0;
351 }
352 }
This page was automatically generated by Maven