KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > filters > UpdateActiveFiltersOperation


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.navigator.filters;
13
14 import java.util.Arrays JavaDoc;
15
16 import org.eclipse.core.commands.operations.AbstractOperation;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.ViewerFilter;
24 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
25 import org.eclipse.ui.internal.navigator.NavigatorFilterService;
26 import org.eclipse.ui.navigator.CommonViewer;
27 import org.eclipse.ui.navigator.ICommonFilterDescriptor;
28 import org.eclipse.ui.navigator.INavigatorContentService;
29 import org.eclipse.ui.navigator.INavigatorFilterService;
30
31 /**
32  * Ensures that a given set of filters is <i>active</i> and the complement of
33  * that set of filters are not <i>active</i>.
34  *
35  * <p>
36  * This operation is smart enough not to force any change if each id in each set
37  * is already in its desired state (<i>active</i> or <i>inactive</i>).
38  * </p>
39  *
40  * @since 3.2
41  *
42  */

43 public class UpdateActiveFiltersOperation extends AbstractOperation {
44
45     private String JavaDoc[] filterIdsToActivate;
46
47     private final CommonViewer commonViewer;
48
49     private final INavigatorContentService contentService;
50
51     private boolean disableTheComplement;
52
53     /**
54      * Create an operation to activate extensions and refresh the viewer.
55      *
56      *
57      * @param aCommonViewer
58      * The CommonViewer instance to update
59      * @param theActiveFilterIds
60      * An array of ids that correspond to the filters that should be
61      * in the <i>active</i> state after this operation executes. The
62      * complement of this set will likewise be in the <i>inactive</i>
63      * state after this operation executes.
64      * @param toDisableTheComplement True indicates that all filters not in the set should be made inactive.
65      */

66     public UpdateActiveFiltersOperation(CommonViewer aCommonViewer,
67             String JavaDoc[] theActiveFilterIds, boolean toDisableTheComplement) {
68         super(
69                 CommonNavigatorMessages.UpdateFiltersOperation_Update_CommonViewer_Filter_);
70         Assert.isNotNull(theActiveFilterIds);
71         
72         commonViewer = aCommonViewer;
73         contentService = commonViewer.getNavigatorContentService();
74         filterIdsToActivate = theActiveFilterIds;
75         disableTheComplement = toDisableTheComplement;
76
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor,
83      * org.eclipse.core.runtime.IAdaptable)
84      */

85     public IStatus execute(IProgressMonitor monitor, IAdaptable info) {
86
87         boolean updateFilterActivation = false;
88         
89         // we sort the array in order to use Array.binarySearch();
90
Arrays.sort(filterIdsToActivate);
91         
92
93         try {
94             commonViewer.getControl().setRedraw(false);
95
96             INavigatorFilterService filterService = contentService
97                     .getFilterService();
98
99             if(disableTheComplement) {
100             
101                 ICommonFilterDescriptor[] visibleFilterDescriptors = filterService
102                         .getVisibleFilterDescriptors();
103
104                 int indexofFilterIdToBeActivated;
105
106                 /* is there a delta? */
107                 for (int i = 0; i < visibleFilterDescriptors.length
108                         && !updateFilterActivation; i++) {
109                     indexofFilterIdToBeActivated = Arrays.binarySearch(
110                             filterIdsToActivate, visibleFilterDescriptors[i]
111                                     .getId());
112
113                     /*
114                      * Either we have a filter that should be active that isn't
115                      * XOR a filter that shouldn't be active that is currently
116                      */

117                     if (indexofFilterIdToBeActivated >= 0
118                             ^ filterService
119                                     .isActive(visibleFilterDescriptors[i]
120                                             .getId())) {
121                         updateFilterActivation = true;
122                     }
123                 }
124
125                 /* If so, update */
126                 if (updateFilterActivation) {
127
128                     filterService.setActiveFilterIds(filterIdsToActivate);
129                     filterService.persistFilterActivationState();
130
131                     commonViewer.resetFilters();
132
133                     ViewerFilter[] visibleFilters = filterService
134                             .getVisibleFilters(true);
135                     for (int i = 0; i < visibleFilters.length; i++) {
136                         commonViewer.addFilter(visibleFilters[i]);
137                     }
138
139                     // the action providers may no longer be enabled, so we
140
// reset the selection.
141
commonViewer.setSelection(StructuredSelection.EMPTY);
142                 }
143             } else {
144                 NavigatorFilterService internalFilterService = (NavigatorFilterService)filterService;
145                 
146                 internalFilterService.addActiveFilterIds(filterIdsToActivate);
147
148                 ICommonFilterDescriptor[] visibleDescriptors = filterService
149                         .getVisibleFilterDescriptors();
150                 for (int i = 0; i < visibleDescriptors.length; i++) {
151                     if(Arrays.binarySearch(filterIdsToActivate, visibleDescriptors[i].getId()) >= 0 ) {
152                         commonViewer.addFilter(internalFilterService.getViewerFilter(visibleDescriptors[i]));
153                     }
154                 }
155
156                 // the action providers may no longer be enabled, so we
157
// reset the selection.
158
commonViewer.setSelection(StructuredSelection.EMPTY);
159             }
160
161         } finally {
162             commonViewer.getControl().setRedraw(true);
163         }
164         return Status.OK_STATUS;
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor,
171      * org.eclipse.core.runtime.IAdaptable)
172      */

173     public IStatus redo(IProgressMonitor monitor, IAdaptable info) {
174         return null;
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor,
181      * org.eclipse.core.runtime.IAdaptable)
182      */

183     public IStatus undo(IProgressMonitor monitor, IAdaptable info) {
184         return null;
185     }
186 }
187
Popular Tags