blob: ccf044cd24fbe6bac0a9a473c29f3a634bcddf7e [file] [log] [blame]
jlanuti4df6c742005-12-01 21:54:48 +00001/*******************************************************************************
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
cbridgha102ce4c2005-04-06 21:42:41 +00005 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
jlanuti4df6c742005-12-01 21:54:48 +00007 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
cbridgha102ce4c2005-04-06 21:42:41 +000011/*
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 */
17package org.eclipse.wst.common.frameworks.internal.operations;
18
19import java.io.File;
cbridgha2013e562005-09-06 17:24:51 +000020import java.util.Set;
cbridgha102ce4c2005-04-06 21:42:41 +000021
vbhadrir0b2e4132005-08-23 19:46:44 +000022import org.eclipse.core.internal.resources.Resource;
cbridgha102ce4c2005-04-06 21:42:41 +000023import org.eclipse.core.resources.IProject;
24import org.eclipse.core.resources.IProjectDescription;
25import org.eclipse.core.resources.IResource;
26import org.eclipse.core.resources.IWorkspace;
27import org.eclipse.core.resources.ResourcesPlugin;
28import org.eclipse.core.runtime.IPath;
29import org.eclipse.core.runtime.IStatus;
30import org.eclipse.core.runtime.Path;
31import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider;
cbridgha102ce4c2005-04-06 21:42:41 +000032import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation;
cbridgha1239a3c2005-11-08 18:46:39 +000033import org.eclipse.wst.common.frameworks.internal.WTPPlugin;
cbridgha102ce4c2005-04-06 21:42:41 +000034import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonMessages;
35import org.eclipse.wst.common.frameworks.internal.plugin.WTPCommonPlugin;
36
cbridgha35febdc2005-11-07 16:10:38 +000037/**
38 * @deprecated use ProjectCreationDataModelProviderNew instead.
39 */
cbridgha102ce4c2005-04-06 21:42:41 +000040public class ProjectCreationDataModelProvider extends AbstractDataModelProvider implements IProjectCreationProperties {
41
42 public IDataModelOperation getDefaultOperation() {
vbhadrirbd9918f2005-06-03 14:34:11 +000043 return new ProjectCreationOperation(model);
cbridgha102ce4c2005-04-06 21:42:41 +000044 }
cbridgha35febdc2005-11-07 16:10:38 +000045
46 public void init() {
47 super.init();
48 }
49
cbridgha2013e562005-09-06 17:24:51 +000050 public Set getPropertyNames() {
51 Set propertyNames = super.getPropertyNames();
cbridgha3bfc2a82005-09-06 15:37:26 +000052 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;
cbridgha102ce4c2005-04-06 21:42:41 +000058 }
59
60 public Object getDefaultProperty(String propertyName) {
cbridgha35febdc2005-11-07 16:10:38 +000061 if (propertyName.equals(PROJECT_LOCATION))
62 return getDefaultLocation();
63 else if (propertyName.equals(PROJECT_DESCRIPTION))
64 return getProjectDescription();
cbridgha102ce4c2005-04-06 21:42:41 +000065 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))) {
vbhadrirc8fb50b2005-06-17 14:23:42 +000072 model.setProperty(propertyName, null);
73 }
cbridgha35febdc2005-11-07 16:10:38 +000074 model.setProperty(PROJECT_DESCRIPTION, getProjectDescription());
vbhadrirc8fb50b2005-06-17 14:23:42 +000075 } else if (propertyName.equals(PROJECT_NAME)) {
vbhadrir562d8792005-05-27 17:16:28 +000076 IStatus stat = model.validateProperty(PROJECT_NAME);
cbridgha35febdc2005-11-07 16:10:38 +000077 if (stat != OK_STATUS)
vbhadrir562d8792005-05-27 17:16:28 +000078 return false;
cbridgha35febdc2005-11-07 16:10:38 +000079 model.setProperty(PROJECT, getProject());
80 model.setProperty(PROJECT_DESCRIPTION, getProjectDescription());
cbridgha102ce4c2005-04-06 21:42:41 +000081 }
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);
cbridgha6308d662005-04-08 17:50:25 +0000100 if (getDataModel().isPropertySet(PROJECT_LOCATION)) {
vbhadrir0b2e4132005-08-23 19:46:44 +0000101 String projectLocation = (String) getProperty(IProjectCreationProperties.PROJECT_LOCATION);
cbridgha35febdc2005-11-07 16:10:38 +0000102 if (projectLocation != null)
103 desc.setLocation(new Path(projectLocation));
104 else
105 desc.setLocation(null);
cbridgha102ce4c2005-04-06 21:42:41 +0000106 }
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
cbridgha1239a3c2005-11-08 18:46:39 +0000176 if (!WTPPlugin.isPlatformCaseSensitive()) {
cbridgha102ce4c2005-04-06 21:42:41 +0000177 // now look for a matching case variant in the tree
vbhadrir0b2e4132005-08-23 19:46:44 +0000178 IResource variant = ((Resource) getProject()).findExistingResourceVariant(getProject().getFullPath());
cbridgha102ce4c2005-04-06 21:42:41 +0000179 if (variant != null) {
180 // TODO Fix this string
vbhadrir0b2e4132005-08-23 19:46:44 +0000181 return WTPCommonPlugin.createErrorStatus("Resource already exists with a different case."); //$NON-NLS-1$
cbridgha102ce4c2005-04-06 21:42:41 +0000182 }
183 }
184 return OK_STATUS;
185 }
186
187 private IStatus validateLocation() {
cbridgha6308d662005-04-08 17:50:25 +0000188 if (getDataModel().isPropertySet(PROJECT_LOCATION)) {
cbridgha102ce4c2005-04-06 21:42:41 +0000189 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 }
jlanuti4df6c742005-12-01 21:54:48 +0000196}