KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > model > AbstractTask


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  *******************************************************************************/

11
12 package org.eclipse.ui.internal.cheatsheets.composite.model;
13
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Dictionary JavaDoc;
18 import java.util.Hashtable JavaDoc;
19
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.ui.internal.cheatsheets.composite.parser.ITaskParseStrategy;
22 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheet;
23 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
24 import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup;
25 import org.osgi.framework.Bundle;
26
27 /**
28  * A single task within a composite cheatsheet. This class encapsulates the
29  * behavior common to editable tasks and taskGroups
30  */

31
32 public abstract class AbstractTask implements ICompositeCheatSheetTask {
33     protected CompositeCheatSheetModel model;
34     protected int state = NOT_STARTED;
35     private String JavaDoc id;
36
37     private String JavaDoc name;
38
39     protected String JavaDoc kind;
40
41     private Dictionary JavaDoc parameters;
42     
43     private String JavaDoc description;
44         
45     private String JavaDoc completionMessage;
46
47     private ArrayList JavaDoc requiredTasks;
48     
49     private ArrayList JavaDoc successorTasks;
50     
51     private boolean skippable;
52     
53     private TaskGroup parent;
54
55     protected static final ICompositeCheatSheetTask[] EMPTY = new ICompositeCheatSheetTask[0];
56
57     public AbstractTask(CompositeCheatSheetModel model, String JavaDoc id, String JavaDoc name, String JavaDoc kind) {
58         this.model = model;
59         this.id = id;
60         this.name = name;
61         this.kind = kind;
62         this.parameters = new Hashtable JavaDoc();
63         this.description = ""; //$NON-NLS-1$
64
requiredTasks = new ArrayList JavaDoc();
65     }
66
67     public String JavaDoc getId() {
68         return id;
69     }
70
71     public String JavaDoc getName() {
72         return name;
73     }
74
75     public String JavaDoc getKind() {
76         return kind;
77     }
78
79     public Dictionary JavaDoc getParameters() {
80         return parameters;
81     }
82
83     public String JavaDoc getDescription() {
84         return description;
85     }
86     
87     public void setDescription(String JavaDoc description) {
88         this.description = description;
89     }
90     
91     public void setCompletionMessage(String JavaDoc completionMessage) {
92         this.completionMessage = completionMessage;
93     }
94
95     public String JavaDoc getCompletionMessage() {
96         return completionMessage;
97     }
98
99     public ICompositeCheatSheetTask[] getRequiredTasks() {
100         if (requiredTasks==null) return EMPTY;
101         return (ICompositeCheatSheetTask[])requiredTasks.toArray(new ICompositeCheatSheetTask[requiredTasks.size()]);
102     }
103
104     public ICompositeCheatSheetTask[] getSuccessorTasks() {
105         if (successorTasks==null) return EMPTY;
106         return (ICompositeCheatSheetTask[])successorTasks.toArray(new ICompositeCheatSheetTask[successorTasks.size()]);
107     }
108     
109     public void addRequiredTask(AbstractTask task) {
110         if (requiredTasks==null)
111             requiredTasks = new ArrayList JavaDoc();
112         requiredTasks.add(task);
113         if (task.successorTasks==null)
114             task.successorTasks = new ArrayList JavaDoc();
115         task.successorTasks.add(this);
116     }
117
118     public int getState() {
119         return state;
120     }
121
122     public void complete() {
123         setState(COMPLETED);
124     }
125
126     public boolean requiredTasksCompleted() {
127         boolean startable = true;
128         ICompositeCheatSheetTask[] requiredTasks = getRequiredTasks();
129         for (int i = 0; i < requiredTasks.length; i++) {
130             if (requiredTasks[i].getState() != COMPLETED &&
131                 requiredTasks[i].getState() != SKIPPED ) {
132                 startable = false;
133             }
134         }
135         return startable;
136     }
137     
138     /**
139      * Determine whether the candidate task is a required task for this task.
140      * This function does not test for indirectly required tasks
141      * @param candidateTask a task which may be a required task
142      * @return true if candidateTask is in the list of required tasks.
143      */

144     public boolean requiresTask(ICompositeCheatSheetTask candidateTask) {
145         return (requiredTasks.contains(candidateTask));
146     }
147
148     /**
149      * Interface used when restoring state from a file.
150      * Not intended to be called from task editors.
151      * @param state
152      */

153     public void setState(int state) {
154         setStateNoNotify(state);
155         model.sendTaskChangeEvents();
156     }
157     
158     /**
159      * Set the state of a task but don't send out any events yet,
160      * let them collect so we don't send out multiple events for
161      * one task
162      * @param state
163      */

164     public void setStateNoNotify(int state) {
165         this.state = state;
166         if (parent != null) {
167             parent.checkState();
168         }
169         model.stateChanged(this);
170     }
171
172     /*
173      * Resolves the given path to a URL. The path can either be fully qualified with
174      * the plugin id, e.g. "/plugin_id/path/file.xml" or relative to the composite cheat
175      * sheet file, e.g. "tasks/task1.xml".
176      */

177     public URL JavaDoc getInputUrl(String JavaDoc path) throws MalformedURLException JavaDoc {
178         int index = path.indexOf('/', 1);
179         if (index >= 1) {
180             String JavaDoc bundleName = path.substring(1, index);
181             String JavaDoc relativePath = path.substring(index + 1);
182             Bundle bundle = Platform.getBundle(bundleName);
183             if (bundle != null) {
184                 return bundle.getEntry(relativePath);
185             }
186         }
187         return new URL JavaDoc(model.getContentUrl(), path);
188
189     }
190
191     public ICompositeCheatSheet getCompositeCheatSheet() {
192         return model;
193     }
194     
195     public abstract ITaskParseStrategy getParserStrategy();
196
197     public abstract ICompositeCheatSheetTask[] getSubtasks();
198
199     public void setSkippable(boolean skippable) {
200         this.skippable = skippable;
201     }
202
203     public boolean isSkippable() {
204         return skippable;
205     }
206
207     protected void setParent(TaskGroup parent) {
208         this.parent = parent;
209     }
210
211     public ITaskGroup getParent() {
212         return parent;
213     }
214     
215     public int hashCode() {
216         return getId().hashCode();
217     }
218
219 }
220
Popular Tags