jlanuti | 4df6c74 | 2005-12-01 21:54:48 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2003, 2005 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
jlanuti | 4df6c74 | 2005-12-01 21:54:48 +0000 | [diff] [blame] | 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 11 | /* |
| 12 | * Created on Oct 27, 2003 |
| 13 | * |
| 14 | * To change the template for this generated file go to Window>Preferences>Java>Code |
| 15 | * Generation>Code and Comments |
| 16 | */ |
| 17 | package org.eclipse.wst.common.frameworks.internal.operations; |
| 18 | |
| 19 | import java.io.File; |
cbridgha | 2013e56 | 2005-09-06 17:24:51 +0000 | [diff] [blame] | 20 | import java.util.Set; |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 21 | |
vbhadrir | 0b2e413 | 2005-08-23 19:46:44 +0000 | [diff] [blame] | 22 | import org.eclipse.core.internal.resources.Resource; |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 23 | import org.eclipse.core.resources.IProject; |
| 24 | import org.eclipse.core.resources.IProjectDescription; |
| 25 | import org.eclipse.core.resources.IResource; |
| 26 | import org.eclipse.core.resources.IWorkspace; |
| 27 | import org.eclipse.core.resources.ResourcesPlugin; |
| 28 | import org.eclipse.core.runtime.IPath; |
| 29 | import org.eclipse.core.runtime.IStatus; |
| 30 | import org.eclipse.core.runtime.Path; |
| 31 | import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider; |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 32 | import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation; |
cbridgha | 1239a3c | 2005-11-08 18:46:39 +0000 | [diff] [blame] | 33 | import org.eclipse.wst.common.frameworks.internal.WTPPlugin; |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 34 | import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonMessages; |
| 35 | import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonPlugin; |
| 36 | |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 37 | /** |
| 38 | * @deprecated use ProjectCreationDataModelProviderNew instead. |
| 39 | */ |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 40 | public class ProjectCreationDataModelProvider extends AbstractDataModelProvider implements IProjectCreationProperties { |
| 41 | |
| 42 | public IDataModelOperation getDefaultOperation() { |
vbhadrir | bd9918f | 2005-06-03 14:34:11 +0000 | [diff] [blame] | 43 | return new ProjectCreationOperation(model); |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 44 | } |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 45 | |
| 46 | public void init() { |
| 47 | super.init(); |
| 48 | } |
| 49 | |
cbridgha | 2013e56 | 2005-09-06 17:24:51 +0000 | [diff] [blame] | 50 | public Set getPropertyNames() { |
| 51 | Set propertyNames = super.getPropertyNames(); |
cbridgha | 3bfc2a8 | 2005-09-06 15:37:26 +0000 | [diff] [blame] | 52 | propertyNames.add(PROJECT); |
| 53 | propertyNames.add(PROJECT_NAME); |
| 54 | propertyNames.add(PROJECT_LOCATION); |
| 55 | propertyNames.add(PROJECT_NATURES); |
| 56 | propertyNames.add(PROJECT_DESCRIPTION); |
| 57 | return propertyNames; |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public Object getDefaultProperty(String propertyName) { |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 61 | if (propertyName.equals(PROJECT_LOCATION)) |
| 62 | return getDefaultLocation(); |
| 63 | else if (propertyName.equals(PROJECT_DESCRIPTION)) |
| 64 | return getProjectDescription(); |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 65 | return super.getDefaultProperty(propertyName); |
| 66 | } |
| 67 | |
| 68 | public boolean propertySet(String propertyName, Object propertyValue) { |
| 69 | if (propertyValue != null && propertyName.equals(PROJECT_LOCATION)) { |
| 70 | IPath path = getRootLocation(); |
| 71 | if (path.equals(new Path((String) propertyValue))) { |
vbhadrir | c8fb50b | 2005-06-17 14:23:42 +0000 | [diff] [blame] | 72 | model.setProperty(propertyName, null); |
| 73 | } |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 74 | model.setProperty(PROJECT_DESCRIPTION, getProjectDescription()); |
vbhadrir | c8fb50b | 2005-06-17 14:23:42 +0000 | [diff] [blame] | 75 | } else if (propertyName.equals(PROJECT_NAME)) { |
vbhadrir | 562d879 | 2005-05-27 17:16:28 +0000 | [diff] [blame] | 76 | IStatus stat = model.validateProperty(PROJECT_NAME); |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 77 | if (stat != OK_STATUS) |
vbhadrir | 562d879 | 2005-05-27 17:16:28 +0000 | [diff] [blame] | 78 | return false; |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 79 | model.setProperty(PROJECT, getProject()); |
| 80 | model.setProperty(PROJECT_DESCRIPTION, getProjectDescription()); |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private String getDefaultLocation() { |
| 86 | IPath path = getRootLocation(); |
| 87 | String projectName = (String) getProperty(PROJECT_NAME); |
| 88 | if (projectName != null) |
| 89 | path = path.append(projectName); |
| 90 | return path.toOSString(); |
| 91 | } |
| 92 | |
| 93 | private IPath getRootLocation() { |
| 94 | return ResourcesPlugin.getWorkspace().getRoot().getLocation(); |
| 95 | } |
| 96 | |
| 97 | private IProjectDescription getProjectDescription() { |
| 98 | String projectName = (String) getProperty(PROJECT_NAME); |
| 99 | IProjectDescription desc = ResourcesPlugin.getWorkspace().newProjectDescription(projectName); |
cbridgha | 6308d66 | 2005-04-08 17:50:25 +0000 | [diff] [blame] | 100 | if (getDataModel().isPropertySet(PROJECT_LOCATION)) { |
vbhadrir | 0b2e413 | 2005-08-23 19:46:44 +0000 | [diff] [blame] | 101 | String projectLocation = (String) getProperty(IProjectCreationProperties.PROJECT_LOCATION); |
cbridgha | 35febdc | 2005-11-07 16:10:38 +0000 | [diff] [blame] | 102 | if (projectLocation != null) |
| 103 | desc.setLocation(new Path(projectLocation)); |
| 104 | else |
| 105 | desc.setLocation(null); |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 106 | } |
| 107 | return desc; |
| 108 | } |
| 109 | |
| 110 | protected IProject getProject() { |
| 111 | String projectName = (String) getProperty(PROJECT_NAME); |
| 112 | return (null != projectName && projectName.length() > 0) ? ResourcesPlugin.getWorkspace().getRoot().getProject(projectName) : null; |
| 113 | } |
| 114 | |
| 115 | public IStatus validate(String propertyName) { |
| 116 | if (propertyName.equals(PROJECT_NAME)) { |
| 117 | IStatus status = validateName(); |
| 118 | if (!status.isOK()) |
| 119 | return status; |
| 120 | } |
| 121 | if (propertyName.equals(PROJECT_LOCATION)) { |
| 122 | IStatus status = validateLocation(); |
| 123 | if (!status.isOK()) |
| 124 | return status; |
| 125 | } |
| 126 | if (propertyName.equals(PROJECT_LOCATION) || propertyName.equals(PROJECT_NAME)) { |
| 127 | String projectName = getStringProperty(PROJECT_NAME); |
| 128 | String projectLoc = getStringProperty(PROJECT_LOCATION); |
| 129 | return validateExisting(projectName, projectLoc); |
| 130 | } |
| 131 | return OK_STATUS; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param projectName |
| 136 | * @param projectLoc |
| 137 | * @todo Generated comment |
| 138 | */ |
| 139 | private IStatus validateExisting(String projectName, String projectLoc) { |
| 140 | if (projectName != null && !projectName.equals("")) {//$NON-NLS-1$ |
| 141 | File file = new File(projectLoc); |
| 142 | if (file.exists()) { |
| 143 | if (file.isDirectory()) { |
| 144 | File dotProject = new File(file, ".project");//$NON-NLS-1$ |
| 145 | if (dotProject.exists()) { |
| 146 | return WTPCommonPlugin.createErrorStatus(WTPCommonPlugin.getResourceString(WTPCommonMessages.PROJECT_EXISTS_AT_LOCATION_ERROR, new Object[]{file.toString()})); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return OK_STATUS; |
| 152 | } |
| 153 | |
| 154 | public static IProject getProjectHandleFromProjectName(String projectName) { |
| 155 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 156 | IStatus status = workspace.validateName(projectName, IResource.PROJECT); |
| 157 | return (null != projectName && projectName.length() > 0 && status.isOK()) ? ResourcesPlugin.getWorkspace().getRoot().getProject(projectName) : null; |
| 158 | } |
| 159 | |
| 160 | public static IStatus validateProjectName(String projectName) { |
| 161 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 162 | IStatus status = workspace.validateName(projectName, IResource.PROJECT); |
| 163 | if (!status.isOK()) |
| 164 | return status; |
| 165 | return OK_STATUS; |
| 166 | } |
| 167 | |
| 168 | private IStatus validateName() { |
| 169 | String name = getStringProperty(PROJECT_NAME); |
| 170 | IStatus status = validateProjectName(name); |
| 171 | if (!status.isOK()) |
| 172 | return status; |
| 173 | if (getProject().exists()) |
| 174 | return WTPCommonPlugin.createErrorStatus(WTPCommonPlugin.getResourceString(WTPCommonMessages.PROJECT_EXISTS_ERROR, new Object[]{name})); |
| 175 | |
cbridgha | 1239a3c | 2005-11-08 18:46:39 +0000 | [diff] [blame] | 176 | if (!WTPPlugin.isPlatformCaseSensitive()) { |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 177 | // now look for a matching case variant in the tree |
vbhadrir | 0b2e413 | 2005-08-23 19:46:44 +0000 | [diff] [blame] | 178 | IResource variant = ((Resource) getProject()).findExistingResourceVariant(getProject().getFullPath()); |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 179 | if (variant != null) { |
| 180 | // TODO Fix this string |
vbhadrir | 0b2e413 | 2005-08-23 19:46:44 +0000 | [diff] [blame] | 181 | return WTPCommonPlugin.createErrorStatus("Resource already exists with a different case."); //$NON-NLS-1$ |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | return OK_STATUS; |
| 185 | } |
| 186 | |
| 187 | private IStatus validateLocation() { |
cbridgha | 6308d66 | 2005-04-08 17:50:25 +0000 | [diff] [blame] | 188 | if (getDataModel().isPropertySet(PROJECT_LOCATION)) { |
cbridgha | 102ce4c | 2005-04-06 21:42:41 +0000 | [diff] [blame] | 189 | String loc = (String) getProperty(PROJECT_LOCATION); |
| 190 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
| 191 | IPath path = new Path(loc); |
| 192 | return workspace.validateProjectLocation(getProject(), path); |
| 193 | } |
| 194 | return OK_STATUS; |
| 195 | } |
jlanuti | 4df6c74 | 2005-12-01 21:54:48 +0000 | [diff] [blame] | 196 | } |