KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidget;
26 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroup;
27 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetActionGroupWithAttribute;
28 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetConfiguration;
29 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetUniversalListener;
30 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.OpenDefaultEditorAction;
31 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
32 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
33 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
34 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
35 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry;
36 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
37 import org.apache.directory.ldapstudio.browser.core.model.IValue;
38 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
39 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
40 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
41 import org.eclipse.jface.viewers.StructuredSelection;
42 import org.eclipse.jface.wizard.WizardPage;
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.Composite;
47
48
49 /**
50  * The NewEntryAttributesWizardPage is used to fill the attributes of
51  * the new entry.
52  *
53  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
54  * @version $Rev$, $Date$
55  */

56 public class NewEntryAttributesWizardPage extends WizardPage implements EntryUpdateListener
57 {
58
59     /** The wizard. */
60     private NewEntryWizard wizard;
61
62     /** The configuration. */
63     private EntryEditorWidgetConfiguration configuration;
64
65     /** The action group. */
66     private EntryEditorWidgetActionGroup actionGroup;
67
68     /** The main widget. */
69     private EntryEditorWidget mainWidget;
70
71     /** The universal listener. */
72     private EntryEditorWidgetUniversalListener universalListener;
73
74
75     /**
76      * Creates a new instance of NewEntryAttributesWizardPage.
77      *
78      * @param pageName the page name
79      * @param wizard the wizard
80      */

81     public NewEntryAttributesWizardPage( String JavaDoc pageName, NewEntryWizard wizard )
82     {
83         super( pageName );
84         setTitle( "Attributes" );
85         setDescription( "Please enter the attributes for the entry. Enter at least the MUST attributes." );
86         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) );
87         setPageComplete( false );
88
89         this.wizard = wizard;
90     }
91
92
93     /**
94      * {@inheritDoc}
95      */

96     public void dispose()
97     {
98         if ( configuration != null )
99         {
100             EventRegistry.removeEntryUpdateListener( this );
101             universalListener.dispose();
102             universalListener = null;
103             mainWidget.dispose();
104             mainWidget = null;
105             actionGroup.dispose();
106             actionGroup = null;
107             configuration.dispose();
108             configuration = null;
109         }
110         super.dispose();
111     }
112
113
114     /**
115      * {@inheritDoc}
116      *
117      * This implementation initializes the must attributes of the
118      * protoype entry and initializes the entry widget when this
119      * page becomes visible.
120      */

121     public void setVisible( boolean visible )
122     {
123         super.setVisible( visible );
124
125         if ( visible )
126         {
127             DummyEntry newEntry = wizard.getPrototypeEntry();
128             IValue editValue = null;
129
130             try
131             {
132                 EventRegistry.suspendEventFireingInCurrentThread();
133
134                 // remove empty must attributes
135
// necessary when navigating back, modifying object classes
136
// and DN and navigation forward again.
137
String JavaDoc[] oldMust = newEntry.getSubschema().getMustAttributeNames();
138                 for ( int i = 0; i < oldMust.length; i++ )
139                 {
140                     IAttribute attribute = newEntry.getAttribute( oldMust[i] );
141                     if ( attribute != null )
142                     {
143                         IValue[] values = attribute.getValues();
144                         for ( int v = 0; v < values.length; v++ )
145                         {
146                             if ( values[v].isEmpty() )
147                             {
148                                 attribute.deleteValue( values[v] );
149                             }
150                         }
151                         if ( attribute.getValueSize() == 0 )
152                         {
153                             newEntry.deleteAttribute( attribute );
154                         }
155                     }
156                 }
157
158                 // add must attributes
159
String JavaDoc[] newMust = newEntry.getSubschema().getMustAttributeNames();
160                 for ( int i = 0; i < newMust.length; i++ )
161                 {
162                     if ( newEntry.getAttribute( newMust[i] ) == null )
163                     {
164                         IAttribute att = new Attribute( newEntry, newMust[i] );
165                         newEntry.addAttribute( att );
166                         att.addEmptyValue();
167
168                         if ( editValue == null )
169                         {
170                             editValue = att.getValues()[0];
171                         }
172                     }
173                 }
174             }
175             catch ( ModelModificationException e )
176             {
177                 e.printStackTrace();
178             }
179             finally
180             {
181                 EventRegistry.resumeEventFireingInCurrentThread();
182             }
183
184             // set the input
185
mainWidget.getViewer().setInput( newEntry );
186             mainWidget.getViewer().refresh();
187             validate();
188
189             // set focus to the viewer
190
mainWidget.getViewer().getControl().setFocus();
191
192             // start editing if there is an empty value
193
if ( editValue != null )
194             {
195                 mainWidget.getViewer().setSelection( new StructuredSelection( editValue ), true );
196                 OpenDefaultEditorAction openDefaultEditorAction = actionGroup.getOpenDefaultEditorAction();
197                 if ( openDefaultEditorAction.isEnabled() )
198                 {
199                     openDefaultEditorAction.run();
200                 }
201             }
202         }
203         else
204         {
205             mainWidget.getViewer().setInput( "" );
206             mainWidget.getViewer().refresh();
207             setPageComplete( false );
208         }
209     }
210
211
212     /**
213      * Checks if the prototype entry is completed.
214      */

215     private void validate()
216     {
217         if ( wizard.getPrototypeEntry() != null && wizard.getPrototypeEntry().isConsistent() )
218         {
219             setPageComplete( true );
220         }
221         else
222         {
223             setPageComplete( false );
224         }
225     }
226
227
228     /**
229      * {@inheritDoc}
230      */

231     public void createControl( Composite parent )
232     {
233         Composite composite = new Composite( parent, SWT.NONE );
234         GridLayout gl = new GridLayout( 1, false );
235         composite.setLayout( gl );
236         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
237
238         // create configuration
239
configuration = new EntryEditorWidgetConfiguration();
240
241         // create main widget
242
mainWidget = new EntryEditorWidget( this.configuration );
243         mainWidget.createWidget( composite );
244         mainWidget.getViewer().getTree().setFocus();
245
246         // create actions
247
actionGroup = new EntryEditorWidgetActionGroupWithAttribute( mainWidget, configuration );
248         actionGroup.fillToolBar( mainWidget.getToolBarManager() );
249         actionGroup.fillMenu( mainWidget.getMenuManager() );
250         actionGroup.fillContextMenu( mainWidget.getContextMenuManager() );
251
252         // create the listener
253
universalListener = new EntryEditorWidgetUniversalListener( mainWidget.getViewer(), actionGroup
254             .getOpenDefaultEditorAction() );
255         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
256
257         setControl( composite );
258     }
259
260
261     /**
262      * {@inheritDoc}
263      */

264     public void entryUpdated( EntryModificationEvent event )
265     {
266         if ( event.getModifiedEntry() == wizard.getPrototypeEntry() && !isDisposed() && getControl().isVisible() )
267         {
268             validate();
269         }
270     }
271
272
273     /**
274      * Checks if is disposed.
275      *
276      * @return true, if is disposed
277      */

278     private boolean isDisposed()
279     {
280         return configuration == null;
281     }
282
283 }
284
Popular Tags