1 11 12 package org.eclipse.ui.internal.cheatsheets.composite.model; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.ui.internal.cheatsheets.Messages; 25 import org.eclipse.ui.internal.cheatsheets.composite.parser.IStatusContainer; 26 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask; 27 28 31 32 public class TaskDependencies { 33 34 private class Dependency { 35 private AbstractTask sourceTask; 36 37 private String requiredTaskId; 38 39 public Dependency(AbstractTask sourceTask, String requiredTaskId) { 40 this.sourceTask = sourceTask; 41 this.requiredTaskId = requiredTaskId; 42 } 43 44 public AbstractTask getSourceTask() { 45 return sourceTask; 46 } 47 48 public String getRequiredTaskId() { 49 return requiredTaskId; 50 } 51 } 52 53 private List dependencies; 54 55 private Map taskIdMap = new HashMap (); 56 57 public void saveId(AbstractTask task) { 58 String id = task.getId(); 59 if (id != null) { 60 taskIdMap.put(id, task); 61 } 62 } 63 64 public AbstractTask getTask(String id) { 65 return (AbstractTask)taskIdMap.get(id); 66 } 67 68 public TaskDependencies() { 69 dependencies = new ArrayList (); 70 } 71 72 77 public void addDependency(AbstractTask sourceTask, String requiredTaskId) { 78 dependencies.add(new Dependency(sourceTask, requiredTaskId)); 79 } 80 81 86 public void resolveDependencies(IStatusContainer status) { 87 for (Iterator dependencyIterator = dependencies.iterator(); dependencyIterator.hasNext();) { 88 Dependency dep = (Dependency)dependencyIterator.next(); 89 AbstractTask sourceTask = dep.getSourceTask(); 90 AbstractTask requiredTask = getTask(dep.requiredTaskId); 91 if (requiredTask == null) { 92 String message = NLS.bind(Messages.ERROR_PARSING_INVALID_ID, (new Object [] {dep.getRequiredTaskId()})); 93 status.addStatus(IStatus.ERROR, message, null); 94 } else if (!sourceTask.requiresTask(requiredTask)) { 95 sourceTask.addRequiredTask(requiredTask); 96 } 97 } 98 checkForCircularities (status); 99 } 100 101 112 private void checkForCircularities (IStatusContainer status) { 113 Set tasks = new HashSet (); 114 for (Iterator idIterator = taskIdMap.values().iterator(); idIterator.hasNext(); ) { 116 AbstractTask nextTask = (AbstractTask)idIterator.next(); 117 if (nextTask.getRequiredTasks().length > 0) { 118 tasks.add(nextTask); 119 } 120 } 121 boolean makingProgress = true; 122 while (makingProgress) { 123 Set remainingTasks = new HashSet (); 126 makingProgress = false; 127 for (Iterator taskIterator = tasks.iterator(); taskIterator.hasNext() && !makingProgress; ) { 128 boolean mayBeInCycle = false; 129 ICompositeCheatSheetTask nextTask = (ICompositeCheatSheetTask)taskIterator.next(); 130 ICompositeCheatSheetTask[] requiredTasks = nextTask.getRequiredTasks(); 131 for (int i = 0; i < requiredTasks.length; i++) { 132 if (tasks.contains(requiredTasks[i])) { 133 mayBeInCycle = true; 134 } 135 } 136 if (mayBeInCycle) { 137 remainingTasks.add(nextTask); 138 } else { 139 makingProgress = true; 140 } 141 } 142 tasks = remainingTasks; 143 } 144 if (!tasks.isEmpty()) { 145 status.addStatus(IStatus.ERROR, Messages.ERROR_PARSING_CYCLE_DETECTED, null); 146 List cycle = new ArrayList (); 148 ICompositeCheatSheetTask cycleStartTask = (ICompositeCheatSheetTask)tasks.iterator().next(); 149 while (!cycle.contains(cycleStartTask)) { 150 cycle.add(cycleStartTask);ICompositeCheatSheetTask[] requiredTasks = cycleStartTask.getRequiredTasks(); 151 for (int i = 0; i < requiredTasks.length; i++) { 152 if (tasks.contains(requiredTasks[i])) { 153 cycleStartTask=requiredTasks[i]; 154 } 155 } 156 } 157 boolean cycleStarted = false; 160 String thisTask = null; 161 String lastTask = null; 162 String firstTask = null; 163 for (Iterator cycleIterator = cycle.iterator(); cycleIterator.hasNext();) { 164 ICompositeCheatSheetTask task = (ICompositeCheatSheetTask)cycleIterator.next(); 165 if (task == cycleStartTask) { 166 cycleStarted = true; 167 firstTask = task.getName(); 168 } 169 if (cycleStarted) { 170 lastTask = thisTask; 172 thisTask = task.getName(); 173 if (lastTask != null) { 174 String message = NLS.bind(Messages.ERROR_PARSING_CYCLE_CONTAINS, (new Object [] {lastTask, thisTask})); 175 status.addStatus(IStatus.ERROR, message, null); 176 } 177 } 178 } 179 String message = NLS.bind(Messages.ERROR_PARSING_CYCLE_CONTAINS, (new Object [] {thisTask, firstTask})); 180 status.addStatus(IStatus.ERROR, message, null); 181 } 182 } 183 184 } 185 | Popular Tags |