bbed6ac6f91ed92ef5e93d41296a39c53b162bf4
[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.PreferenceConstants;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
19
20 /**
21  * JavaUILabelProvider that respects settings from the Appearance preference
22  * page. Triggers a viewer update when a preference changes.
23  */
24 public class AppearanceAwareLabelProvider extends JavaUILabelProvider implements
25                 IPropertyChangeListener {
26
27         public final static int DEFAULT_TEXTFLAGS = JavaElementLabels.ROOT_VARIABLE
28                         | JavaElementLabels.M_PARAMETER_TYPES
29                         | JavaElementLabels.M_APP_RETURNTYPE
30                         | JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED;
31
32         public final static int DEFAULT_IMAGEFLAGS = JavaElementImageProvider.OVERLAY_ICONS;
33
34         private int fTextFlagMask;
35
36         private int fImageFlagMask;
37
38         /**
39          * Constructor for AppearanceAwareLabelProvider.
40          */
41         public AppearanceAwareLabelProvider(int textFlags, int imageFlags) {
42                 super(textFlags, imageFlags);
43                 initMasks();
44                 PreferenceConstants.getPreferenceStore()
45                                 .addPropertyChangeListener(this);
46         }
47
48         /**
49          * Creates a labelProvider with DEFAULT_TEXTFLAGS and DEFAULT_IMAGEFLAGS
50          */
51         public AppearanceAwareLabelProvider() {
52                 this(DEFAULT_TEXTFLAGS, DEFAULT_IMAGEFLAGS);
53         }
54
55         private void initMasks() {
56                 IPreferenceStore store = PreferenceConstants.getPreferenceStore();
57                 fTextFlagMask = -1;
58                 if (!store.getBoolean(PreferenceConstants.APPEARANCE_METHOD_RETURNTYPE)) {
59                         fTextFlagMask ^= JavaElementLabels.M_APP_RETURNTYPE;
60                 }
61                 if (!store
62                                 .getBoolean(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES)) {
63                         fTextFlagMask ^= JavaElementLabels.P_COMPRESSED;
64                 }
65
66                 fImageFlagMask = -1;
67         }
68
69         /*
70          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
71          */
72         public void propertyChange(PropertyChangeEvent event) {
73                 String property = event.getProperty();
74                 if (property.equals(PreferenceConstants.APPEARANCE_METHOD_RETURNTYPE)
75                                 || property
76                                                 .equals(PreferenceConstants.APPEARANCE_PKG_NAME_PATTERN_FOR_PKG_VIEW)
77                                 || property
78                                                 .equals(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES)) {
79                         initMasks();
80                         LabelProviderChangedEvent lpEvent = new LabelProviderChangedEvent(
81                                         this, null); // refresh all
82                         fireLabelProviderChanged(lpEvent);
83                 }
84         }
85
86         /*
87          * @see IBaseLabelProvider#dispose()
88          */
89         public void dispose() {
90                 PreferenceConstants.getPreferenceStore().removePropertyChangeListener(
91                                 this);
92                 super.dispose();
93         }
94
95         /*
96          * @see JavaUILabelProvider#evaluateImageFlags()
97          */
98         protected int evaluateImageFlags(Object element) {
99                 return getImageFlags() & fImageFlagMask;
100         }
101
102         /*
103          * @see JavaUILabelProvider#evaluateTextFlags()
104          */
105         protected int evaluateTextFlags(Object element) {
106                 return getTextFlags() & fTextFlagMask;
107         }
108
109 }