KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > patch > PreviewPatchLabelDecorator


1 /*******************************************************************************
2  * Copyright (c) 2005 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.compare.internal.patch;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.compare.internal.CompareUIPlugin;
20 import org.eclipse.compare.internal.DiffImage;
21 import org.eclipse.compare.structuremergeviewer.Differencer;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.ILabelDecorator;
26 import org.eclipse.jface.viewers.ILabelProviderListener;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.graphics.Image;
29
30 public class PreviewPatchLabelDecorator implements ILabelDecorator {
31
32     /** Maps strings to images */
33     private Map JavaDoc fImages= new Hashtable JavaDoc(10);
34     private List JavaDoc fDisposeOnShutdownImages= new ArrayList JavaDoc();
35
36     ImageDescriptor errId= CompareUIPlugin.getImageDescriptor("ovr16/error_ov.gif"); //$NON-NLS-1$
37

38     static final String JavaDoc error = "error"; //$NON-NLS-1$
39
static final String JavaDoc add = "add"; //$NON-NLS-1$
40
static final String JavaDoc delete = "del"; //$NON-NLS-1$
41

42     public Image decorateImage(Image image, Object JavaDoc element) {
43         
44         if (element instanceof Diff){
45           Diff diff = (Diff) element;
46           switch (diff.getType()){
47               case Differencer.ADDITION:
48               return getImageFor(add + (diff.fMatches ? "" : error), image, diff.fMatches); //$NON-NLS-1$
49

50               case Differencer.DELETION:
51               return getImageFor(delete + (diff.fMatches ? "" : error), image, diff.fMatches); //$NON-NLS-1$
52

53               default:
54               return getImageFor(diff.fMatches ? "" : error, image, diff.fMatches); //$NON-NLS-1$
55
}
56         } else if (element instanceof Hunk){
57             Hunk hunk = (Hunk) element;
58             return getImageFor((hunk.fMatches ? "" : error),image, hunk.fMatches); //$NON-NLS-1$
59
}
60     
61         return null;
62     }
63
64     private Image getImageFor(String JavaDoc id, Image image, boolean hasMatches) {
65         Image cached_image = (Image) fImages.get(id);
66         if (cached_image == null){
67             DiffImage diffImage = new DiffImage(image, hasMatches ? null : errId, 16, false);
68             cached_image = diffImage.createImage();
69             fImages.put(id, cached_image);
70             fDisposeOnShutdownImages.add(cached_image);
71         }
72         return cached_image;
73     }
74
75     public String JavaDoc decorateText(String JavaDoc text, Object JavaDoc element) {
76         if (element instanceof DiffProject){
77             DiffProject project = (DiffProject)element;
78             //Check to see if this project exists in the workspace
79
IResource projectExistsInWorkspace = ResourcesPlugin.getWorkspace().getRoot().findMember(project.getProject().getFullPath());
80             if(projectExistsInWorkspace == null)
81                 return NLS.bind(PatchMessages.Diff_2Args, new String JavaDoc[]{text, PatchMessages.PreviewPatchLabelDecorator_ProjectDoesNotExist});
82             
83             if (!project.getName().equals(project.getOriginalProjectName()))
84                 return NLS.bind(PatchMessages.Diff_2Args,
85                         new String JavaDoc[]{project.getOriginalProjectName(),
86                         NLS.bind(PatchMessages.PreviewPatchPage_Target, new String JavaDoc[]{project.getName()})});
87             
88         }
89         return null;
90     }
91     
92     public void dispose() {
93         if (fDisposeOnShutdownImages != null) {
94             Iterator JavaDoc i= fDisposeOnShutdownImages.iterator();
95             while (i.hasNext()) {
96                 Image img= (Image) i.next();
97                 if (!img.isDisposed())
98                     img.dispose();
99             }
100             fImages= null;
101         }
102     }
103
104     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
105         return false;
106     }
107
108     public void addListener(ILabelProviderListener listener) {
109         //don't need listener
110
}
111     
112     public void removeListener(ILabelProviderListener listener) {
113         //don't need listener
114
}
115 }
116
Popular Tags