1 11 12 package org.eclipse.ui.internal.cheatsheets.composite.model; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask; 18 import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup; 19 20 public class SuccesorTaskFinder { 21 22 private AbstractTask currentTask; 23 ICompositeCheatSheetTask bestLaterTask; 24 ICompositeCheatSheetTask bestEarlierTask; 25 private boolean seenThisTask; 26 27 public SuccesorTaskFinder(ICompositeCheatSheetTask task) { 28 currentTask = (AbstractTask)task; 29 } 30 31 43 public ICompositeCheatSheetTask[] getRecommendedSuccessors() 44 { 45 if (ITaskGroup.CHOICE.equals(currentTask.getKind())) { 47 List runnableChoices = findRunnableChoices(); 49 if (runnableChoices.size() != 0) { 50 return (ICompositeCheatSheetTask[])runnableChoices.toArray 51 ( new ICompositeCheatSheetTask[runnableChoices.size()]); 52 } 53 } 54 return getBestSuccessor(); 55 } 56 57 private List findRunnableChoices() { 58 List result; 59 result = new ArrayList (); 60 if (isStartable(currentTask)) { 61 ICompositeCheatSheetTask[] subtasks = currentTask.getSubtasks(); 62 for (int i = 0; i < subtasks.length; i++) { 63 if (isStartable(subtasks[i])) { 64 result.add(subtasks[i]); 65 } 66 } 67 } 68 return result; 69 } 70 71 private boolean isStartable(ICompositeCheatSheetTask task) { 72 int state = task.getState(); 73 return (state != ICompositeCheatSheetTask.COMPLETED && 74 state != ICompositeCheatSheetTask.SKIPPED && 75 task.requiredTasksCompleted()); 76 } 77 78 private ICompositeCheatSheetTask[] getBestSuccessor() { 79 bestLaterTask = null; 80 bestEarlierTask = null; 81 seenThisTask = false; 82 searchRunnableChildren(currentTask.getCompositeCheatSheet().getRootTask()); 83 if (bestLaterTask != null) { 86 return new ICompositeCheatSheetTask[] {bestLaterTask}; 87 } 88 if (bestEarlierTask != null) { 89 return new ICompositeCheatSheetTask[] {bestEarlierTask}; 90 } 91 return new ICompositeCheatSheetTask[0]; 92 } 93 94 private void searchRunnableChildren(ICompositeCheatSheetTask task) { 95 if (bestLaterTask != null) { 98 return; 99 } 100 if (task == currentTask) { 101 seenThisTask = true; 102 } 103 if (task.getState() == ICompositeCheatSheetTask.COMPLETED || 104 task.getState() == ICompositeCheatSheetTask.SKIPPED ) { 105 if (isTaskAncestor(task, currentTask)) { 106 seenThisTask = true; 107 } 108 return; 109 } 110 111 if ( isStartable(task) && task != currentTask) { 112 if (seenThisTask) { 113 if (bestLaterTask == null) { 114 bestLaterTask = task; 115 } 116 } else { 117 if (bestEarlierTask == null) { 118 bestEarlierTask = task; 119 } 120 } 121 } 122 123 ICompositeCheatSheetTask[] subtasks = task.getSubtasks(); 124 for (int i = 0; i < subtasks.length; i++) { 125 searchRunnableChildren(subtasks[i]); 126 } 127 128 } 129 130 private boolean isTaskAncestor(ICompositeCheatSheetTask ancestorCandididate, ICompositeCheatSheetTask task) { 131 ICompositeCheatSheetTask nextTask = task; 132 while (nextTask != null) { 133 if (nextTask == ancestorCandididate) { 134 return true; 135 } 136 nextTask = nextTask.getParent(); 137 } 138 return false; 139 } 140 } 141 | Popular Tags |