1 11 12 package org.eclipse.ui.internal.cheatsheets.composite.model; 13 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask; 20 import org.eclipse.ui.internal.provisional.cheatsheets.IEditableTask; 21 import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup; 22 23 27 28 public class TaskStateUtilities { 29 30 35 public static ICompositeCheatSheetTask findBlockedAncestor(ICompositeCheatSheetTask task) { 36 ITaskGroup parent = ((AbstractTask)task).getParent(); 37 if (parent == null) { 38 return null; 39 } 40 if (!parent.requiredTasksCompleted()) { 41 return parent; 42 } 43 return findBlockedAncestor(parent); 44 } 45 46 51 public static ICompositeCheatSheetTask findSkippedAncestor(ICompositeCheatSheetTask task) { 52 ITaskGroup parent = ((AbstractTask)task).getParent(); 53 if (parent == null) { 54 return null; 55 } 56 if (parent.getState() == ICompositeCheatSheetTask.SKIPPED) { 57 return parent; 58 } 59 return findSkippedAncestor(parent); 60 } 61 62 67 public static ICompositeCheatSheetTask findCompletedAncestor(ICompositeCheatSheetTask task) { 68 ITaskGroup parent = ((AbstractTask)task).getParent(); 69 if (parent == null) { 70 return null; 71 } 72 if (parent.getState() == ICompositeCheatSheetTask.COMPLETED) { 73 return parent; 74 } 75 return findCompletedAncestor(parent); 76 } 77 78 83 public static boolean isSkipEnabled(ICompositeCheatSheetTask task) { 84 if (!task.isSkippable()) return false; 85 if (task.getState() == ICompositeCheatSheetTask.COMPLETED) return false; 86 if (task.getState() == ICompositeCheatSheetTask.SKIPPED) return false; 87 if (findCompletedAncestor(task) != null) return false; 88 if (findSkippedAncestor(task) != null) return false; 89 return true; 90 } 91 92 97 public static boolean isStartEnabled(ICompositeCheatSheetTask task) { 98 if (!(task instanceof IEditableTask)) return false; 99 return isStartable(task); 100 } 101 102 106 public static boolean isBlocked(ICompositeCheatSheetTask task) { 107 return (task.getState() == ICompositeCheatSheetTask.NOT_STARTED && !isStartable(task)); 108 } 109 110 114 private static boolean isStartable(ICompositeCheatSheetTask task) { 115 if (task.getState() != ICompositeCheatSheetTask.NOT_STARTED) return false; 116 if (findSkippedAncestor(task) != null) return false; 117 if (findCompletedAncestor(task) != null) return false; 118 if (!task.requiredTasksCompleted()) return false; 119 if (findBlockedAncestor(task) != null) return false; 120 return true; 121 } 122 123 126 public static AbstractTask[] getRestartTasks(ICompositeCheatSheetTask task) { 127 List restartables = new ArrayList (); 128 Set visited = new HashSet (); 129 addRestartableTasks(restartables, task, visited); 130 return (AbstractTask[])restartables.toArray(new AbstractTask[restartables.size()]); 131 } 132 133 134 private static void addRestartableTasks(List restartables, ICompositeCheatSheetTask task, Set visited) { 135 if (visited.contains(task)) { 136 return; 137 } 138 visited.add(task); 139 if (task instanceof IEditableTask && task.getState() != ICompositeCheatSheetTask.NOT_STARTED) { 140 restartables.add(task); 141 } else if (task.getState() == ICompositeCheatSheetTask.SKIPPED){ 142 restartables.add(task); 143 } 144 145 ICompositeCheatSheetTask[] children = task.getSubtasks(); 147 for (int i = 0; i < children.length; i++) { 148 addRestartableTasks(restartables, children[i], visited); 149 } 150 151 ICompositeCheatSheetTask[] successors = ((AbstractTask)task).getSuccessorTasks(); 153 for (int i = 0; i < successors.length; i++) { 154 int state = successors[i].getState(); 155 if (state == ICompositeCheatSheetTask.COMPLETED || state == ICompositeCheatSheetTask.IN_PROGRESS) { 156 addRestartableTasks(restartables, successors[i], visited); 157 } 158 } 159 } 160 161 162 163 } 164 | Popular Tags |