KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.resources.IResource;
14
15 import org.eclipse.swt.graphics.Image;
16
17 import org.eclipse.jdt.core.IImportDeclaration;
18 import org.eclipse.jdt.core.IJavaElement;
19
20 import org.eclipse.jdt.ui.JavaElementLabels;
21
22 import org.eclipse.jdt.internal.ui.viewsupport.ColoredJavaElementLabels;
23 import org.eclipse.jdt.internal.ui.viewsupport.ColoredString;
24
25 public class SortingLabelProvider extends SearchLabelProvider {
26     
27     public static final int SHOW_ELEMENT_CONTAINER= 1; // default
28
public static final int SHOW_CONTAINER_ELEMENT= 2;
29     public static final int SHOW_PATH= 3;
30     
31     private static final long FLAGS_QUALIFIED= DEFAULT_SEARCH_TEXTFLAGS | JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.I_FULLY_QUALIFIED
32         | JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.D_QUALIFIED | JavaElementLabels.CF_QUALIFIED | JavaElementLabels.CU_QUALIFIED | ColoredJavaElementLabels.COLORIZE;
33     
34     
35     private int fCurrentOrder;
36     
37     public SortingLabelProvider(JavaSearchResultPage page) {
38         super(page);
39         fCurrentOrder= SHOW_ELEMENT_CONTAINER;
40     }
41
42     public Image getImage(Object JavaDoc element) {
43         Image image= null;
44         if (element instanceof IJavaElement || element instanceof IResource)
45             image= super.getImage(element);
46         if (image != null)
47             return image;
48         return getParticipantImage(element);
49     }
50         
51     public final String JavaDoc getText(Object JavaDoc element) {
52         if (element instanceof IImportDeclaration)
53             element= ((IImportDeclaration)element).getParent().getParent();
54         
55         String JavaDoc text= super.getText(element);
56         if (text.length() > 0) {
57             String JavaDoc labelWithCount= getLabelWithCounts(element, text);
58             if (fCurrentOrder == SHOW_ELEMENT_CONTAINER) {
59                 labelWithCount += getPostQualification(element, text);
60             }
61             return labelWithCount;
62         }
63         return getParticipantText(element);
64     }
65     
66     /* (non-Javadoc)
67      * @see org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider#getRichTextLabel(java.lang.Object)
68      */

69     public ColoredString getRichTextLabel(Object JavaDoc element) {
70         if (element instanceof IImportDeclaration)
71             element= ((IImportDeclaration)element).getParent().getParent();
72         
73         ColoredString text= super.getRichTextLabel(element);
74         if (text.length() > 0) {
75             ColoredString countLabel= getColoredLabelWithCounts(element, text);
76             if (fCurrentOrder == SHOW_ELEMENT_CONTAINER) {
77                 countLabel.append(getPostQualification(element, text.getString()), ColoredJavaElementLabels.QUALIFIER_STYLE);
78             }
79             return countLabel;
80         }
81         return new ColoredString(getParticipantText(element));
82     }
83
84     private String JavaDoc getPostQualification(Object JavaDoc element, String JavaDoc text) {
85         String JavaDoc textLabel= JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_POST_QUALIFIED);
86         int indexOf= textLabel.indexOf(JavaElementLabels.CONCAT_STRING);
87         if (indexOf != -1) {
88             return textLabel.substring(indexOf);
89         }
90         return new String JavaDoc();
91     }
92
93     public void setOrder(int orderFlag) {
94         fCurrentOrder= orderFlag;
95         long flags= 0;
96         if (orderFlag == SHOW_ELEMENT_CONTAINER)
97             flags= DEFAULT_SEARCH_TEXTFLAGS;
98         else if (orderFlag == SHOW_CONTAINER_ELEMENT)
99             flags= FLAGS_QUALIFIED;
100         else if (orderFlag == SHOW_PATH) {
101             flags= FLAGS_QUALIFIED | JavaElementLabels.PREPEND_ROOT_PATH;
102         }
103         setTextFlags(flags);
104     }
105 }
106
Popular Tags