Skip to main content
summaryrefslogtreecommitdiffstats
blob: ef4d549efa0458ccd3ba70040f71b8ab3a93807e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2006, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.cheatsheets.composite.model;

import java.util.ArrayList;

import org.eclipse.ui.internal.cheatsheets.composite.parser.ITaskParseStrategy;
import org.eclipse.ui.internal.cheatsheets.composite.parser.TaskGroupParseStrategy;
import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup;

public class TaskGroup extends AbstractTask implements ITaskGroup {

	public interface CompletionStrategy {
		public int computeState(TaskGroup taskGroup);
	}

	private ITaskParseStrategy parserStrategy;

	private ArrayList<ICompositeCheatSheetTask> subtasks;

	private CompletionStrategy completionStrategy;

	public TaskGroup(CompositeCheatSheetModel model, String id, String name, String kind) {
		super(model, id, name, kind);
		if (kind == null) {
			this.kind = ITaskGroup.SET;
		}
		parserStrategy = new TaskGroupParseStrategy();
		completionStrategy = determineCompletionStrategy(kind);
	}

	private CompletionStrategy determineCompletionStrategy(String kind) {
		if (ITaskGroup.CHOICE.equals(kind)) {
			return new TaskChoiceCompletionStrategy();
		}
		return new TaskSetCompletionStrategy();
	}

	@Override
	public ITaskParseStrategy getParserStrategy() {
		return parserStrategy;
	}

	@Override
	public ICompositeCheatSheetTask[] getSubtasks() {
		if (subtasks==null) return EMPTY;
		return subtasks.toArray(new ICompositeCheatSheetTask[subtasks.size()]);
	}

	public void addSubtask(ICompositeCheatSheetTask task) {
		if (subtasks==null) {
			subtasks = new ArrayList<>();
		}
		subtasks.add(task);
		((AbstractTask)task).setParent(this);
	}

	/**
	 * Called when the state of a child has changed or when the model
	 * has been restored.
	 */
	public void checkState() {
		int newState = computeState();
		if (newState != state) {
			setStateNoNotify(newState);
		}
	}

	/**
	 * Determine the state based on the state of the children, which
     * will use a different computation depending on whether this is a set,
     * sequence or choice.
	 */
	public int computeState() {
		return completionStrategy.computeState(this);
	}

}

Back to the top