KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > entryeditor > EntryEditorWidgetLabelProvider


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.widgets.entryeditor;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
26 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
27 import org.apache.directory.ldapstudio.browser.core.model.IValue;
28 import org.apache.directory.ldapstudio.valueeditors.IValueEditor;
29 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
30 import org.eclipse.jface.preference.PreferenceConverter;
31 import org.eclipse.jface.viewers.IColorProvider;
32 import org.eclipse.jface.viewers.IFontProvider;
33 import org.eclipse.jface.viewers.ITableLabelProvider;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.swt.graphics.Color;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.graphics.FontData;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.graphics.RGB;
40
41
42 /**
43  * The EntryEditorWidgetLabelProvider implements the label provider for
44  * the entry editor widget.
45  *
46  * It provides the type value pairs for {@link IValue} objects and type plus
47  * the number of values for {@link IAttribute} objects. It also implements
48  * {@link IFontProvider} and {@link IColorProvider} to set the font and color
49  * depending on whether the attribte is a must, may or operational attribute.
50  *
51  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
52  * @version $Rev$, $Date$
53  */

54 public class EntryEditorWidgetLabelProvider extends LabelProvider implements ITableLabelProvider, IFontProvider,
55     IColorProvider
56 {
57
58     /** The value editor manager. */
59     private ValueEditorManager valueEditorManager;
60
61
62     /**
63      * Creates a new instance of EntryEditorWidgetLabelProvider.
64      *
65      * @param valueEditorManager the value editor manager
66      */

67     public EntryEditorWidgetLabelProvider( ValueEditorManager valueEditorManager )
68     {
69         this.valueEditorManager = valueEditorManager;
70     }
71
72
73     /**
74      * {@inheritDoc}
75      */

76     public void dispose()
77     {
78         super.dispose();
79         valueEditorManager = null;
80     }
81
82
83     /**
84      * {@inheritDoc}
85      */

86     public final String JavaDoc getColumnText( Object JavaDoc obj, int index )
87     {
88         if ( obj != null && obj instanceof IValue )
89         {
90             IValue value = ( IValue ) obj;
91             switch ( index )
92             {
93                 case EntryEditorWidgetTableMetadata.KEY_COLUMN_INDEX:
94                     return value.getAttribute().getDescription();
95                 case EntryEditorWidgetTableMetadata.VALUE_COLUMN_INDEX:
96                     IValueEditor vp = this.valueEditorManager.getCurrentValueEditor( value );
97                     String JavaDoc dv = vp.getDisplayValue( value );
98                     return dv;
99                 default:
100                     return "";
101             }
102         }
103         else if ( obj != null && obj instanceof IAttribute )
104         {
105             IAttribute attribute = ( IAttribute ) obj;
106             if ( index == EntryEditorWidgetTableMetadata.KEY_COLUMN_INDEX )
107             {
108                 return attribute.getDescription() + " (" + attribute.getValueSize() + " values)";
109             }
110             else
111             {
112                 return "";
113             }
114         }
115         else
116         {
117             return "";
118         }
119     }
120
121
122     /**
123      * {@inheritDoc}
124      */

125     public final Image getColumnImage( Object JavaDoc element, int index )
126     {
127         return null;
128     }
129
130
131     /**
132      * {@inheritDoc}
133      */

134     public Font getFont( Object JavaDoc element )
135     {
136         IAttribute attribute = null;
137         IValue value = null;
138         if ( element instanceof IAttribute )
139         {
140             attribute = ( IAttribute ) element;
141         }
142         else if ( element instanceof IValue )
143         {
144             value = ( IValue ) element;
145             attribute = value.getAttribute();
146         }
147
148         // inconsistent attributes and values
149
if ( value != null )
150         {
151             if ( value.isEmpty() )
152             {
153                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
154                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_ERROR_FONT );
155                 return BrowserCommonActivator.getDefault().getFont( fontData );
156             }
157         }
158         if ( attribute != null && value == null )
159         {
160             if ( !attribute.isConsistent() )
161             {
162                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
163                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_ERROR_FONT );
164                 return BrowserCommonActivator.getDefault().getFont( fontData );
165             }
166         }
167
168         // attribute type
169
if ( attribute != null )
170         {
171             if ( attribute.isObjectClassAttribute() )
172             {
173                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
174                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_OBJECTCLASS_FONT );
175                 return BrowserCommonActivator.getDefault().getFont( fontData );
176             }
177             else if ( attribute.isMustAttribute() )
178             {
179                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
180                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_MUSTATTRIBUTE_FONT );
181                 return BrowserCommonActivator.getDefault().getFont( fontData );
182             }
183             else if ( attribute.isOperationalAttribute() )
184             {
185                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
186                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_OPERATIONALATTRIBUTE_FONT );
187                 return BrowserCommonActivator.getDefault().getFont( fontData );
188             }
189             else
190             {
191                 FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
192                     .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_MAYATTRIBUTE_FONT );
193                 return BrowserCommonActivator.getDefault().getFont( fontData );
194             }
195         }
196         else
197         {
198             return null;
199         }
200     }
201
202
203     /**
204      * {@inheritDoc}
205      */

206     public Color getForeground( Object JavaDoc element )
207     {
208         IAttribute attribute = null;
209         IValue value = null;
210         if ( element instanceof IAttribute )
211         {
212             attribute = ( IAttribute ) element;
213         }
214         else if ( element instanceof IValue )
215         {
216             value = ( IValue ) element;
217             attribute = value.getAttribute();
218         }
219
220         // inconsistent attributes and values
221
if ( value != null )
222         {
223             if ( value.isEmpty() )
224             {
225                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
226                     BrowserCommonConstants.PREFERENCE_ERROR_COLOR );
227                 return BrowserCommonActivator.getDefault().getColor( rgb );
228             }
229         }
230         if ( attribute != null && value == null )
231         {
232             if ( !attribute.isConsistent() )
233             {
234                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
235                     BrowserCommonConstants.PREFERENCE_ERROR_COLOR );
236                 return BrowserCommonActivator.getDefault().getColor( rgb );
237             }
238         }
239
240         // attribute type
241
if ( attribute != null )
242         {
243             if ( attribute.isObjectClassAttribute() )
244             {
245                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
246                     BrowserCommonConstants.PREFERENCE_OBJECTCLASS_COLOR );
247                 return BrowserCommonActivator.getDefault().getColor( rgb );
248             }
249             else if ( attribute.isMustAttribute() )
250             {
251                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
252                     BrowserCommonConstants.PREFERENCE_MUSTATTRIBUTE_COLOR );
253                 return BrowserCommonActivator.getDefault().getColor( rgb );
254             }
255             else if ( attribute.isOperationalAttribute() )
256             {
257                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
258                     BrowserCommonConstants.PREFERENCE_OPERATIONALATTRIBUTE_COLOR );
259                 return BrowserCommonActivator.getDefault().getColor( rgb );
260             }
261             else
262             {
263                 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
264                     BrowserCommonConstants.PREFERENCE_MAYATTRIBUTE_COLOR );
265                 return BrowserCommonActivator.getDefault().getColor( rgb );
266             }
267         }
268         else
269         {
270             return null;
271         }
272     }
273
274
275     /**
276      * {@inheritDoc}
277      */

278     public Color getBackground( Object JavaDoc element )
279     {
280         return null;
281     }
282
283 }
284
Popular Tags