KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.preference.IPreferenceStore;
14 import org.eclipse.jface.util.IPropertyChangeListener;
15 import org.eclipse.jface.util.PropertyChangeEvent;
16 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
17
18 import org.eclipse.ui.IEditorRegistry;
19 import org.eclipse.ui.IPropertyListener;
20 import org.eclipse.ui.PlatformUI;
21
22 import org.eclipse.jdt.ui.JavaElementLabels;
23 import org.eclipse.jdt.ui.PreferenceConstants;
24
25 /**
26  * JavaUILabelProvider that respects settings from the Appearance preference page.
27  * Triggers a viewer update when a preference changes.
28  */

29 public class AppearanceAwareLabelProvider extends JavaUILabelProvider implements IPropertyChangeListener, IPropertyListener {
30
31     public final static long DEFAULT_TEXTFLAGS= JavaElementLabels.ROOT_VARIABLE | JavaElementLabels.T_TYPE_PARAMETERS | JavaElementLabels.M_PARAMETER_TYPES |
32         JavaElementLabels.M_APP_TYPE_PARAMETERS | JavaElementLabels.M_APP_RETURNTYPE | JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED;
33     public final static int DEFAULT_IMAGEFLAGS= JavaElementImageProvider.OVERLAY_ICONS;
34     
35     private long fTextFlagMask;
36     private int fImageFlagMask;
37     
38     /**
39      * Constructor for AppearanceAwareLabelProvider.
40      */

41     public AppearanceAwareLabelProvider(long textFlags, int imageFlags) {
42         super(textFlags, imageFlags);
43         initMasks();
44         PreferenceConstants.getPreferenceStore().addPropertyChangeListener(this);
45         PlatformUI.getWorkbench().getEditorRegistry().addPropertyListener(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.getBoolean(PreferenceConstants.APPEARANCE_METHOD_TYPEPARAMETERS)) {
62             fTextFlagMask ^= JavaElementLabels.M_APP_TYPE_PARAMETERS;
63         }
64         if (!store.getBoolean(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES)) {
65             fTextFlagMask ^= JavaElementLabels.P_COMPRESSED;
66         }
67         if (!store.getBoolean(PreferenceConstants.APPEARANCE_CATEGORY)) {
68             fTextFlagMask ^= JavaElementLabels.ALL_CATEGORY;
69         }
70         
71         fImageFlagMask= -1;
72     }
73
74     /*
75      * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
76      */

77     public void propertyChange(PropertyChangeEvent event) {
78         String JavaDoc property= event.getProperty();
79         if (property.equals(PreferenceConstants.APPEARANCE_METHOD_RETURNTYPE)
80                 || property.equals(PreferenceConstants.APPEARANCE_METHOD_TYPEPARAMETERS)
81                 || property.equals(PreferenceConstants.APPEARANCE_CATEGORY)
82                 || property.equals(PreferenceConstants.APPEARANCE_PKG_NAME_PATTERN_FOR_PKG_VIEW)
83                 || property.equals(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES)) {
84             initMasks();
85             LabelProviderChangedEvent lpEvent= new LabelProviderChangedEvent(this, null); // refresh all
86
fireLabelProviderChanged(lpEvent);
87         }
88     }
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.ui.IPropertyListener#propertyChanged(java.lang.Object, int)
92      */

93     public void propertyChanged(Object JavaDoc source, int propId) {
94         if (propId == IEditorRegistry.PROP_CONTENTS) {
95             fireLabelProviderChanged(new LabelProviderChangedEvent(this, null)); // refresh all
96
}
97     }
98
99     /*
100      * @see IBaseLabelProvider#dispose()
101      */

102     public void dispose() {
103         PreferenceConstants.getPreferenceStore().removePropertyChangeListener(this);
104         PlatformUI.getWorkbench().getEditorRegistry().removePropertyListener(this);
105         super.dispose();
106     }
107
108     /*
109      * @see JavaUILabelProvider#evaluateImageFlags()
110      */

111     protected int evaluateImageFlags(Object JavaDoc element) {
112         return getImageFlags() & fImageFlagMask;
113     }
114
115     /*
116      * @see JavaUILabelProvider#evaluateTextFlags()
117      */

118     protected long evaluateTextFlags(Object JavaDoc element) {
119         return getTextFlags() & fTextFlagMask;
120     }
121
122 }
123
Popular Tags