KickJava   Java API By Example, From Geeks To Geeks.

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


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

58 public class RestrictedByValueEditor extends AbstractDialogStringValueEditor
59 {
60
61     private static final String JavaDoc L_CURLY_TYPE = "{ type "; //$NON-NLS-1$
62
private static final String JavaDoc SEP_VALUESIN = ", valuesIn "; //$NON-NLS-1$
63
private static final String JavaDoc R_CURLY = " }"; //$NON-NLS-1$
64
private static final String JavaDoc EMPTY = ""; //$NON-NLS-1$
65

66
67     /**
68      * {@inheritDoc}
69      *
70      * This implementation opens the RestrictedByDialog.
71      */

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

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

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

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

154         private Schema schema;
155
156         /** The type, used as initial type. */
157         private String JavaDoc type;
158
159         /** The values in, used as initial values in. */
160         private String JavaDoc valuesIn;
161
162
163         /**
164          * Creates a new instance of RestrictedByValueEditorRawValueWrapper.
165          *
166          * @param schema the schema
167          * @param type the type
168          * @param valuesIn the values in
169          */

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

184     private class RestrictedByDialog extends Dialog
185     {
186
187         /** The schema. */
188         private Schema schema;
189
190         /** The initial type. */
191         private String JavaDoc initialType;
192
193         /** The initial values in. */
194         private String JavaDoc initialValuesIn;
195
196         /** The type combo field. */
197         private DecoratedField typeComboField;
198
199         /** The type combo. */
200         private Combo typeCombo;
201
202         /** The type content proposal adapter */
203         private ContentProposalAdapter typeCPA;
204
205         /** The values in combo field. */
206         private DecoratedField valuesInComboField;
207
208         /** The values in combo. */
209         private Combo valuesInCombo;
210
211         /** The values in content proposal adapter */
212         private ContentProposalAdapter valuesInCPA;
213
214         /** The return type. */
215         private String JavaDoc returnType;
216
217         /** The return values in. */
218         private String JavaDoc returnValuesIn;
219
220
221         /**
222          * Creates a new instance of RestrictedByDialog.
223          *
224          * @param parentShell the parent shell
225          * @param schema the schema
226          * @param initialType the initial type
227          * @param initialValuesIn the initial values in
228          */

229         public RestrictedByDialog( Shell parentShell, Schema schema, String JavaDoc initialType, String JavaDoc initialValuesIn )
230         {
231             super( parentShell );
232             super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
233             this.initialType = initialType;
234             this.initialValuesIn = initialValuesIn;
235             this.schema = schema;
236             this.returnType = null;
237             this.returnValuesIn = null;
238         }
239
240
241         /**
242          * {@inheritDoc}
243          */

244         protected void configureShell( Shell shell )
245         {
246             super.configureShell( shell );
247             shell.setText( Messages.getString( "RestrictedByValueEditor.title" ) ); //$NON-NLS-1$
248
shell.setImage( Activator.getDefault().getImage( Messages.getString( "RestrictedByValueEditor.icon" ) ) ); //$NON-NLS-1$
249
}
250
251
252         /**
253          * {@inheritDoc}
254          */

255         protected void createButtonsForButtonBar( Composite parent )
256         {
257             super.createButtonsForButtonBar( parent );
258         }
259
260
261         /**
262          * {@inheritDoc}
263          */

264         protected void okPressed()
265         {
266             returnType = typeCombo.getText();
267             returnValuesIn = valuesInCombo.getText();
268             super.okPressed();
269         }
270
271
272         /**
273          * {@inheritDoc}
274          */

275         protected Control createDialogArea( Composite parent )
276         {
277             // create composite
278
Composite composite = ( Composite ) super.createDialogArea( parent );
279             GridData gd = new GridData( GridData.FILL_BOTH );
280             gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
281             composite.setLayoutData( gd );
282             composite.setLayout( new GridLayout( 5, false ) );
283
284             BaseWidgetUtils.createLabel( composite, L_CURLY_TYPE, 1 );
285
286             // combo widget
287
String JavaDoc[] allAtNames = schema.getAttributeTypeDescriptionNames();
288             Arrays.sort( allAtNames );
289
290             final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
291                 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL );
292
293             typeComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator()
294             {
295                 public Control createControl( Composite parent, int style )
296                 {
297                     Combo combo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
298                     combo.setVisibleItemCount( 20 );
299                     return combo;
300                 }
301             } );
302             typeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true );
303             typeComboField.getLayoutControl().setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
304             typeCombo = ( Combo ) typeComboField.getControl();
305             typeCombo.setItems( allAtNames );
306             typeCombo.setText( initialType );
307
308             // content proposal adapter
309
typeCPA = new ContentProposalAdapter( typeCombo, new ComboContentAdapter(),
310                 new ListContentProposalProvider( typeCombo.getItems() ), null, null );
311             typeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE );
312             typeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE );
313
314             BaseWidgetUtils.createLabel( composite, SEP_VALUESIN, 1 );
315
316             valuesInComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator()
317             {
318                 public Control createControl( Composite parent, int style )
319                 {
320                     Combo combo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
321                     combo.setVisibleItemCount( 20 );
322                     return combo;
323                 }
324             } );
325             valuesInComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true );
326             valuesInComboField.getLayoutControl().setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
327             valuesInCombo = ( Combo ) valuesInComboField.getControl();
328             valuesInCombo.setItems( allAtNames );
329             valuesInCombo.setText( initialValuesIn );
330
331             // content proposal adapter
332
valuesInCPA = new ContentProposalAdapter( valuesInCombo, new ComboContentAdapter(),
333                 new ListContentProposalProvider( valuesInCombo.getItems() ), null, null );
334             valuesInCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE );
335             valuesInCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE );
336
337             BaseWidgetUtils.createLabel( composite, R_CURLY, 1 );
338
339             applyDialogFont( composite );
340             return composite;
341         }
342
343
344         /**
345          * Gets the type.
346          *
347          * @return the type, null if canceled
348          */

349         public String JavaDoc getType()
350         {
351             return returnType;
352         }
353
354
355         /**
356          * Gets the values in.
357          *
358          * @return the values in, null if canceled
359          */

360         public String JavaDoc getValuesIn()
361         {
362             return returnValuesIn;
363         }
364
365     }
366
367 }
368
Popular Tags