KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > valueeditors > MaxValueCountValueEditor


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.aciitemeditor.valueeditors;
22
23
24 import java.util.Arrays JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import org.apache.directory.ldapstudio.aciitemeditor.Activator;
29 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog;
30 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
31 import org.apache.directory.ldapstudio.browser.common.widgets.ListContentProposalProvider;
32 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
33 import org.apache.directory.ldapstudio.browser.core.model.IValue;
34 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
35 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
36 import org.eclipse.jface.dialogs.Dialog;
37 import org.eclipse.jface.dialogs.IDialogConstants;
38 import org.eclipse.jface.fieldassist.ComboContentAdapter;
39 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
40 import org.eclipse.jface.fieldassist.DecoratedField;
41 import org.eclipse.jface.fieldassist.FieldDecoration;
42 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
43 import org.eclipse.jface.fieldassist.IControlCreator;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Combo;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.swt.widgets.Control;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Spinner;
52
53
54 /**
55  * ACI item editor specific value editor to edit the MaxValueCount protected item.
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class MaxValueCountValueEditor extends AbstractDialogStringValueEditor
61 {
62
63     private static final String JavaDoc L_CURLY_TYPE = "{ type "; //$NON-NLS-1$
64
private static final String JavaDoc SEP_MAXCOUNT = ", maxCount "; //$NON-NLS-1$
65
private static final String JavaDoc R_CURLY = " }"; //$NON-NLS-1$
66
private static final String JavaDoc EMPTY = ""; //$NON-NLS-1$
67

68
69     /**
70      * {@inheritDoc}
71      *
72      * This implementation opens the MaxValueCountDialog.
73      */

74     public boolean openDialog( Shell shell )
75     {
76         Object JavaDoc value = getValue();
77         if ( value != null && value instanceof MaxValueCountValueEditorRawValueWrapper )
78         {
79             MaxValueCountValueEditorRawValueWrapper wrapper = ( MaxValueCountValueEditorRawValueWrapper ) value;
80             MaxValueCountDialog dialog = new MaxValueCountDialog( shell, wrapper.schema, wrapper.type, wrapper.maxCount );
81             if ( dialog.open() == TextDialog.OK && !EMPTY.equals( dialog.getType() ) && dialog.getMaxCount() > -1 )
82             {
83                 setValue( L_CURLY_TYPE + dialog.getType() + SEP_MAXCOUNT + dialog.getMaxCount() + R_CURLY );
84                 return true;
85             }
86         }
87         return false;
88     }
89
90
91     /**
92      * {@inheritDoc}
93      *
94      * Returns an AttributeTypeAndValueValueEditorRawValueWrapper.
95      */

96     public Object JavaDoc getRawValue( IValue value )
97     {
98         return value != null ? getRawValue( value.getAttribute().getEntry().getConnection(), value.getStringValue() )
99             : null;
100     }
101
102
103     /**
104      * {@inheritDoc}
105      *
106      * Returns an MaxValueCountValueEditorRawValueWrapper.
107      */

108     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
109     {
110         Schema schema = null;
111         if ( connection != null )
112         {
113             schema = connection.getSchema();
114         }
115         if ( schema == null || value == null || !( value instanceof String JavaDoc ) )
116         {
117             return null;
118         }
119
120         String JavaDoc stringValue = ( String JavaDoc ) value;
121         String JavaDoc type = EMPTY;
122         int maxCount = 0;
123         try
124         {
125             // for example: { type userPassword, maxCount 10 }
126
Pattern JavaDoc pattern = Pattern.compile( "\\s*\\{\\s*type\\s*([^,]*),\\s*maxCount\\s*(\\d*)\\s*\\}\\s*" ); //$NON-NLS-1$
127
Matcher JavaDoc matcher = pattern.matcher( stringValue );
128             type = matcher.matches() ? matcher.group( 1 ) : EMPTY;
129             maxCount = matcher.matches() ? Integer.valueOf( matcher.group( 2 ) ) : 0;
130         }
131         catch ( Exception JavaDoc e )
132         {
133         }
134
135         MaxValueCountValueEditorRawValueWrapper wrapper = new MaxValueCountValueEditorRawValueWrapper( schema, type,
136             maxCount );
137         return wrapper;
138     }
139
140     /**
141      * The MaxValueCountValueEditorRawValueWrapper is used to pass contextual
142      * information to the opened MaxValueCountDialog.
143      *
144      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
145      * @version $Rev$, $Date$
146      */

147     private class MaxValueCountValueEditorRawValueWrapper
148     {
149         /**
150          * The schema, used in MaxValueCountDialog to build the list
151          * with possible attribute types.
152          */

153         private Schema schema;
154
155         /** The attribute type, used as initial attribute type. */
156         private String JavaDoc type;
157
158         /** The max count, used as initial value. */
159         private int maxCount;
160
161
162         /**
163          * Creates a new instance of AttributeTypeAndValueValueEditorRawValueWrapper.
164          *
165          * @param schema the schema
166          * @param attributeType the attribute type
167          * @param value the value
168          */

169         private MaxValueCountValueEditorRawValueWrapper( Schema schema, String JavaDoc type, int maxCount )
170         {
171             this.schema = schema;
172             this.type = type;
173             this.maxCount = maxCount;
174         }
175     }
176
177     /**
178      * This class provides a dialog to enter the MaxValueCount values.
179      *
180      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
181      * @version $Rev$, $Date$
182      */

