KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > wizards > NewEntryObjectclassWizardPage


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.ui.wizards;
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 import java.util.Set JavaDoc;
29
30 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
31 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
32 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
34 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry;
35 import org.apache.directory.ldapstudio.browser.core.internal.model.Value;
36 import org.apache.directory.ldapstudio.browser.core.jobs.OpenConnectionsJob;
37 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
38 import org.apache.directory.ldapstudio.browser.core.model.IValue;
39 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
40 import org.apache.directory.ldapstudio.browser.core.model.schema.ObjectClassDescription;
41 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
42 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
43 import org.eclipse.jface.fieldassist.DecoratedField;
44 import org.eclipse.jface.fieldassist.FieldDecoration;
45 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
46 import org.eclipse.jface.fieldassist.IControlCreator;
47 import org.eclipse.jface.viewers.ArrayContentProvider;
48 import org.eclipse.jface.viewers.DoubleClickEvent;
49 import org.eclipse.jface.viewers.IDoubleClickListener;
50 import org.eclipse.jface.viewers.ISelection;
51 import org.eclipse.jface.viewers.IStructuredSelection;
52 import org.eclipse.jface.viewers.LabelProvider;
53 import org.eclipse.jface.viewers.ListViewer;
54 import org.eclipse.jface.viewers.StructuredSelection;
55 import org.eclipse.jface.viewers.Viewer;
56 import org.eclipse.jface.viewers.ViewerFilter;
57 import org.eclipse.jface.viewers.ViewerSorter;
58 import org.eclipse.jface.wizard.WizardPage;
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.events.ModifyEvent;
61 import org.eclipse.swt.events.ModifyListener;
62 import org.eclipse.swt.events.SelectionAdapter;
63 import org.eclipse.swt.events.SelectionEvent;
64 import org.eclipse.swt.layout.GridData;
65 import org.eclipse.swt.layout.GridLayout;
66 import org.eclipse.swt.widgets.Button;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Control;
69 import org.eclipse.swt.widgets.Label;
70 import org.eclipse.swt.widgets.Text;
71
72
73 /**
74  * The NewEntryTypeWizardPage is used to select the entry's object classes.
75  *
76  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
77  * @version $Rev$, $Date$
78  */

79 public class NewEntryObjectclassWizardPage extends WizardPage
80 {
81
82     /** The Constant SIZING_SELECTION_WIDGET_HEIGHT. */
83     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
84
85     /** The Constant SIZING_SELECTION_WIDGET_WIDTH. */
86     private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
87
88     /** The wizard. */
89     private NewEntryWizard wizard;
90
91     /** The available object classes. */
92     private List JavaDoc<ObjectClassDescription> availableObjectClasses;
93
94     /** The available object classes instant search. */
95     private Text availableObjectClassesInstantSearch;
96
97     /** The available object classes viewer. */
98     private ListViewer availableObjectClassesViewer;
99
100     /** The selected object classes. */
101     private List JavaDoc<ObjectClassDescription> selectedObjectClasses;
102
103     /** The selected object classes viewer. */
104     private ListViewer selectedObjectClassesViewer;
105
106     /** The add button. */
107     private Button addButton;
108
109     /** The remove button. */
110     private Button removeButton;
111
112
113     /**
114      * Creates a new instance of NewEntryObjectclassWizardPage.
115      *
116      * @param pageName the page name
117      * @param wizard the wizard
118      */

119     public NewEntryObjectclassWizardPage( String JavaDoc pageName, NewEntryWizard wizard )
120     {
121         super( pageName );
122         setTitle( "Object Classes" );
123         setDescription( "Please select object classes of the new entry. Select at least one structural object class." );
124         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) );
125         setPageComplete( false );
126
127         this.wizard = wizard;
128         this.availableObjectClasses = new ArrayList JavaDoc<ObjectClassDescription>();
129         this.selectedObjectClasses = new ArrayList JavaDoc<ObjectClassDescription>();
130     }
131
132
133     /**
134      * Validates the input fields.
135      */

136     private void validate()
137     {
138         if ( !selectedObjectClasses.isEmpty() )
139         {
140             setPageComplete( true );
141             saveState();
142         }
143         else
144         {
145             setPageComplete( false );
146         }
147     }
148
149
150     /**
151      * Loads the state of selected and available object classes from
152      * the prototype entry. Called when this page becomes visible.
153      */

