Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9965f3a89ce1f9d451d0e42717f74d35c39476e7 (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
/*******************************************************************************
 * Copyright (c) 2007, 2015 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.jpa.core.internal.jpa1.context.persistence;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jpt.common.core.resource.ProjectResourceLocator;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.iterable.EmptyIterable;
import org.eclipse.jpt.common.utility.internal.iterable.SingleElementIterable;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpa.core.internal.context.persistence.AbstractOrmXmlRef;
import org.eclipse.jpt.jpa.core.resource.persistence.XmlMappingFileRef;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.ReplaceEdit;

/**
 * <code>persistence.xml</code> file
 * <br>
 * <code>mapping-file</code> element
 */
public class GenericOrmXmlRef
	extends AbstractOrmXmlRef
{
	protected final XmlMappingFileRef xmlMappingFileRef;


	public GenericOrmXmlRef(PersistenceUnit parent, XmlMappingFileRef xmlMappingFileRef) {
		super(parent, xmlMappingFileRef.getFileName());
		this.xmlMappingFileRef = xmlMappingFileRef;
	}


	// ********** synchronize/update **********

	@Override
	public void synchronizeWithResourceModel() {
		// set the file name *before* calling super
		this.setFileName_(this.xmlMappingFileRef.getFileName());
		super.synchronizeWithResourceModel();
	}


	// ********** file name **********

	public void setFileName(String fileName) {
		this.setFileName_(fileName);
		this.xmlMappingFileRef.setFileName(fileName);
	}

	protected void setFileName_(String xmlFileName) {
		String old = this.fileName;
		this.fileName = xmlFileName;
		if (this.firePropertyChanged(FILE_NAME_PROPERTY, old, xmlFileName)) {
			this.setMappingFile(null);
		}
	}


	// ********** misc **********

	public XmlMappingFileRef getXmlMappingFileRef() {
		return this.xmlMappingFileRef;
	}

	public boolean isDefault() {
		return false;
	}


	// ********** JpaStructureNode implementation **********

	public TextRange getFullTextRange() {
		return this.xmlMappingFileRef.getFullTextRange();
	}

	public boolean containsOffset(int textOffset) {
		return this.xmlMappingFileRef.containsOffset(textOffset);
	}

	public TextRange getSelectionTextRange() {
		return this.xmlMappingFileRef.getFileNameTextRange();
	}


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

	public TextRange getValidationTextRange() {
		TextRange textRange = this.getXmlValidationTextRange();
		return (textRange != null) ? textRange : this.getPersistenceUnit().getValidationTextRange();
	}

	protected TextRange getXmlValidationTextRange() {
		return this.xmlMappingFileRef.getFileNameTextRange();
	}


	// ********** refactoring **********

	public Iterable<DeleteEdit> createDeleteMappingFileEdits(IFile file) {
		return this.isFor(file) ?
				new SingleElementIterable<DeleteEdit>(this.createDeleteEdit()) :
				EmptyIterable.<DeleteEdit>instance();
	}

	protected DeleteEdit createDeleteEdit() {
		return this.xmlMappingFileRef.createDeleteEdit();
	}

	@Override
	protected ReplaceEdit createRenameEdit(IFile originalFile, String newName) {
		return this.xmlMappingFileRef.createRenameEdit(originalFile, newName);
	}

	public Iterable<ReplaceEdit> createRenameFolderEdits(IFolder originalFolder, String newName) {
		return this.isIn(originalFolder) ?
				new SingleElementIterable<ReplaceEdit>(this.createRenameFolderEdit(originalFolder, newName)) :
				EmptyIterable.<ReplaceEdit>instance();
	}

	protected ReplaceEdit createRenameFolderEdit(IFolder originalFolder, String newName) {
		return this.xmlMappingFileRef.createRenameFolderEdit(originalFolder, newName);
	}

	@Override
	protected ReplaceEdit createMoveEdit(IFile originalFile, IPath runtineDestination) {
		return this.xmlMappingFileRef.createMoveEdit(originalFile, runtineDestination);
	}

	public Iterable<ReplaceEdit> createMoveFolderEdits(IFolder originalFolder, IPath runtimeDestination) {
		return this.isIn(originalFolder) ?
				new SingleElementIterable<ReplaceEdit>(this.createMoveEdit(originalFolder, runtimeDestination)) :
				EmptyIterable.<ReplaceEdit>instance();
	}

	protected ReplaceEdit createMoveEdit(IFolder originalFolder, IPath runtimeDestination) {
		IProject project = originalFolder.getProject();
		IPath fullPath = originalFolder.getFullPath();
		ProjectResourceLocator locator = (ProjectResourceLocator) project.getAdapter(ProjectResourceLocator.class);
		IPath originalLocation = locator.getRuntimePath(fullPath);
		return this.createMoveEdit(originalLocation, runtimeDestination);
	}

	protected ReplaceEdit createMoveEdit(IPath originalLocation, IPath runtineDestination) {
		return this.xmlMappingFileRef.createMoveEdit(originalLocation, runtineDestination);
	}
}

Back to the top