268cfaf5e84988639c5f565ae465a764aa883d26
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.viewsupport;
12
13 import net.sourceforge.phpdt.ui.OverrideIndicatorLabelDecorator;
14 import net.sourceforge.phpdt.ui.ProblemsLabelDecorator;
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.viewers.DecoratingLabelProvider;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.ui.IDecoratorManager;
21 import org.eclipse.ui.PlatformUI;
22
23 /**
24  * Decorator prepared for the switch to use lightweight label decorators:
25  * uncomment the lbale decorator entries in plugin.xml and change
26  * USE_LIGHTWEIGHT to true. Certain views don't want problems or override
27  * indicators, so they signal this in the constructor. So on each getImage the
28  * corrsponding decorators are turned off and on again.
29  */
30 public class DecoratingJavaLabelProvider extends DecoratingLabelProvider {
31
32         private static final boolean USE_LIGHTWEIGHT = false;
33
34         private static final String PROBLEM_DECORATOR_ID = "net.sourceforge.phpdt.ui.problem.decorator"; //$NON-NLS-1$
35
36         private static final String OVERRIDE_DECORATOR_ID = "net.sourceforge.phpdt.ui.override.decorator"; //$NON-NLS-1$
37
38         private boolean fUseErrorTick;
39
40         private boolean fUseOverride;
41
42         /**
43          * Decorating label provider for Java. Combines a JavaUILabelProvider with
44          * problem and override indicuator with the workbench decorator (label
45          * decorator extension point).
46          */
47         public DecoratingJavaLabelProvider(JavaUILabelProvider labelProvider) {
48                 this(labelProvider, true, true);
49         }
50
51         /**
52          * Decorating label provider for Java. Combines a JavaUILabelProvider (if
53          * enabled with problem and override indicator) with the workbench decorator
54          * (label decorator extension point).
55          */
56         public DecoratingJavaLabelProvider(JavaUILabelProvider labelProvider,
57                         boolean errorTick, boolean override) {
58                 super(labelProvider, PlatformUI.getWorkbench().getDecoratorManager()
59                                 .getLabelDecorator());
60                 fUseErrorTick = errorTick;
61                 fUseOverride = override;
62                 if (!USE_LIGHTWEIGHT) {
63                         if (errorTick) {
64                                 labelProvider
65                                                 .addLabelDecorator(new ProblemsLabelDecorator(null));
66                         }
67                         if (override) {
68                                 labelProvider
69                                                 .addLabelDecorator(new OverrideIndicatorLabelDecorator(
70                                                                 null));
71                         }
72                 }
73         }
74
75         /*
76          * (non-Javadoc)
77          * 
78          * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
79          */
80         public Image getImage(Object element) {
81                 if (USE_LIGHTWEIGHT) {
82                         IDecoratorManager manager = PlatformUI.getWorkbench()
83                                         .getDecoratorManager();
84
85                         boolean disableErrorTick = manager.getEnabled(PROBLEM_DECORATOR_ID)
86                                         && !fUseErrorTick;
87                         boolean disableOverride = manager.getEnabled(OVERRIDE_DECORATOR_ID)
88                                         && !fUseOverride;
89                         try {
90                                 if (disableErrorTick) {
91                                         manager.setEnabled(PROBLEM_DECORATOR_ID, false);
92                                 }
93                                 if (disableOverride) {
94                                         manager.setEnabled(OVERRIDE_DECORATOR_ID, false);
95                                 }
96                                 Image image = super.getImage(element);
97                                 if (disableErrorTick) {
98                                         manager.setEnabled(PROBLEM_DECORATOR_ID, true);
99                                 }
100                                 if (disableOverride) {
101                                         manager.setEnabled(OVERRIDE_DECORATOR_ID, true);
102                                 }
103                                 return image;
104                         } catch (CoreException e) {
105                                 PHPeclipsePlugin.log(e);
106                         }
107                 }
108                 return super.getImage(element);
109         }
110
111         /*
112          * (non-Javadoc)
113          * 
114          * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
115          */
116         public String getText(Object element) {
117                 if (USE_LIGHTWEIGHT) {
118                         IDecoratorManager manager = PlatformUI.getWorkbench()
119                                         .getDecoratorManager();
120
121                         boolean disableErrorTick = manager.getEnabled(PROBLEM_DECORATOR_ID)
122                                         && !fUseErrorTick;
123                         boolean disableOverride = manager.getEnabled(OVERRIDE_DECORATOR_ID)
124                                         && !fUseOverride;
125                         try {
126                                 if (disableErrorTick) {
127                                         manager.setEnabled(PROBLEM_DECORATOR_ID, false);
128                                 }
129                                 if (disableOverride) {
130                                         manager.setEnabled(OVERRIDE_DECORATOR_ID, false);
131                                 }
132                                 String text = super.getText(element);
133                                 if (disableErrorTick) {
134                                         manager.setEnabled(PROBLEM_DECORATOR_ID, true);
135                                 }
136                                 if (disableOverride) {
137                                         manager.setEnabled(OVERRIDE_DECORATOR_ID, true);
138                                 }
139                                 return text;
140                         } catch (CoreException e) {
141                                 PHPeclipsePlugin.log(e);
142                         }
143                 }
144                 return super.getText(element);
145         }
146
147 }