plugins/org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WRTProjectDetailsWizardPage.java
changeset 475 77edd0cbdfe0
parent 474 e2f461f0a9e0
child 476 20536eb3b9ff
equal deleted inserted replaced
474:e2f461f0a9e0 475:77edd0cbdfe0
     1 /**
       
     2  * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.wrttools.wizards;
       
    20 
       
    21 import java.io.File;
       
    22 import java.net.URI;
       
    23 
       
    24 import org.eclipse.core.databinding.DataBindingContext;
       
    25 import org.eclipse.core.databinding.validation.IValidator;
       
    26 import org.eclipse.core.resources.IProject;
       
    27 import org.eclipse.core.resources.IResource;
       
    28 import org.eclipse.core.resources.IWorkspace;
       
    29 import org.eclipse.core.resources.ResourcesPlugin;
       
    30 import org.eclipse.core.runtime.IPath;
       
    31 import org.eclipse.core.runtime.IStatus;
       
    32 import org.eclipse.core.runtime.Path;
       
    33 import org.eclipse.core.runtime.Status;
       
    34 import org.eclipse.jface.databinding.wizard.WizardPageSupport;
       
    35 import org.eclipse.jface.dialogs.Dialog;
       
    36 import org.eclipse.jface.wizard.WizardPage;
       
    37 import org.eclipse.swt.SWT;
       
    38 import org.eclipse.swt.layout.GridData;
       
    39 import org.eclipse.swt.layout.GridLayout;
       
    40 import org.eclipse.swt.widgets.Composite;
       
    41 import org.eclipse.swt.widgets.Event;
       
    42 import org.eclipse.swt.widgets.Label;
       
    43 import org.eclipse.swt.widgets.Listener;
       
    44 import org.eclipse.swt.widgets.Text;
       
    45 import org.eclipse.ui.PlatformUI;
       
    46 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
       
    47 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
       
    48 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
       
    49 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
       
    50 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
       
    51 import org.symbian.tools.wrttools.Activator;
       
    52 import org.symbian.tools.wrttools.util.RegexpValidator;
       
    53 
       
    54 @SuppressWarnings("restriction")
       
    55 public final class WRTProjectDetailsWizardPage extends WizardPage {
       
    56     public static final class ProjectNameValidator implements IValidator {
       
    57 
       
    58         public IStatus validate(Object value) {
       
    59             IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
       
    60 
       
    61             String projectFieldContents = (String) value;
       
    62             IStatus nameStatus = workspace.validateName(projectFieldContents, IResource.PROJECT);
       
    63             if (!nameStatus.isOK()) {
       
    64                 return nameStatus;
       
    65             }
       
    66 
       
    67             IProject handle = ResourcesPlugin.getWorkspace().getRoot().getProject(projectFieldContents);
       
    68             if (handle.exists()) {
       
    69                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
       
    70                         IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
       
    71             }
       
    72             return Status.OK_STATUS;
       
    73         }
       
    74 
       
    75     }
       
    76 
       
    77     private ProjectContentsLocationArea locationArea;
       
    78     private final DataBindingContext bindingContext;
       
    79     private final WizardContext context;
       
    80 
       
    81     public WRTProjectDetailsWizardPage(WizardContext context, DataBindingContext bindingContext) {
       
    82         super("WRTApp");
       
    83         setImageDescriptor(null);
       
    84         setTitle("Application Details");
       
    85         setDescription("Specify application details");
       
    86         this.context = context;
       
    87         this.bindingContext = bindingContext;
       
    88     }
       
    89 
       
    90     public void createControl(Composite parent) {
       
    91         Composite root = new Composite(parent, SWT.NONE);
       
    92 
       
    93         initializeDialogUnits(parent);
       
    94 
       
    95         PlatformUI.getWorkbench().getHelpSystem().setHelp(root, IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
       
    96 
       
    97         WizardPageSupport.create(this, bindingContext);
       
    98         root.setLayout(new GridLayout(2, false));
       
    99         createProjectNameGroup(root);
       
   100 
       
   101         context.createLabel(root, "");
       
   102         context.createLabel(root, "");
       
   103         context.createLabel(root, "Application identifier:");
       
   104 
       
   105         context.createText(root, WizardContext.WIDGET_ID, "applicatoin identifier", bindingContext, null,
       
   106                 new RegexpValidator("[\\w]*(\\.\\w[\\w]*)*", "{0} is not a valid applicatoin ID", true));
       
   107         context.createLabel(root, "");
       
   108         context.createLabel(root, "This id should be unique for successful installation of application on the device");
       
   109 
       
   110         context.createLabel(root, "");
       
   111         context.createLabel(root, "");
       
   112 
       
   113         context.createLabel(root, "Application name:");
       
   114 
       
   115         context.createText(root, WizardContext.WIDGET_NAME, "application name", bindingContext, null,
       
   116                 new RegexpValidator("[^\\w\\. ]", "Application name cannot contain {0} character", false));
       
   117 
       
   118         context.createLabel(root, "");
       
   119         context.createLabel(root, "This will be the application display name on the device");
       
   120 
       
   121         Composite composite = new Composite(root, SWT.NONE);
       
   122         GridLayout gridLayout = new GridLayout(1, false);
       
   123         gridLayout.marginWidth = 0;
       
   124         composite.setLayout(gridLayout);
       
   125         composite.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true, 2, 1));
       
   126 
       
   127         locationArea = new ProjectContentsLocationArea(getErrorReporter(), composite);
       
   128         if (context.getProjectName() != null && context.getProjectName().trim().length() > 0) {
       
   129             locationArea.updateProjectName(context.getProjectName());
       
   130         }
       
   131 
       
   132         // Scale the button based on the rest of the dialog
       
   133         setButtonLayoutData(locationArea.getBrowseButton());
       
   134 
       
   135         setPageComplete(validatePage());
       
   136         // Show description on opening
       
   137         setErrorMessage(null);
       
   138         setMessage(null);
       
   139         setControl(root);
       
   140         Dialog.applyDialogFont(root);
       
   141     }
       
   142 
       
   143     /**
       
   144      * Creates the project name specification controls.
       
   145      *
       
   146      * @param parent
       
   147      *            the parent composite
       
   148      */
       
   149     private final void createProjectNameGroup(Composite parent) {
       
   150         // new project label
       
   151         Label projectLabel = new Label(parent, SWT.NONE);
       
   152         projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel);
       
   153         projectLabel.setFont(parent.getFont());
       
   154 
       
   155         Text projectNameField = context.createText(parent, WizardContext.PROJECT_NAME, "project name", bindingContext,
       
   156                 null, new ProjectNameValidator());
       
   157 
       
   158         projectNameField.setFont(parent.getFont());
       
   159 
       
   160         projectNameField.addListener(SWT.Modify, nameModifyListener);
       
   161     }
       
   162 
       
   163     /**
       
   164      * Returns the current project location path as entered by the user, or its
       
   165      * anticipated initial value. Note that if the default has been returned the
       
   166      * path in a project description used to create a project should not be set.
       
   167      *
       
   168      * @return the project location path or its anticipated initial value.
       
   169      */
       
   170     public IPath getLocationPath() {
       
   171         return new Path(locationArea.getProjectLocation());
       
   172     }
       
   173 
       
   174     /**
       
   175      * /** Returns the current project location URI as entered by the user, or
       
   176      * <code>null</code> if a valid project location has not been entered.
       
   177      *
       
   178      * @return the project location URI, or <code>null</code>
       
   179      * @since 3.2
       
   180      */
       
   181     public URI getLocationURI() {
       
   182         return locationArea.getProjectLocationURI();
       
   183     }
       
   184 
       
   185     private final Listener nameModifyListener = new Listener() {
       
   186         public void handleEvent(Event e) {
       
   187             if (isPageComplete()) {
       
   188                 setLocationForSelection();
       
   189                 boolean valid = validatePage();
       
   190                 setPageComplete(valid);
       
   191             }
       
   192         }
       
   193     };
       
   194 
       
   195     /**
       
   196      * Get an error reporter for the receiver.
       
   197      *
       
   198      * @return IErrorMessageReporter
       
   199      */
       
   200     private IErrorMessageReporter getErrorReporter() {
       
   201         return new IErrorMessageReporter() {
       
   202             /*
       
   203              * (non-Javadoc)
       
   204              *
       
   205              * @see
       
   206              * org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea
       
   207              * .IErrorMessageReporter#reportError(java.lang.String)
       
   208              */
       
   209             public void reportError(String errorMessage, boolean infoOnly) {
       
   210                 if (infoOnly) {
       
   211                     setMessage(errorMessage, IStatus.INFO);
       
   212                     setErrorMessage(null);
       
   213                 } else {
       
   214                     setErrorMessage(errorMessage);
       
   215                 }
       
   216                 boolean valid = errorMessage == null;
       
   217                 if (valid) {
       
   218                     valid = validatePage();
       
   219                 }
       
   220 
       
   221                 setPageComplete(valid);
       
   222             }
       
   223         };
       
   224     }
       
   225 
       
   226     /**
       
   227      * Set the location to the default location if we are set to useDefaults.
       
   228      */
       
   229     void setLocationForSelection() {
       
   230         locationArea.updateProjectName(context.getProjectName());
       
   231     }
       
   232 
       
   233     /**
       
   234      * Returns whether this page's controls currently all contain valid values.
       
   235      *
       
   236      * @return <code>true</code> if all controls are valid, and
       
   237      *         <code>false</code> if at least one is invalid
       
   238      */
       
   239     protected boolean validatePage() {
       
   240         if (isPageComplete() || context.getProjectName().trim().length() == 0) {
       
   241             return false;
       
   242         }
       
   243         String projectName = context.getProjectName();
       
   244         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
       
   245         locationArea.setExistingProject(project);
       
   246 
       
   247         String validLocationMessage = locationArea.checkValidLocation();
       
   248         if (validLocationMessage != null) { // there is no destination location
       
   249                                             // given
       
   250             setErrorMessage(validLocationMessage);
       
   251             return false;
       
   252         }
       
   253         File file = new File(locationArea.getProjectLocationURI());
       
   254         if (file.isFile()) {
       
   255             setErrorMessage(String.format("%s is an existing file", file));
       
   256             return false;
       
   257         } else if (file.isDirectory() && file.listFiles().length > 0) {
       
   258             setErrorMessage(String.format("%s is a non-empty folder", file));
       
   259             return false;
       
   260         }
       
   261 
       
   262         setErrorMessage(null);
       
   263         setMessage(null);
       
   264         return true;
       
   265     }
       
   266 
       
   267     /**
       
   268      * Returns the useDefaults.
       
   269      *
       
   270      * @return boolean
       
   271      */
       
   272     public boolean useDefaults() {
       
   273         return locationArea.isDefault();
       
   274     }
       
   275 
       
   276     @Override
       
   277     public void setVisible(boolean visible) {
       
   278         // TODO Auto-generated method stub
       
   279         super.setVisible(visible);
       
   280         setErrorMessage(null);
       
   281     }
       
   282 }