Skip to main content
summaryrefslogtreecommitdiffstats
blob: c1c0f91783e60cc369f282cae00c4254d35e038d (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.workingset;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.help.internal.criteria.CriterionResource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WorkingSet {
	private String name;
	private List<AdaptableHelpResource> elements;
	private List<CriterionResource> criteria;

	public WorkingSet(String name) {
		this(name, (List<AdaptableHelpResource>) null, (List<CriterionResource>) null);
	}

	public WorkingSet(String name, List<AdaptableHelpResource> elements) {
		this(name, elements, null);
	}

	public WorkingSet(String name, List<AdaptableHelpResource> elements, List<CriterionResource> criteria) {
		this.name = name;

		if (elements == null)
			elements = new ArrayList<>();
		this.elements = elements;

		if (criteria != null) {
			this.criteria = criteria;
		} else {
			this.criteria = new ArrayList<>();
		}
	}

	public WorkingSet(String name, AdaptableHelpResource[] elements) {
		this(name, elements, null);
	}

	public WorkingSet(String name, AdaptableHelpResource[] elements, CriterionResource[] criteria) {
		this.name = name;

		if (elements == null)
			elements = new AdaptableHelpResource[0];

		this.elements = new ArrayList<>(elements.length);
		for (int i = 0; i < elements.length; i++) {
			this.elements.add(elements[i]);
		}

		if (criteria == null)
			criteria = new CriterionResource[0];

		this.criteria = new ArrayList<>(criteria.length);
		for (int j = 0; j < criteria.length; j++) {
			this.criteria.add(criteria[j]);
		}
	}

	public void removeElement(AdaptableHelpResource element) {
		// Note: this is based on equality of IHelpResource and
		// AdaptableHelpResource
		elements.remove(element);
	}

	public String getName() {
		return name;
	}

	public void setName(String newName) {
		if (newName == null)
			return;
		name = newName;
	}

	public AdaptableHelpResource[] getElements() {
		AdaptableHelpResource[] array = new AdaptableHelpResource[elements
				.size()];
		elements.toArray(array);
		return array;
	}

	public void setElements(AdaptableHelpResource[] elements) {
		this.elements = new ArrayList<>(elements.length);
		for (int i = 0; i < elements.length; i++)
			this.elements.add(elements[i]);
	}


	public void setCriteria(CriterionResource[] criteria) {
		this.criteria = new ArrayList<>(criteria.length);
		for(int i = 0; i < criteria.length; i++) {
			this.criteria.add(criteria[i]);
		}
	}

	public CriterionResource[] getCriteria(){
		CriterionResource[] array = new CriterionResource[criteria.size()];
		criteria.toArray(array);
		return array;
	}

	public void saveState(Element parent) {
		Document doc = parent.getOwnerDocument();
		Element ws = doc.createElement("workingSet"); //$NON-NLS-1$
		ws.setAttribute("name", name); //$NON-NLS-1$
		parent.appendChild(ws);

		Element contents = doc.createElement("contents"); //$NON-NLS-1$
		ws.appendChild(contents);
		for (Iterator<AdaptableHelpResource> it = elements.iterator(); it.hasNext();) {
			Element child = doc.createElement("item"); //$NON-NLS-1$
			AdaptableHelpResource helpResource = it.next();
			helpResource.saveState(child);
			contents.appendChild(child);
		}

		if (!criteria.isEmpty()){
			Element criteriaElement = doc.createElement("criteria"); //$NON-NLS-1$
			ws.appendChild(criteriaElement);

			for(Iterator<CriterionResource> iterator = criteria.iterator(); iterator.hasNext();){
				Element criterionItem = doc.createElement("criterion"); //$NON-NLS-1$
				criteriaElement.appendChild(criterionItem);
				CriterionResource criterion = iterator.next();
				String criterionName = criterion.getCriterionName();
				criterionItem.setAttribute("name", criterionName);//$NON-NLS-1$
				List<String> criterionValues = criterion.getCriterionValues();
				if(!criterionValues.isEmpty()){
					for(Iterator<String> iter = criterionValues.iterator(); iter.hasNext();){
						String value = iter.next();
						if(value != null){
							Element item = doc.createElement("item"); //$NON-NLS-1$
							criterionItem.appendChild(item);
							item.setAttribute("value", value.trim());//$NON-NLS-1$
						}
					}
				}
			}
		}
	}
}

Back to the top