183     private class MaxValueCountDialog extends Dialog
184     {
185
186         /** The schema. */
187         private Schema schema;
188
189         /** The initial attribute type. */
190         private String JavaDoc initialType;
191
192         /** The initial max count. */
193         private int initialMaxCount;
194
195         /** The attribute type combo field. */
196         private DecoratedField attributeTypeComboField;
197
198         /** The attribute type combo. */
199         private Combo attributeTypeCombo;
200
201         /** The attribute type content proposal adapter */
202         private ContentProposalAdapter attributeTypeCPA;
203
204         /** The max count spinner. */
205         private Spinner maxCountSpinner;
206
207         /** The return attribute type. */
208         private String JavaDoc returnType;
209
210         /** The return value. */
211         private int returnMaxCount;
212
213
214         /**
215          * Creates a new instance of AttributeTypeDialog.
216          *
217          * @param parentShell the parent shell
218          * @param schema the schema
219          * @param initialType the initial attribute type
220          * @param initialMaxCount the initial max count
221          */

222         public MaxValueCountDialog( Shell parentShell, Schema schema, String JavaDoc initialType, int initialMaxCount )
223         {
224             super( parentShell );
225             super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
226             this.initialType = initialType;
227             this.initialMaxCount = initialMaxCount;
228             this.schema = schema;
229             this.returnType = null;
230             this.returnMaxCount = -1;
231         }
232
233
234         /**
235          * {@inheritDoc}
236          */

237         protected void configureShell( Shell shell )
238         {
239             super.configureShell( shell );
240             shell.setText( Messages.getString( "MaxValueCountValueEditor.title" ) ); //$NON-NLS-1$
241
shell.setImage( Activator.getDefault().getImage( Messages.getString( "MaxValueCountValueEditor.icon" ) ) ); //$NON-NLS-1$
242
}
243
244
245         /**
246          * {@inheritDoc}
247          */

248         protected void createButtonsForButtonBar( Composite parent )
249         {
250             super.createButtonsForButtonBar( parent );
251         }
252
253
254         /**
255          * {@inheritDoc}
256          */

257         protected void okPressed()
258         {
259             returnType = attributeTypeCombo.getText();
260             returnMaxCount = maxCountSpinner.getSelection();
261             super.okPressed();
262         }
263
264
265         /**
266          * {@inheritDoc}
267          */

268         protected Control createDialogArea( Composite parent )
269         {
270             // create composite
271
Composite composite = ( Composite ) super.createDialogArea( parent );
272             GridData gd = new GridData( GridData.FILL_BOTH );
273             gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
274             composite.setLayoutData( gd );
275             composite.setLayout( new GridLayout( 5, false ) );
276
277             BaseWidgetUtils.createLabel( composite, L_CURLY_TYPE, 1 );
278
279             // combo widget
280
String JavaDoc[] allAtNames = schema.getAttributeTypeDescriptionNames();
281             Arrays.sort( allAtNames );
282
283             final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
284                 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL );
285             attributeTypeComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator()
286             {
287                 public Control createControl( Composite parent, int style )
288                 {
289                     Combo combo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
290                     combo.setVisibleItemCount( 20 );
291                     return combo;
292                 }
293             } );
294             attributeTypeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true );
295             attributeTypeComboField.getLayoutControl()
296                 .setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
297             attributeTypeCombo = ( Combo ) attributeTypeComboField.getControl();
298             attributeTypeCombo.setItems( allAtNames );
299             attributeTypeCombo.setText( initialType );
300
301             // content proposal adapter
302
attributeTypeCPA = new ContentProposalAdapter( attributeTypeCombo, new ComboContentAdapter(),
303                 new ListContentProposalProvider( attributeTypeCombo.getItems() ), null, null );
304             attributeTypeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE );
305             attributeTypeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE );
306
307             BaseWidgetUtils.createLabel( composite, SEP_MAXCOUNT, 1 );
308
309             maxCountSpinner = new Spinner( composite, SWT.BORDER );
310             maxCountSpinner.setMinimum( 0 );
311             maxCountSpinner.setMaximum( Integer.MAX_VALUE );
312             maxCountSpinner.setDigits( 0 );
313             maxCountSpinner.setIncrement( 1 );
314             maxCountSpinner.setPageIncrement( 100 );
315             maxCountSpinner.setSelection( initialMaxCount );
316             maxCountSpinner.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
317
318             BaseWidgetUtils.createLabel( composite, R_CURLY, 1 );
319
320             applyDialogFont( composite );
321             return composite;
322         }
323
324
325         /**
326          * Gets the attribute type.
327          *
328          * @return the attribute type, null if canceled
329          */

330         public String JavaDoc getType()
331         {
332             return returnType;
333         }
334
335
336         /**
337          * Gets the max count.
338          *
339          * @return the max count, -1 if canceled
340          */

341         public int getMaxCount()
342         {
343             return returnMaxCount;
344         }
345
346     }
347
348 }
349
Popular Tags