KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > DecoratingJavaLabelProvider


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.viewsupport;
12
13 import org.eclipse.jface.viewers.DecoratingLabelProvider;
14 import org.eclipse.jface.viewers.DecorationContext;
15 import org.eclipse.jface.viewers.ILabelDecorator;
16 import org.eclipse.jface.viewers.ILabelProvider;
17 import org.eclipse.jface.viewers.LabelDecorator;
18
19 import org.eclipse.ui.PlatformUI;
20
21 import org.eclipse.jdt.ui.ProblemsLabelDecorator;
22
23 import org.eclipse.jdt.internal.ui.packageview.HierarchicalDecorationContext;
24
25 public class DecoratingJavaLabelProvider extends DecoratingLabelProvider implements IRichLabelProvider {
26     
27     /**
28      * Decorating label provider for Java. Combines a JavaUILabelProvider
29      * with problem and override indicator with the workbench decorator (label
30      * decorator extension point).
31      * @param labelProvider the label provider to decorate
32      */

33     public DecoratingJavaLabelProvider(JavaUILabelProvider labelProvider) {
34         this(labelProvider, true);
35     }
36
37     /**
38      * Decorating label provider for Java. Combines a JavaUILabelProvider
39      * (if enabled with problem indicator) with the workbench
40      * decorator (label decorator extension point).
41      * @param labelProvider the label provider to decorate
42      * @param errorTick show error ticks
43      */

44     public DecoratingJavaLabelProvider(JavaUILabelProvider labelProvider, boolean errorTick) {
45         this(labelProvider, errorTick, true);
46     }
47     
48     /**
49      * Decorating label provider for Java. Combines a JavaUILabelProvider
50      * (if enabled with problem indicator) with the workbench
51      * decorator (label decorator extension point).
52      * @param labelProvider the label provider to decorate
53      * @param errorTick show error ticks
54      * @param flatPackageMode configure flat package mode
55      */

56     public DecoratingJavaLabelProvider(JavaUILabelProvider labelProvider, boolean errorTick, boolean flatPackageMode) {
57         super(labelProvider, PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator());
58         if (errorTick) {
59             labelProvider.addLabelDecorator(new ProblemsLabelDecorator(null));
60         }
61         setFlatPackageMode(flatPackageMode);
62     }
63     
64     /**
65      * Tells the label decorator if the view presents packages flat or hierarchical.
66      * @param enable If set, packages are presented in flat mode.
67      */

68     public void setFlatPackageMode(boolean enable) {
69         if (enable) {
70             setDecorationContext(DecorationContext.DEFAULT_CONTEXT);
71         } else {
72             setDecorationContext(HierarchicalDecorationContext.CONTEXT);
73         }
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jdt.internal.ui.viewsupport.IRichLabelProvider#getRichTextLabel(Object)
78      */

79     public ColoredString getRichTextLabel(Object JavaDoc element) {
80         ILabelProvider labelProvider= getLabelProvider();
81         if (labelProvider instanceof IRichLabelProvider) {
82             // get a rich label from the label decorator
83
IRichLabelProvider richLabelProvider= (IRichLabelProvider) labelProvider;
84             ColoredString richLabel= richLabelProvider.getRichTextLabel(element);
85             if (richLabel != null) {
86                 String JavaDoc decorated= null;
87                 ILabelDecorator labelDecorator= getLabelDecorator();
88                 if (labelDecorator != null) {
89                     if (labelDecorator instanceof LabelDecorator) {
90                         decorated= ((LabelDecorator) labelDecorator).decorateText(richLabel.getString(), element, getDecorationContext());
91                     } else {
92                         decorated= labelDecorator.decorateText(richLabel.getString(), element);
93                     }
94                 }
95                 if (decorated != null) {
96                     return ColoredJavaElementLabels.decorateColoredString(richLabel, decorated, ColoredJavaElementLabels.DECORATIONS_STYLE);
97                 }
98                 return richLabel;
99             }
100         }
101         return null;
102     }
103
104 }
105
Popular Tags