+ * It is used to let the user enter the informations about the
+ * schemas he wants to export and where to export.
+ *
+ * @author Apache Directory Project
+ * @version $Rev$, $Date$
+ */
+public class ExportSchemasAsXmlWizardPage extends WizardPage
+{
+ /** The SchemaHandler */
+ private SchemaHandler schemaHandler;
+
+ public static final int EXPORT_MULTIPLE_FILES = 0;
+ public static final int EXPORT_SINGLE_FILE = 1;
+
+ // UI Fields
+ private CheckboxTableViewer schemasTableViewer;
+ private Button exportMultipleFilesRadio;
+ private Label exportMultipleFilesLabel;
+ private Text exportMultipleFilesText;
+ private Button exportMultipleFilewButton;
+ private Button exportSingleFileRadio;
+ private Label exportSingleFileLabel;
+ private Text exportSingleFileText;
+ private Button exportSingleFileButton;
+
+ private Button schemasTableSelectAllButton;
+
+ private Button schemasTableDeselectAllButton;
+
+
+ /**
+ * Creates a new instance of ExportSchemasAsXmlWizardPage.
+ */
+ protected ExportSchemasAsXmlWizardPage()
+ {
+ super( "ExportSchemasAsXmlWizardPage" );
+ setTitle( "Export schemas as XML" );
+ setDescription( "Please select the schemas to export as XML." );
+ setImageDescriptor( AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID,
+ PluginConstants.IMG_SCHEMAS_EXPORT_WIZARD ) );
+ schemaHandler = Activator.getDefault().getSchemaHandler();
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
+ */
+ public void createControl( Composite parent )
+ {
+ Composite composite = new Composite( parent, SWT.NULL );
+ GridLayout layout = new GridLayout();
+ composite.setLayout( layout );
+
+ // Schemas Group
+ Group schemasGroup = new Group( composite, SWT.NONE );
+ schemasGroup.setText( "Schemas" );
+ schemasGroup.setLayout( new GridLayout( 2, false ) );
+ schemasGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+
+ // Schemas TableViewer
+ Label schemasLabel = new Label( schemasGroup, SWT.NONE );
+ schemasLabel.setText( "Select the schemas to export:" );
+ schemasLabel.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false, 2, 1 ) );
+ schemasTableViewer = new CheckboxTableViewer( new Table( schemasGroup, SWT.BORDER | SWT.CHECK
+ | SWT.FULL_SELECTION ) );
+ GridData schemasTableViewerGridData = new GridData( SWT.FILL, SWT.NONE, true, false, 1, 2 );
+ schemasTableViewerGridData.heightHint = 100;
+ schemasTableViewer.getTable().setLayoutData( schemasTableViewerGridData );
+ schemasTableViewer.setContentProvider( new ArrayContentProvider() );
+ schemasTableViewer.setLabelProvider( new LabelProvider()
+ {
+ public String getText( Object element )
+ {
+ if ( element instanceof Schema )
+ {
+ return ( ( Schema ) element ).getName();
+ }
+
+ // Default
+ return super.getText( element );
+ }
+
+
+ public Image getImage( Object element )
+ {
+ if ( element instanceof Schema )
+ {
+ return AbstractUIPlugin.imageDescriptorFromPlugin( Activator.PLUGIN_ID, PluginConstants.IMG_SCHEMA )
+ .createImage();
+ }
+
+ // Default
+ return super.getImage( element );
+ }
+ } );
+ schemasTableViewer.addCheckStateListener( new ICheckStateListener()
+ {
+ /**
+ * Notifies of a change to the checked state of an element.
+ *
+ * @param event
+ * event object describing the change
+ */
+ public void checkStateChanged( CheckStateChangedEvent event )
+ {
+ dialogChanged();
+ }
+ } );
+ schemasTableSelectAllButton = new Button( schemasGroup, SWT.PUSH );
+ schemasTableSelectAllButton.setText( "Select All" );
+ schemasTableSelectAllButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
+ schemasTableSelectAllButton.addSelectionListener( new SelectionAdapter()
+ {
+ public void widgetSelected( SelectionEvent e )
+ {
+ schemasTableViewer.setAllChecked( true );
+ dialogChanged();
+ }
+ } );
+ schemasTableDeselectAllButton = new Button( schemasGroup, SWT.PUSH );
+ schemasTableDeselectAllButton.setText( "Deselect All" );
+ schemasTableDeselectAllButton.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, false, false ) );
+ schemasTableDeselectAllButton.addSelectionListener( new SelectionAdapter()
+ {
+ public void widgetSelected( SelectionEvent e )
+ {
+ schemasTableViewer.setAllChecked( false );
+ dialogChanged();
+ }
+ } );
+
+ // Export Destination Group
+ Group exportDestinationGroup = new Group( composite, SWT.NULL );
+ exportDestinationGroup.setText( "Export Destination" );
+ exportDestinationGroup.setLayout( new GridLayout( 4, false ) );
+ exportDestinationGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+
+ // Export Multiple Files
+ exportMultipleFilesRadio = new Button( exportDestinationGroup, SWT.RADIO );
+ exportMultipleFilesRadio.setText( "Export each schema as a separate file." );
+ exportMultipleFilesRadio.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false, 4, 1 ) );
+ exportMultipleFilesRadio.addSelectionListener( new SelectionAdapter()
+ {
+ public void widgetSelected( SelectionEvent e )
+ {
+ exportMultipleFilesSelected();
+ dialogChanged();
+ }
+ } );
+ Label exportMultipleFilesFiller = new Label( exportDestinationGroup, SWT.NONE );
+ exportMultipleFilesFiller.setText( " " );
+ exportMultipleFilesLabel = new Label( exportDestinationGroup, SWT.NONE );
+ exportMultipleFilesLabel.setText( "Directory:" );
+ exportMultipleFilesText = new Text( exportDestinationGroup, SWT.BORDER );
+ exportMultipleFilesText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+ exportMultipleFilesText.addModifyListener( new ModifyListener()
+ {
+ public void modifyText( ModifyEvent e )
+ {
+ dialogChanged();
+ }
+ } );
+ exportMultipleFilewButton = new Button( exportDestinationGroup, SWT.PUSH );
+ exportMultipleFilewButton.setText( "Browse..." );
+ exportMultipleFilewButton.addSelectionListener( new SelectionAdapter()
+ {
+ public void widgetSelected( SelectionEvent e )
+ {
+ chooseExportDirectory();
+ dialogChanged();
+ }
+ } );
+
+ // Export Single File
+ exportSingleFileRadio = new Button( exportDestinationGroup, SWT.RADIO );
+ exportSingleFileRadio.setText( "Export the schemas as a single file." );
+ exportSingleFileRadio.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false, 4, 1 ) );
+ exportSingleFileRadio.addSelectionListener( new SelectionAdapter()
+ {
+ public void widgetSelected( SelectionEvent e )
+ {
+ exportSingleFileSelected();
+ dialogChanged();
+ }
+ } );
+ Label exportSingleFileFiller = new Label( exportDestinationGroup, SWT.NONE );
+ exportSingleFileFiller.setText( " " );
+ exportSingleFileLabel = new Label( exportDestinationGroup, SWT.NONE );
+ exportSingleFileLabel.setText( "Export File:" );
+ exportSingleFileText = new Text( exportDestinationGroup, SWT.BORDER );
+ exportSingleFileText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
+ exportSingleFileText.addModifyListener( new ModifyListener()
+ {
+ public void modifyText( ModifyEvent e )
+ {
+ dialogChanged();
+ }
+ } );
+ exportSingleFileButton = new Button( exportDestinationGroup, SWT.PUSH );
+ exportSingleFileButton.setText( "Browse..." );
+ exportSingleFileButton.addSelectionListener( new SelectionAdapter()
+ {
+ /* (non-Javadoc)
+ * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ public void widgetSelected( SelectionEvent e )
+ {
+ chooseExportFile();
+ dialogChanged();
+ }
+ } );
+
+ initFields();
+
+ setControl( composite );
+ }
+
+
+ /**
+ * Initializes the UI Fields.
+ */
+ private void initFields()
+ {
+ // Filling the Schemas table
+ schemasTableViewer.setInput( schemaHandler.getSchemas() );
+
+ // Selecting the Multiple Files choice
+ exportMultipleFilesSelected();
+
+ displayErrorMessage( null );
+ setPageComplete( false );
+ }
+
+
+ /**
+ * This method is called when the exportMultipleFiles radio button is selected.
+ */
+ private void exportMultipleFilesSelected()
+ {
+ exportMultipleFilesRadio.setSelection( true );
+ exportMultipleFilesLabel.setEnabled( true );
+ exportMultipleFilesText.setEnabled( true );
+ exportMultipleFilewButton.setEnabled( true );
+
+ exportSingleFileRadio.setSelection( false );
+ exportSingleFileLabel.setEnabled( false );
+ exportSingleFileText.setEnabled( false );
+ exportSingleFileButton.setEnabled( false );
+ }
+
+
+ /**
+ * This method is called when the exportSingleFile radio button is selected.
+ */
+ private void exportSingleFileSelected()
+ {
+ exportMultipleFilesRadio.setSelection( false );
+ exportMultipleFilesLabel.setEnabled( false );
+ exportMultipleFilesText.setEnabled( false );
+ exportMultipleFilewButton.setEnabled( false );
+
+ exportSingleFileRadio.setSelection( true );
+ exportSingleFileLabel.setEnabled( true );
+ exportSingleFileText.setEnabled( true );
+ exportSingleFileButton.setEnabled( true );
+ }
+
+
+ /**
+ * This method is called when the exportMultipleFiles 'browse' button is selected.
+ */
+ private void chooseExportDirectory()
+ {
+ DirectoryDialog dialog = new DirectoryDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() );
+ dialog.setText( "Choose Folder" );
+ dialog.setMessage( "Select the folder in which export the files." );
+
+ String selectedDirectory = dialog.open();
+ if ( selectedDirectory != null )
+ {
+ exportMultipleFilesText.setText( selectedDirectory );
+ }
+ }
+
+
+ /**
+ * This method is called when the exportSingleFile 'browse' button is selected.
+ */
+ private void chooseExportFile()
+ {
+ FileDialog dialog = new FileDialog( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell() );
+ dialog.setText( "Select File" );
+ dialog.setFilterExtensions( new String[]
+ { "*.xml", "*" } );
+ dialog.setFilterNames( new String[]
+ { "XML Files", "All Files" } );
+ dialog.setFilterPath( exportSingleFileText.getText() );
+
+ String selectedFile = dialog.open();
+ if ( selectedFile != null )
+ {
+ exportSingleFileText.setText( selectedFile );
+ }
+ }
+
+
+ /**
+ * This method is called when the user modifies something in the UI.
+ */
+ private void dialogChanged()
+ {
+ // Schemas table
+ if ( schemasTableViewer.getCheckedElements().length == 0 )
+ {
+ displayErrorMessage( "One or several schemas must be selected." );
+ return;
+ }
+
+ // Export option
+ if ( exportMultipleFilesRadio.getSelection() )
+ {
+ String directory = exportMultipleFilesText.getText();
+ if ( ( directory == null ) || ( directory.equals( "" ) ) )
+ {
+ displayErrorMessage( "A directory must be selected." );
+ return;
+ }
+ else
+ {
+ File directoryFile = new File( directory );
+ if ( !directoryFile.exists() )
+ {
+ displayErrorMessage( "The selected directory does not exist." );
+ return;
+ }
+ else if ( !directoryFile.isDirectory() )
+ {
+ displayErrorMessage( "The selected directory is not a directory." );
+ return;
+ }
+ else if ( !directoryFile.canWrite() )
+ {
+ displayErrorMessage( "The selected directory is not writable." );
+ return;
+ }
+ }
+ }
+ else if ( exportSingleFileRadio.getSelection() )
+ {
+ String exportFile = exportSingleFileText.getText();
+ if ( ( exportFile == null ) || ( exportFile.equals( "" ) ) )
+ {
+ displayErrorMessage( "A file must be selected." );
+ return;
+ }
+ else
+ {
+ File file = new File( exportFile );
+ if ( !file.getParentFile().canWrite() )
+ {
+ displayErrorMessage( "The selected file is not writable." );
+ return;
+ }
+ }
+ }
+
+ displayErrorMessage( null );
+ }
+
+
+ /**
+ * Displays an error message and set the page status as incomplete
+ * if the message is not null.
+ *
+ * @param message
+ * the message to display
+ */
+ private void displayErrorMessage( String message )
+ {
+ setErrorMessage( message );
+ setPageComplete( message == null );
+ }
+
+
+ /**
+ * Gets the selected schemas.
+ *
+ * @return
+ * the selected schemas
+ */
+ public Schema[] getSelectedSchemas()
+ {
+ Object[] selectedSchemas = schemasTableViewer.getCheckedElements();
+
+ List
+ * Values can either EXPORT_MULTIPLE_FILES or EXPORT_SINGLE_FILE.
+ *
+ * @return
+ * the type of export
+ */
+ public int getExportType()
+ {
+ if ( exportMultipleFilesRadio.getSelection() )
+ {
+ return EXPORT_MULTIPLE_FILES;
+ }
+ else if ( exportSingleFileRadio.getSelection() )
+ {
+ return EXPORT_SINGLE_FILE;
+ }
+
+ // Default
+ return EXPORT_MULTIPLE_FILES;
+ }
+
+
+ /**
+ * Gets the export directory.
+ *
+ * @return
+ * the export directory
+ */
+ public String getExportDirectory()
+ {
+ return exportMultipleFilesText.getText();
+ }
+
+
+ /**
+ * Gets the export file.
+ *
+ * @return
+ * the export file
+ */
+ public String getExportFile()
+ {
+ return exportSingleFileText.getText();
+ }
+}