KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > model > JavaSynchronizationLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.jdt.internal.ui.model;
12
13 import org.eclipse.core.resources.IFolder;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.ResourcesPlugin;
17
18 import org.eclipse.swt.graphics.Font;
19 import org.eclipse.swt.graphics.Image;
20
21 import org.eclipse.jface.viewers.DecoratingLabelProvider;
22 import org.eclipse.jface.viewers.ILabelProvider;
23 import org.eclipse.jface.viewers.ITreePathLabelProvider;
24 import org.eclipse.jface.viewers.TreePath;
25 import org.eclipse.jface.viewers.ViewerLabel;
26
27 import org.eclipse.team.core.diff.IDiff;
28 import org.eclipse.team.core.diff.IDiffTree;
29 import org.eclipse.team.core.mapping.ISynchronizationContext;
30
31 import org.eclipse.ltk.ui.refactoring.model.AbstractSynchronizationLabelProvider;
32
33 import org.eclipse.jdt.core.IPackageFragment;
34 import org.eclipse.jdt.core.JavaCore;
35
36 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
37
38 import org.eclipse.jdt.internal.ui.JavaPluginImages;
39
40 /**
41  * Java-aware synchronization label provider.
42  *
43  * @since 3.2
44  */

45 public final class JavaSynchronizationLabelProvider extends AbstractSynchronizationLabelProvider implements ITreePathLabelProvider{
46
47     /** The delegate label provider, or <code>null</code> */
48     private ILabelProvider fLabelProvider= null;
49
50     /** The model root, or <code>null</code> */
51     private Object JavaDoc fModelRoot= null;
52
53     /** The package image, or <code>null</code> */
54     private Image fPackageImage= null;
55
56     /**
57      * {@inheritDoc}
58      */

59     public void dispose() {
60         if (fPackageImage != null && !fPackageImage.isDisposed())
61             fPackageImage.dispose();
62         if (fLabelProvider != null)
63             fLabelProvider.dispose();
64         super.dispose();
65     }
66
67     /**
68      * {@inheritDoc}
69      */

70     public Image getDelegateImage(final Object JavaDoc element) {
71         if (element instanceof IPackageFragment) {
72             final IPackageFragment fragment= (IPackageFragment) element;
73             final IResource resource= fragment.getResource();
74             if (resource == null || !resource.exists()) {
75                 if (fPackageImage == null)
76                     fPackageImage= JavaPluginImages.DESC_OBJS_PACKAGE.createImage();
77                 return fPackageImage;
78             }
79         }
80         return super.getDelegateImage(element);
81     }
82
83     /**
84      * {@inheritDoc}
85      */

86     protected ILabelProvider getDelegateLabelProvider() {
87         if (fLabelProvider == null)
88             fLabelProvider= new DecoratingLabelProvider(new JavaModelLabelProvider(ModelMessages.JavaModelLabelProvider_project_preferences_label, ModelMessages.JavaModelLabelProvider_refactorings_label), new ProblemsLabelDecorator(null));
89         return fLabelProvider;
90     }
91
92     /**
93      * {@inheritDoc}
94      */

95     protected IDiff getDiff(final Object JavaDoc element) {
96         final ISynchronizationContext context= getContext();
97         final IResource resource= JavaModelProvider.getResource(element);
98         if (context != null && resource != null) {
99             final IDiff[] diff= JavaSynchronizationContentProvider.getDiffs(context, element);
100             for (int index= 0; index < diff.length; index++) {
101                 if (context.getDiffTree().getResource(diff[index]).equals(resource))
102                     return diff[index];
103             }
104         }
105         return super.getDiff(element);
106     }
107
108     /**
109      * {@inheritDoc}
110      */

111     protected int getMarkerSeverity(final Object JavaDoc element) {
112         // Decoration label provider is handling this
113
return -1;
114     }
115
116     /**
117      * {@inheritDoc}
118      */

119     protected Object JavaDoc getModelRoot() {
120         if (fModelRoot == null)
121             fModelRoot= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
122         return fModelRoot;
123     }
124
125     /**
126      * {@inheritDoc}
127      */

128     protected boolean hasDecendantConflicts(final Object JavaDoc element) {
129         final ISynchronizationContext context= getContext();
130         final IResource resource= JavaModelProvider.getResource(element);
131         if (context != null && resource != null)
132             return context.getDiffTree().getProperty(resource.getFullPath(), IDiffTree.P_HAS_DESCENDANT_CONFLICTS);
133         return super.hasDecendantConflicts(element);
134     }
135
136     /**
137      * {@inheritDoc}
138      */

139     protected boolean isBusy(final Object JavaDoc element) {
140         final ISynchronizationContext context= getContext();
141         final IResource resource= JavaModelProvider.getResource(element);
142         if (context != null && resource != null)
143             return context.getDiffTree().getProperty(resource.getFullPath(), IDiffTree.P_BUSY_HINT);
144         return super.isBusy(element);
145     }
146
147     /**
148      * {@inheritDoc}
149      */

150     protected boolean isIncludeOverlays() {
151         return true;
152     }
153
154     /* (non-Javadoc)
155      * @see org.eclipse.jface.viewers.ITreePathLabelProvider#updateLabel(org.eclipse.jface.viewers.ViewerLabel, org.eclipse.jface.viewers.TreePath)
156      */

157     public void updateLabel(ViewerLabel label, TreePath elementPath) {
158         Object JavaDoc firstSegment = elementPath.getFirstSegment();
159         if (firstSegment instanceof IProject && elementPath.getSegmentCount() == 2) {
160             IProject project = (IProject) firstSegment;
161             Object JavaDoc lastSegment = elementPath.getLastSegment();
162             if (lastSegment instanceof IFolder) {
163                 IFolder folder = (IFolder) lastSegment;
164                 if (!folder.getParent().equals(project)) {
165                     // This means that a folder that is not a direct child of the project
166
// is a child in the tree. Therefore, the resource content provider
167
// must be active and in compress folder mode so we will leave
168
// it to the resource provider to provide the proper label.
169
// We need to do this because of bug 153912
170
return;
171                 }
172             }
173         }
174         label.setImage(getImage(elementPath.getLastSegment()));
175         label.setText(getText(elementPath.getLastSegment()));
176         Font f = getFont(elementPath.getLastSegment());
177         if (f != null)
178             label.setFont(f);
179     }
180 }
181
Popular Tags