KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.util.*;
14
15 import org.eclipse.core.resources.mapping.IModelProviderDescriptor;
16 import org.eclipse.core.resources.mapping.ModelProvider;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.preference.PreferencePage;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.team.internal.ui.*;
27 import org.eclipse.team.ui.TeamUI;
28 import org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor;
29 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
30 import org.eclipse.ui.*;
31
32 public class ModelEnablementPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
33
34     private Set previosulyEnabled = new HashSet();
35     
36     public ModelEnablementPreferencePage() {
37         setTitle(TeamUIMessages.ModelEnablementPreferencePage_0);
38         setPreferenceStore(TeamUIPlugin.getPlugin().getPreferenceStore());
39     }
40
41     private CheckboxTableViewer tableViewer;
42
43     protected Control createContents(Composite parent) {
44         
45         Composite composite = new Composite(parent, SWT.NONE);
46         composite.setLayout(new GridLayout());
47         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
48         
49         Label l = SWTUtils.createLabel(composite, TeamUIMessages.ModelEnablementPreferencePage_1);
50         l.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
51         
52         tableViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
53         tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
54         tableViewer.setContentProvider(new IStructuredContentProvider() {
55             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
56                 // noting to do
57
}
58             public void dispose() {
59                 // nothing to do
60
}
61             public Object JavaDoc[] getElements(Object JavaDoc element) {
62                 if (element instanceof ITeamContentProviderManager) {
63                     ITeamContentProviderManager manager = (ITeamContentProviderManager) element;
64                     return manager.getDescriptors();
65                 }
66                 return new Object JavaDoc[0];
67             }
68         });
69         tableViewer.setLabelProvider(new LabelProvider() {
70             Map images = new HashMap();
71             public String JavaDoc getText(Object JavaDoc element) {
72                 if (element instanceof ITeamContentProviderDescriptor) {
73                     ITeamContentProviderDescriptor desc = (ITeamContentProviderDescriptor) element;
74                     return getTextFor(desc);
75                 }
76                 return super.getText(element);
77             }
78             private String JavaDoc getTextFor(ITeamContentProviderDescriptor teamContentDescriptor) {
79                 String JavaDoc name = teamContentDescriptor.getName();
80                 
81                 if (name != null && !name.equals("")) //$NON-NLS-1$
82
return name;
83                 
84                 String JavaDoc modelProviderID = teamContentDescriptor.getModelProviderId();
85                 IModelProviderDescriptor desc = ModelProvider.getModelProviderDescriptor(modelProviderID);
86                 if (desc != null) {
87                     return getLabel(desc);
88                 }
89                 return modelProviderID;
90             }
91             public Image getImage(Object JavaDoc element) {
92                 if (element instanceof ITeamContentProviderDescriptor) {
93                     ITeamContentProviderDescriptor desc = (ITeamContentProviderDescriptor) element;
94                     Image image = (Image)images.get(desc);
95                     if (image == null) {
96                         ImageDescriptor idesc = desc.getImageDescriptor();
97                         if (idesc != null) {
98                             image = idesc.createImage();
99                             if (image != null) {
100                                 images.put(desc, image);
101                             }
102                         }
103                     }
104                     return image;
105                 }
106                 return super.getImage(element);
107             }
108             public void dispose() {
109                 for (Iterator iter = images.values().iterator(); iter.hasNext();) {
110                     Image image = (Image) iter.next();
111                     image.dispose();
112                 }
113                 super.dispose();
114             }
115         });
116         tableViewer.setComparator(new ViewerComparator() {
117             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
118                 if (e1 instanceof ITeamContentProviderDescriptor && e2 instanceof ITeamContentProviderDescriptor) {
119                     ITeamContentProviderDescriptor d1 = (ITeamContentProviderDescriptor) e1;
120                     ITeamContentProviderDescriptor d2 = (ITeamContentProviderDescriptor) e2;
121                     IModelProviderDescriptor md1 = ModelProvider.getModelProviderDescriptor(d1.getModelProviderId());
122                     IModelProviderDescriptor md2 = ModelProvider.getModelProviderDescriptor(d2.getModelProviderId());
123                     if (md1 != null && md2 != null)
124                         return getLabel(md1).compareTo(getLabel(md2));
125                 }
126                 return super.compare(viewer, e1, e2);
127             }
128         });
129         tableViewer.setInput(TeamUI.getTeamContentProviderManager());
130         updateChecks();
131         applyDialogFont(composite);
132         
133         //F1
134
PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.MODEL_PREFERENCE_PAGE);
135         
136         return composite;
137     }
138
139     private void updateChecks() {
140         ITeamContentProviderDescriptor[] descriptors = TeamUI.getTeamContentProviderManager().getDescriptors();
141         for (int i = 0; i < descriptors.length; i++) {
142             ITeamContentProviderDescriptor descriptor = descriptors[i];
143             if (descriptor.isEnabled()) {
144                 previosulyEnabled.add(descriptor);
145             }
146         }
147         tableViewer.setCheckedElements(previosulyEnabled.toArray());
148     }
149     
150     public boolean performOk() {
151         Object JavaDoc[] checked = tableViewer.getCheckedElements();
152         Set nowEnabled = new HashSet();
153         nowEnabled.addAll(Arrays.asList(checked));
154         if (hasDescriptorEnablementChanged(checked)) {
155             TeamUI.getTeamContentProviderManager().setEnabledDescriptors(
156                     (ITeamContentProviderDescriptor[]) nowEnabled.toArray(new ITeamContentProviderDescriptor[nowEnabled.size()]));
157             previosulyEnabled = nowEnabled;
158         }
159         return true;
160     }
161
162     private boolean hasDescriptorEnablementChanged(Object JavaDoc[] checked) {
163         ITeamContentProviderDescriptor[] descriptors = TeamUI.getTeamContentProviderManager().getDescriptors();
164         for (int i = 0; i < descriptors.length; i++) {
165             ITeamContentProviderDescriptor descriptor = descriptors[i];
166             boolean enable = false;
167             for (int j = 0; j < checked.length; j++) {
168                 ITeamContentProviderDescriptor checkedDesc = (ITeamContentProviderDescriptor)checked[j];
169                 if (checkedDesc.getModelProviderId().equals(descriptor.getModelProviderId())) {
170                     enable = true;
171                     break;
172                 }
173             }
174             if (descriptor.isEnabled() != enable) {
175                 return true;
176             }
177         }
178         return false;
179     }
180     
181     protected void performDefaults() {
182         tableViewer.setCheckedElements(TeamUI.getTeamContentProviderManager().getDescriptors());
183     }
184
185     public void init(IWorkbench workbench) {
186         // ignore
187
}
188
189     private String JavaDoc getLabel(IModelProviderDescriptor desc) {
190         // Only do this for the resource model since we don;t want to
191
// load all model providers (see bug 133604)
192
if (desc.getId().equals(ModelProvider.RESOURCE_MODEL_PROVIDER_ID))
193             try {
194                 return Utils.getLabel(desc.getModelProvider());
195             } catch (CoreException e) {
196                 TeamUIPlugin.log(e);
197             }
198         return desc.getLabel();
199     }
200
201 }
202
Popular Tags