001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.composite.model;
011:
012: import java.net.URL;
013: import java.util.HashSet;
014: import java.util.Hashtable;
015: import java.util.Iterator;
016: import java.util.Map;
017: import java.util.Observable;
018: import java.util.Set;
019:
020: import org.eclipse.ui.IMemento;
021: import org.eclipse.ui.cheatsheets.ICheatSheetManager;
022: import org.eclipse.ui.internal.cheatsheets.data.ICheatSheet;
023: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
024: import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheet;
025: import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
026:
027: public class CompositeCheatSheetModel extends Observable implements
028: ICompositeCheatSheet, ICheatSheet {
029:
030: private String name;
031: private String description;
032: private String explorerId;
033: private ICompositeCheatSheetTask rootTask;
034: private TaskDependencies dependencies;
035: private String id;
036: private CompositeCheatSheetSaveHelper saveHelper;
037: private URL contentURL;
038: private CheatSheetManager manager;
039: private Set stateChangedSet = new HashSet();
040:
041: public void setRootTask(ICompositeCheatSheetTask task) {
042: rootTask = task;
043: }
044:
045: public CompositeCheatSheetModel(String name, String description,
046: String explorerId) {
047: this .name = name;
048: this .description = description;
049: this .explorerId = explorerId;
050: this .dependencies = new TaskDependencies();
051: }
052:
053: public String getName() {
054: return name;
055: }
056:
057: public String getDescription() {
058: return description;
059: }
060:
061: public String getTaskExplorerId() {
062: return explorerId;
063: }
064:
065: public ICompositeCheatSheetTask getRootTask() {
066: return rootTask;
067: }
068:
069: public void setDependencies(TaskDependencies dependencies) {
070: this .dependencies = dependencies;
071: }
072:
073: public TaskDependencies getDependencies() {
074: return dependencies;
075: }
076:
077: public URL getContentUrl() {
078: return contentURL;
079: }
080:
081: public void setContentUrl(URL newUrl) {
082: contentURL = newUrl;
083: }
084:
085: public void setId(String id) {
086: this .id = id;
087: }
088:
089: public String getId() {
090: return id;
091: }
092:
093: public void setSaveHelper(CompositeCheatSheetSaveHelper saveHelper) {
094: this .saveHelper = saveHelper;
095: }
096:
097: /**
098: * Maintain a set of tasks which have been changed which will
099: * 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 blockedStateChanged = new BlockedTaskFinder()
115: .findBlockedTaskChanges(stateChangedSet);
116: for (Iterator iter = stateChangedSet.iterator(); iter.hasNext();) {
117: setChanged();
118: notifyObservers(iter.next());
119: }
120: for (Iterator iter = blockedStateChanged.iterator(); iter
121: .hasNext();) {
122: setChanged();
123: notifyObservers(iter.next());
124: }
125: stateChangedSet.clear();
126: }
127:
128: public IMemento getTaskMemento(String id) {
129: return saveHelper.getTaskMemento(id);
130: }
131:
132: public ICheatSheetManager getCheatSheetManager() {
133: return manager;
134: }
135:
136: public void setCheatSheetManager(CheatSheetManager manager) {
137: this .manager = manager;
138: }
139:
140: public void loadState(Map layoutData) {
141: saveHelper.loadCompositeState(this , layoutData);
142: }
143:
144: /*
145: * Reset the state of a task and it's children
146: */
147: private void resetTask(ICompositeCheatSheetTask task) {
148: if (task instanceof EditableTask) {
149: EditableTask editable = (EditableTask) task;
150: editable.reset();
151: if (saveHelper != null) {
152: saveHelper.clearTaskMemento(task.getId());
153: }
154: } else if (task instanceof TaskGroup) {
155: TaskGroup group = (TaskGroup) task;
156: ICompositeCheatSheetTask[] subtasks = group.getSubtasks();
157: for (int i = 0; i < subtasks.length; i++) {
158: resetTask(subtasks[i]);
159: }
160: group
161: .setStateNoNotify(ICompositeCheatSheetTask.NOT_STARTED);
162: }
163: }
164:
165: public void resetAllTasks(Map cheatSheetData) {
166: if (manager != null) {
167: if (cheatSheetData == null) {
168: manager.setData(new Hashtable());
169: } else {
170: manager.setData(cheatSheetData);
171: }
172: }
173: saveHelper.clearTaskMementos();
174: resetTask(getRootTask());
175: sendTaskChangeEvents();
176: }
177:
178: /**
179: * Restart one or more tasks
180: * @param restartTasks An array of the tasks to be restarted
181: */
182: public void resetTasks(ICompositeCheatSheetTask[] restartTasks) {
183: for (int i = 0; i < restartTasks.length; i++) {
184: resetTask(restartTasks[i]);
185: }
186: sendTaskChangeEvents();
187: }
188:
189: }
|