KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > model > TaskStateUtilities


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.cheatsheets.composite.model;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
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 /**
24  * This class contains utility functions to determine the state of a task based on
25  * dependencies, parent state etc.
26  */

27
28 public class TaskStateUtilities {
29     
30     /**
31      * Find the most recent ancestor of this task that is blocked
32      * @param task
33      * @return A blocked task or null if no ancestors are blocked
34      */

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     /**
47      * Find the most recent ancestor of this task that is skipped
48      * @param task
49      * @return A skipped task or null if no ancestors are skipped
50      */

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     /**
63      * Find the most recent ancestor of this task that is completed
64      * @param task
65      * @return A completed task or null if no ancestors are completed
66      */

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     /**
79      * Determine whether a task can be skipped.
80      * A task can be skipped if it is skippable, its state is not SKIPPED or completed
81      * and it has no skipped ot completed ancestors.
82      */

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     /**
93      * Determine whether a task can be started
94      * Only editable tasks which are not blocked and whose ancestors
95      * are not completed can be started
96      */

97     public static boolean isStartEnabled(ICompositeCheatSheetTask task) {
98         if (!(task instanceof IEditableTask)) return false;
99         return isStartable(task);
100     }
101     
102     /**
103      * Determines whether a task is in a state where it has net been started and
104      * cannot be started. This is used to determine when to gray out the icon for a task.
105      */

106     public static boolean isBlocked(ICompositeCheatSheetTask task) {
107         return (task.getState() == ICompositeCheatSheetTask.NOT_STARTED && !isStartable(task));
108     }
109
110     /**
111      * Determines whether an editable task, or a task group has anything
112      * that would prevent it or its children from being started.
113      */

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     /**
124      * Determine which tasks need to be restarted if this tasks is restarted
125      */

126     public static AbstractTask[] getRestartTasks(ICompositeCheatSheetTask task) {
127         List JavaDoc restartables = new ArrayList JavaDoc();
128         Set JavaDoc visited = new HashSet JavaDoc();
129         addRestartableTasks(restartables, task, visited);
130         return (AbstractTask[])restartables.toArray(new AbstractTask[restartables.size()]);
131     }
132
133     
134     private static void addRestartableTasks(List JavaDoc restartables, ICompositeCheatSheetTask task, Set JavaDoc 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         // Add all children
146
ICompositeCheatSheetTask[] children = task.getSubtasks();
147         for (int i = 0; i < children.length; i++) {
148             addRestartableTasks(restartables, children[i], visited);
149         }
150         
151         // Add all dependents that are started or in progress but not skipped
152
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