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.filters;
011:
012: import java.util.Arrays;
013:
014: import org.eclipse.core.commands.operations.AbstractOperation;
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Status;
020: import org.eclipse.jface.viewers.StructuredSelection;
021: import org.eclipse.jface.viewers.ViewerFilter;
022: import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
023: import org.eclipse.ui.internal.navigator.NavigatorFilterService;
024: import org.eclipse.ui.navigator.CommonViewer;
025: import org.eclipse.ui.navigator.ICommonFilterDescriptor;
026: import org.eclipse.ui.navigator.INavigatorContentService;
027: import org.eclipse.ui.navigator.INavigatorFilterService;
028:
029: /**
030: * Ensures that a given set of filters is <i>active</i> and the complement of
031: * that set of filters are not <i>active</i>.
032: *
033: * <p>
034: * This operation is smart enough not to force any change if each id in each set
035: * is already in its desired state (<i>active</i> or <i>inactive</i>).
036: * </p>
037: *
038: * @since 3.2
039: *
040: */
041: public class UpdateActiveFiltersOperation extends AbstractOperation {
042:
043: private String[] filterIdsToActivate;
044:
045: private final CommonViewer commonViewer;
046:
047: private final INavigatorContentService contentService;
048:
049: private boolean disableTheComplement;
050:
051: /**
052: * Create an operation to activate extensions and refresh the viewer.
053: *
054: *
055: * @param aCommonViewer
056: * The CommonViewer instance to update
057: * @param theActiveFilterIds
058: * An array of ids that correspond to the filters that should be
059: * in the <i>active</i> state after this operation executes. The
060: * complement of this set will likewise be in the <i>inactive</i>
061: * state after this operation executes.
062: * @param toDisableTheComplement True indicates that all filters not in the set should be made inactive.
063: */
064: public UpdateActiveFiltersOperation(CommonViewer aCommonViewer,
065: String[] theActiveFilterIds, boolean toDisableTheComplement) {
066: super (
067: CommonNavigatorMessages.UpdateFiltersOperation_Update_CommonViewer_Filter_);
068: Assert.isNotNull(theActiveFilterIds);
069:
070: commonViewer = aCommonViewer;
071: contentService = commonViewer.getNavigatorContentService();
072: filterIdsToActivate = theActiveFilterIds;
073: disableTheComplement = toDisableTheComplement;
074:
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor,
081: * org.eclipse.core.runtime.IAdaptable)
082: */
083: public IStatus execute(IProgressMonitor monitor, IAdaptable info) {
084:
085: boolean updateFilterActivation = false;
086:
087: // we sort the array in order to use Array.binarySearch();
088: Arrays.sort(filterIdsToActivate);
089:
090: try {
091: commonViewer.getControl().setRedraw(false);
092:
093: INavigatorFilterService filterService = contentService
094: .getFilterService();
095:
096: if (disableTheComplement) {
097:
098: ICommonFilterDescriptor[] visibleFilterDescriptors = filterService
099: .getVisibleFilterDescriptors();
100:
101: int indexofFilterIdToBeActivated;
102:
103: /* is there a delta? */
104: for (int i = 0; i < visibleFilterDescriptors.length
105: && !updateFilterActivation; i++) {
106: indexofFilterIdToBeActivated = Arrays.binarySearch(
107: filterIdsToActivate,
108: visibleFilterDescriptors[i].getId());
109:
110: /*
111: * Either we have a filter that should be active that isn't
112: * XOR a filter that shouldn't be active that is currently
113: */
114: if (indexofFilterIdToBeActivated >= 0
115: ^ filterService
116: .isActive(visibleFilterDescriptors[i]
117: .getId())) {
118: updateFilterActivation = true;
119: }
120: }
121:
122: /* If so, update */
123: if (updateFilterActivation) {
124:
125: filterService
126: .setActiveFilterIds(filterIdsToActivate);
127: filterService.persistFilterActivationState();
128:
129: commonViewer.resetFilters();
130:
131: ViewerFilter[] visibleFilters = filterService
132: .getVisibleFilters(true);
133: for (int i = 0; i < visibleFilters.length; i++) {
134: commonViewer.addFilter(visibleFilters[i]);
135: }
136:
137: // the action providers may no longer be enabled, so we
138: // reset the selection.
139: commonViewer
140: .setSelection(StructuredSelection.EMPTY);
141: }
142: } else {
143: NavigatorFilterService internalFilterService = (NavigatorFilterService) filterService;
144:
145: internalFilterService
146: .addActiveFilterIds(filterIdsToActivate);
147:
148: ICommonFilterDescriptor[] visibleDescriptors = filterService
149: .getVisibleFilterDescriptors();
150: for (int i = 0; i < visibleDescriptors.length; i++) {
151: if (Arrays.binarySearch(filterIdsToActivate,
152: visibleDescriptors[i].getId()) >= 0) {
153: commonViewer
154: .addFilter(internalFilterService
155: .getViewerFilter(visibleDescriptors[i]));
156: }
157: }
158:
159: // the action providers may no longer be enabled, so we
160: // reset the selection.
161: commonViewer.setSelection(StructuredSelection.EMPTY);
162: }
163:
164: } finally {
165: commonViewer.getControl().setRedraw(true);
166: }
167: return Status.OK_STATUS;
168: }
169:
170: /*
171: * (non-Javadoc)
172: *
173: * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor,
174: * org.eclipse.core.runtime.IAdaptable)
175: */
176: public IStatus redo(IProgressMonitor monitor, IAdaptable info) {
177: return null;
178: }
179:
180: /*
181: * (non-Javadoc)
182: *
183: * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor,
184: * org.eclipse.core.runtime.IAdaptable)
185: */
186: public IStatus undo(IProgressMonitor monitor, IAdaptable info) {
187: return null;
188: }
189: }
|