001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.studio.apacheds.wizards;
021    
022    
023    import org.apache.directory.studio.apacheds.ApacheDsPlugin;
024    import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
025    import org.apache.directory.studio.apacheds.model.ServersHandler;
026    import org.eclipse.jface.wizard.WizardPage;
027    import org.eclipse.swt.SWT;
028    import org.eclipse.swt.events.ModifyEvent;
029    import org.eclipse.swt.events.ModifyListener;
030    import org.eclipse.swt.layout.GridData;
031    import org.eclipse.swt.layout.GridLayout;
032    import org.eclipse.swt.widgets.Composite;
033    import org.eclipse.swt.widgets.Label;
034    import org.eclipse.swt.widgets.Text;
035    
036    
037    /**
038     * This class implements the wizard page for the new server wizard.
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$, $Date$
042     */
043    public class NewServerWizardPage extends WizardPage
044    {
045        /** The servers handler */
046        private ServersHandler serversHandler;
047    
048        // UI fields
049        private Text nameText;
050    
051    
052        /**
053         * Creates a new instance of NewServerWizardPage.
054         */
055        public NewServerWizardPage()
056        {
057            super( NewServerWizardPage.class.getCanonicalName() );
058            setTitle( Messages.getString( "NewServerWizardPage.CreateNewServer" ) ); //$NON-NLS-1$
059            setDescription( Messages.getString( "NewServerWizardPage.PleaseSpecifyName" ) ); //$NON-NLS-1$
060            setImageDescriptor( ApacheDsPlugin.getDefault().getImageDescriptor(
061                ApacheDsPluginConstants.IMG_SERVER_NEW_WIZARD ) );
062            setPageComplete( false );
063            serversHandler = ServersHandler.getDefault();
064        }
065    
066    
067        /* (non-Javadoc)
068         * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
069         */
070        public void createControl( Composite parent )
071        {
072            Composite composite = new Composite( parent, SWT.NONE );
073            composite.setLayout( new GridLayout( 2, false ) );
074    
075            Label nameLabel = new Label( composite, SWT.NONE );
076            nameLabel.setText( Messages.getString( "NewServerWizardPage.Name" ) ); //$NON-NLS-1$
077            nameText = new Text( composite, SWT.BORDER );
078            nameText.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
079            nameText.addModifyListener( new ModifyListener()
080            {
081                public void modifyText( ModifyEvent e )
082                {
083                    validate();
084                }
085            } );
086    
087            setControl( composite );
088        }
089    
090    
091        /**
092         * Validates the page.
093         */
094        private void validate()
095        {
096            displayErrorMessage( null );
097    
098            String name = nameText.getText();
099            if ( ( name != null ) )
100            {
101                if ( "".equals( name ) ) //$NON-NLS-1$
102                {
103                    displayErrorMessage( Messages.getString( "NewServerWizardPage.ErrorEnterName" ) ); //$NON-NLS-1$
104                    return;
105                }
106                if ( !serversHandler.isNameAvailable( name ) )
107                {
108                    displayErrorMessage( Messages.getString( "NewServerWizardPage.ErrorNameExists" ) ); //$NON-NLS-1$
109                    return;
110                }
111            }
112        }
113    
114    
115        /**
116         * Displays an error message and set the page status as incomplete
117         * if the message is not null.
118         *
119         * @param message
120         *      the message to display
121         */
122        protected void displayErrorMessage( String message )
123        {
124            setErrorMessage( message );
125            setPageComplete( message == null );
126        }
127    
128    
129        /**
130         * Gets the name of the server.
131         *
132         * @return
133         *      the name of the server
134         */
135        public String getServerName()
136        {
137            return nameText.getText();
138        }
139    }