View Javadoc

1   /* ======================================
2    * Copyright (c) 2004-2005 Achim Nierbeck
3    * All rights reserved.
4    *
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.gnu.org/licenses/lgpl.html
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * ======================================
16   */
17  
18  /*
19   * EditEntryDialog.java
20   * $Revision: 1.4 $
21   * Date 14.12.2005
22   * 
23   * $Author: nierbeck $ 
24   * $Date: 2006/02/02 21:33:07 $ 
25   * 
26   */
27  package de.nierbeck.timeTrack.views;
28  
29  /*
30   * (c) Copyright IBM Corp. 2000, 2002. This file is made available under the
31   * terms of the Common Public License v1.0 which accompanies this distribution,
32   * and is available at http://www.eclipse.org/legal/cpl-v10.html
33   */
34  
35  /* Imports */
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 				// Integer index = (Integer) (text.getData("index"));
122 				// values[index.intValue()] = text.getText();
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 							// TODO Auto-generated catch block
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 				// AN: Hier muss der vorherige Zustand zurückgesetzt werden.
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 		// if (labels == null) return;
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 		// if (values == null) values = new String[labels.length];
276 
277 		/*
278 		 * for (int i = 0; i < labels.length; i++) { if (i < 2) { Label label =
279 		 * new Label(composite, SWT.RIGHT); label.setText(labels[i]);
280 		 * 
281 		 * Text text = new Text(composite, SWT.BORDER); GridData gridData = new
282 		 * GridData(); gridData.widthHint = 400; text.setLayoutData(gridData);
283 		 * if (values[i] != null) { text.setText(values[i]); }
284 		 * text.setData("index", new Integer(i)); addTextListener(text); } else
285 		 * if (i == 2) { Label label = new Label(composite, SWT.RIGHT);
286 		 * label.setText(labels[i]);
287 		 * 
288 		 * Collection c = apt.getActivities(); Iterator it = c.iterator();
289 		 * activities = new String[c.size()]; int k = 0; while (it.hasNext())
290 		 * activities[k++]=(String) it.next();
291 		 * 
292 		 * ActivityCombo combo = new ActivityCombo( composite, SWT.NONE, entry,
293 		 * entryList, apt, activities);
294 		 * 
295 		 * ActivityFocusController focusController = new
296 		 * ActivityFocusController(combo);
297 		 * combo.addFocusListener(focusController); ActivityController
298 		 * activityController = new ActivityController(combo);
299 		 * combo.addListener(SWT.DefaultSelection, activityController); } else
300 		 * if (i == 3) { Collection c = apt.getProjects(entry.getActivity());
301 		 * Iterator it = c.iterator(); int k = 0; projects = new
302 		 * String[c.size()]; while (it.hasNext()) projects[k++] = (String)
303 		 * it.next(); Label label = new Label(composite, SWT.RIGHT);
304 		 * label.setText(labels[i]);
305 		 * 
306 		 * ProjectCombo combo = new ProjectCombo(composite, SWT.NONE, entry,
307 		 * entryList, apt, projects);
308 		 * 
309 		 * ProjectFocusController pFocusController = new
310 		 * ProjectFocusController(combo); ProjectController projectController =
311 		 * new ProjectController(combo);
312 		 * 
313 		 * combo.addFocusListener(pFocusController);
314 		 * combo.addListener(SWT.DefaultSelection, projectController); } else if
315 		 * (i == 4) { Label label = new Label(composite, SWT.RIGHT);
316 		 * label.setText(labels[i]);
317 		 * 
318 		 * Collection c = apt.getTasks(entry.getActivity(), entry.getProject());
319 		 * Iterator it = c.iterator(); int k = 0; tasks = new String[c.size()];
320 		 * while (it.hasNext()) tasks[k++] = (String) it.next();
321 		 * 
322 		 * TaskCombo combo = new TaskCombo(composite, SWT.NONE, entry,
323 		 * entryList, apt, tasks); TaskFocusController tFocusControll = new
324 		 * TaskFocusController(combo); TaskController taskController = new
325 		 * TaskController(combo);
326 		 * 
327 		 * combo.addFocusListener(tFocusControll);
328 		 * combo.addListener(SWT.DefaultSelection, taskController); } }
329 		 */
330 	}
331 
332 	/*
333 	 * public String[] getLabels() { return labels; }
334 	 * 
335 	 * public String getTitle() { return shell.getText(); }
336 	 */
337 
338 	/*
339 	 * /** Returns the contents of the <code>Text</code> widgets in the dialog
340 	 * in a <code>String</code> array.
341 	 * 
342 	 * @return String[] The contents of the text widgets of the dialog. May
343 	 * return null if all text widgets are empty.
344 	 */
345 	/*
346 	 * public String[] getValues() { return values; }
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 		// return getValues();
366 	}
367 
368 	/*
369 	 * public void setLabels(String[] labels) { this.labels = labels; }
370 	 */
371 
372 	/*
373 	 * public void setTitle(String title) { shell.setText(title); } /** Sets the
374 	 * values of the <code>Text</code> widgets of the dialog to the values
375 	 * supplied in the parameter array.
376 	 * 
377 	 * @param itemInfo String[] The values to which the dialog contents will be
378 	 * set.
379 	 */
380 	/*
381 	 * public void setValues(String[] itemInfo) { if (labels == null) return;
382 	 * 
383 	 * if (values == null) values = new String[labels.length];
384 	 * 
385 	 * int numItems = Math.min(values.length, itemInfo.length); // for (int i =
386 	 * 0; i < numItems; i++) { // values[i] = itemInfo[i]; // } // Datefield
387 	 * values[0] = itemInfo[0]; // Duration values[1] = itemInfo[1]; // Activity
388 	 * values[2] = itemInfo[2]; // Project values[3] = itemInfo[3]; // Task
389 	 * values[4] = itemInfo[4]; }
390 	 */
391 
392 	/*
393 	 * public void setValues(DataEntryValueObject vo) { if (labels == null)
394 	 * return;
395 	 * 
396 	 * if (values == null) values = new String[2];
397 	 * 
398 	 * DateFormat df = DateFormat.getDateInstance(); values[0] =
399 	 * df.format(vo.getEntry().getStartDate()); values[1] =
400 	 * Long.toString(vo.getEntry().getDuration()); apt = vo.getApt(); entry =
401 	 * vo.getEntry(); entryList = vo.getEntryList();
402 	 * 
403 	 * activities = vo.getActivities(); projects = vo.getProjects(); tasks =
404 	 * vo.getTasks(); }
405 	 */
406 }