KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ModelElementSelectionPage


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 package org.eclipse.team.internal.ui.mapping;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.*;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.viewers.*;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.TreeEvent;
25 import org.eclipse.swt.events.TreeListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.team.core.mapping.ISynchronizationScope;
29 import org.eclipse.team.core.mapping.ISynchronizationScopeManager;
30 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager;
31 import org.eclipse.team.internal.ui.*;
32 import org.eclipse.team.internal.ui.synchronize.GlobalRefreshElementSelectionPage;
33 import org.eclipse.team.ui.TeamUI;
34 import org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor;
35 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
36 import org.eclipse.ui.IWorkingSet;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
39 import org.eclipse.ui.navigator.*;
40 import org.eclipse.ui.views.navigator.ResourceSorter;
41
42 public class ModelElementSelectionPage extends GlobalRefreshElementSelectionPage implements INavigatorContentServiceListener {
43     
44     private INavigatorContentService service;
45     private ISynchronizationScopeManager manager;
46     private ContainerCheckedTreeViewer fViewer;
47     private boolean initialized;
48
49     public ModelElementSelectionPage(IResource[] roots) {
50         super("elementSelection"); //$NON-NLS-1$
51
setDescription(TeamUIMessages.GlobalRefreshResourceSelectionPage_2);
52         setTitle(TeamUIMessages.GlobalRefreshResourceSelectionPage_3);
53         List result = new ArrayList();
54         for (int i = 0; i < roots.length; i++) {
55             IResource resource = roots[i];
56             result.add(Utils.getResourceMapping(resource));
57         }
58         manager = new SynchronizationScopeManager(TeamUIMessages.ModelElementSelectionPage_0, (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]),
59                         ResourceMappingContext.LOCAL_CONTEXT, true);
60     }
61
62     protected ContainerCheckedTreeViewer createViewer(Composite top) {
63         GridData data;
64         fViewer = new ContainerCheckedTreeViewer(top, SWT.BORDER);
65         service = NavigatorContentServiceFactory.INSTANCE.createContentService(CommonViewerAdvisor.TEAM_NAVIGATOR_CONTENT, fViewer);
66         service.bindExtensions(TeamUI.getTeamContentProviderManager().getContentProviderIds(manager.getScope()), true);
67         service.getActivationService().activateExtensions(TeamUI.getTeamContentProviderManager().getContentProviderIds(manager.getScope()), true);
68         service.addListener(this);
69         data = new GridData(GridData.FILL_BOTH);
70         //data.widthHint = 200;
71
data.heightHint = 100;
72         fViewer.getControl().setLayoutData(data);
73         fViewer.setContentProvider(service.createCommonContentProvider());
74         fViewer.setLabelProvider(new DecoratingLabelProvider(service.createCommonLabelProvider(), PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
75         fViewer.addCheckStateListener(new ICheckStateListener() {
76             public void checkStateChanged(CheckStateChangedEvent event) {
77                 Object JavaDoc element = event.getElement();
78                 //If the workspace model has been checked, switch the scope to workspace
79
if (event.getChecked() && element instanceof ModelProvider && ((ModelProvider) element).getId().equals(ModelProvider.RESOURCE_MODEL_PROVIDER_ID)) {
80                         setWorkspaceSelected(true);
81                 } else {
82                     //Get the resource mapping from the element
83
ResourceMapping mapping = Utils.getResourceMapping(element);
84                     if (mapping != null) {
85                         if (!(element instanceof ModelProvider)) {
86                             uncheckOtherModels(mapping.getModelProviderId());
87                             event.getCheckable().setChecked(event.getElement(), event.getChecked());
88                         }
89                         updateOKStatus();
90                     } else
91                         updateOKStatus();
92                 }
93             }
94         });
95         fViewer.getTree().addTreeListener(new TreeListener(){
96
97             public void treeCollapsed(TreeEvent e) {
98                 //no-op
99
}
100
101             public void treeExpanded(TreeEvent e) {
102                 if (isWorkingSetSelected())
103                     checkWorkingSetElements();
104             }
105         });
106         fViewer.setSorter(new ResourceSorter(ResourceSorter.NAME));
107         return fViewer;
108     }
109     
110     public ResourceMapping[] getSelectedMappings() {
111         if (isWorkingSetSelected()) {
112             List result = new ArrayList();
113             IWorkingSet[] sets = getWorkingSets();
114             for (int i = 0; i < sets.length; i++) {
115                 IWorkingSet set = sets[i];
116                 result.add(Utils.getResourceMapping(set));
117             }
118             return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
119         }
120         if (isWorkspaceSelected()) {
121             try {
122                 ModelProvider provider = ModelProvider.getModelProviderDescriptor(ModelProvider.RESOURCE_MODEL_PROVIDER_ID).getModelProvider();
123                 ResourceMapping mapping = Utils.getResourceMapping(provider);
124                 if (mapping != null) {
125                     return new ResourceMapping[] {mapping };
126                 }
127             } catch (CoreException e) {
128                 // Shouldn't happen
129
TeamUIPlugin.log(e);
130             }
131             ResourceMapping[] mappings = manager.getScope().getMappings(ModelProvider.RESOURCE_MODEL_PROVIDER_ID);
132             return mappings;
133         }
134         List result = new ArrayList();
135         Object JavaDoc[] objects = getRootElement();
136         for (int i = 0; i < objects.length; i++) {
137             Object JavaDoc object = objects[i];
138             ResourceMapping mapping = Utils.getResourceMapping(object);
139             if (mapping != null) {
140                 result.add(mapping);
141             }
142         }
143         return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
144     }
145     
146     public void dispose() {
147         service.dispose();
148         super.dispose();
149     }
150
151     protected void checkAll() {
152         getViewer().setCheckedElements(manager.getScope().getModelProviders());
153     }
154
155     protected boolean checkWorkingSetElements() {
156         List allWorkingSetElements = new ArrayList();
157         IWorkingSet[] workingSets = getWorkingSets();
158         for (int i = 0; i < workingSets.length; i++) {
159             IWorkingSet set = workingSets[i];
160             allWorkingSetElements.addAll(computeSelectedResources(new StructuredSelection(set.getElements())));
161         }
162         getViewer().setCheckedElements(allWorkingSetElements.toArray());
163         return !allWorkingSetElements.isEmpty();
164     }
165
166     private Collection computeSelectedResources(StructuredSelection selection) {
167         List result = new ArrayList();
168         for (Iterator iter = selection.iterator(); iter.hasNext();) {
169             Object JavaDoc element = iter.next();
170             ResourceMapping mapping = Utils.getResourceMapping(element);
171             if (mapping != null && scopeContainsMapping(mapping)) {
172                 result.add(element);
173             }
174         }
175         return result;
176     }
177
178     private boolean scopeContainsMapping(ResourceMapping mapping) {
179         ResourceMapping[] mappings = manager.getScope().getMappings();
180         for (int i = 0; i < mappings.length; i++) {
181             ResourceMapping m = mappings[i];
182             if (m.contains(mapping)) {
183                 return true;
184             }
185         }
186         return false;
187     }
188
189     public void onLoad(INavigatorContentExtension anExtension) {
190         anExtension.getStateModel().setProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_SCOPE, manager.getScope());
191     }
192     
193     public void setVisible(boolean visible) {
194         super.setVisible(visible);
195         if (visible && !initialized) {
196             initialize();
197             if (initialized) {
198                 //check to see if all models are disabled
199
ISynchronizationScope syncScope = manager.getScope();
200                 ModelProvider[] providers = syncScope.getModelProviders();
201                 boolean foundEnabledModelProvider = false;
202                 for (int i = 0; i < providers.length; i++) {
203                     if (isEnabled(providers[i])){
204                         foundEnabledModelProvider = true;
205                         break;
206                     }
207                 }
208                 
209                 if (!foundEnabledModelProvider){
210                     if (MessageDialog.openConfirm(getShell(), TeamUIMessages.ModelElementSelectionPage_AllModelsDisabledTitle, TeamUIMessages.ModelElementSelectionPage_AllModelsDisabledMessage)) {
211                         ArrayList teamProviderDescriptors = new ArrayList();
212                         for (int i = 0; i < providers.length; i++)
213                             teamProviderDescriptors.add(TeamUI.getTeamContentProviderManager().getDescriptor(providers[i].getId()));
214                         
215                         ITeamContentProviderDescriptor[] desc = (ITeamContentProviderDescriptor[]) teamProviderDescriptors.toArray(new ITeamContentProviderDescriptor[teamProviderDescriptors.size()]);
216                         TeamUI.getTeamContentProviderManager().setEnabledDescriptors(desc);
217                     }
218                 }
219                 service.bindExtensions(TeamUI.getTeamContentProviderManager().getContentProviderIds(syncScope), true);
220                 service.getActivationService().activateExtensions(TeamUI.getTeamContentProviderManager().getContentProviderIds(syncScope), true);
221                 fViewer.setInput(syncScope);
222                 initializeScopingHint();
223             }
224         }
225     }
226
227     private void initialize() {
228         try {
229             getContainer().run(true, true, new IRunnableWithProgress() {
230                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
231                         InterruptedException JavaDoc {
232                     try {
233                         manager.initialize(monitor);
234                         initialized = true;
235                     } catch (CoreException e) {
236                         throw new InvocationTargetException JavaDoc(e);
237                     }
238                 }
239             
240             });
241         } catch (InvocationTargetException JavaDoc e) {
242             Utils.handleError(getShell(), e, null, null);
243         } catch (InterruptedException JavaDoc e) {
244             // ignore
245
}
246     }
247     
248     private boolean isEnabled(ModelProvider provider) {
249         ITeamContentProviderDescriptor desc = TeamUI.getTeamContentProviderManager().getDescriptor(provider.getId());
250         return (desc != null && desc.isEnabled());
251     }
252     
253     private void uncheckOtherModels(String JavaDoc modelProviderId) {
254         
255         if (!isSelectedResourcesSelected()) {
256             ModelProvider[] providers = manager.getScope().getModelProviders();
257             ArrayList disabledProviders = new ArrayList();
258             for (int i = 0; i < providers.length; i++) {
259                 if (!providers[i].getId().equals(modelProviderId)) {
260                     disabledProviders.add(providers[i]);
261                 }
262             }
263
264             for (Iterator iterator = disabledProviders.iterator(); iterator.hasNext();) {
265                 ModelProvider disable = (ModelProvider) iterator.next();
266                 fViewer.setChecked(disable, false);
267             }
268         }
269             
270         
271     }
272
273 }
274
Popular Tags