KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > wizards > AttributeTypeWizardPage


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.wizards;
22
23
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
29 import org.apache.directory.ldapstudio.browser.common.widgets.ListContentProposalProvider;
30 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
31 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
32 import org.eclipse.jface.fieldassist.ComboContentAdapter;
33 import org.eclipse.jface.fieldassist.ContentProposalAdapter;
34 import org.eclipse.jface.fieldassist.DecoratedField;
35 import org.eclipse.jface.fieldassist.FieldDecoration;
36 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
37 import org.eclipse.jface.fieldassist.IControlCreator;
38 import org.eclipse.jface.wizard.WizardPage;
39 import org.eclipse.swt.SWT;
40 import org.eclipse.swt.events.ModifyEvent;
41 import org.eclipse.swt.events.ModifyListener;
42 import org.eclipse.swt.events.SelectionAdapter;
43 import org.eclipse.swt.events.SelectionEvent;
44 import org.eclipse.swt.layout.GridData;
45 import org.eclipse.swt.layout.GridLayout;
46 import org.eclipse.swt.widgets.Button;
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.Label;
51 import org.eclipse.swt.widgets.Text;
52
53
54 /**
55  * The AttributeTypeWizardPage provides a combo to select the attribute type,
56  * some filter and a preview field.
57  *
58  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
59  * @version $Rev$, $Date$
60  */

61 public class AttributeTypeWizardPage extends WizardPage
62 {
63
64     /** The parent wizard. */
65     private AttributeWizard wizard;
66
67     /** The initial show subschema attributes only. */
68     private boolean initialShowSubschemaAttributesOnly;
69
70     /** The initial hide existing attributes. */
71     private boolean initialHideExistingAttributes;
72
73     /** The parsed attribute type. */
74     private String JavaDoc parsedAttributeType;
75
76     /** The possible attribute types. */
77     private String JavaDoc[] possibleAttributeTypes;
78
79     /** The possible attribute types applicable to the entry's schema only. */
80     private String JavaDoc[] possibleAttributeTypesSubschemaOnly;
81
82     /** The possible attribute types applicable to the entry's schema only, existing attributes are hidden. */
83     private String JavaDoc[] possibleAttributeTypesSubschemaOnlyAndExistingHidden;
84     
85     /** The attribute type combo field. */
86     private DecoratedField attributeTypeComboField;
87
88     /** The attribute type combo. */
89     private Combo attributeTypeCombo;
90
91     /** The attribute type content proposal adapter */
92     private ContentProposalAdapter attributeTypeCPA;
93
94     /** The show subschem attributes only button. */
95     private Button showSubschemAttributesOnlyButton;
96
97     /** The hide existing attributes button. */
98     private Button hideExistingAttributesButton;
99
100     /** The preview text. */
101     private Text previewText;
102
103
104     /**
105      * Creates a new instance of AttributeTypeWizardPage.
106      *
107      * @param pageName the page name
108      * @param initialEntry the initial entry
109      * @param initialAttributeDescription the initial attribute description
110      * @param initialShowSubschemaAttributesOnly the initial show subschema attributes only
111      * @param initialHideExistingAttributes the initial hide existing attributes
112      * @param wizard the wizard
113      */

114     public AttributeTypeWizardPage( String JavaDoc pageName, IEntry initialEntry, String JavaDoc initialAttributeDescription,
115         boolean initialShowSubschemaAttributesOnly, boolean initialHideExistingAttributes, AttributeWizard wizard )
116     {
117         super( pageName );
118         super.setTitle( "Attribute Type" );
119         super.setDescription( "Please enter or select the attribute type." );
120         // super.setImageDescriptor(BrowserUIPlugin.getDefault().getImageDescriptor(BrowserUIConstants.IMG_ATTRIBUTE_WIZARD));
121
super.setPageComplete( false );
122
123         this.wizard = wizard;
124         this.initialShowSubschemaAttributesOnly = initialShowSubschemaAttributesOnly;
125         this.initialHideExistingAttributes = initialHideExistingAttributes;
126
127         possibleAttributeTypes = initialEntry.getConnection().getSchema().getAttributeTypeDescriptionNames();
128         Arrays.sort( possibleAttributeTypes );
129         possibleAttributeTypesSubschemaOnly = initialEntry.getSubschema().getAllAttributeNames();
130         Arrays.sort( possibleAttributeTypesSubschemaOnly );
131
132         Set JavaDoc<String JavaDoc> set = new HashSet JavaDoc<String JavaDoc>( Arrays.asList( initialEntry.getSubschema().getAllAttributeNames() ) );
133         IAttribute[] existingAttributes = initialEntry.getAttributes();
134         for ( int i = 0; existingAttributes != null && i < existingAttributes.length; i++ )
135         {
136             set.remove( existingAttributes[i].getDescription() );
137         }
138         possibleAttributeTypesSubschemaOnlyAndExistingHidden = ( String JavaDoc[] ) set.toArray( new String JavaDoc[set.size()] );
139         Arrays.sort( possibleAttributeTypesSubschemaOnlyAndExistingHidden );
140
141         String JavaDoc attributeDescription = initialAttributeDescription;
142         if ( attributeDescription == null )
143         {
144             attributeDescription = "";
145         }
146         String JavaDoc[] attributeDescriptionComponents = attributeDescription.split( ";" );
147         parsedAttributeType = attributeDescriptionComponents[0];
148     }
149
150
151     /**
152      * Validates this page.
153      */

154     private void validate()
155     {
156         previewText.setText( wizard.getAttributeDescription() );
157         setPageComplete( !"".equals( attributeTypeCombo.getText() ) );
158     }
159
160
161     /**
162      * {@inheritDoc}
163      */

164     public void setVisible( boolean visible )
165     {
166         super.setVisible( visible );
167         if ( visible )
168         {
169             validate();
170         }
171     }
172
173
174     /**
175      * {@inheritDoc}
176      */

177     public void createControl( Composite parent )
178     {
179         Composite composite = new Composite( parent, SWT.NONE );
180         GridLayout gl = new GridLayout( 2, false );
181         composite.setLayout( gl );
182         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
183
184         BaseWidgetUtils.createLabel( composite, "Attribute type:", 1 );
185 // attributeTypeCombo = BaseWidgetUtils.createCombo( composite, possibleAttributeTypes, -1, 1 );
186
// attributeTypeCombo.setText( parsedAttributeType );
187

188         // attribute combo with field decoration
189
final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
190             FieldDecorationRegistry.DEC_CONTENT_PROPOSAL );
191         attributeTypeComboField = new DecoratedField( composite, SWT.NONE, new IControlCreator()
192         {
193             public Control createControl( Composite parent, int style )
194             {
195                 Combo combo = BaseWidgetUtils.createCombo( parent, new String JavaDoc[0], -1, 1 );
196                 combo.setVisibleItemCount( 20 );
197                 return combo;
198             }
199         } );
200         attributeTypeComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true );
201         attributeTypeComboField.getLayoutControl().setLayoutData(
202             new GridData( SWT.FILL, SWT.CENTER, true, false ) );
203         attributeTypeCombo = ( Combo ) attributeTypeComboField.getControl();
204         attributeTypeCombo.setItems( possibleAttributeTypes );
205         attributeTypeCombo.setText( parsedAttributeType );
206
207         // content proposal adapter
208
attributeTypeCPA = new ContentProposalAdapter (attributeTypeCombo, new ComboContentAdapter(),
209             new ListContentProposalProvider( attributeTypeCombo.getItems() ), null, null );
210         attributeTypeCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE );
211         attributeTypeCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE );
212
213         BaseWidgetUtils.createSpacer( composite, 1 );
214         showSubschemAttributesOnlyButton = BaseWidgetUtils.createCheckbox( composite, "Show subschema attributes only",
215             1 );
216         showSubschemAttributesOnlyButton.setSelection( initialShowSubschemaAttributesOnly );
217
218         BaseWidgetUtils.createSpacer( composite, 1 );
219         hideExistingAttributesButton = BaseWidgetUtils.createCheckbox( composite, "Hide existing attributes", 1 );
220         hideExistingAttributesButton.setSelection( initialHideExistingAttributes );
221
222         Label l = new Label( composite, SWT.NONE );
223         GridData gd = new GridData( GridData.FILL_BOTH );
224         gd.horizontalSpan = 2;
225         l.setLayoutData( gd );
226
227         BaseWidgetUtils.createLabel( composite, "Preview:", 1 );
228         previewText = BaseWidgetUtils.createReadonlyText( composite, "", 1 );
229
230         // attribute type listener
231
attributeTypeCombo.addModifyListener( new ModifyListener()
232         {
233             public void modifyText( ModifyEvent e )
234             {
235                 validate();
236             }
237         } );
238
239         // filter listener
240
showSubschemAttributesOnlyButton.addSelectionListener( new SelectionAdapter()
241         {
242             public void widgetSelected( SelectionEvent e )
243             {
244                 updateFilter();
245                 validate();
246             }
247         } );
248         hideExistingAttributesButton.addSelectionListener( new SelectionAdapter()
249         {
250             public void widgetSelected( SelectionEvent e )
251             {
252                 updateFilter();
253                 validate();
254             }
255         } );
256         updateFilter();
257
258         setControl( composite );
259     }
260
261
262     /**
263      * Updates the filter.
264      */

