KickJava   Java API By Example, From Geeks To Geeks.

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


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.IAdaptable;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.StructuredSelection;
24 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
25 import org.eclipse.ui.navigator.CommonViewer;
26 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
27 import org.eclipse.ui.navigator.INavigatorContentService;
28
29 /**
30  * Ensures that a given set of content extensions is <i>active</i> and a second
31  * non-intersecting set of content extensions are not <i>active</i>.
32  *
33  * <p>
34  * This operation is smart enough not to force any change if each id in each set
35  * is already in its desired state (<i>active</i> or <i>inactive</i>).
36  * </p>
37  *
38  * @since 3.2
39  *
40  */

41 public class UpdateActiveExtensionsOperation extends AbstractOperation {
42
43     private String JavaDoc[] contentExtensionsToActivate;
44
45     private final CommonViewer commonViewer;
46
47     private final INavigatorContentService contentService;
48
49     /**
50      * Create an operation to activate extensions and refresh the viewer.
51      *
52      * p> To use only one part of this operation (either "activate" or
53      * "deactivate", but not both), then supply <b>null</b> for the array state
54      * you are not concerned with.
55      * </p>
56      *
57      * @param aCommonViewer
58      * The CommonViewer instance to update
59      * @param theExtensionsToActivate
60      * An array of ids that correspond to the extensions that should
61      * be in the <i>active</i> state after this operation executes.
62      */

63     public UpdateActiveExtensionsOperation(CommonViewer aCommonViewer,
64             String JavaDoc[] theExtensionsToActivate) {
65         super(
66                 CommonNavigatorMessages.UpdateFiltersOperation_Update_CommonViewer_Filter_);
67         commonViewer = aCommonViewer;
68         contentService = commonViewer.getNavigatorContentService();
69         contentExtensionsToActivate = theExtensionsToActivate;
70
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor,
77      * org.eclipse.core.runtime.IAdaptable)
78      */

79     public IStatus execute(IProgressMonitor monitor, IAdaptable info) {
80
81         boolean updateExtensionActivation = false;
82
83         // we sort the array in order to use Array.binarySearch();
84
Arrays.sort(contentExtensionsToActivate);
85         
86         IStructuredSelection ssel = null;
87     
88         try {
89             commonViewer.getControl().setRedraw(false);
90             
91             ISelection selection = commonViewer.getSelection();
92             if(selection instanceof IStructuredSelection)
93                 ssel = (IStructuredSelection) selection;
94
95             INavigatorContentDescriptor[] visibleContentDescriptors = contentService
96                     .getVisibleExtensions();
97
98             int indexofContentExtensionIdToBeActivated;
99             /* is there a delta? */
100             for (int i = 0; i < visibleContentDescriptors.length
101                     && !updateExtensionActivation; i++) {
102                 indexofContentExtensionIdToBeActivated = Arrays.binarySearch(
103                         contentExtensionsToActivate,
104                         visibleContentDescriptors[i].getId());
105                 /*
106                  * Either we have a filter that should be active that isn't XOR
107                  * a filter that shouldn't be active that is currently
108                  */

109                 if (indexofContentExtensionIdToBeActivated >= 0
110                         ^ contentService.isActive(visibleContentDescriptors[i]
111                                 .getId())) {
112                     updateExtensionActivation = true;
113                 }
114             }
115
116             /* If so, update */
117             if (updateExtensionActivation) {
118                  
119                 contentService.getActivationService().activateExtensions(
120                         contentExtensionsToActivate, true);
121                 contentService.getActivationService()
122                         .persistExtensionActivations();
123                 
124
125                 Object JavaDoc[] expandedElements = commonViewer.getExpandedElements();
126
127                 contentService.update();
128
129                 commonViewer.refresh();
130                 
131                 Object JavaDoc[] originalObjects = ssel.toArray();
132                 
133                 commonViewer.setExpandedElements(expandedElements);
134
135                 IStructuredSelection newSelection = new StructuredSelection(originalObjects);
136                 commonViewer.setSelection(newSelection, true);
137             }
138
139         } finally {
140             commonViewer.getControl().setRedraw(true);
141         }
142
143         return Status.OK_STATUS;
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor,
150      * org.eclipse.core.runtime.IAdaptable)
151      */

152     public IStatus redo(IProgressMonitor monitor, IAdaptable info) {
153         return null;
154     }
155
156     /*
157      * (non-Javadoc)
158      *
159      * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor,
160      * org.eclipse.core.runtime.IAdaptable)
161      */

162     public IStatus undo(IProgressMonitor monitor, IAdaptable info) {
163         return null;
164     }
165 }
166
Popular Tags