KickJava   Java API By Example, From Geeks To Geeks.

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


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 DSML Import Wizard
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public class ImportDsmlMainWizardPage extends WizardPage
48 {
49     /** The wizard the page is attached to */
50     private ImportDsmlWizard wizard;
51
52     /** The extensions used by DSML files*/
53     private static final String JavaDoc[] EXTENSIONS = new String JavaDoc[]
54         { "*.xml", "*.*" };
55
56     /** The dsml file browser widget. */
57     private FileBrowserWidget dsmlFileBrowserWidget;
58
59     /** The connection widget. */
60     private ConnectionWidget connectionWidget;
61
62     /** The save response button. */
63     private Button saveResponseButton;
64
65     /** The use default response file button. */
66     private Button useDefaultResponseFileButton;
67
68     /** The use custom response file button. */
69     private Button useCustomResponseFileButton;
70
71     /** The response file browser widget. */
72     private FileBrowserWidget responseFileBrowserWidget;
73
74     /** The overwrite response file button. */
75     private Button overwriteResponseFileButton;
76
77     /** The custom response file name. */
78     private String JavaDoc customResponseFileName;
79
80
81     /**
82      * Creates a new instance of ImportDsmlMainWizardPage.
83      *
84      * @param pageName
85      * the name of the page
86      * @param wizard
87      * the wizard the page is attached to
88      */

89     public ImportDsmlMainWizardPage( String JavaDoc pageName, ImportDsmlWizard wizard )
90     {
91         super( pageName );
92         setTitle( ImportDsmlWizard.WIZARD_TITLE );
93         setDescription( "Please select a connection and the DSML file to import" );
94         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_IMPORT_DSML_WIZARD ) );
95         setPageComplete( false );
96         this.wizard = wizard;
97     }
98
99
100     /**
101      * {@inheritDoc}
102      */

103     public void createControl( Composite parent )
104     {
105         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 );
106
107         // DSML file
108
BaseWidgetUtils.createLabel( composite, "DSML file:", 1 );
109         dsmlFileBrowserWidget = new FileBrowserWidget( "Select a DSML File", EXTENSIONS, FileBrowserWidget.TYPE_OPEN );
110         dsmlFileBrowserWidget.createWidget( composite );
111         dsmlFileBrowserWidget.addWidgetModifyListener( new WidgetModifyListener()
112         {
113             public void widgetModified( WidgetModifyEvent event )
114             {
115                 wizard.setDsmlFilename( dsmlFileBrowserWidget.getFilename() );
116                 if ( useDefaultResponseFileButton.getSelection() )
117                 {
118                     responseFileBrowserWidget.setFilename( dsmlFileBrowserWidget.getFilename() + ".response.xml" );
119                 }
120                 validate();
121             }
122         } );
123
124         // Connection
125
BaseWidgetUtils.createLabel( composite, "Import into:", 1 );
126         connectionWidget = new ConnectionWidget( wizard.getImportConnection() );
127         connectionWidget.createWidget( composite );
128         connectionWidget.addWidgetModifyListener( new WidgetModifyListener()
129         {
130             public void widgetModified( WidgetModifyEvent event )
131             {
132                 wizard.setImportConnection( connectionWidget.getConnection() );
133                 validate();
134             }
135         } );
136
137         // Save Response
138
Composite responseOuterComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 3 );
139         Group responseGroup = BaseWidgetUtils.createGroup( responseOuterComposite, "Response", 1 );
140         Composite responseContainer = BaseWidgetUtils.createColumnContainer( responseGroup, 3, 1 );
141
142         saveResponseButton = BaseWidgetUtils.createCheckbox( responseContainer, "Save response", 3 );
143         saveResponseButton.setSelection( true );
144         wizard.setSaveResponse( saveResponseButton.getSelection() );
145         saveResponseButton.addSelectionListener( new SelectionAdapter()
146         {
147             public void widgetSelected( SelectionEvent event )
148             {
149                 wizard.setSaveResponse( saveResponseButton.getSelection() );
150                 useDefaultResponseFileButton.setEnabled( saveResponseButton.getSelection() );
151                 useCustomResponseFileButton.setEnabled( saveResponseButton.getSelection() );
152                 responseFileBrowserWidget.setEnabled( saveResponseButton.getSelection()
153                     && useCustomResponseFileButton.getSelection() );
154                 overwriteResponseFileButton.setEnabled( saveResponseButton.getSelection() );
155                 validate();
156             }
157         } );
158
159         BaseWidgetUtils.createRadioIndent( responseContainer, 1 );
160         useDefaultResponseFileButton = BaseWidgetUtils.createRadiobutton( responseContainer,
161             "Use default response file", 2 );
162         useDefaultResponseFileButton.setSelection( true );
163         useDefaultResponseFileButton.addSelectionListener( new SelectionAdapter()
164         {
165             public void widgetSelected( SelectionEvent event )
166             {
167                 String JavaDoc temp = customResponseFileName;
168                 responseFileBrowserWidget.setFilename( dsmlFileBrowserWidget.getFilename() + ".response.xml" );
169                 responseFileBrowserWidget.setEnabled( false );
170                 customResponseFileName = temp;
171                 validate();
172             }
173         } );
174
175         BaseWidgetUtils.createRadioIndent( responseContainer, 1 );
176         useCustomResponseFileButton = BaseWidgetUtils.createRadiobutton( responseContainer, "Use custom response file",
177             2 );
178         useCustomResponseFileButton.setSelection( false );
179         useCustomResponseFileButton.addSelectionListener( new SelectionAdapter()
180         {
181             public void widgetSelected( SelectionEvent event )
182             {
183                 responseFileBrowserWidget.setFilename( customResponseFileName != null ? customResponseFileName : "" );
184                 responseFileBrowserWidget.setEnabled( true );
185                 validate();
186             }
187         } );
188
189         BaseWidgetUtils.createRadioIndent( responseContainer, 1 );
190         responseFileBrowserWidget = new FileBrowserWidget( "Select save file", null, FileBrowserWidget.TYPE_SAVE );
191         responseFileBrowserWidget.createWidget( responseContainer );
192         responseFileBrowserWidget.addWidgetModifyListener( new WidgetModifyListener()
193         {
194             public void widgetModified( WidgetModifyEvent event )
195             {
196                 customResponseFileName = responseFileBrowserWidget.getFilename();
197                 wizard.setResponseFilename( customResponseFileName );
198                 validate();
199             }
200         } );
201         responseFileBrowserWidget.setEnabled( false );
202
203         BaseWidgetUtils.createRadioIndent( responseContainer, 1 );
204         overwriteResponseFileButton = BaseWidgetUtils.createCheckbox( responseContainer,
205             "Overwrite existing response file", 2 );
206         overwriteResponseFileButton.addSelectionListener( new SelectionAdapter()
207         {
208             public void widgetSelected( SelectionEvent event )
209             {
210                 validate();
211             }
212         } );
213
214         setControl( composite );
215     }
216
217
218     /**
219      * Validates the page. This method is responsible for displaying errors, as well as enabling/disabling the "Finish" button
220      */

