Skip to main content
summaryrefslogtreecommitdiffstats
blob: 44ea96a5413ea16b7835edf670117b8824bd33b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * Copyright (c) 2009, 2013 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.core.internal.operations;

import java.util.Set;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jpt.common.core.JptCommonCoreMessages;
import org.eclipse.jpt.common.core.internal.plugin.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.internal.utility.PathTools;
import org.eclipse.jpt.common.core.resource.ProjectResourceLocator;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider;

public abstract class AbstractJptFileCreationDataModelProvider
	extends AbstractDataModelProvider
	implements JptFileCreationDataModelProperties
{
	protected AbstractJptFileCreationDataModelProvider() {
		super();
	}


	@Override
	public Set<String> getPropertyNames() {
		@SuppressWarnings("unchecked")
		Set<String> propertyNames = super.getPropertyNames();
		propertyNames.add(PROJECT);
		propertyNames.add(CONTAINER_PATH);
		propertyNames.add(FILE_NAME);
		return propertyNames;
	}

	@Override
	public Object getDefaultProperty(String propertyName) {
		if (propertyName.equals(CONTAINER_PATH)) {
			IContainer sourceLocation = getDefaultContainer();
			if (sourceLocation != null && sourceLocation.exists()) {
				return sourceLocation.getFullPath();
			}
		}
		else if (propertyName.equals(FILE_NAME)) {
			return getDefaultFileName();
		}
		return super.getDefaultProperty(propertyName);
	}

	protected abstract String getDefaultFileName();


	// **************** validation *********************************************

	@Override
	public IStatus validate(String propertyName) {
		IStatus status = Status.OK_STATUS;
		if (propertyName.equals(CONTAINER_PATH)
				|| propertyName.equals(FILE_NAME)) {
			status = validateContainerPathAndFileName();
		}
		if (! status.isOK()) {
			return status;
		}
		
		return status;
	}

	protected IStatus validateContainerPathAndFileName() {
		IContainer container = getContainer();
		if (container == null) {
			// verifies container has been specified, but should be unnecessary in most cases.
			// there is almost always a default, and the new file wizard does this validation as well.
			return JptCommonCorePlugin.instance().buildErrorStatus(JptCommonCoreMessages.VALIDATE_CONTAINER_NOT_SPECIFIED);
		}
		String fileName = getStringProperty(FILE_NAME);
		if (StringTools.isBlank(fileName)) {
			// verifies file name has been specified, but should be unnecessary in most cases.
			// there is almost always a default, and the new file wizard does this validation as well.
			return JptCommonCorePlugin.instance().buildErrorStatus(JptCommonCoreMessages.VALIDATE_FILE_NAME_NOT_SPECIFIED);
		}
		if (container.getFile(new Path(fileName)).exists()) {
			// verifies file does not exist, but should be unnecessary in most cases.
			// the new file wizard does this validation as well.
			return JptCommonCorePlugin.instance().buildErrorStatus(JptCommonCoreMessages.VALIDATE_FILE_ALREADY_EXISTS);
		}
		return Status.OK_STATUS;
	}


	// **************** helper methods *****************************************

	protected IPath getContainerPath() {
		return (IPath) this.model.getProperty(CONTAINER_PATH);
	}

	protected IContainer getContainer() {
		IPath containerPath = getContainerPath();
		return (containerPath == null) ? null : PathTools.getContainer(containerPath);
	}

	protected IProject getProject() {
		IContainer container = this.getContainer();
		return (container == null) ? null : container.getProject();
	}

	/**
	 * Return a best guess source location for the for the specified project
	 */
	protected IContainer getDefaultContainer() {
		IProject project = (IProject) this.model.getProperty(PROJECT);
		if (project != null) {
			ProjectResourceLocator locator = (ProjectResourceLocator) project.getAdapter(ProjectResourceLocator.class);
			return locator.getDefaultLocation();
		}
		return null;
	}
}

Back to the top