KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > searchresult > SearchResultEditorLabelProvider


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.ui.editors.searchresult;
22
23
24 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
25 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
26 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
27 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
28 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
29 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
30 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
31 import org.apache.directory.ldapstudio.valueeditors.IValueEditor;
32 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
33 import org.eclipse.jface.preference.PreferenceConverter;
34 import org.eclipse.jface.viewers.ITableColorProvider;
35 import org.eclipse.jface.viewers.ITableFontProvider;
36 import org.eclipse.jface.viewers.ITableLabelProvider;
37 import org.eclipse.jface.viewers.LabelProvider;
38 import org.eclipse.jface.viewers.TableViewer;
39 import org.eclipse.swt.graphics.Color;
40 import org.eclipse.swt.graphics.Font;
41 import org.eclipse.swt.graphics.FontData;
42 import org.eclipse.swt.graphics.Image;
43
44
45 public class SearchResultEditorLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider,
46     ITableColorProvider
47 {
48
49     private ValueEditorManager valueEditorManager;
50
51     private ISearch search;
52
53     private boolean showDn;
54
55
56     public SearchResultEditorLabelProvider( TableViewer viewer, ValueEditorManager valueEditorManager )
57     {
58         this.valueEditorManager = valueEditorManager;
59     }
60
61
62     public void inputChanged( ISearch newSearch, boolean showDn )
63     {
64         this.search = newSearch;
65         this.showDn = showDn;
66     }
67
68
69     public final String JavaDoc getColumnText( Object JavaDoc obj, int index )
70     {
71
72         if ( obj != null && obj instanceof ISearchResult )
73         {
74             String JavaDoc property;
75             try
76             {
77                 ISearchResult result = ( ISearchResult ) obj;
78
79                 if ( this.showDn && index == 0 )
80                 {
81                     property = BrowserUIConstants.DN;
82                 }
83                 else if ( this.showDn && index > 0 )
84                 {
85                     property = this.search.getReturningAttributes()[index - 1];
86                 }
87                 else
88                 {
89                     property = this.search.getReturningAttributes()[index];
90                 }
91
92                 if ( property == BrowserUIConstants.DN )
93                 {
94                     return result.getDn().toString();
95                 }
96                 else
97                 {
98                     AttributeHierarchy ah = result.getAttributeWithSubtypes( property );
99                     return getDisplayValue( ah );
100                 }
101
102             }
103             catch ( ArrayIndexOutOfBoundsException JavaDoc aioobe )
104             {
105                 // occurs on "invisible" columns
106
return "";
107             }
108
109         }
110         else if ( obj != null )
111         {
112             return obj.toString();
113         }
114         else
115         {
116             return "";
117         }
118     }
119
120
121     public final Image getColumnImage( Object JavaDoc obj, int index )
122     {
123         return null;
124     }
125
126
127     private String JavaDoc getDisplayValue( AttributeHierarchy ah )
128     {
129
130         IValueEditor vp = this.valueEditorManager.getCurrentValueEditor( ah );
131         if ( vp == null )
132         {
133             return "";
134         }
135
136         String JavaDoc value = vp.getDisplayValue( ah );
137         if ( value.length() > 50 )
138         {
139             value = value.substring( 0, 47 ) + "...";
140         }
141         return value;
142     }
143
144
145     public Font getFont( Object JavaDoc element, int index )
146     {
147
148         if ( element instanceof ISearchResult )
149         {
150             ISearchResult result = ( ISearchResult ) element;
151             String JavaDoc property = null;
152
153             if ( this.showDn && index == 0 )
154             {
155                 property = BrowserUIConstants.DN;
156             }
157             else if ( this.showDn && index > 0 && index - 1 < result.getSearch().getReturningAttributes().length )
158             {
159                 property = result.getSearch().getReturningAttributes()[index - 1];
160             }
161             else if ( index < result.getSearch().getReturningAttributes().length )
162             {
163                 property = result.getSearch().getReturningAttributes()[index];
164             }
165
166             if ( property != null && property == BrowserUIConstants.DN )
167             {
168                 return null;
169             }
170             else if ( property != null )
171             {
172                 AttributeHierarchy ah = result.getAttributeWithSubtypes( property );
173                 if ( ah != null )
174                 {
175                     for ( int i = 0; i < ah.getAttributes().length; i++ )
176                     {
177                         IAttribute attribute = ah.getAttributes()[i];
178                         if ( attribute.isObjectClassAttribute() )
179                         {
180                             FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
181                                 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_OBJECTCLASS_FONT );
182                             return BrowserCommonActivator.getDefault().getFont( fontData );
183                         }
184                         else if ( attribute.isMustAttribute() )
185                         {
186                             FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
187                                 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_MUSTATTRIBUTE_FONT );
188                             return BrowserCommonActivator.getDefault().getFont( fontData );
189                         }
190                         else if ( attribute.isOperationalAttribute() )
191                         {
192                             FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
193                                 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_OPERATIONALATTRIBUTE_FONT );
194                             return BrowserCommonActivator.getDefault().getFont( fontData );
195                         }
196                         else
197                         {
198                             FontData[] fontData = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
199                                 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_MAYATTRIBUTE_FONT );
200                             return BrowserCommonActivator.getDefault().getFont( fontData );
201                         }
202                     }
203                 }
204             }
205         }
206
207         return null;
208     }
209
210
211     public Color getForeground( Object JavaDoc element, int index )
212     {
213         return null;
214     }
215
216
217     public Color getBackground( Object JavaDoc element, int index )
218     {
219         return null;
220     }
221
222 }
Popular Tags