Skip to main content
summaryrefslogtreecommitdiffstats
blob: 662212fe7b4a8c6a0a331ef6f72629e523fc6756 (plain) (blame)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
351
352
353
354
355
356
357
358
359
360
361
362
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*******************************************************************************
 * Copyright (c) 2004 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
/*
 * Created on Dec 22, 2004
  */
package org.eclipse.mylar.tasks;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasks.internal.TaskCategory;
import org.eclipse.mylar.tasks.ui.TaskEditorInput;
import org.eclipse.mylar.tasks.ui.views.TaskListView;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.Workbench;


/**
 * @author Mik Kersten
 */
public class Task implements ITask {

    private static final long serialVersionUID = 3545518391537382197L;
    private boolean active = false;
    protected String handle = "-1";
    private boolean category = false;
    
    
    /**
     * Either to local resource or URL.
     * TODO: consider changing to java.net.URL
     */
    private String path;
    private String label;
    private String priority = "P3";
    private String notes = "";
    private String estimatedTime = "";
//    private String elapsedTime = "";
    private boolean completed;
    private RelatedLinks links = new RelatedLinks();
    private TaskCategory parentCategory = null;
    
    private Date timeActivated = null;
    private long elapsed;
    /**
     * null if root
     */
    private transient ITask parent;
    
    private List<ITask> children = new ArrayList<ITask>();
    
    @Override
    public String toString() {
        return label;
    }
    
    public String getPath() {
    	// returns relative path Mylar Directory
        return path;
    }
    
    public void setPath(String path) {
    	if (path.startsWith(".mylar")) {
    		this.path = path.substring(path.lastIndexOf('/')+1, path.length());
    	} else if (!path.equals("")) {
    		this.path = path;
    	}
    }
    
    public Task(String handle, String label) {
        this.handle = handle;
        this.label = label;     
        this.path = handle;
    } 
    
    public String getHandle() {
        return handle;
    }
    public void setHandle(String id) {
        this.handle = id;
    }
    public String getLabel() {
        return label;
    }
    public void setLabel(String label) {
        this.label = label;
    }

    public ITask getParent() {
        return parent;
    }

    public void setParent(ITask parent) {
        this.parent = parent;
    }
    public Object getAdapter(Class adapter) {
        return null;
    }    

    /**
     * Package visible in order to prevent sets that don't update the index.
     */
    public void setActive(boolean active) {
    	this.active = active;
    	if (active) {
    		timeActivated = new Date();
    	} else {    		
    		calculateElapsedTime();
    		timeActivated = null;
    	}    	        
    }
    
    private void calculateElapsedTime() {
    	if (timeActivated == null) 
    		return;
    	elapsed += new Date().getTime() - timeActivated.getTime();
    	if (isActive()) {
    		timeActivated = new Date();
    	} else {
    		timeActivated = null;
    	}
    }
    
    public boolean isActive() {
        return active;
    }
    
    public void openTaskInEditor() {
    	Workbench.getInstance().getDisplay().asyncExec(new Runnable() {
			public void run() {
				openTaskEditor();
			}
		});
    }

	/**
	 * Opens the task in an editor.
	 * @return Resulting <code>IStatus</code> of the operation
	 */
	protected void openTaskEditor() {
		
		// get the active page so that we can reuse it
		IWorkbenchPage page = MylarTasksPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();

		// if we couldn't get a page, get out
		if (page == null) {
			return;
		}

		IEditorInput input = new TaskEditorInput(this);
		try 
		{
			// try to open an editor on the input task
			page.openEditor(input, MylarTasksPlugin.TASK_EDITOR_ID);
			
		} 
		catch (PartInitException ex) 
		{
			MylarPlugin.log(ex, "open failed");
		}
	}

    /**
     * Refreshes the tasklist viewer.
     * 
     * TODO: shouldn't be coupled to the TaskListView
     */
    public void notifyTaskDataChange() {
    	final Task task = this;
    	if (Workbench.getInstance() != null && !Workbench.getInstance().getDisplay().isDisposed()) {
	        Workbench.getInstance().getDisplay().asyncExec(new Runnable() {
	            public void run() {
	                if (TaskListView.getDefault() != null) TaskListView.getDefault().notifyTaskDataChanged(task);
	            }
	        });
    	}
    }
    
	public String getToolTipText() {
		// No tool-tip used for a general task as of yet.
		return null;
	}

    @Override
    public boolean equals(Object obj) {
       if (obj instanceof Task && obj != null) {
           return this.getHandle() == ((Task)obj).getHandle();
       } else {
           return false;
       }
    }
    
