001: /*******************************************************************************
002: * Copyright (c) 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.navigator;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Set;
020:
021: import org.eclipse.core.runtime.Assert;
022: import org.eclipse.core.runtime.Preferences;
023: import org.eclipse.jface.viewers.ViewerFilter;
024: import org.eclipse.ui.internal.navigator.filters.CommonFilterDescriptor;
025: import org.eclipse.ui.internal.navigator.filters.CommonFilterDescriptorManager;
026: import org.eclipse.ui.internal.navigator.filters.SkeletonViewerFilter;
027: import org.eclipse.ui.navigator.ICommonFilterDescriptor;
028: import org.eclipse.ui.navigator.INavigatorFilterService;
029:
030: /**
031: * @since 3.2
032: *
033: */
034: public class NavigatorFilterService implements INavigatorFilterService {
035:
036: private static final ViewerFilter[] NO_FILTERS = new ViewerFilter[0];
037:
038: private static final String ACTIVATION_KEY = ".filterActivation"; //$NON-NLS-1$
039:
040: private static final String DELIM = ":"; //$NON-NLS-1$
041:
042: private final NavigatorContentService contentService;
043:
044: /* Map of (ICommonFilterDescriptor, ViewerFilter)-pairs */
045: private final Map declaredViewerFilters = new HashMap();
046:
047: /* Set of ViewerFilters enforced from visible/active content extensions */
048: private final Set enforcedViewerFilters = new HashSet();
049:
050: /* A set of active filter String ids */
051: private final Set activeFilters = new HashSet();
052:
053: /**
054: * @param aContentService
055: * The corresponding content service
056: */
057: public NavigatorFilterService(
058: NavigatorContentService aContentService) {
059: contentService = aContentService;
060: restoreFilterActivation();
061: }
062:
063: private synchronized void restoreFilterActivation() {
064:
065: try {
066: Preferences preferences = NavigatorPlugin.getDefault()
067: .getPluginPreferences();
068:
069: if (preferences
070: .contains(getFilterActivationPreferenceKey())) {
071: String activatedFiltersPreferenceValue = preferences
072: .getString(getFilterActivationPreferenceKey());
073: String[] activeFilterIds = activatedFiltersPreferenceValue
074: .split(DELIM);
075: for (int i = 0; i < activeFilterIds.length; i++) {
076: activeFilters.add(activeFilterIds[i]);
077: }
078:
079: } else {
080: ICommonFilterDescriptor[] visibleFilterDescriptors = getVisibleFilterDescriptors();
081: for (int i = 0; i < visibleFilterDescriptors.length; i++) {
082: if (visibleFilterDescriptors[i].isActiveByDefault()) {
083: activeFilters.add(visibleFilterDescriptors[i]
084: .getId());
085: }
086: }
087: }
088:
089: } catch (RuntimeException e) {
090: NavigatorPlugin.logError(0, e.getMessage(), e);
091: }
092:
093: }
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see org.eclipse.ui.navigator.INavigatorFilterService#persistFilterActivationState()
099: */
100: public void persistFilterActivationState() {
101:
102: try {
103: synchronized (activeFilters) {
104:
105: /* by creating a StringBuffer with DELIM, we ensure the string is not empty when persisted.*/
106: StringBuffer activatedFiltersPreferenceValue = new StringBuffer(
107: DELIM);
108:
109: for (Iterator activeItr = activeFilters.iterator(); activeItr
110: .hasNext();) {
111: activatedFiltersPreferenceValue.append(
112: activeItr.next().toString()).append(DELIM);
113: }
114:
115: Preferences preferences = NavigatorPlugin.getDefault()
116: .getPluginPreferences();
117:
118: preferences.setValue(
119: getFilterActivationPreferenceKey(),
120: activatedFiltersPreferenceValue.toString());
121: }
122:
123: } catch (RuntimeException e) {
124: NavigatorPlugin.logError(0, e.getMessage(), e);
125: }
126:
127: }
128:
129: /**
130: * @return The correct filter activation preference key for the
131: * corresponding content service.
132: */
133: private String getFilterActivationPreferenceKey() {
134: return contentService.getViewerId() + ACTIVATION_KEY;
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see org.eclipse.ui.navigator.INavigatorFilterService#getVisibleFilters(boolean)
141: */
142: public ViewerFilter[] getVisibleFilters(
143: boolean toReturnOnlyActiveFilters) {
144: CommonFilterDescriptor[] descriptors = CommonFilterDescriptorManager
145: .getInstance().findVisibleFilters(contentService);
146:
147: List filters = new ArrayList();
148:
149: ViewerFilter instance;
150: for (int i = 0; i < descriptors.length; i++) {
151: if (!toReturnOnlyActiveFilters
152: || isActive(descriptors[i].getId())) {
153: instance = getViewerFilter(descriptors[i]);
154: if (instance != null) {
155: filters.add(instance);
156: }
157: }
158: }
159:
160: /* return the enforced viewer filters always */
161: filters.addAll(enforcedViewerFilters);
162:
163: if (filters.size() == 0) {
164: return NO_FILTERS;
165: }
166: return (ViewerFilter[]) filters
167: .toArray(new ViewerFilter[filters.size()]);
168: }
169:
170: /**
171: * @param descriptor
172: * A key into the viewerFilters map.
173: * @return A non-null ViewerFilter from the extension (or
174: * {@link SkeletonViewerFilter#INSTANCE}).
175: */
176: public ViewerFilter getViewerFilter(
177: ICommonFilterDescriptor descriptor) {
178: ViewerFilter filter = null;
179: synchronized (declaredViewerFilters) {
180: filter = (ViewerFilter) declaredViewerFilters
181: .get(descriptor);
182: if (filter == null) {
183: declaredViewerFilters.put(descriptor,
184: (filter = ((CommonFilterDescriptor) descriptor)
185: .createFilter()));
186: }
187: }
188: return filter;
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see org.eclipse.ui.navigator.INavigatorFilterService#getVisibleFilterIds()
195: */
196: public ICommonFilterDescriptor[] getVisibleFilterDescriptors() {
197: return CommonFilterDescriptorManager.getInstance()
198: .findVisibleFilters(contentService);
199: }
200:
201: /*
202: * (non-Javadoc)
203: *
204: * @see org.eclipse.ui.navigator.INavigatorFilterService#isActive(java.lang.String)
205: */
206: public boolean isActive(String aFilterId) {
207: synchronized (activeFilters) {
208: return activeFilters.contains(aFilterId);
209: }
210: }
211:
212: /*
213: * (non-Javadoc)
214: *
215: * @see org.eclipse.ui.navigator.INavigatorFilterService#activateFilters(java.lang.String[])
216: */
217: public void setActiveFilterIds(String[] theFilterIds) {
218: Assert.isNotNull(theFilterIds);
219: synchronized (activeFilters) {
220: activeFilters.clear();
221: activeFilters.addAll(Arrays.asList(theFilterIds));
222: }
223: }
224:
225: /**
226: * Activate the given array without disabling all other filters.
227: *
228: * @param theFilterIds
229: * The filter ids to activate.
230: */
231: public void addActiveFilterIds(String[] theFilterIds) {
232: Assert.isNotNull(theFilterIds);
233: synchronized (activeFilters) {
234: activeFilters.addAll(Arrays.asList(theFilterIds));
235: }
236: }
237:
238: /**
239: *
240: * @param aFilterId The id of the filter to activate or deactivate
241: * @param toMakeActive True to make the filter active, false to make the filter inactive
242: */
243: public void setActive(String aFilterId, boolean toMakeActive) {
244:
245: synchronized (activeFilters) {
246: boolean isActive = activeFilters.contains(aFilterId);
247: if (isActive ^ toMakeActive) {
248: if (toMakeActive)
249: activeFilters.remove(aFilterId);
250: else
251: activeFilters.add(aFilterId);
252:
253: }
254:
255: }
256: }
257:
258: }
|