KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
27 import org.apache.directory.ldapstudio.browser.common.widgets.FileBrowserWidget;
28 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent;
29 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener;
30 import org.apache.directory.ldapstudio.browser.common.widgets.search.ConnectionWidget;
31 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
32 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
33 import org.eclipse.jface.wizard.WizardPage;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Group;
39
40
41 /**
42  * This class implements the Main Page of the LDIF Import Wizard
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class ImportLdifMainWizardPage extends WizardPage
48 {
49
50     /** The continue on error flag key */
51     public static final String JavaDoc CONTINUE_ON_ERROR_DIALOGSETTING_KEY = ImportLdifMainWizardPage.class.getName()
52         + ".continueOnError";
53
54     /** The valid extension. */
55     private static final String JavaDoc[] EXTENSIONS = new String JavaDoc[]
56         { "*.ldif", "*.*" };
57
58     /** The wizard. */
59     private ImportLdifWizard wizard;
60
61     /** The ldif file browser widget. */
62     private FileBrowserWidget ldifFileBrowserWidget;
63
64     /** The connection widget. */
65     private ConnectionWidget connectionWidget;
66
67     /** The enable logging button. */
68     private Button enableLoggingButton;
69
70     /** The use default logfile button. */
71     private Button useDefaultLogfileButton;
72
73     /** The use custom logfile button. */
74     private Button useCustomLogfileButton;
75
76     /** The custom logfile name. */
77     private String JavaDoc customLogfileName;
78
79     /** The log file browser widget. */
80     private FileBrowserWidget logFileBrowserWidget;
81
82     /** The overwrite logfile button. */
83     private Button overwriteLogfileButton;
84
85     /** The continue on error button. */
86     private Button continueOnErrorButton;
87
88
89     /**
90      * Creates a new instance of ImportLdifMainWizardPage.
91      *
92      * @param pageName the page name
93      * @param wizard the wizard
94      */

95     public ImportLdifMainWizardPage( String JavaDoc pageName, ImportLdifWizard wizard )
96     {
97         super( pageName );
98         setTitle( "LDIF Import" );
99         setDescription( "Please select a connection and the LDIF to import" );
100         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_IMPORT_LDIF_WIZARD ) );
101         setPageComplete( false );
102
103         this.wizard = wizard;
104     }
105
106
107     /**
108      * Validates the page. This method is responsible for displaying errors,
109      * as well as enabling/disabling the "Finish" button
110      */

111     private void validate()
112     {
113         boolean ok = true;
114
115         File JavaDoc ldifFile = new File JavaDoc( ldifFileBrowserWidget.getFilename() );
116         if ( "".equals( ldifFileBrowserWidget.getFilename() ) )
117         {
118             setErrorMessage( null );
119             ok = false;
120         }
121         else if ( !ldifFile.isFile() || !ldifFile.exists() )
122         {
123             setErrorMessage( "Selected LDIF file doesn't exist." );
124             ok = false;
125         }
126         else if ( !ldifFile.canRead() )
127         {
128             setErrorMessage( "Selected LDIF file is not readable." );
129             ok = false;
130         }
131         else if ( enableLoggingButton.getSelection() )
132         {
133             File JavaDoc logFile = new File JavaDoc( logFileBrowserWidget.getFilename() );
134             File JavaDoc logFileDirectory = logFile.getParentFile();
135
136             if ( logFile.equals( ldifFile ) )
137             {
138                 setErrorMessage( "LDIF file and Logfile must not be equal." );
139                 ok = false;
140             }
141             else if ( logFile.isDirectory() )
142             {
143                 setErrorMessage( "Selected logfile is no file." );
144                 ok = false;
145             }
146             else if ( logFile.exists() && !overwriteLogfileButton.getSelection() )
147             {
148                 setErrorMessage( "Selected logfile already exists. Select option 'Overwrite existing logfile' if you want to overwrite the logfile." );
149                 ok = false;
150             }
151             else if ( logFile.exists() && !logFile.canWrite() )
152             {
153                 setErrorMessage( "Selected logfile is not writeable." );
154                 ok = false;
155             }
156             else if ( logFile.getParentFile() == null )
157             {
158                 setErrorMessage( "Selected logfile directory is not writeable." );
159                 ok = false;
160             }
161             else if ( !logFile.exists() && ( logFileDirectory == null || !logFileDirectory.canWrite() ) )
162             {
163                 setErrorMessage( "Selected logfile directory is not writeable." );
164                 ok = false;
165             }
166         }
167
168         if ( wizard.getImportConnection() == null )
169         {
170             ok = false;
171         }
172
173         if ( ok )
174         {
175             setErrorMessage( null );
176         }
177         setPageComplete( ok );
178         getContainer().updateButtons();
179     }
180
181
182     /**
183      * {@inheritDoc}
184      */

