1 11 12 package org.eclipse.ui.internal.cheatsheets.composite.model; 13 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Dictionary ; 18 import java.util.Hashtable ; 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 31 32 public abstract class AbstractTask implements ICompositeCheatSheetTask { 33 protected CompositeCheatSheetModel model; 34 protected int state = NOT_STARTED; 35 private String id; 36 37 private String name; 38 39 protected String kind; 40 41 private Dictionary parameters; 42 43 private String description; 44 45 private String completionMessage; 46 47 private ArrayList requiredTasks; 48 49 private ArrayList 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 id, String name, String kind) { 58 this.model = model; 59 this.id = id; 60 this.name = name; 61 this.kind = kind; 62 this.parameters = new Hashtable (); 63 this.description = ""; requiredTasks = new ArrayList (); 65 } 66 67 public String getId() { 68 return id; 69 } 70 71 public String getName() { 72 return name; 73 } 74 75 public String getKind() { 76 return kind; 77 } 78 79 public Dictionary getParameters() { 80 return parameters; 81 } 82 83 public String getDescription() { 84 return description; 85 } 86 87 public void setDescription(String description) { 88 this.description = description; 89 } 90 91 public void setCompletionMessage(String completionMessage) { 92 this.completionMessage = completionMessage; 93 } 94 95 public String 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 (); 112 requiredTasks.add(task); 113 if (task.successorTasks==null) 114 task.successorTasks = new ArrayList (); 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 144 public boolean requiresTask(ICompositeCheatSheetTask candidateTask) { 145 return (requiredTasks.contains(candidateTask)); 146 } 147 148 153 public void setState(int state) { 154 setStateNoNotify(state); 155 model.sendTaskChangeEvents(); 156 } 157 158 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 177 public URL getInputUrl(String path) throws MalformedURLException { 178 int index = path.indexOf('/', 1); 179 if (index >= 1) { 180 String bundleName = path.substring(1, index); 181 String 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 (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 |