154     private void loadState()
155     {
156         availableObjectClasses.clear();
157         selectedObjectClasses.clear();
158
159         if ( wizard.getSelectedConnection() != null )
160         {
161             if( !wizard.getSelectedConnection().isOpened() )
162             {
163                 OpenConnectionsJob job = new OpenConnectionsJob(wizard.getSelectedConnection());
164                 RunnableContextJobAdapter.execute( job, getContainer() );
165             }
166             
167             availableObjectClasses.addAll( Arrays.asList( wizard.getSelectedConnection().getSchema()
168                 .getObjectClassDescriptions() ) );
169
170             DummyEntry newEntry = wizard.getPrototypeEntry();
171             IAttribute ocAttribute = newEntry.getAttribute( IAttribute.OBJECTCLASS_ATTRIBUTE );
172             if ( ocAttribute != null )
173             {
174                 String JavaDoc[] ocValues = ocAttribute.getStringValues();
175                 for ( int i = 0; i < ocValues.length; i++ )
176                 {
177                     String JavaDoc ocValue = ocValues[i];
178                     ObjectClassDescription ocd = wizard.getSelectedConnection().getSchema().getObjectClassDescription(
179                         ocValue );
180                     availableObjectClasses.remove( ocd );
181                     selectedObjectClasses.add( ocd );
182                 }
183             }
184         }
185
186         availableObjectClassesViewer.refresh();
187         selectedObjectClassesViewer.refresh();
188     }
189
190
191     /**
192      * Saves the state of selected object classes to the entry.
193      */

194     private void saveState()
195     {
196         DummyEntry newEntry = wizard.getPrototypeEntry();
197
198         try
199         {
200             EventRegistry.suspendEventFireingInCurrentThread();
201
202             // set new objectClass values
203
IAttribute ocAttribute = newEntry.getAttribute( IAttribute.OBJECTCLASS_ATTRIBUTE );
204             if ( ocAttribute == null )
205             {
206                 ocAttribute = new Attribute( newEntry, IAttribute.OBJECTCLASS_ATTRIBUTE );
207                 newEntry.addAttribute( ocAttribute );
208             }
209             IValue[] values = ocAttribute.getValues();
210             for ( int i = 0; i < values.length; i++ )
211             {
212                 ocAttribute.deleteValue( values[i] );
213             }
214             for ( Iterator JavaDoc<ObjectClassDescription> it = selectedObjectClasses.iterator(); it.hasNext(); )
215             {
216                 ObjectClassDescription ocd = it.next();
217                 ocAttribute.addValue( new Value( ocAttribute, ocd.getNames()[0] ) );
218             }
219         }
220         catch ( ModelModificationException e )
221         {
222             e.printStackTrace();
223         }
224         finally
225         {
226             EventRegistry.resumeEventFireingInCurrentThread();
227         }
228     }
229
230
231     /**
232      * {@inheritDoc}
233      *
234      * This implementation initializes the list of available and selected
235      * object classes when this page becomes visible.
236      */

237     public void setVisible( boolean visible )
238     {
239         super.setVisible( visible );
240
241         if ( visible )
242         {
243             loadState();
244             validate();
245             availableObjectClassesInstantSearch.setFocus();
246         }
247     }
248
249
250     /**
251      * {@inheritDoc}
252      */

