Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bbbac99f19ca56bb787ca7c3ee93b24ba6f9f649 (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
/*******************************************************************************
 * Copyright (c) 2007, 2010 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.core.internal.context.java;

import java.util.Iterator;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.core.context.BaseOverride;
import org.eclipse.jpt.core.context.java.JavaJpaContextNode;
import org.eclipse.jpt.core.context.java.JavaOverride;
import org.eclipse.jpt.core.internal.context.JptValidator;
import org.eclipse.jpt.core.internal.context.OverrideTextRangeResolver;
import org.eclipse.jpt.core.resource.java.OverrideAnnotation;
import org.eclipse.jpt.core.utility.TextRange;
import org.eclipse.jpt.utility.Filter;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.FilteringIterator;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;


public abstract class AbstractJavaOverride
	extends AbstractJavaJpaContextNode
	implements JavaOverride
{

	protected String name;

	protected final Owner owner;

	protected OverrideAnnotation overrideAnnotation;
	
	public AbstractJavaOverride(JavaJpaContextNode parent, Owner owner) {
		super(parent);
		this.owner = owner;
	}
	
	protected void initialize(OverrideAnnotation overrideAnnotation) {
		this.overrideAnnotation = overrideAnnotation;
		this.initializeName();
	}

	protected void update(OverrideAnnotation overrideResource) {
		this.overrideAnnotation = overrideResource;
		this.updateName();
	}

	public OverrideAnnotation getOverrideAnnotation() {
		return this.overrideAnnotation;
	}
	
	// ********************* name ****************
	public String getName() {
		return this.name;
	}

	public void setName(String newName) {
		String prefix = getOwner().getPossiblePrefix();
		String unprefixedName = newName;
		if (newName != null && prefix != null && newName.startsWith(prefix)) {
			unprefixedName = newName.substring(newName.indexOf('.') + 1);
		}
		String oldName = this.name;
		this.name = unprefixedName; //set the name without the prefix in the context model
		this.overrideAnnotation.setName(newName); // set the name with the prefix in the resource model
		firePropertyChanged(NAME_PROPERTY, oldName, unprefixedName);
	}
	
	protected void setName_(String newName) {
		String oldName = this.name;
		this.name = newName;
		firePropertyChanged(NAME_PROPERTY, oldName, newName);
	}

	protected void initializeName() {
		String name = this.getResourceName();
		String prefix = getOwner().getPossiblePrefix();
		if (name != null && prefix != null && name.startsWith(prefix)) {
			name = name.substring(name.indexOf('.') + 1);
		}
		this.name = name;
	}

	protected void updateName() {
		String name = this.getResourceName();
		String prefix = getOwner().getPossiblePrefix();
		if (name != null && prefix != null && name.startsWith(prefix)) {
			name = name.substring(name.indexOf('.') + 1);
		}
		this.setName_(name);
	}

	protected String getResourceName() {
		return this.overrideAnnotation.getName();
	}

	public boolean isVirtual() {
		return getOwner().isVirtual(this);
	}

	public BaseOverride setVirtual(boolean virtual) {
		return getOwner().setVirtual(virtual, this);
	}
	
	public Owner getOwner() {
		return this.owner;
	}
	
	@Override
	public JavaJpaContextNode getParent() {
		return (JavaJpaContextNode) super.getParent();
	}

	protected abstract Iterator<String> candidateNames();

	private Iterator<String> candidateNames(Filter<String> filter) {
		return new FilteringIterator<String>(this.candidateNames(), filter);
	}

	private Iterator<String> javaCandidateNames(Filter<String> filter) {
		return StringTools.convertToJavaStringLiterals(this.candidateNames(filter));
	}

	@Override
	public Iterator<String> javaCompletionProposals(int pos, Filter<String> filter, CompilationUnit astRoot) {
		Iterator<String> result = super.javaCompletionProposals(pos, filter, astRoot);
		if (result != null) {
			return result;
		}
		if (this.nameTouches(pos, astRoot)) {
			return this.javaCandidateNames(filter);
		}
		return null;
	}
	
	public boolean nameTouches(int pos, CompilationUnit astRoot) {
		return this.overrideAnnotation.nameTouches(pos, astRoot);
	}
	
	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		TextRange textRange = this.overrideAnnotation.getTextRange(astRoot);
		return (textRange != null) ? textRange : this.getParent().getValidationTextRange(astRoot);
	}

	public TextRange getNameTextRange(CompilationUnit astRoot) {
		TextRange textRange = this.overrideAnnotation.getNameTextRange(astRoot);
		return (textRange != null) ? textRange : this.getValidationTextRange(astRoot);
	}

	@Override
	public void toString(StringBuilder sb) {
		super.toString(sb);
		sb.append(getName());
	}


	// ********** validation **********
	
	@Override
	public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
		super.validate(messages, reporter, astRoot);
		this.buildValidator(astRoot).validate(messages, reporter);
	}

	protected JptValidator buildValidator(CompilationUnit astRoot) {
		return this.getOwner().buildValidator(this, buildTextRangeResolver(astRoot));
	}

	protected OverrideTextRangeResolver buildTextRangeResolver(CompilationUnit astRoot) {
		return new JavaOverrideTextRangeResolver(this, astRoot);
	}
}

Back to the top