KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.net.URL JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Observable JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.cheatsheets.ICheatSheetManager;
24 import org.eclipse.ui.internal.cheatsheets.data.ICheatSheet;
25 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
26 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheet;
27 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
28
29 public class CompositeCheatSheetModel extends Observable JavaDoc implements ICompositeCheatSheet, ICheatSheet{
30
31     private String JavaDoc name;
32     private String JavaDoc description;
33     private String JavaDoc explorerId;
34     private ICompositeCheatSheetTask rootTask;
35     private TaskDependencies dependencies;
36     private String JavaDoc id;
37     private CompositeCheatSheetSaveHelper saveHelper;
38     private URL JavaDoc contentURL;
39     private CheatSheetManager manager;
40     private Set JavaDoc stateChangedSet = new HashSet JavaDoc();
41     
42     public void setRootTask(ICompositeCheatSheetTask task) {
43         rootTask = task;
44     }
45     
46     public CompositeCheatSheetModel(String JavaDoc name, String JavaDoc description, String JavaDoc explorerId) {
47         this.name = name;
48         this.description = description;
49         this.explorerId = explorerId;
50         this.dependencies = new TaskDependencies();
51     }
52     
53     public String JavaDoc getName() {
54         return name;
55     }
56
57     public String JavaDoc getDescription() {
58         return description;
59     }
60
61     public String JavaDoc getTaskExplorerId() {
62         return explorerId;
63     }
64     
65     public ICompositeCheatSheetTask getRootTask() {
66         return rootTask;
67     }
68
69     public void setDependencies(TaskDependencies dependencies) {
70         this.dependencies = dependencies;
71     }
72
73     public TaskDependencies getDependencies() {
74         return dependencies;
75     }
76     
77     public URL JavaDoc getContentUrl() {
78         return contentURL;
79     }
80
81     public void setContentUrl(URL JavaDoc newUrl) {
82         contentURL=newUrl;
83     }
84     
85     public void setId(String JavaDoc id) {
86         this.id = id;
87     }
88
89     public String JavaDoc getId() {
90         return id;
91     }
92
93     public void setSaveHelper(CompositeCheatSheetSaveHelper saveHelper) {
94         this.saveHelper = saveHelper;
95     }
96     
97     /**
98      * Maintain a set of tasks which have been changed which will
99      * be used to send events to observers.
100      * @param task
101      */

102     void stateChanged(ICompositeCheatSheetTask task) {
103         if (!stateChangedSet.contains(task)) {
104             stateChangedSet.add(task);
105         }
106     }
107     
108     /**
109      * At this point we need to determine which blocked tasks have
110      * become unblocked and which unblocked tasks are now blocked and
111      * send events for those also.
112      */

113     public void sendTaskChangeEvents() {
114         Set JavaDoc blockedStateChanged = new BlockedTaskFinder().findBlockedTaskChanges(stateChangedSet);
115         for (Iterator JavaDoc iter = stateChangedSet.iterator(); iter.hasNext();) {
116             setChanged();
117             notifyObservers(iter.next());
118         }
119         for (Iterator JavaDoc iter = blockedStateChanged.iterator(); iter.hasNext();) {
120             setChanged();
121             notifyObservers(iter.next());
122         }
123         stateChangedSet.clear();
124     }
125     
126     public IMemento getTaskMemento(String JavaDoc id) {
127          return saveHelper.getTaskMemento(id);
128     }
129
130     public ICheatSheetManager getCheatSheetManager() {
131         return manager;
132     }
133     
134     public void setCheatSheetManager(CheatSheetManager manager) {
135         this.manager = manager;
136     }
137
138     public void loadState(Map JavaDoc layoutData) {
139         saveHelper.loadCompositeState(this, layoutData);
140     }
141     
142     /*
143      * Reset the state of a task and it's children
144      */

145     private void resetTask(ICompositeCheatSheetTask task) {
146         if (task instanceof EditableTask) {
147             EditableTask editable = (EditableTask)task;
148             editable.reset();
149             if (saveHelper != null) {
150                 saveHelper.clearTaskMemento(task.getId());
151             }
152         } else if (task instanceof TaskGroup) {
153             TaskGroup group = (TaskGroup)task;
154             ICompositeCheatSheetTask[] subtasks = group.getSubtasks();
155             for (int i = 0; i < subtasks.length; i++) {
156                 resetTask(subtasks[i]);
157             }
158            group.setStateNoNotify(ICompositeCheatSheetTask.NOT_STARTED);
159         }
160     }
161
162     public void resetAllTasks(Map JavaDoc cheatSheetData) {
163         if (manager != null) {
164             if (cheatSheetData == null) {
165                 manager.setData(new Hashtable JavaDoc());
166             } else {
167                 manager.setData(cheatSheetData);
168             }
169         }
170         saveHelper.clearTaskMementos();
171         resetTask(getRootTask());
172         sendTaskChangeEvents();
173     }
174
175     /**
176      * Restart one or more tasks
177      * @param restartTasks An array of the tasks to be restarted
178      */

179     public void resetTasks(ICompositeCheatSheetTask[] restartTasks) {
180         for (int i = 0; i < restartTasks.length; i++) {
181             resetTask(restartTasks[i]);
182         }
183         sendTaskChangeEvents();
184     }
185
186 }
187
Popular Tags