253     public void createControl( Composite parent )
254     {
255
256         Composite composite = new Composite( parent, SWT.NONE );
257         GridLayout gl = new GridLayout( 3, false );
258         composite.setLayout( gl );
259         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
260
261         Label availableLabel = new Label( composite, SWT.NONE );
262         availableLabel.setText( "Available object classes" );
263         Label buttonLabel = new Label( composite, SWT.NONE );
264         buttonLabel.setText( "" );
265         Label selectedLabel = new Label( composite, SWT.NONE );
266         selectedLabel.setText( "Selected object classes" );
267
268         Composite availableObjectClassesComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 );
269
270         if ( FieldDecorationRegistry.getDefault().getFieldDecoration( getClass().getName() ) == null )
271         {
272             FieldDecoration dummy = FieldDecorationRegistry.getDefault().getFieldDecoration(
273                 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL );
274             FieldDecorationRegistry.getDefault().registerFieldDecoration( getClass().getName(),
275                 "You may enter a filter to restrict the list below", dummy.getImage() );
276         }
277         final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(
278             getClass().getName() );
279         final DecoratedField availabeObjectClassesInstantSearchField = new DecoratedField(
280             availableObjectClassesComposite, SWT.BORDER, new IControlCreator()
281             {
282                 public Control createControl( Composite parent, int style )
283                 {
284                     return BaseWidgetUtils.createText( parent, "", 1 );
285                 }
286             } );
287         availabeObjectClassesInstantSearchField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true );
288         availabeObjectClassesInstantSearchField.getLayoutControl().setLayoutData(
289             new GridData( SWT.FILL, SWT.CENTER, true, false ) );
290         availableObjectClassesInstantSearch = ( Text ) availabeObjectClassesInstantSearchField.getControl();
291         availableObjectClassesInstantSearch.addModifyListener( new ModifyListener()
292         {
293             public void modifyText( ModifyEvent e )
294             {
295                 availableObjectClassesViewer.refresh();
296                 if ( availableObjectClassesViewer.getList().getItemCount() == 1 )
297                 {
298                     Object JavaDoc item = availableObjectClassesViewer.getElementAt( 0 );
299                     availableObjectClassesViewer.setSelection( new StructuredSelection( item ) );
300                 }
301             }
302         } );
303
304         availableObjectClassesViewer = new ListViewer( availableObjectClassesComposite );
305         GridData data = new GridData( GridData.FILL_BOTH );
306         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
307         data.widthHint = ( int ) ( SIZING_SELECTION_WIDGET_WIDTH * 0.4 );
308         availableObjectClassesViewer.getList().setLayoutData( data );
309         availableObjectClassesViewer.setContentProvider( new ArrayContentProvider() );
310         availableObjectClassesViewer.setLabelProvider( new LabelProvider() );
311         availableObjectClassesViewer.setSorter( new ViewerSorter() );
312         availableObjectClassesViewer.addFilter( new InstantSearchFilter( availableObjectClassesInstantSearch ) );
313         availableObjectClassesViewer.setInput( availableObjectClasses );
314         availableObjectClassesViewer.addDoubleClickListener( new IDoubleClickListener()
315         {
316             public void doubleClick( DoubleClickEvent event )
317             {
318                 add( event.getSelection() );
319             }
320         } );
321
322         Composite buttonComposite = new Composite( composite, SWT.NONE );
323         gl = new GridLayout( 1, true );
324         buttonComposite.setLayout( gl );
325         data = new GridData( GridData.FILL_BOTH );
326         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
327         // data.widthHint = (int)(SIZING_SELECTION_WIDGET_WIDTH * 0.2);
328
data.horizontalAlignment = SWT.CENTER;
329         buttonComposite.setLayoutData( data );
330         Label label0 = new Label( buttonComposite, SWT.NONE );
331         data = new GridData();
332         data.grabExcessHorizontalSpace = true;
333         data.grabExcessVerticalSpace = true;
334         label0.setLayoutData( data );
335         addButton = BaseWidgetUtils.createButton( buttonComposite, "&Add", 1 );
336         removeButton = BaseWidgetUtils.createButton( buttonComposite, "&Remove", 1 );
337         Label label3 = new Label( buttonComposite, SWT.NONE );
338         data = new GridData();
339         data.grabExcessHorizontalSpace = true;
340         data.grabExcessVerticalSpace = true;
341         label3.setLayoutData( data );
342
343         addButton.addSelectionListener( new SelectionAdapter()
344         {
345             public void widgetSelected( SelectionEvent e )
346             {
347                 add( availableObjectClassesViewer.getSelection() );
348             }
349         } );
350
351         removeButton.addSelectionListener( new SelectionAdapter()
352         {
353             public void widgetSelected( SelectionEvent e )
354             {
355                 remove( selectedObjectClassesViewer.getSelection() );
356             }
357         } );
358
359         selectedObjectClassesViewer = new ListViewer( composite );
360         data = new GridData( GridData.FILL_BOTH );
361         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
362         data.widthHint = ( int ) ( SIZING_SELECTION_WIDGET_WIDTH * 0.4 );
363         selectedObjectClassesViewer.getList().setLayoutData( data );
364         selectedObjectClassesViewer.setContentProvider( new ArrayContentProvider() );
365         selectedObjectClassesViewer.setLabelProvider( new LabelProvider() );
366         selectedObjectClassesViewer.setSorter( new ViewerSorter() );
367         selectedObjectClassesViewer.setInput( selectedObjectClasses );
368         selectedObjectClassesViewer.addDoubleClickListener( new IDoubleClickListener()
369         {
370             public void doubleClick( DoubleClickEvent event )
371             {
372                 remove( event.getSelection() );
373             }
374         } );
375
376         setControl( composite );
377     }
378
379
380     /**
381      * Adds the selected object classes to the list of selected
382      * object classes.
383      *
384      * @param iselection the selection
385      */

