Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 92275935dca1e32671f00abd6d59e712c704bcdc (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2010, 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.jaxb.ui.internal.wizards.schemagen;

import static org.eclipse.jpt.common.core.internal.operations.JptFileCreationDataModelProperties.*;
import static org.eclipse.jpt.jaxb.ui.internal.wizards.schemagen.SchemaGeneratorWizard.XSD_EXTENSION;
import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jpt.common.core.internal.utility.ProjectTools;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.io.FileTools;
import org.eclipse.jpt.jaxb.ui.JptJaxbUiMessages;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

/**
 *  NewSchemaFileWizardPage
 */
public class NewSchemaFileWizardPage extends WizardNewFileCreationPage {
	
	protected IDataModel dataModel; 

	private IStructuredSelection initialSelection;
	
	static public String DEFAULT_SCHEMA_NAME = "NewXMLSchema" + XSD_EXTENSION;   //$NON-NLS-1$
	
	// ********** constructor **********
	
	public NewSchemaFileWizardPage(String pageName, IStructuredSelection selection, IDataModel dataModel,
			String title, String description) {
		
		super(pageName, selection);
		this.initialSelection = selection;

		this.init(dataModel);
		this.setTitle(title);
		this.setDescription(description);
	}
	
	protected void init(IDataModel dataModel) {
		this.dataModel = dataModel;
		
		IProject project = this.getProjectFromInitialSelection();
		this.dataModel.setProperty(PROJECT, project);
		
		IPath containerPath = (IPath) this.dataModel.getProperty(CONTAINER_PATH);
		if (containerPath != null) {
			this.setContainerFullPath(containerPath);
		}
	}

	// ********** IDialogPage implementation  **********
    @Override
	public void createControl(Composite parent) {
    	super.createControl(parent);
    	
    	this.setAllowExistingResources(true);
		this.setFileName(DEFAULT_SCHEMA_NAME);
    }
	
	// ********** intra-wizard methods **********

	public IProject getProject() {
		return (IProject) this.dataModel.getProperty(PROJECT);
	}

	/**
	 * @return the path of the schema file relative to the project
	 */
	public IPath getFileRelativePath() {
		IPath filePath = this.getContainerFullPath();
    	String projectName = filePath.segment(0);

    	if(this.getProject().getName().equals(projectName)) {
    		filePath = filePath.removeFirstSegments(1);
    	}
    	return filePath;
	}

	// ********** validation **********
	
	@Override
	protected boolean validatePage() {
		this.dataModel.setProperty(PROJECT, this.getProjectNamed(this.getContainerName()));
		this.dataModel.setProperty(CONTAINER_PATH, this.getContainerFullPath());
		this.dataModel.setProperty(FILE_NAME, this.getFileName());

		boolean valid = super.validatePage();
		if( ! valid) {
			return valid;
		}
		this.overrideFileExistsWarning();
		
		//TODO move this validation to the dataModel - see MappingFileNewFileWizardPage
		// Validate Project
		valid = ProjectTools.isJavaProject(this.getProject());
		if( ! valid) {
			this.setErrorMessage(JptJaxbUiMessages.NEW_SCHEMA_FILE_WIZARD_PAGE_ERROR_NOT_JAVA_PROJECT);
			return valid;
		}
		// Validate XSD file not exists.
		valid = this.xsdFileNotExists();
		if( ! valid) {
			this.setMessage(JptJaxbUiMessages.NEW_SCHEMA_FILE_WIZARD_PAGE_OVERWRITE_EXISTING_SCHEMAS, IMessageProvider.WARNING);
			return true;
		}
		this.setErrorMessage(null);

		return valid;
	}

	// ********** internal methods **********

    private IProject getProjectFromInitialSelection() {
		Object firstElement = initialSelection.getFirstElement();
		if(firstElement instanceof IProject) {
			return (IProject)firstElement;
		}
		else if(firstElement instanceof IResource) {
			return ((IResource) firstElement).getProject();
		}
		else if(firstElement instanceof IJavaElement) {
			return ((IJavaElement)firstElement).getJavaProject().getProject();
		}
		return null;
    }

	private IProject getProjectNamed(String projectName) {
		if(StringTools.isBlank(projectName)) {
			return null;
		}
		return ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
	}
	
	private String getContainerName() {
		IPath containerPath = this.getContainerFullPath();
		if(containerPath == null) {
			return null;
		}
		String containerName = containerPath.segment(0);
		return containerName;
	}

	private void overrideFileExistsWarning() {
		String existsString= IDEWorkbenchMessages.ResourceGroup_nameExists;
		existsString.toString();
		
		existsString = existsString.substring("''{0}''".length(), existsString.length());    //$NON-NLS-1$
		String message = this.getMessage();
		if(message != null && message.endsWith(existsString)) { 
			this.setMessage(null);
		}
	}

	private boolean xsdFileNotExists() {
		
		return ! this.xsdFileExists(this.getContainerAbsolutePath().toFile());
	}
	
	private boolean xsdFileExists(File directory) {
		
		if(directory.listFiles() == null) {
			throw new RuntimeException("Could not find directory: " + directory);   //$NON-NLS-1$
		}
		for(File file : directory.listFiles()) {
			if (file.isDirectory()) {
				continue;
			}
			else if(XSD_EXTENSION.equalsIgnoreCase(FileTools.extension(file))) {
				return true;
			}
		}
		return false;
	}

	private IPath getContainerAbsolutePath() {
		IPath projectRelativePath = this.getContainerFullPath().makeRelativeTo(this.getProject().getFullPath());
		IResource directory = (projectRelativePath.isEmpty()) ?
				this.getProject() :
				this.getProject().getFile(projectRelativePath);
		return directory.getLocation();
	}

}

Back to the top