KickJava   Java API By Example, From Geeks To Geeks.

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


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.jobs.RunnableContextJobAdapter;
25 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
26 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent;
27 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener;
28 import org.apache.directory.ldapstudio.browser.common.widgets.search.EntryWidget;
29 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
30 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry;
31 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeAttributesJob;
32 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob;
33 import org.apache.directory.ldapstudio.browser.core.model.DN;
34 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
35 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
36 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
38 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils;
39 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter;
40 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
41 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
42 import org.eclipse.jface.dialogs.MessageDialog;
43 import org.eclipse.jface.wizard.IWizardPage;
44 import org.eclipse.jface.wizard.WizardPage;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.SelectionListener;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52
53
54 /**
55  * The NewEntryTypeWizardPage is used to choose the entry creation method.
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class NewEntryTypeWizardPage extends WizardPage implements WidgetModifyListener, SelectionListener
61 {
62
63     /** The Constant PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY. */
64     public static final String JavaDoc PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY = NewEntryTypeWizardPage.class
65         .getName()
66         + ".preferredEntryCreationMethod";
67
68     /** The wizard. */
69     private NewEntryWizard wizard;
70
71     /** The schema button. */
72     private Button schemaButton;
73
74     /** The template button. */
75     private Button templateButton;
76
77     /** The entry widget to select the template entry. */
78     private EntryWidget entryWidget;
79
80
81     /**
82      * Creates a new instance of NewEntryTypeWizardPage.
83      *
84      * @param pageName the page name
85      * @param wizard the wizard
86      */

87     public NewEntryTypeWizardPage( String JavaDoc pageName, NewEntryWizard wizard )
88     {
89         super( pageName );
90         setTitle( "Entry Creation Method" );
91         setDescription( "Please select the entry creation method." );
92         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) );
93         setPageComplete( false );
94
95         this.wizard = wizard;
96     }
97
98
99     /**
100      * Validates the input fields.
101      */

102     private void validate()
103     {
104         if ( schemaButton.getSelection() )
105         {
106             setPageComplete( true );
107         }
108         else if ( templateButton.getSelection() )
109         {
110             setPageComplete( entryWidget.getConnection() != null && entryWidget.getDn() != null );
111         }
112         else
113         {
114             setPageComplete( false );
115         }
116     }
117
118     
119     /**
120      * {@inheritDoc}
121      *
122      * This implementation just checks if this page is complete. IIt
123      * doesn't call {@link #getNextPage()} to avoid unneeded
124      * creations of new prototype entries.
125      */

126     public boolean canFlipToNextPage()
127     {
128         return isPageComplete();
129     }
130     
131
132     /**
133      * {@inheritDoc}
134      *
135      * This implementation creates the prototype entry depending on the
136      * selected entry creation method before flipping to the next page.
137      */

