// iterating over and deleting from the same set.
Set remainingTasks = new HashSet();
makingProgress = false;
for (Iterator taskIterator = tasks.iterator(); taskIterator.hasNext() && !makingProgress; ) {
boolean mayBeInCycle = false;
ICompositeCheatSheetTask nextTask = (ICompositeCheatSheetTask)taskIterator.next();
ICompositeCheatSheetTask[] requiredTasks = nextTask.getRequiredTasks();
for (int i = 0; i < requiredTasks.length; i++) {
if (tasks.contains(requiredTasks[i])) {
mayBeInCycle = true;
}
}
if (mayBeInCycle) {
remainingTasks.add(nextTask);
} else {
makingProgress = true;
}
}
tasks = remainingTasks;
}
if (!tasks.isEmpty()) {
status.addStatus(IStatus.ERROR, Messages.ERROR_PARSING_CYCLE_DETECTED, null);
// Detect one of the cycles and report its members
List cycle = new ArrayList();
ICompositeCheatSheetTask cycleStartTask = (ICompositeCheatSheetTask)tasks.iterator().next();
while (!cycle.contains(cycleStartTask)) {
cycle.add(cycleStartTask);ICompositeCheatSheetTask[] requiredTasks = cycleStartTask.getRequiredTasks();
for (int i = 0; i < requiredTasks.length; i++) {
if (tasks.contains(requiredTasks[i])) {
cycleStartTask=requiredTasks[i];
}
}
}
// Now the list contains a cycle and possibly additional tasks at the start
// of the list
boolean cycleStarted = false;
String thisTask = null;
String lastTask = null;
String firstTask = null;
for (Iterator cycleIterator = cycle.iterator(); cycleIterator.hasNext();) {
ICompositeCheatSheetTask task = (ICompositeCheatSheetTask)cycleIterator.next();
if (task == cycleStartTask) {
cycleStarted = true;
firstTask = task.getName();
}
if (cycleStarted) {
// Save the name of this task
lastTask = thisTask;
thisTask = task.getName();
if (lastTask != null) {
String message = NLS.bind(Messages.ERROR_PARSING_CYCLE_CONTAINS, (new Object[] {lastTask, thisTask}));
status.addStatus(IStatus.ERROR, message, null);
}
}