KickJava   Java API By Example, From Geeks To Geeks.

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


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.BrowserCommonActivator;
30 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
31 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
33 import org.apache.directory.ldapstudio.browser.core.model.IValue;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.jface.viewers.CellEditor;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Shell;
39
40
41 /**
42  * Abstract base class for value editors that handle values
43  * in a dialog.
44  *
45  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
46  * @version $Rev$, $Date$
47  */

48 public abstract class AbstractDialogValueEditor extends CellEditor implements IValueEditor
49 {
50
51     /** The value to handle */
52     private Object JavaDoc value;
53
54     /** The shell, used to open the editor */
55     private Shell shell;
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
64     /**
65      *
66      * Creates a new instance of AbstractDialogEditor.
67      */

68     protected AbstractDialogValueEditor()
69     {
70     }
71
72
73     /**
74      * Returns true if the user wishes to show raw values rather than
75      * user-friendly values. If true the getDisplayValue() methods
76      * shouldnot modify the value.
77      *
78      * @return true if raw values should be displayed
79      */

80     protected boolean showRawValues()
81     {
82         return BrowserCommonActivator.getDefault().getPreferenceStore()
83             .getBoolean( BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES );
84     }
85
86
87     /**
88      * {@inheritDoc}
89      *
90      * This implementation simple returns itself.
91      */

92     public CellEditor getCellEditor()
93     {
94         return this;
95     }
96
97
98     /**
99      * {@inheritDoc}
100      *
101      * This is a dialog editor, it doesn't create a control.
102      * It just extracts and saves the shell reference from parent.
103      */

104     protected final Control createControl( Composite parent )
105     {
106         this.shell = parent.getShell();
107         return null;
108     }
109
110
111     /**
112      * {@inheritDoc}
113      *
114      * This is a dialog editor, doesn't set focus.
115      */

116     protected final void doSetFocus()
117     {
118     }
119
120
121     /**
122      * {@inheritDoc}
123      *
124      * Returns the value object stored in a member.
125      */

126     protected final Object JavaDoc doGetValue()
127     {
128         return this.value;
129     }
130
131
132     /**
133      * {@inheritDoc}
134      *
135      * Stores the value object in a member.
136      */

137     protected final void doSetValue( Object JavaDoc value )
138     {
139         if ( value != null && value instanceof IValue.EmptyValue )
140         {
141             IValue.EmptyValue emptyValue = ( IValue.EmptyValue ) value;
142             if ( emptyValue.isBinary() )
143                 value = emptyValue.getBinaryValue();
144             else
145                 value = emptyValue.getStringValue();
146         }
147         this.value = value;
148     }
149
150
151     /**
152      * {@inheritDoc}
153      *
154      * The activate method is called from the JFace framework
155      * to start editing.
156      */

157     public final void activate()
158     {
159         boolean save = this.openDialog( shell );
160         //doSetValue( newValue );
161
if ( !save || this.value == null )
162         {
163             this.value = null;
164             fireCancelEditor();
165         }
166         else
167         {
168             fireApplyEditorValue();
169             deactivate();
170         }
171     }
172
173
174     /**
175      * Opens the edit dialog.
176      * Call getValue() to get the current value to edit.
177      * Call setValue() to set the new value after editing.
178      *
179      * @param shell The shell to use to open the dialog
180      * @return true if the new value should be stored, false
181      * to cancel the editor.
182      */

183     protected abstract boolean openDialog( Shell shell );
184
185
186     /**
187      * Returns a raw value that represents an empty value.
188      *
189      * @param attribute the attribute
190      * @return a raw value that represents an empty value
191      */

192     protected abstract Object JavaDoc getEmptyRawValue( IAttribute attribute );
193
194
195     /**
196      * {@inheritDoc}
197      *
198      * This implementation of getDisplayValue() returns a
199      * comma-separated list of all values.
200      */

201     public String JavaDoc getDisplayValue( AttributeHierarchy attributeHierarchy )
202     {
203         if ( attributeHierarchy == null )
204         {
205             return "NULL";
206         }
207
208         List JavaDoc<IValue> valueList = new ArrayList JavaDoc<IValue>();
209         for ( Iterator JavaDoc it = attributeHierarchy.iterator(); it.hasNext(); )
210         {
211             IAttribute attribute = ( IAttribute ) it.next();
212             valueList.addAll( Arrays.asList( attribute.getValues() ) );
213         }
214
215         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
216         for ( Iterator JavaDoc<IValue> it = valueList.iterator(); it.hasNext(); )
217         {
218             IValue value = it.next();
219             sb.append( getDisplayValue( value ) );
220             if ( it.hasNext() )
221                 sb.append( ", " );
222         }
223         return sb.toString();
224     }
225
226
227     /**
228      * {@inheritDoc}
229      *
230      * This implementation calls getEmptyRawValue(IAttribute) if there are no values
231      * in attributeHierarchy and getRawValue(IValue) if attributeHierarchy
232      * contains exactly one value. Otherwise null is returned.
233      */

234     public Object JavaDoc getRawValue( AttributeHierarchy attributeHierarchy )
235     {
236         if ( attributeHierarchy == null )
237         {
238             return null;
239         }
240         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 )
241         {
242             return getEmptyRawValue( attributeHierarchy.getAttribute() );
243         }
244         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 )
245         {
246             return getRawValue( attributeHierarchy.getAttribute().getValues()[0] );
247         }
248         else
249         {
250             return null;
251         }
252     }
253
254
255     /**
256      * {@inheritDoc}
257      */

258     public void setValueEditorName( String JavaDoc name )
259     {
260         this.name = name;
261     }
262
263
264     /**
265      * {@inheritDoc}
266      */

267     public String JavaDoc getValueEditorName()
268     {
269         return name;
270     }
271
272
273     /**
274      * {@inheritDoc}
275      */

276     public void setValueEditorImageDescriptor( ImageDescriptor imageDescriptor )
277     {
278         this.imageDescriptor = imageDescriptor;
279     }
280
281
282     /**
283      * {@inheritDoc}
284      */

285     public ImageDescriptor getValueEditorImageDescriptor()
286     {
287         return imageDescriptor;
288     }
289
290 }
291
Popular Tags