1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package de.nierbeck.timeTrack.views;
28
29
30
31
32
33
34
35
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.text.DateFormat;
39 import java.text.ParseException;
40 import java.util.Calendar;
41 import java.util.Date;
42 import java.util.GregorianCalendar;
43
44 import org.eclipse.core.runtime.ILog;
45 import org.eclipse.core.runtime.IStatus;
46 import org.eclipse.core.runtime.Platform;
47 import org.eclipse.core.runtime.Status;
48 import org.eclipse.swt.SWT;
49 import org.eclipse.swt.events.ModifyEvent;
50 import org.eclipse.swt.events.ModifyListener;
51 import org.eclipse.swt.events.SelectionAdapter;
52 import org.eclipse.swt.events.SelectionEvent;
53 import org.eclipse.swt.graphics.FontMetrics;
54 import org.eclipse.swt.graphics.GC;
55 import org.eclipse.swt.layout.GridData;
56 import org.eclipse.swt.layout.GridLayout;
57 import org.eclipse.swt.widgets.Button;
58 import org.eclipse.swt.widgets.Composite;
59 import org.eclipse.swt.widgets.Display;
60 import org.eclipse.swt.widgets.Label;
61 import org.eclipse.swt.widgets.Shell;
62 import org.eclipse.swt.widgets.Text;
63
64 import de.nierbeck.timeTrack.TimeTrackPlugin;
65 import de.nierbeck.timeTrack.model.impl.EntryTypeImpl;
66
67 /***
68 * DataEntryDialog class uses <code>org.eclipse.swt</code> libraries to
69 * implement a dialog that accepts basic personal information that is added to a
70 * <code>Table</code> widget or edits a <code>TableItem</code> entry to
71 * represent the entered data.
72 */
73 public class EditEntryDialog {
74
75 /***
76 * Logger for this class
77 */
78 private static final ILog LOGGER = Platform.getLog(TimeTrackPlugin
79 .getDefault().getBundle());
80
81 private Shell shell;
82
83 private EntryTypeImpl selectedEntry;
84
85 private String projectBackup;
86
87 private String taskBackup;
88
89 private String commentBackup;
90
91 private long durationBackup;
92
93 private Calendar startTimeBackup;
94
95 private Button okButton;
96
97 /***
98 * Constructor of the Dialog
99 *
100 * @param parent - the parent of this dialog
101 * @param selectedEntry - the selected Entry of the Table
102 */
103 public EditEntryDialog(Shell parent, EntryTypeImpl selectedEntry) {
104 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
105 shell.setLayout(new GridLayout());
106 this.selectedEntry = selectedEntry;
107 projectBackup = new String(selectedEntry.getProject());
108 taskBackup = new String(selectedEntry.getTask());
109 commentBackup = new String(selectedEntry.getComment());
110 durationBackup = selectedEntry.getDuration();
111 Calendar cal = GregorianCalendar.getInstance();
112 cal.setTime(selectedEntry.getStartTime().getTime());
113 startTimeBackup = cal;
114 }
115
116 private void addTextListener(final Text text) {
117 text.addModifyListener(new ModifyListener() {
118
119 public void modifyText(ModifyEvent e) {
120 ColumnEnum columnEnum = (ColumnEnum) text.getData("field");
121
122
123 switch (columnEnum.getOrd()) {
124 case ColumnEnum.COMMENT_ORD:
125 selectedEntry.setComment(text.getText());
126 break;
127 case ColumnEnum.DURATION_ORD:
128 if (!"".equals(text.getText())) {
129 selectedEntry.setDuration(Long.valueOf(text.getText())
130 .longValue());
131 if (!okButton.getEnabled()
132 && !"".equals(startTimeBackup))
133 okButton.setEnabled(true);
134 } else
135 okButton.setEnabled(false);
136 break;
137 case ColumnEnum.PROJECT_ORD:
138 selectedEntry.setProject(text.getText());
139 break;
140 case ColumnEnum.START_DATE_ORD:
141 if (!"".equals(text.getText())) {
142 Calendar calendar = GregorianCalendar.getInstance();
143 try {
144 Date date = DateFormat.getInstance().parse(
145 text.getText());
146 calendar.setTime(date);
147 selectedEntry.setStartTime(calendar);
148 } catch (ParseException e1) {
149
150 e1.printStackTrace();
151 }
152 if (!okButton.getEnabled() && durationBackup != 0)
153 okButton.setEnabled(true);
154 } else
155 okButton.setEnabled(false);
156 break;
157 case ColumnEnum.TASK_ORD:
158 selectedEntry.setTask(text.getText());
159 break;
160 default:
161
162 }
163 }
164 });
165 }
166
167 private void createControlButtons() {
168 Composite composite = new Composite(shell, SWT.NULL);
169 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
170 GridLayout layout = new GridLayout();
171 layout.numColumns = 2;
172 composite.setLayout(layout);
173
174 okButton = new Button(composite, SWT.PUSH);
175 okButton.setText("OK");
176 okButton.addSelectionListener(new SelectionAdapter() {
177
178 public void widgetSelected(SelectionEvent e) {
179 shell.close();
180 }
181 });
182
183 Button cancelButton = new Button(composite, SWT.PUSH);
184 cancelButton.setText("Cancel");
185 cancelButton.addSelectionListener(new SelectionAdapter() {
186
187 public void widgetSelected(SelectionEvent e) {
188
189 selectedEntry.setComment(commentBackup);
190 selectedEntry.setDuration(durationBackup);
191 selectedEntry.setProject(projectBackup);
192 selectedEntry.setStartTime(startTimeBackup);
193 selectedEntry.setTask(taskBackup);
194 shell.close();
195 }
196 });
197
198 shell.setDefaultButton(okButton);
199 }
200
201 private void createTextWidgets() {
202
203
204 Composite composite = new Composite(shell, SWT.NULL);
205 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
206 GridLayout layout = new GridLayout();
207 layout.numColumns = 2;
208 composite.setLayout(layout);
209
210 for (int i = 0; i <= 4; i++) {
211 Label label = new Label(composite, SWT.RIGHT);
212 Text text = new Text(composite, SWT.BORDER);
213 GridData gridData = new GridData();
214 gridData.widthHint = 400;
215 text.setLayoutData(gridData);
216
217 GC gc = new GC(text);
218 FontMetrics fm = gc.getFontMetrics();
219 int width = 45 * fm.getAverageCharWidth();
220 int height = fm.getHeight();
221 gc.dispose();
222 text.setSize(width, height);
223
224 ColumnEnum colEnum = ColumnEnum.find(i);
225 label.setText(colEnum.getName());
226 String methodName = colEnum.getRetrievalMethodName();
227 try {
228 Method method = EntryTypeImpl.class.getMethod(methodName, null);
229 Object object = method.invoke(selectedEntry, null);
230 String textString = null;
231 if (object instanceof String)
232 textString = (String) object;
233 else if (object instanceof Calendar) {
234 Calendar cal = (Calendar) object;
235 textString = TimeTrackFormater.DATEFORMAT.format(cal
236 .getTime())
237 + " "
238 + TimeTrackFormater.TIMEFORMAT
239 .format(cal.getTime());
240 } else if (object instanceof Long) {
241 textString = ((Long) object).toString();
242 }
243 text.setText(textString);
244 text.setData("field", ColumnEnum.find(i));
245 addTextListener(text);
246 } catch (IllegalArgumentException e) {
247 IStatus status = new Status(IStatus.ERROR,
248 TimeTrackPlugin.PI_RUNTIME, 1, this
249 + "createTextWidgets()", e);
250 LOGGER.log(status);
251 } catch (IllegalAccessException e) {
252 IStatus status = new Status(IStatus.ERROR,
253 TimeTrackPlugin.PI_RUNTIME, 1, this
254 + "createTextWidgets()", e);
255 LOGGER.log(status);
256 } catch (InvocationTargetException e) {
257 IStatus status = new Status(IStatus.ERROR,
258 TimeTrackPlugin.PI_RUNTIME, 1, this
259 + "createTextWidgets()", e);
260 LOGGER.log(status);
261 } catch (SecurityException e) {
262 IStatus status = new Status(IStatus.ERROR,
263 TimeTrackPlugin.PI_RUNTIME, 1, this
264 + "createTextWidgets()", e);
265 LOGGER.log(status);
266 } catch (NoSuchMethodException e) {
267 IStatus status = new Status(IStatus.ERROR,
268 TimeTrackPlugin.PI_RUNTIME, 1, this
269 + "createTextWidgets()", e);
270 LOGGER.log(status);
271 }
272
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 /***
350 * Opens the dialog in the given state. Sets <code>Text</code> widget
351 * contents and dialog behaviour accordingly.
352 *
353 */
354 public final void open() {
355 createTextWidgets();
356 createControlButtons();
357 shell.pack();
358 shell.open();
359 Display display = shell.getDisplay();
360 while (!shell.isDisposed()) {
361 if (!display.readAndDispatch())
362 display.sleep();
363 }
364
365
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 }