265     private void updateFilter()
266     {
267         // enable/disable filter buttons
268
hideExistingAttributesButton.setEnabled( showSubschemAttributesOnlyButton.getSelection() );
269         if ( possibleAttributeTypesSubschemaOnly.length == 0 )
270         {
271             showSubschemAttributesOnlyButton.setSelection( false );
272             showSubschemAttributesOnlyButton.setEnabled( false );
273         }
274         if ( possibleAttributeTypesSubschemaOnlyAndExistingHidden.length == 0 )
275         {
276             hideExistingAttributesButton.setEnabled( false );
277             hideExistingAttributesButton.setSelection( false );
278         }
279
280         // update combo items and proposals
281
String JavaDoc value = attributeTypeCombo.getText();
282         if ( hideExistingAttributesButton.getSelection() && showSubschemAttributesOnlyButton.getSelection() )
283         {
284             attributeTypeCombo.setItems( possibleAttributeTypesSubschemaOnlyAndExistingHidden );
285         }
286         else if ( showSubschemAttributesOnlyButton.getSelection() )
287         {
288             attributeTypeCombo.setItems( possibleAttributeTypesSubschemaOnly );
289         }
290         else
291         {
292             attributeTypeCombo.setItems( possibleAttributeTypes );
293         }
294         attributeTypeCPA.setContentProposalProvider( new ListContentProposalProvider( attributeTypeCombo.getItems() ) );
295         attributeTypeCombo.setText( value );
296     }
297
298
299     /**
300      * Gets the attribute type.
301      *
302      * @return the attribute type
303      */

304     String JavaDoc getAttributeType()
305     {
306         if ( attributeTypeCombo == null | attributeTypeCombo.isDisposed() )
307         {
308             return "";
309         }
310
311         return attributeTypeCombo.getText();
312     }
313
314 }
315
Popular Tags