01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.composite.model;
11:
12: import java.util.ArrayList;
13: import org.eclipse.ui.internal.cheatsheets.composite.parser.ITaskParseStrategy;
14: import org.eclipse.ui.internal.cheatsheets.composite.parser.TaskGroupParseStrategy;
15: import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
16: import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup;
17:
18: public class TaskGroup extends AbstractTask implements ITaskGroup {
19:
20: public interface CompletionStrategy {
21: public int computeState(TaskGroup taskGroup);
22: }
23:
24: private ITaskParseStrategy parserStrategy;
25:
26: private ArrayList subtasks;
27:
28: private CompletionStrategy completionStrategy;
29:
30: public TaskGroup(CompositeCheatSheetModel model, String id,
31: String name, String kind) {
32: super (model, id, name, kind);
33: if (kind == null) {
34: this .kind = ITaskGroup.SET;
35: }
36: parserStrategy = new TaskGroupParseStrategy();
37: completionStrategy = determineCompletionStrategy(kind);
38: }
39:
40: private CompletionStrategy determineCompletionStrategy(String kind) {
41: if (ITaskGroup.CHOICE.equals(kind)) {
42: return new TaskChoiceCompletionStrategy();
43: }
44: return new TaskSetCompletionStrategy();
45: }
46:
47: public ITaskParseStrategy getParserStrategy() {
48: return parserStrategy;
49: }
50:
51: public ICompositeCheatSheetTask[] getSubtasks() {
52: if (subtasks == null)
53: return EMPTY;
54: return (ICompositeCheatSheetTask[]) subtasks
55: .toArray(new ICompositeCheatSheetTask[subtasks.size()]);
56: }
57:
58: public void addSubtask(ICompositeCheatSheetTask task) {
59: if (subtasks == null) {
60: subtasks = new ArrayList();
61: }
62: subtasks.add(task);
63: ((AbstractTask) task).setParent(this );
64: }
65:
66: /**
67: * Called when the state of a child has changed or when the model
68: * has been restored.
69: */
70: public void checkState() {
71: int newState = computeState();
72: if (newState != state) {
73: setStateNoNotify(newState);
74: }
75: }
76:
77: /**
78: * Determine the state based on the state of the children, which
79: * will use a different computation depending on whether this is a set,
80: * sequence or choice.
81: */
82: public int computeState() {
83: return completionStrategy.computeState(this);
84: }
85:
86: }
|