KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
29 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeAttributesJob;
30 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
31 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.IValue;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.Viewer;
36
37
38 /**
39  * The EntryEditorWidgetContentProvider implements the content provider for
40  * the entry editor widget. It accepts an {@link IEntry} or an
41  * {@link AttributeHierarchy} as input.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class EntryEditorWidgetContentProvider implements ITreeContentProvider
47 {
48
49     /** The preferences. */
50     protected EntryEditorWidgetPreferences preferences;
51
52     /** The main widget. */
53     protected EntryEditorWidget mainWidget;
54
55
56     /**
57      * Creates a new instance of EntryEditorWidgetContentProvider.
58      *
59      * @param preferences the preferences
60      * @param mainWidget the main widget
61      */

62     public EntryEditorWidgetContentProvider( EntryEditorWidgetPreferences preferences, EntryEditorWidget mainWidget )
63     {
64         this.preferences = preferences;
65         this.mainWidget = mainWidget;
66     }
67
68
69     /**
70      * {@inheritDoc}
71      *
72      * This implementations updates the enabled state and the info text.
73      */

74     public void inputChanged( Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput )
75     {
76         if ( mainWidget != null )
77         {
78             String JavaDoc dn = "";
79             boolean enabled = true;
80
81             if ( newInput != null && newInput instanceof IEntry )
82             {
83                 IEntry entry = ( IEntry ) newInput;
84                 dn = "DN: " + entry.getDn().toString();
85             }
86             else if ( newInput != null && newInput instanceof AttributeHierarchy )
87             {
88                 AttributeHierarchy ah = ( AttributeHierarchy ) newInput;
89                 dn = "DN: " + ah.getAttribute().getEntry().getDn().toString();
90             }
91             else
92             {
93                 dn = "No entry selected";
94                 enabled = false;
95             }
96
97             if ( mainWidget.getInfoText() != null && !mainWidget.getInfoText().isDisposed() )
98             {
99                 mainWidget.getInfoText().setText( dn );
100             }
101             if ( mainWidget.getQuickFilterWidget() != null )
102             {
103                 mainWidget.getQuickFilterWidget().setEnabled( enabled );
104             }
105             if ( mainWidget.getViewer() != null && !mainWidget.getViewer().getTree().isDisposed() )
106             {
107                 mainWidget.getViewer().getTree().setEnabled( enabled );
108             }
109         }
110     }
111
112
113     /**
114      * {@inheritDoc}
115      */

116     public void dispose()
117     {
118         preferences = null;
119         mainWidget = null;
120     }
121
122
123     /**
124      * {@inheritDoc}
125      */

126     public Object JavaDoc[] getElements( Object JavaDoc inputElement )
127     {
128
129         if ( inputElement != null && inputElement instanceof IEntry )
130         {
131             IEntry entry = ( IEntry ) inputElement;
132
133             if ( !entry.isAttributesInitialized() && entry.isDirectoryEntry() )
134             {
135                 boolean soa = BrowserCommonActivator.getDefault().getPreferenceStore().getBoolean(
136                     BrowserCommonConstants.PREFERENCE_ENTRYEDITOR_SHOW_OPERATIONAL_ATTRIBUTES );
137                 InitializeAttributesJob job = new InitializeAttributesJob( new IEntry[]
138                     { entry }, soa );
139                 job.execute();
140                 return new Object JavaDoc[0];
141             }
142             else
143             {
144                 IAttribute[] attributes = entry.getAttributes();
145                 Object JavaDoc[] values = getValues( attributes );
146                 return values;
147             }
148         }
149         else if ( inputElement != null && inputElement instanceof AttributeHierarchy )
150         {
151             AttributeHierarchy ah = ( AttributeHierarchy ) inputElement;
152             IAttribute[] attributes = ah.getAttributes();
153             Object JavaDoc[] values = getValues( attributes );
154             return values;
155         }
156         else
157         {
158             return new Object JavaDoc[0];
159         }
160     }
161
162
163     /**
164      * Gets the values of the given attributes.
165      *
166      * @param attributes the attributes
167      *
168      * @return the values
169      */

170     private Object JavaDoc[] getValues( IAttribute[] attributes )
171     {
172         List JavaDoc<Object JavaDoc> valueList = new ArrayList JavaDoc<Object JavaDoc>();
173         for ( int i = 0; attributes != null && i < attributes.length; i++ )
174         {
175             IValue[] values = attributes[i].getValues();
176             if ( this.preferences == null || !this.preferences.isUseFolding()
177                 || ( values.length <= this.preferences.getFoldingThreshold() ) )
178             {
179                 for ( int j = 0; j < values.length; j++ )
180                 {
181                     valueList.add( values[j] );
182                 }
183             }
184             else
185             {
186                 // if folding threshold is exceeded then return the attribute itself
187
valueList.add( attributes[i] );
188             }
189         }
190         return valueList.toArray();
191     }
192
193
194     /**
195      * {@inheritDoc}
196      */

197     public Object JavaDoc[] getChildren( Object JavaDoc parentElement )
198     {
199         if ( parentElement instanceof IAttribute )
200         {
201             IAttribute attribute = ( IAttribute ) parentElement;
202             IValue[] values = attribute.getValues();
203             return values;
204         }
205         return null;
206     }
207
208
209     /**
210      * {@inheritDoc}
211      */

212     public Object JavaDoc getParent( Object JavaDoc element )
213     {
214         if ( element instanceof IValue )
215         {
216             return ( ( IValue ) element ).getAttribute();
217         }
218         return null;
219     }
220
221
222     /**
223      * {@inheritDoc}
224      */

225     public boolean hasChildren( Object JavaDoc element )
226     {
227         return ( element instanceof IAttribute );
228     }
229
230 }
Popular Tags