KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > PostfixLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.search;
12
13 import org.eclipse.swt.graphics.Image;
14
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IJavaModel;
20 import org.eclipse.jdt.core.IType;
21
22 import org.eclipse.jdt.ui.JavaElementLabels;
23
24 import org.eclipse.jdt.internal.ui.viewsupport.ColoredJavaElementLabels;
25 import org.eclipse.jdt.internal.ui.viewsupport.ColoredString;
26
27 public class PostfixLabelProvider extends SearchLabelProvider {
28     private ITreeContentProvider fContentProvider;
29     
30     public PostfixLabelProvider(JavaSearchResultPage page) {
31         super(page);
32         fContentProvider= new LevelTreeContentProvider.FastJavaElementProvider();
33     }
34
35     public Image getImage(Object JavaDoc element) {
36         Image image= super.getImage(element);
37         if (image != null)
38             return image;
39         return getParticipantImage(element);
40     }
41     
42     public String JavaDoc getText(Object JavaDoc element) {
43         String JavaDoc labelWithCounts= getLabelWithCounts(element, internalGetText(element));
44         return labelWithCounts + getQualification(element);
45     }
46     
47     private String JavaDoc getQualification(Object JavaDoc element) {
48         StringBuffer JavaDoc res= new StringBuffer JavaDoc();
49         
50         ITreeContentProvider provider= (ITreeContentProvider) fPage.getViewer().getContentProvider();
51         Object JavaDoc visibleParent= provider.getParent(element);
52         Object JavaDoc realParent= fContentProvider.getParent(element);
53         Object JavaDoc lastElement= element;
54         while (realParent != null && !(realParent instanceof IJavaModel) && !realParent.equals(visibleParent)) {
55             if (!isSameInformation(realParent, lastElement)) {
56                 res.append(JavaElementLabels.CONCAT_STRING).append(internalGetText(realParent));
57             }
58             lastElement= realParent;
59             realParent= fContentProvider.getParent(realParent);
60         }
61         return res.toString();
62     }
63
64     protected boolean hasChildren(Object JavaDoc element) {
65         ITreeContentProvider contentProvider= (ITreeContentProvider) fPage.getViewer().getContentProvider();
66         return contentProvider.hasChildren(element);
67     }
68
69     private String JavaDoc internalGetText(Object JavaDoc element) {
70         String JavaDoc text= super.getText(element);
71         if (text != null && text.length() > 0)
72             return text;
73         return getParticipantText(element);
74     }
75
76     private boolean isSameInformation(Object JavaDoc realParent, Object JavaDoc lastElement) {
77         if (lastElement instanceof IType) {
78             IType type= (IType) lastElement;
79             if (realParent instanceof IClassFile) {
80                 if (type.getClassFile().equals(realParent))
81                     return true;
82             } else if (realParent instanceof ICompilationUnit) {
83                 if (type.getCompilationUnit().equals(realParent))
84                     return true;
85             }
86         }
87         return false;
88     }
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.jdt.internal.ui.viewsupport.IRichLabelProvider#getRichTextLabel(java.lang.Object)
92      */

93     public ColoredString getRichTextLabel(Object JavaDoc element) {
94         ColoredString coloredString= getColoredLabelWithCounts(element, super.getRichTextLabel(element));
95         coloredString.append(getQualification(element), ColoredJavaElementLabels.QUALIFIER_STYLE);
96         return coloredString;
97     }
98
99 }
100
Popular Tags