KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > valueeditors > MultivaluedValueEditor


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.valueeditors;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.common.dialogs.MultivaluedDialog;
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.IConnection;
33 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
34 import org.apache.directory.ldapstudio.browser.core.model.IValue;
35 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
36 import org.eclipse.jface.resource.ImageDescriptor;
37 import org.eclipse.jface.viewers.CellEditor;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40
41
42 /**
43  * Special ValueEditor to handle attributes with multiple values in a dialog.
44  *
45  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
46  * @version $Rev$, $Date$
47  */

48 public class MultivaluedValueEditor extends CellEditor implements IValueEditor
49 {
50
51     /** The value to handle */
52     private Object JavaDoc value;
53
54     /** The parent composite, used to instanciate a new control */
55     private Composite parent;
56
57     /** The name of this value editor */
58     private String JavaDoc name;
59
60     /** The image of this value editor */
61     private ImageDescriptor imageDescriptor;
62
63     /** The value editor manager, used to get proper value editors */
64     protected ValueEditorManager valueEditorManager;
65
66
67     /**
68      * Creates a new instance of MultivaluedValueEditor.
69      *
70      * @param parent the parent composite
71      * @param valueEditorManager the value editor manager, used to get
72      * proper value editors
73      */

74     public MultivaluedValueEditor( Composite parent, ValueEditorManager valueEditorManager )
75     {
76         super( parent );
77         this.parent = parent;
78         this.valueEditorManager = valueEditorManager;
79     }
80
81
82     /**
83      * {@inheritDoc}
84      *
85      * This is a dialog editor, it doesn't create a control.
86      */

87     protected Control createControl( Composite parent )
88     {
89         return null;
90     }
91
92
93     /**
94      * {@inheritDoc}
95      *
96      * Returns the value object stored in a member.
97      */

98     protected final Object JavaDoc doGetValue()
99     {
100         return this.value;
101     }
102
103
104     /**
105      * {@inheritDoc}
106      *
107      * This is a dialog editor, doesn't set focus.
108      */

109     protected void doSetFocus()
110     {
111     }
112
113
114     /**
115      * {@inheritDoc}
116      *
117      * Stores the value object in a member.
118      */

119     protected void doSetValue( Object JavaDoc value )
120     {
121         this.value = value;
122     }
123
124
125     /**
126      * {@inheritDoc}
127      *
128      * Opens the MulitvaluedDialog. Expects that an AttributeHierarchy
129      * object is in value member.
130      */

131     public void activate()
132     {
133         if ( this.getValue() != null && this.getValue() instanceof AttributeHierarchy )
134         {
135             AttributeHierarchy ah = ( AttributeHierarchy ) this.getValue();
136             if ( ah != null )
137             {
138                 MultivaluedDialog dialog = new MultivaluedDialog( this.parent.getShell(), ah );
139                 dialog.open();
140             }
141         }
142
143         fireCancelEditor();
144     }
145
146
147     /**
148      * {@inheritDoc}
149      *
150      * Returns this.
151      */

152     public CellEditor getCellEditor()
153     {
154         return this;
155     }
156
157
158     /**
159      * {@inheritDoc}
160      *
161      * This implementation of getDisplayValue() returns a
162      * comma-separated list of all values.
163      */

164     public String JavaDoc getDisplayValue( AttributeHierarchy attributeHierarchy )
165     {
166
167         List JavaDoc<IValue> valueList = new ArrayList JavaDoc<IValue>();
168         for ( Iterator JavaDoc it = attributeHierarchy.iterator(); it.hasNext(); )
169         {
170             IAttribute attribute = ( IAttribute ) it.next();
171             valueList.addAll( Arrays.asList( attribute.getValues() ) );
172         }
173
174         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
175         if ( valueList.size() > 1 )
176             sb.append( valueList.size() + " values: " );
177         for ( Iterator JavaDoc it = valueList.iterator(); it.hasNext(); )
178         {
179             IValue value = ( IValue ) it.next();
180             IValueEditor vp = this.valueEditorManager.getCurrentValueEditor( value );
181             sb.append( vp.getDisplayValue( value ) );
182             if ( it.hasNext() )
183                 sb.append( ", " );
184         }
185         return sb.toString();
186     }
187
188
189     /**
190      * {@inheritDoc}
191      *
192      * It doesn't make sense to use the MultivaluedValueEditor with a single value.
193      * Returns an empty string.
194      */

195     public String JavaDoc getDisplayValue( IValue value )
196     {
197         return "";
198     }
199
200
201     /**
202      * {@inheritDoc}
203      *
204      * Returns the attributeHierarchy.
205      */

206     public Object JavaDoc getRawValue( AttributeHierarchy attributeHierarchy )
207     {
208         return attributeHierarchy;
209     }
210
211
212     /**
213      * {@inheritDoc}
214      *
215      * It doesn't make sense to use the MultivaluedValueEditor with a single value.
216      * Returns null.
217      */

218     public Object JavaDoc getRawValue( IValue value )
219     {
220         return null;
221     }
222
223
224     /**
225      * {@inheritDoc}
226      *
227      * It doesn't make sense to use the MultivaluedValueEditor with a single value.
228      * Returns null.
229      */

230     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
231     {
232         return null;
233     }
234
235
236     /**
237      * {@inheritDoc}
238      *
239      * Modification is performed in the concrete single-ValueEditors.
240      */

241     public void modifyValue( IValue oldValue, Object JavaDoc newRawValue ) throws ModelModificationException
242     {
243     }
244
245
246     /**
247      * {@inheritDoc}
248      *
249      * Creationg is performed in the concrete single-ValueEditors.
250      */

251     public void createValue( IEntry entry, String JavaDoc attributeName, Object JavaDoc newRawValue ) throws ModelModificationException
252     {
253     }
254
255
256     /**
257      * {@inheritDoc}
258      *
259      * Deletion is performed in the concrete single-ValueEditors.
260      */

261     public void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException
262     {
263     }
264
265
266     /**
267      * {@inheritDoc}
268      *
269      * Deletion is performed in the concrete single-ValueEditors.
270      */

271     public void deleteValue( IValue oldValue ) throws ModelModificationException
272     {
273     }
274
275
276     /**
277      * {@inheritDoc}
278      *
279      * Modification is performed in the concrete single-ValueEditors. No need
280      * to return a value.
281      */

282     public Object JavaDoc getStringOrBinaryValue( Object JavaDoc rawValue )
283     {
284         return null;
285     }
286
287
288     /**
289      * {@inheritDoc}
290      */

291     public void setValueEditorName( String JavaDoc name )
292     {
293         this.name = name;
294     }
295
296
297     /**
298      * {@inheritDoc}
299      */

300     public String JavaDoc getValueEditorName()
301     {
302         return name;
303     }
304
305
306     /**
307      * {@inheritDoc}
308      */

309     public void setValueEditorImageDescriptor( ImageDescriptor imageDescriptor )
310     {
311         this.imageDescriptor = imageDescriptor;
312     }
313
314
315     /**
316      * {@inheritDoc}
317      */

318     public ImageDescriptor getValueEditorImageDescriptor()
319     {
320         return imageDescriptor;
321     }
322
323 }
324
Popular Tags