blob: e48badcd00c7c0dca3fe619cdff15f83b8976bd3 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2011 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.resource.java.source;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.internal.resource.java.source.SourceAnnotation;
import org.eclipse.jpt.common.core.resource.java.JavaResourceNode;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.core.utility.jdt.AnnotatedElement;
import org.eclipse.jpt.common.core.utility.jdt.AnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.AnnotationElementAdapter;
import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationElementAdapter;
import org.eclipse.jpt.jpa.core.resource.java.OverrideAnnotation;
/**
* <ul>
* <li><code>javax.persistence.AttributeOverride</code>
* <li><code>javax.persistence.AssociationOverride</code>
* </ul>
*/
abstract class SourceOverrideAnnotation
extends SourceAnnotation
implements OverrideAnnotation
{
DeclarationAnnotationElementAdapter<String> nameDeclarationAdapter;
AnnotationElementAdapter<String> nameAdapter;
String name;
SourceOverrideAnnotation(JavaResourceNode parent, AnnotatedElement element, DeclarationAnnotationAdapter daa, AnnotationAdapter annotationAdapter) {
super(parent, element, daa, annotationAdapter);
this.nameDeclarationAdapter = this.buildNameDeclarationAdapter();
this.nameAdapter = this.buildNameAdapter();
}
public void initialize(CompilationUnit astRoot) {
this.name = this.buildName(astRoot);
}
public void synchronizeWith(CompilationUnit astRoot) {
this.syncName(this.buildName(astRoot));
}
// ********** OverrideAnnotation implementation **********
// ***** name
public String getName() {
return this.name;
}
public void setName(String name) {
if (this.attributeValueHasChanged(this.name, name)) {
this.name = name;
this.nameAdapter.setValue(name);
}
}
private void syncName(String astName) {
String old = this.name;
this.name = astName;
this.firePropertyChanged(NAME_PROPERTY, old, astName);
}
private String buildName(CompilationUnit astRoot) {
return this.nameAdapter.getValue(astRoot);
}
public TextRange getNameTextRange(CompilationUnit astRoot) {
return this.getElementTextRange(this.nameDeclarationAdapter, astRoot);
}
public boolean nameTouches(int pos, CompilationUnit astRoot) {
return this.elementTouches(this.nameDeclarationAdapter, pos, astRoot);
}
private DeclarationAnnotationElementAdapter<String> buildNameDeclarationAdapter() {
return this.buildStringElementAdapter(this.getNameElementName());
}
private AnnotationElementAdapter<String> buildNameAdapter() {
return this.buildStringElementAdapter(this.nameDeclarationAdapter);
}
protected abstract String getNameElementName();
// ********** misc **********
@Override
public boolean isUnset() {
return super.isUnset() &&
(this.name == null);
}
@Override
public void toString(StringBuilder sb) {
sb.append(this.name);
}
}