    @Override
    public int hashCode() {
        return this.getHandle().hashCode(); 
    }
    public boolean isCompleted() {
    	return completed;
    }
    public void setCompleted(boolean completed) {
        this.completed = completed;
    }

    public boolean isCategory() {
        return category;
    }

    public void setIsCategory(boolean category) {
        this.category = category;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

	public RelatedLinks getRelatedLinks() {
		// TODO: removed check for null once xml updated.
		if (links == null) {
			links = new RelatedLinks();
		}
		return links;
	}

	public void setRelatedLinks(RelatedLinks relatedLinks) {
		this.links = relatedLinks;
	}

	public void addLink(String url) {
		links.add(url);
	}

	public void removeLink(String url) {
		links.remove(url);
	}

	public String getNotes() {
		// TODO: removed check for null once xml updated.
		if (notes == null) {
			notes = "";
		}
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public String getElapsedTime() {
		if (isActive()) {
			calculateElapsedTime();			
		}
		return "" + elapsed;
	}

	public void setElapsedTime(String elapsedString) {
		if (elapsedString.equals("")) {
			elapsed = 0;
		} else {
			elapsed = Long.parseLong(elapsedString);
		}
	}

	public String getEstimatedTime() {
		if (estimatedTime == null) {
			estimatedTime = "";
		}
		return estimatedTime;
	}

	public void setEstimatedTime(String estimated) {
		this.estimatedTime = estimated;
	}	
	
	public List<ITask> getChildren() {
		return children;
	}
	
	public void addSubTask(ITask t) {
		children.add(t);
	}
	
	public void removeSubTask(ITask t) {
		children.remove(t);
	}
	
	public void setCategory(TaskCategory cat) {
		this.parentCategory = cat;
	}
    
    public TaskCategory getCategory() {
    	return parentCategory;
    }

	public Image getIcon() {
		return TaskListImages.getImage(TaskListImages.TASK);
	}

	public String getDescription(boolean label) {
		return getLabel();
	}

	public Image getStatusIcon() {
		if (isActive()) {
    		return TaskListImages.getImage(TaskListImages.TASK_ACTIVE);
    	} else {
    		return TaskListImages.getImage(TaskListImages.TASK_INACTIVE);
    	}        	
	}
	
	public String getElapsedTimeForDisplay() {
		long seconds = elapsed / 1000;
		long minutes = 0;
		long hours = 0;
//		final long SECOND = 1000;
		final long MIN = 60;
		final long HOUR = MIN * 60;
		
		String hour = "";
		String min = "";
		String sec = "";
		if (seconds >= HOUR) {
			hours = seconds / HOUR;
			if (hours == 1) {
				hour = hours + " hour ";
			} else if (hours > 1) {
				hour = hours + " hours ";
			}
			seconds -= hours * HOUR;
			
			minutes = seconds / MIN;
			if (minutes == 1) {
				min = minutes + " minute ";
			} else if (minutes > 1) {
				min = minutes + " minutes ";
			}
//			seconds -= minutes * MIN;
//			if (seconds == 1) {
//				sec = seconds + " second";
//			} else if (seconds > 1) {
//				sec = seconds + " seconds";
//			}
			return hour + min + sec;
		} else if (seconds >= MIN) {
			minutes = seconds / MIN;
			if (minutes == 1) {
				min = minutes + " minute ";
			} else if (minutes > 1) {
				min = minutes + " minutes ";
			}
//			seconds -= minutes * MIN;
//			if (seconds == 1) {
//				sec = seconds + " second";
//			} else if (seconds > 1) {
//				sec = seconds + " seconds";
//			}
			return min + sec;
		} else {
//			if (seconds == 1) {
//				sec = seconds + " second";
//			} else if (seconds > 1) {
//				sec = seconds + " seconds";
//			}
			return sec;
		}
	}
	
    public boolean canEditDescription() {
    	return true;
    }
    
    public String getDeleteConfirmationMessage() {
    	return "Delete the selected task and discard task context?";
    }

	public boolean isDirectlyModifiable() {
		return true;
	}

	public ITask getCorrespondingActivatableTask() {
		return this;
	}
	
	public boolean hasCorrespondingActivatableTask() {
		return true;
	}
	
	public boolean isActivatable() {
		return true;
	}
	
	public boolean isDragAndDropEnabled() {
		return true;
	}

	public boolean participatesInTaskHandles() {
		return true;
	}
	
	public Color getForeground() {
        if (isCompleted()){
        	return GRAY_VERY_LIGHT;
        } else {
        	return null;
        }
	}
	
	public Font getFont(){
		if (isActive()) return BOLD;            
        for (ITask child : getChildren()) {
			if (child.isActive())
				return BOLD;
		}
        return null;
	}
}

Back to the top