185     public void createControl( Composite parent )
186     {
187         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 );
188
189         // LDIF file
190
BaseWidgetUtils.createLabel( composite, "LDIF file:", 1 );
191         ldifFileBrowserWidget = new FileBrowserWidget( "Select LDIF File", EXTENSIONS, FileBrowserWidget.TYPE_OPEN );
192         ldifFileBrowserWidget.createWidget( composite );
193         ldifFileBrowserWidget.addWidgetModifyListener( new WidgetModifyListener()
194         {
195             public void widgetModified( WidgetModifyEvent event )
196             {
197                 wizard.setLdifFilename( ldifFileBrowserWidget.getFilename() );
198                 if ( useDefaultLogfileButton.getSelection() )
199                 {
200                     logFileBrowserWidget.setFilename( ldifFileBrowserWidget.getFilename() + ".log" );
201                 }
202                 validate();
203             }
204         } );
205
206         // Connection
207
BaseWidgetUtils.createLabel( composite, "Import into:", 1 );
208         connectionWidget = new ConnectionWidget( wizard.getImportConnection() );
209         connectionWidget.createWidget( composite );
210         connectionWidget.addWidgetModifyListener( new WidgetModifyListener()
211         {
212             public void widgetModified( WidgetModifyEvent event )
213             {
214                 wizard.setImportConnection( connectionWidget.getConnection() );
215                 validate();
216             }
217         } );
218
219         // Logging
220
Composite loggingOuterComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 3 );
221         Group loggingGroup = BaseWidgetUtils.createGroup( loggingOuterComposite, "Logging", 1 );
222         Composite loggingContainer = BaseWidgetUtils.createColumnContainer( loggingGroup, 3, 1 );
223
224         enableLoggingButton = BaseWidgetUtils.createCheckbox( loggingContainer, "Enable logging", 3 );
225         enableLoggingButton.setSelection( true );
226         wizard.setEnableLogging( enableLoggingButton.getSelection() );
227         enableLoggingButton.addSelectionListener( new SelectionAdapter()
228         {
229             public void widgetSelected( SelectionEvent event )
230             {
231                 wizard.setEnableLogging( enableLoggingButton.getSelection() );
232                 useDefaultLogfileButton.setEnabled( enableLoggingButton.getSelection() );
233                 useCustomLogfileButton.setEnabled( enableLoggingButton.getSelection() );
234                 logFileBrowserWidget.setEnabled( enableLoggingButton.getSelection()
235                     && useCustomLogfileButton.getSelection() );
236                 overwriteLogfileButton.setEnabled( enableLoggingButton.getSelection() );
237                 validate();
238             }
239         } );
240
241         BaseWidgetUtils.createRadioIndent( loggingContainer, 1 );
242         useDefaultLogfileButton = BaseWidgetUtils.createRadiobutton( loggingContainer, "Use default logfile", 2 );
243         useDefaultLogfileButton.setSelection( true );
244         useDefaultLogfileButton.addSelectionListener( new SelectionAdapter()
245         {
246             public void widgetSelected( SelectionEvent event )
247             {
248                 String JavaDoc temp = customLogfileName;
249                 logFileBrowserWidget.setFilename( ldifFileBrowserWidget.getFilename() + ".log" );
250                 logFileBrowserWidget.setEnabled( false );
251                 customLogfileName = temp;
252                 validate();
253             }
254         } );
255
256         BaseWidgetUtils.createRadioIndent( loggingContainer, 1 );
257         useCustomLogfileButton = BaseWidgetUtils.createRadiobutton( loggingContainer, "Use custom logfile", 2 );
258         useCustomLogfileButton.setSelection( false );
259         useCustomLogfileButton.addSelectionListener( new SelectionAdapter()
260         {
261             public void widgetSelected( SelectionEvent event )
262             {
263                 logFileBrowserWidget.setFilename( customLogfileName != null ? customLogfileName : "" );
264                 logFileBrowserWidget.setEnabled( true );
265                 validate();
266             }
267         } );
268
269         BaseWidgetUtils.createRadioIndent( loggingContainer, 1 );
270         logFileBrowserWidget = new FileBrowserWidget( "Select Logfile", null, FileBrowserWidget.TYPE_SAVE );
271         logFileBrowserWidget.createWidget( loggingContainer );
272         logFileBrowserWidget.addWidgetModifyListener( new WidgetModifyListener()
273         {
274             public void widgetModified( WidgetModifyEvent event )
275             {
276                 customLogfileName = logFileBrowserWidget.getFilename();
277                 wizard.setLogFilename( customLogfileName );
278                 validate();
279             }
280         } );
281         logFileBrowserWidget.setEnabled( false );
282
283         BaseWidgetUtils.createRadioIndent( loggingContainer, 1 );
284         overwriteLogfileButton = BaseWidgetUtils.createCheckbox( loggingContainer, "Overwrite existing logfile", 2 );
285         overwriteLogfileButton.addSelectionListener( new SelectionAdapter()
286         {
287             public void widgetSelected( SelectionEvent event )
288             {
289                 validate();
290             }
291         } );
292
293         // Continue
294
continueOnErrorButton = BaseWidgetUtils.createCheckbox( composite, "Continue on error", 3 );
295         if ( BrowserUIPlugin.getDefault().getDialogSettings().get( CONTINUE_ON_ERROR_DIALOGSETTING_KEY ) == null )
296         {
297             BrowserUIPlugin.getDefault().getDialogSettings().put( CONTINUE_ON_ERROR_DIALOGSETTING_KEY, false );
298         }
299         continueOnErrorButton.setSelection( BrowserUIPlugin.getDefault().getDialogSettings().getBoolean(
300             CONTINUE_ON_ERROR_DIALOGSETTING_KEY ) );
301         wizard.setContinueOnError( continueOnErrorButton.getSelection() );
302         continueOnErrorButton.addSelectionListener( new SelectionAdapter()
303         {
304             public void widgetSelected( SelectionEvent event )
305             {
306                 wizard.setContinueOnError( continueOnErrorButton.getSelection() );
307                 validate();
308             }
309         } );
310
311         setControl( composite );
312         // nameText.setFocus();
313
}
314
315
316     /**
317      * Saves the dialog settings.
318      */

319     public void saveDialogSettings()
320     {
321         ldifFileBrowserWidget.saveDialogSettings();
322         BrowserUIPlugin.getDefault().getDialogSettings().put( CONTINUE_ON_ERROR_DIALOGSETTING_KEY,
323             continueOnErrorButton.getSelection() );
324     }
325
326 }
Popular Tags