221     private void validate()
222     {
223         boolean ok = true;
224
225         File JavaDoc dsmlFile = new File JavaDoc( dsmlFileBrowserWidget.getFilename() );
226         if ( "".equals( dsmlFileBrowserWidget.getFilename() ) )
227         {
228             setErrorMessage( null );
229             ok = false;
230         }
231         else if ( !dsmlFile.isFile() || !dsmlFile.exists() )
232         {
233             setErrorMessage( "Selected DSML file doesn't exist." );
234             ok = false;
235         }
236         else if ( !dsmlFile.canRead() )
237         {
238             setErrorMessage( "Selected DSML file is not readable." );
239             ok = false;
240         }
241         else if ( saveResponseButton.getSelection() )
242         {
243             File JavaDoc responseFile = new File JavaDoc( responseFileBrowserWidget.getFilename() );
244             File JavaDoc responseFileDirectory = responseFile.getParentFile();
245
246             if ( responseFile.equals( dsmlFile ) )
247             {
248                 setErrorMessage( "DSML file and response file must not be equal." );
249                 ok = false;
250             }
251             else if ( responseFile.isDirectory() )
252             {
253                 setErrorMessage( "Selected response file is not a file." );
254                 ok = false;
255             }
256             else if ( responseFile.exists() && !overwriteResponseFileButton.getSelection() )
257             {
258                 setErrorMessage( "Selected response file already exists. Select option 'Overwrite existing response file' if you want to overwrite the response file." );
259                 ok = false;
260             }
261             else if ( responseFile.exists() && !responseFile.canWrite() )
262             {
263                 setErrorMessage( "Selected response file is not writeable." );
264                 ok = false;
265             }
266             else if ( responseFile.getParentFile() == null )
267             {
268                 setErrorMessage( "Selected response file directory is not writeable." );
269                 ok = false;
270             }
271             else if ( !responseFile.exists() && ( responseFileDirectory == null || !responseFileDirectory.canWrite() ) )
272             {
273                 setErrorMessage( "Selected response file directory is not writeable." );
274                 ok = false;
275             }
276         }
277
278         if ( ( wizard.getImportConnection() == null ) || ( connectionWidget.getConnection() == null ) )
279         {
280             setErrorMessage( "Please select a Connection." );
281             ok = false;
282         }
283
284         if ( ok )
285         {
286             setErrorMessage( null );
287         }
288         setPageComplete( ok );
289         getContainer().updateButtons();
290     }
291
292
293     /**
294      * Saves the Dialog Settings of the Page
295      */

296     public void saveDialogSettings()
297     {
298         dsmlFileBrowserWidget.saveDialogSettings();
299     }
300 }
301
Popular Tags