KickJava   Java API By Example, From Geeks To Geeks.

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


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.eclipse.jface.wizard.WizardPage;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35
36
37 /**
38  * This class is a base implementation of the page to select the target export file.
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public abstract class ExportBaseToPage extends WizardPage
44 {
45
46     /** The wizard. */
47     protected ExportBaseWizard wizard;
48
49     /** The file browser widget. */
50     protected FileBrowserWidget fileBrowserWidget;
51
52     /** The overwrite file button. */
53     protected Button overwriteFileButton;
54
55
56     /**
57      * Creates a new instance of ExportBaseToPage.
58      *
59      * @param pageName the page name
60      * @param wizard the wizard
61      */

62     public ExportBaseToPage( String JavaDoc pageName, ExportBaseWizard wizard )
63     {
64         super( pageName );
65         setPageComplete( false );
66         setTitle( getFileType() + " File" );
67         setDescription( "Please enter the target " + getFileType() + " file." );
68
69         this.wizard = wizard;
70     }
71
72
73     /**
74      * Validates this page. This method is responsible for displaying errors,
75      * as well as enabling/disabling the "Finish" button
76      */

77     protected void validate()
78     {
79         boolean ok = true;
80         File JavaDoc file = new File JavaDoc( fileBrowserWidget.getFilename() );
81         File JavaDoc fileDirectory = file.getParentFile();
82         if ( "".equals( fileBrowserWidget.getFilename() ) )
83         {
84             setErrorMessage( null );
85             ok = false;
86         }
87         else if ( file.isDirectory() )
88         {
89             setErrorMessage( "Selected " + getFileType() + " is no file." );
90             ok = false;
91         }
92         else if ( file.exists() && !overwriteFileButton.getSelection() )
93         {
94             setErrorMessage( "Selected " + getFileType() + " file already exists. Select option 'Overwrite existing "
95                 + getFileType() + " file' if you want to overwrite the " + getFileType() + " file." );
96             ok = false;
97         }
98         else if ( file.exists() && !file.canWrite() )
99         {
100             setErrorMessage( "Selected " + getFileType() + " file is not writeable." );
101             ok = false;
102         }
103         else if ( file.getParentFile() == null )
104         {
105             setErrorMessage( "Selected " + getFileType() + " file directory is not writeable." );
106             ok = false;
107         }
108         else if ( !file.exists() && ( fileDirectory == null || !fileDirectory.canWrite() ) )
109         {
110             setErrorMessage( "Selected " + getFileType() + " file directory is not writeable." );
111             ok = false;
112         }
113
114         if ( ok )
115         {
116             setErrorMessage( null );
117         }
118
119         setPageComplete( ok && wizard.getExportFilename() != null && !"".equals( wizard.getExportFilename() ) );
120     }
121
122
123     /**
124      * {@inheritDoc}
125      */

126     public void createControl( Composite composite )
127     {
128         // Export file
129
BaseWidgetUtils.createLabel( composite, getFileType() + " File:", 1 );
130         fileBrowserWidget = new FileBrowserWidget( "Select " + getFileType() + " File", getExtensions(),
131             FileBrowserWidget.TYPE_SAVE );
132         fileBrowserWidget.createWidget( composite );
133         fileBrowserWidget.addWidgetModifyListener( new WidgetModifyListener()
134         {
135             public void widgetModified( WidgetModifyEvent event )
136             {
137                 wizard.setExportFilename( fileBrowserWidget.getFilename() );
138                 validate();
139             }
140         } );
141         BaseWidgetUtils.createRadioIndent( composite, 1 );
142         overwriteFileButton = BaseWidgetUtils.createCheckbox( composite, "O&verwrite existing " + getFileType()
143             + " file", 2 );
144         overwriteFileButton.addSelectionListener( new SelectionAdapter()
145         {
146             public void widgetSelected( SelectionEvent event )
147             {
148                 validate();
149             }
150         } );
151
152         fileBrowserWidget.setFocus();
153         setControl( composite );
154         validate();
155     }
156
157
158     /**
159      * Gets the valid file extensions.
160      *
161      * @return the valid file extensions
162      */

163     protected abstract String JavaDoc[] getExtensions();
164
165
166     /**
167      * Gets the file type.
168      *
169      * @return the file type
170      */

171     protected abstract String JavaDoc getFileType();
172
173
174     /**
175      * Saves the dialog settings.
176      */

177     public void saveDialogSettings()
178     {
179         fileBrowserWidget.saveDialogSettings();
180     }
181
182 }
183
Popular Tags