386     private void add( ISelection iselection )
387     {
388         IStructuredSelection selection = ( IStructuredSelection ) iselection;
389         Iterator JavaDoc it = selection.iterator();
390         while ( it.hasNext() )
391         {
392             ObjectClassDescription ocd = ( ObjectClassDescription ) it.next();
393             if ( availableObjectClasses.contains( ocd ) && !selectedObjectClasses.contains( ocd ) )
394             {
395                 availableObjectClasses.remove( ocd );
396                 selectedObjectClasses.add( ocd );
397
398                 // recursively add superior object classes
399
ObjectClassDescription[] superiorObjectClassDescriptions = ocd.getSuperiorObjectClassDescriptions();
400                 if ( superiorObjectClassDescriptions.length > 0 )
401                 {
402                     add( new StructuredSelection( superiorObjectClassDescriptions ) );
403                 }
404             }
405         }
406
407         availableObjectClassesViewer.refresh();
408         selectedObjectClassesViewer.refresh();
409         validate();
410
411         if ( !"".equals( availableObjectClassesInstantSearch.getText() ) )
412         {
413             availableObjectClassesInstantSearch.setText( "" );
414             availableObjectClassesInstantSearch.setFocus();
415         }
416     }
417
418
419     /**
420      * Removes the selected object classes from the list of selected
421      * object classes.
422      *
423      * @param iselection the iselection
424      */

425     private void remove( ISelection iselection )
426     {
427         IStructuredSelection selection = ( IStructuredSelection ) iselection;
428         Iterator JavaDoc it = selection.iterator();
429         while ( it.hasNext() )
430         {
431             ObjectClassDescription ocd = ( ObjectClassDescription ) it.next();
432             if ( !availableObjectClasses.contains( ocd ) && selectedObjectClasses.contains( ocd ) )
433             {
434                 selectedObjectClasses.remove( ocd );
435                 availableObjectClasses.add( ocd );
436
437                 // recursively remove sub object classes
438
ObjectClassDescription[] subObjectClassDescriptions = ocd.getSubObjectClassDescriptions();
439                 if ( subObjectClassDescriptions.length > 0 )
440                 {
441                     remove( new StructuredSelection( subObjectClassDescriptions ) );
442                 }
443             }
444         }
445
446         availableObjectClassesViewer.refresh();
447         selectedObjectClassesViewer.refresh();
448         validate();
449     }
450
451
452     /**
453      * Saves dialog settings.
454      */

455     public void saveDialogSettings()
456     {
457
458     }
459
460     /**
461      * The Class InstantSearchFilter.
462      */

463     private class InstantSearchFilter extends ViewerFilter
464     {
465
466         /** The filter text. */
467         private Text filterText;
468
469
470         /**
471          * Creates a new instance of InstantSearchFilter.
472          *
473          * @param filterText the filter text
474          */

475         private InstantSearchFilter( Text filterText )
476         {
477             this.filterText = filterText;
478         }
479
480
481         /**
482          * {@inheritDoc}
483          */

484         public boolean select( Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element )
485         {
486             if ( element instanceof ObjectClassDescription )
487             {
488                 ObjectClassDescription ocd = ( ObjectClassDescription ) element;
489                 Set JavaDoc<String JavaDoc> lowerCaseIdentifierSet = ocd.getLowerCaseIdentifierSet();
490                 for ( Iterator JavaDoc<String JavaDoc> it = lowerCaseIdentifierSet.iterator(); it.hasNext(); )
491                 {
492                     String JavaDoc s = it.next();
493                     if ( s.toLowerCase().startsWith( filterText.getText().toLowerCase() ) )
494                     {
495                         return true;
496                     }
497                 }
498             }
499             return false;
500         }
501     }
502 }
503
Popular Tags