138     public IWizardPage getNextPage()
139     {
140         if ( templateButton.getSelection() )
141         {
142             final IConnection connection = entryWidget.getConnection();
143             final DN dn = entryWidget.getDn();
144             final IEntry[] templateEntries = new IEntry[1];
145
146             if ( connection == null )
147             {
148                 getShell().getDisplay().syncExec( new Runnable JavaDoc()
149                 {
150                     public void run()
151                     {
152                         MessageDialog.openError( getShell(), "Error", "No connection" );
153                     }
154                 } );
155                 return null;
156             }
157             if ( dn == null )
158             {
159                 getShell().getDisplay().syncExec( new Runnable JavaDoc()
160                 {
161                     public void run()
162                     {
163                         MessageDialog.openError( getShell(), "Error", "No dn" );
164                     }
165                 } );
166                 return null;
167             }
168
169             // check if selected DN exists
170
ReadEntryJob readEntryJob = new ReadEntryJob( connection, dn );
171             RunnableContextJobAdapter.execute( readEntryJob, getContainer(), false );
172             templateEntries[0] = readEntryJob.getReadEntry();
173             if ( templateEntries[0] == null )
174             {
175                 getShell().getDisplay().syncExec( new Runnable JavaDoc()
176                 {
177                     public void run()
178                     {
179                         MessageDialog.openError( getShell(), "Error", "Entry " + dn.toString() + " doesn't exists" );
180                     }
181                 } );
182                 return null;
183             }
184
185             // init attributes
186
if ( !templateEntries[0].isAttributesInitialized() )
187             {
188                 InitializeAttributesJob job = new InitializeAttributesJob( templateEntries, false );
189                 RunnableContextJobAdapter.execute( job, getContainer() );
190             }
191
192             // clone entry and remove non-modifyable attributes
193
try
194             {
195                 EventRegistry.suspendEventFireingInCurrentThread();
196
197                 LdifContentRecord record = ModelConverter.entryToLdifContentRecord( templateEntries[0] );
198                 DummyEntry prototypeEntry = ModelConverter.ldifContentRecordToEntry( record, connection );
199                 IAttribute[] attributes = prototypeEntry.getAttributes();
200                 for ( int i = 0; i < attributes.length; i++ )
201                 {
202                     if ( !SchemaUtils.isModifyable( attributes[i].getAttributeTypeDescription() ) )
203                     {
204                         prototypeEntry.deleteAttribute( attributes[i] );
205                     }
206                 }
207                 wizard.setPrototypeEntry( prototypeEntry );
208             }
209             catch ( Exception JavaDoc e )
210             {
211                 e.printStackTrace();
212             }
213             finally
214             {
215                 EventRegistry.resumeEventFireingInCurrentThread();
216             }
217         }
218         else
219         {
220             wizard.setPrototypeEntry( new DummyEntry( new DN(), wizard.getSelectedConnection() ) );
221         }
222
223         return super.getNextPage();
224     }
225
226
227     /**
228      * {@inheritDoc}
229      */

230     public void createControl( Composite parent )
231     {
232         Composite composite = new Composite( parent, SWT.NONE );
233         GridLayout gl = new GridLayout( 1, false );
234         composite.setLayout( gl );
235         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
236
237         schemaButton = BaseWidgetUtils.createRadiobutton( composite, "Create entry from scratch", 1 );
238         schemaButton.addSelectionListener( this );
239         templateButton = BaseWidgetUtils.createRadiobutton( composite, "Use existing entry as template", 1 );
240         templateButton.addSelectionListener( this );
241
242         Composite entryComposite = BaseWidgetUtils.createColumnContainer( composite, 3, 1 );
243         BaseWidgetUtils.createRadioIndent( entryComposite, 1 );
244         entryWidget = new EntryWidget( wizard.getSelectedConnection(), wizard.getSelectedEntry() != null ? wizard
245             .getSelectedEntry().getDn() : null );
246         entryWidget.createWidget( entryComposite );
247         entryWidget.addWidgetModifyListener( this );
248
249         if ( BrowserUIPlugin.getDefault().getDialogSettings().get( PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY ) == null )
250             BrowserUIPlugin.getDefault().getDialogSettings().put( PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY,
251                 true );
252         schemaButton.setSelection( BrowserUIPlugin.getDefault().getDialogSettings().getBoolean(
253             PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY ) );
254         templateButton.setSelection( !BrowserUIPlugin.getDefault().getDialogSettings().getBoolean(
255             PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY ) );
256         widgetSelected( null );
257
258         setControl( composite );
259     }
260
261
262     /**
263      * {@inheritDoc}
264      */

265     public void widgetModified( WidgetModifyEvent event )
266     {
267         validate();
268     }
269
270
271     /**
272      * {@inheritDoc}
273      */

274     public void widgetDefaultSelected( SelectionEvent e )
275     {
276     }
277
278
279     /**
280      * {@inheritDoc}
281      */

282     public void widgetSelected( SelectionEvent e )
283     {
284         entryWidget.setEnabled( templateButton.getSelection() );
285         validate();
286     }
287
288
289     /**
290      * Saves the dialog settings.
291      */

292     public void saveDialogSettings()
293     {
294         BrowserUIPlugin.getDefault().getDialogSettings().put( PREFERRED_ENTRY_CREATION_METHOD_DIALOGSETTING_KEY,
295             schemaButton.getSelection() );
296     }
297
298 }
299
Popular Tags