org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/wizards/WizardNewProjectCreationPage.java
changeset 441 85f06000ab13
parent 440 533ef48d3b15
child 442 980aaebb8022
equal deleted inserted replaced
440:533ef48d3b15 441:85f06000ab13
     1 package org.symbian.tools.wrttools.wizards;
       
     2 
       
     3 import java.net.URI;
       
     4 
       
     5 import org.eclipse.core.resources.IProject;
       
     6 import org.eclipse.core.resources.IResource;
       
     7 import org.eclipse.core.resources.IWorkspace;
       
     8 import org.eclipse.core.resources.ResourcesPlugin;
       
     9 import org.eclipse.core.runtime.IPath;
       
    10 import org.eclipse.core.runtime.IStatus;
       
    11 import org.eclipse.core.runtime.Path;
       
    12 import org.eclipse.jface.dialogs.Dialog;
       
    13 import org.eclipse.jface.viewers.IStructuredSelection;
       
    14 import org.eclipse.jface.wizard.WizardPage;
       
    15 import org.eclipse.swt.SWT;
       
    16 import org.eclipse.swt.layout.GridData;
       
    17 import org.eclipse.swt.layout.GridLayout;
       
    18 import org.eclipse.swt.widgets.Composite;
       
    19 import org.eclipse.swt.widgets.Event;
       
    20 import org.eclipse.swt.widgets.Label;
       
    21 import org.eclipse.swt.widgets.Listener;
       
    22 import org.eclipse.swt.widgets.Text;
       
    23 import org.eclipse.ui.IWorkingSet;
       
    24 import org.eclipse.ui.PlatformUI;
       
    25 import org.eclipse.ui.dialogs.WorkingSetGroup;
       
    26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
       
    27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
       
    28 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
       
    29 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea;
       
    30 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter;
       
    31 
       
    32 /**
       
    33  * Standard main page for a wizard that is creates a project resource.
       
    34  * <p>
       
    35  * This page may be used by clients as-is; it may be also be subclassed to suit.
       
    36  * </p>
       
    37  * <p>
       
    38  * Example usage:
       
    39  * <pre>
       
    40  * mainPage = new WizardNewProjectCreationPage("basicNewProjectPage");
       
    41  * mainPage.setTitle("Project");
       
    42  * mainPage.setDescription("Create a new project resource.");
       
    43  * </pre>
       
    44  * </p>
       
    45  */
       
    46 public class WizardNewProjectCreationPage extends WizardPage {
       
    47 
       
    48        // initial value stores
       
    49     private String initialProjectFieldValue;
       
    50 
       
    51     // widgets
       
    52     Text projectNameField;
       
    53 
       
    54     private final Listener nameModifyListener = new Listener() {
       
    55         public void handleEvent(Event e) {
       
    56         	setLocationForSelection();
       
    57             boolean valid = validatePage();
       
    58             setPageComplete(valid);
       
    59                 
       
    60         }
       
    61     };
       
    62 
       
    63 	private ProjectContentsLocationArea locationArea;
       
    64 
       
    65 	private WorkingSetGroup workingSetGroup;
       
    66 
       
    67     // constants
       
    68     private static final int SIZING_TEXT_FIELD_WIDTH = 250;
       
    69 
       
    70     /**
       
    71      * Creates a new project creation wizard page.
       
    72      *
       
    73      * @param pageName the name of this page
       
    74      */
       
    75     public WizardNewProjectCreationPage(String pageName) {
       
    76     	super(pageName);
       
    77 	    setPageComplete(false);
       
    78     }
       
    79 
       
    80 	/** (non-Javadoc)
       
    81      * Method declared on IDialogPage.
       
    82      */
       
    83     public void createControl(Composite parent) {
       
    84         Composite composite = new Composite(parent, SWT.NULL);
       
    85     
       
    86 
       
    87         initializeDialogUnits(parent);
       
    88 
       
    89         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
       
    90                 IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE);
       
    91 
       
    92         composite.setLayout(new GridLayout());
       
    93         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
       
    94 
       
    95         createProjectNameGroup(composite);
       
    96 
       
    97         createChildControls(composite);
       
    98 
       
    99         locationArea = new ProjectContentsLocationArea(getErrorReporter(), composite);
       
   100         if(initialProjectFieldValue != null) {
       
   101 			locationArea.updateProjectName(initialProjectFieldValue);
       
   102 		}
       
   103         
       
   104 		// Scale the button based on the rest of the dialog
       
   105 		setButtonLayoutData(locationArea.getBrowseButton());
       
   106 		
       
   107         setPageComplete(validatePage());
       
   108         // Show description on opening
       
   109         setErrorMessage(null);
       
   110         setMessage(null);
       
   111         setControl(composite);
       
   112         Dialog.applyDialogFont(composite);
       
   113     }
       
   114     
       
   115     protected void createChildControls(Composite composite) {
       
   116         // TODO Auto-generated method stub
       
   117 
       
   118     }
       
   119 
       
   120     /**
       
   121 	 * Create a working set group for this page. This method can only be called
       
   122 	 * once.
       
   123 	 * 
       
   124 	 * @param composite
       
   125 	 *            the composite in which to create the group
       
   126 	 * @param selection
       
   127 	 *            the current workbench selection
       
   128 	 * @param supportedWorkingSetTypes
       
   129 	 *            an array of working set type IDs that will restrict what types
       
   130 	 *            of working sets can be chosen in this group
       
   131 	 * @return the created group. If this method has been called previously the
       
   132 	 *         original group will be returned.
       
   133 	 * @since 3.4
       
   134 	 */
       
   135 	public WorkingSetGroup createWorkingSetGroup(Composite composite,
       
   136 			IStructuredSelection selection, String[] supportedWorkingSetTypes) {
       
   137 		if (workingSetGroup != null) {
       
   138             return workingSetGroup;
       
   139         }
       
   140 		workingSetGroup = new WorkingSetGroup(composite, selection,
       
   141 				supportedWorkingSetTypes);
       
   142 		return workingSetGroup;
       
   143 	}
       
   144     
       
   145     /**
       
   146 	 * Get an error reporter for the receiver.
       
   147 	 * @return IErrorMessageReporter
       
   148 	 */
       
   149 	private IErrorMessageReporter getErrorReporter() {
       
   150 		return new IErrorMessageReporter(){
       
   151 			/* (non-Javadoc)
       
   152 			 * @see org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter#reportError(java.lang.String)
       
   153 			 */
       
   154 			public void reportError(String errorMessage, boolean infoOnly) {
       
   155 				if (infoOnly) {
       
   156 					setMessage(errorMessage, IStatus.INFO);
       
   157 					setErrorMessage(null);
       
   158 				} else {
       
   159                     setErrorMessage(errorMessage);
       
   160                 }
       
   161 				boolean valid = errorMessage == null;
       
   162 				if(valid) {
       
   163 					valid = validatePage();
       
   164 				}
       
   165 				
       
   166 				setPageComplete(valid);
       
   167 			}
       
   168 		};
       
   169 	}
       
   170 
       
   171     /**
       
   172      * Creates the project name specification controls.
       
   173      *
       
   174      * @param parent the parent composite
       
   175      */
       
   176     private final void createProjectNameGroup(Composite parent) {
       
   177         // project specification group
       
   178         Composite projectGroup = new Composite(parent, SWT.NONE);
       
   179         GridLayout layout = new GridLayout();
       
   180         layout.numColumns = 2;
       
   181         projectGroup.setLayout(layout);
       
   182         projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   183 
       
   184         // new project label
       
   185         Label projectLabel = new Label(projectGroup, SWT.NONE);
       
   186         projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel);
       
   187         projectLabel.setFont(parent.getFont());
       
   188 
       
   189         // new project name entry field
       
   190         projectNameField = new Text(projectGroup, SWT.BORDER);
       
   191         GridData data = new GridData(GridData.FILL_HORIZONTAL);
       
   192         data.widthHint = SIZING_TEXT_FIELD_WIDTH;
       
   193         projectNameField.setLayoutData(data);
       
   194         projectNameField.setFont(parent.getFont());
       
   195 
       
   196         // Set the initial value first before listener
       
   197         // to avoid handling an event during the creation.
       
   198         if (initialProjectFieldValue != null) {
       
   199 			projectNameField.setText(initialProjectFieldValue);
       
   200 		}
       
   201         projectNameField.addListener(SWT.Modify, nameModifyListener);
       
   202     }
       
   203 
       
   204 
       
   205     /**
       
   206      * Returns the current project location path as entered by 
       
   207      * the user, or its anticipated initial value.
       
   208      * Note that if the default has been returned the path
       
   209      * in a project description used to create a project
       
   210      * should not be set.
       
   211      *
       
   212      * @return the project location path or its anticipated initial value.
       
   213      */
       
   214     public IPath getLocationPath() {
       
   215         return new Path(locationArea.getProjectLocation());
       
   216     }
       
   217     
       
   218     /**
       
   219     /**
       
   220      * Returns the current project location URI as entered by 
       
   221      * the user, or <code>null</code> if a valid project location
       
   222      * has not been entered.
       
   223      *
       
   224      * @return the project location URI, or <code>null</code>
       
   225      * @since 3.2
       
   226      */
       
   227     public URI getLocationURI() {
       
   228     	return locationArea.getProjectLocationURI();
       
   229     }
       
   230 
       
   231     /**
       
   232 	 * Creates a project resource handle for the current project name field
       
   233 	 * value. The project handle is created relative to the workspace root.
       
   234 	 * <p>
       
   235 	 * This method does not create the project resource; this is the
       
   236 	 * responsibility of <code>IProject::create</code> invoked by the new
       
   237 	 * project resource wizard.
       
   238 	 * </p>
       
   239 	 * 
       
   240 	 * @return the new project resource handle
       
   241 	 */
       
   242     public IProject getProjectHandle() {
       
   243         return ResourcesPlugin.getWorkspace().getRoot().getProject(
       
   244                 getProjectName());
       
   245     }
       
   246 
       
   247     /**
       
   248      * Returns the current project name as entered by the user, or its anticipated
       
   249      * initial value.
       
   250      *
       
   251      * @return the project name, its anticipated initial value, or <code>null</code>
       
   252      *   if no project name is known
       
   253      */
       
   254     public String getProjectName() {
       
   255         if (projectNameField == null) {
       
   256 			return initialProjectFieldValue;
       
   257 		}
       
   258 
       
   259         return getProjectNameFieldValue();
       
   260     }
       
   261 
       
   262     /**
       
   263      * Returns the value of the project name field
       
   264      * with leading and trailing spaces removed.
       
   265      * 
       
   266      * @return the project name in the field
       
   267      */
       
   268     private String getProjectNameFieldValue() {
       
   269         if (projectNameField == null) {
       
   270 			return ""; //$NON-NLS-1$
       
   271 		}
       
   272 
       
   273         return projectNameField.getText().trim();
       
   274     }
       
   275 
       
   276     /**
       
   277      * Sets the initial project name that this page will use when
       
   278      * created. The name is ignored if the createControl(Composite)
       
   279      * method has already been called. Leading and trailing spaces
       
   280      * in the name are ignored.
       
   281      * Providing the name of an existing project will not necessarily 
       
   282      * cause the wizard to warn the user.  Callers of this method 
       
   283      * should first check if the project name passed already exists 
       
   284      * in the workspace.
       
   285      * 
       
   286      * @param name initial project name for this page
       
   287      * 
       
   288      * @see IWorkspace#validateName(String, int)
       
   289      * 
       
   290      */
       
   291     public void setInitialProjectName(String name) {
       
   292         if (name == null) {
       
   293 			initialProjectFieldValue = null;
       
   294 		} else {
       
   295             initialProjectFieldValue = name.trim();
       
   296             if(locationArea != null) {
       
   297 				locationArea.updateProjectName(name.trim());
       
   298 			}
       
   299         }
       
   300     }
       
   301 
       
   302     /**
       
   303      * Set the location to the default location if we are set to useDefaults.
       
   304      */
       
   305     void setLocationForSelection() {
       
   306     	locationArea.updateProjectName(getProjectNameFieldValue());
       
   307     }
       
   308 
       
   309   
       
   310     /**
       
   311      * Returns whether this page's controls currently all contain valid 
       
   312      * values.
       
   313      *
       
   314      * @return <code>true</code> if all controls are valid, and
       
   315      *   <code>false</code> if at least one is invalid
       
   316      */
       
   317     protected boolean validatePage() {
       
   318         IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace();
       
   319 
       
   320         String projectFieldContents = getProjectNameFieldValue();
       
   321         if (projectFieldContents.equals("")) { //$NON-NLS-1$
       
   322             setErrorMessage(null);
       
   323             setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectNameEmpty);
       
   324             return false;
       
   325         }
       
   326 
       
   327         IStatus nameStatus = workspace.validateName(projectFieldContents,
       
   328                 IResource.PROJECT);
       
   329         if (!nameStatus.isOK()) {
       
   330             setErrorMessage(nameStatus.getMessage());
       
   331             return false;
       
   332         }
       
   333 
       
   334         IProject handle = getProjectHandle();
       
   335         if (handle.exists()) {
       
   336             setErrorMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage);
       
   337             return false;
       
   338         }
       
   339                 
       
   340         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(
       
   341 				getProjectNameFieldValue());
       
   342 		locationArea.setExistingProject(project);
       
   343 		
       
   344 		String validLocationMessage = locationArea.checkValidLocation();
       
   345 		if (validLocationMessage != null) { // there is no destination location given
       
   346 			setErrorMessage(validLocationMessage);
       
   347 			return false;
       
   348 		}
       
   349 
       
   350         setErrorMessage(null);
       
   351         setMessage(null);
       
   352         return true;
       
   353     }
       
   354 
       
   355     /*
       
   356      * see @DialogPage.setVisible(boolean)
       
   357      */
       
   358     public void setVisible(boolean visible) {
       
   359         super.setVisible(visible);
       
   360         if (visible) {
       
   361 			projectNameField.setFocus();
       
   362 		}
       
   363     }
       
   364 
       
   365     /**
       
   366      * Returns the useDefaults.
       
   367      * @return boolean
       
   368      */
       
   369     public boolean useDefaults() {
       
   370         return locationArea.isDefault();
       
   371     }
       
   372 
       
   373     /**
       
   374 	 * Return the selected working sets, if any. If this page is not configured
       
   375 	 * to interact with working sets this will be an empty array.
       
   376 	 * 
       
   377 	 * @return the selected working sets
       
   378 	 * @since 3.4
       
   379 	 */
       
   380 	public IWorkingSet[] getSelectedWorkingSets() {
       
   381 		return workingSetGroup == null ? new IWorkingSet[0] : workingSetGroup
       
   382 				.getSelectedWorkingSets();
       
   383 	}
       
   384 }