Skip to main content
summaryrefslogtreecommitdiffstats
blob: 276ba4f48bda8f9045302517e3177dc04c0a1d1c (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2000, 2016 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.ui.internal.views;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Observable;

import org.eclipse.core.runtime.IPath;
import org.eclipse.help.ui.internal.HelpUIPlugin;
import org.eclipse.jface.dialogs.IDialogSettings;

/**
 * Manages the scope for the federated search.
 */
public class ScopeSetManager extends Observable {
	private ScopeSet activeSet;

	private ScopeSet lastExplicitSet;

	private static final String ACTIVE_SET = "activeScopeSet"; //$NON-NLS-1$

	private ArrayList<ScopeSet> sets;

	private ScopeSet defSet;

	public ScopeSetManager() {
		ensureLocation();
		loadScopeSets();
	}

	public void add(ScopeSet set) {
		sets.add(set);
		setChanged();
	}

	public void remove(ScopeSet set) {
		sets.remove(set);
		set.dispose();
		setChanged();
	}

	public void setActiveSet(ScopeSet set) {
		if (this.activeSet != null) {
			this.activeSet.save();
		}
		this.activeSet = set;
		if (!activeSet.isImplicit())
			lastExplicitSet = set;
		setChanged();
	}

	public boolean restoreLastExplicitSet() {
		if (activeSet != null && activeSet.isImplicit()
				&& lastExplicitSet != null) {
			setActiveSet(lastExplicitSet);
			setChanged();
			return true;
		}
		return false;
	}

	public static void ensureLocation() {
		IPath location = HelpUIPlugin.getDefault().getStateLocation();
		location = location.append(ScopeSet.SCOPE_DIR_NAME);
		File dir = location.toFile();
		if (dir.exists() == false)
			dir.mkdir();
	}

	public void save() {
		ensureLocation();
		for (int i = 0; i < sets.size(); i++) {
			ScopeSet set = sets.get(i);
			set.save();
		}
		IDialogSettings settings = HelpUIPlugin.getDefault()
				.getDialogSettings();
		if (activeSet != null)
			settings.put(ACTIVE_SET, activeSet.getName());
	}

	public ScopeSet[] getScopeSets(boolean implicit) {
		ArrayList<ScopeSet> result = new ArrayList<>();
		for (int i = 0; i < sets.size(); i++) {
			ScopeSet set = sets.get(i);
			if (set.isImplicit() == implicit)
				result.add(set);
			if (!implicit && set.isImplicit() && activeSet==set)
				result.add(set);
		}
		return result.toArray(new ScopeSet[result.size()]);
	}

	private void loadScopeSets() {
		sets = new ArrayList<>();
		IPath location = HelpUIPlugin.getDefault().getStateLocation();
		location = location.append("scope_sets"); //$NON-NLS-1$
		File dir = location.toFile();
		defSet = null;
		if (dir.exists() && dir.isDirectory()) {
			File[] files = dir.listFiles((FilenameFilter) (dir1, name) -> name.endsWith(ScopeSet.EXT)
					|| name.endsWith(HistoryScopeSet.EXT));
			if(files == null) {
				files = new File[0];
			}
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				String name = file.getName();
				int loc = name.lastIndexOf(ScopeSet.EXT);
				if (loc != -1) {
					ScopeSet set = new ScopeSet(name.substring(0, loc));
					sets.add(set);
					if (set.isDefault())
						defSet = set;
					continue;
				}
				loc = name.lastIndexOf(HistoryScopeSet.EXT);
				if (loc != -1) {
					HistoryScopeSet set = new HistoryScopeSet(name.substring(0,
							loc), null);
					sets.add(set);
				}
			}
		}
		if (sets.size() == 1) {
			activeSet = sets.get(0);
		}
		if (defSet == null) {
			defSet = new ScopeSet();
			sets.add(defSet);
		}

	}

	public ScopeSet getDefaultScope() {
		return defSet;
	}

	/**
	 * @return Returns the activeSet.
	 */
	public ScopeSet getActiveSet() {
		if (activeSet == null) {
			IDialogSettings settings = HelpUIPlugin.getDefault()
					.getDialogSettings();
			String name = settings.get(ACTIVE_SET);
			activeSet = findSet(name);
			if (activeSet == null) {
				return sets.get(0);
			}
			if (!activeSet.isImplicit())
				lastExplicitSet = activeSet;
		}
		return activeSet;
	}

	public ScopeSet findSet(String name) {
		return findSet(name, false);
	}

	public HistoryScopeSet findSearchSet(String expression) {
		for (int i = 0; i < sets.size(); i++) {
			ScopeSet set = sets.get(i);
			if (!set.isImplicit() || !(set instanceof HistoryScopeSet))
				continue;
			HistoryScopeSet sset = (HistoryScopeSet) set;
			if (sset.getExpression().equals(expression))
				return sset;
		}
		return null;
	}

	public ScopeSet findSet(String name, boolean implicit) {
		ScopeSet defaultSet = null;
		for (int i = 0; i < sets.size(); i++) {
			ScopeSet set = sets.get(i);
			if (name != null && set.isImplicit() == implicit) {
				if (set.getName().equals(name))
					return set;
			} else if (set.isDefault())
				defaultSet = set;
		}
		if (!implicit)
			return defaultSet;
		return null;
	}
}

Back to the top