blob: af86e3b55c3beb6b51390e4d82d677f475159b95 [file] [log] [blame]
/*******************************************************************************
* 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.jpa.core.internal.resource.java.source;
import java.util.Map;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.utility.TextRange;
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.common.core.utility.jdt.Member;
import org.eclipse.jpt.jpa.core.resource.java.JavaResourceNode;
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<Member>
implements OverrideAnnotation
{
DeclarationAnnotationElementAdapter<String> nameDeclarationAdapter;
AnnotationElementAdapter<String> nameAdapter;
String name;
SourceOverrideAnnotation(JavaResourceNode parent, Member member, DeclarationAnnotationAdapter daa, AnnotationAdapter annotationAdapter) {
super(parent, member, 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();
// ********** NestableAnnotation implementation **********
/**
* Convenience implementation of
* {@link org.eclipse.jpt.jpa.core.resource.java.NestableAnnotation#moveAnnotation(int)}
* used by subclasses.
*/
public void moveAnnotation(int index) {
this.getIndexedAnnotationAdapter().moveAnnotation(index);
}
// ********** misc **********
@Override
public boolean isUnset() {
return super.isUnset() &&
(this.name == null);
}
@Override
protected void rebuildAdapters() {
super.rebuildAdapters();
this.nameDeclarationAdapter = this.buildNameDeclarationAdapter();
this.nameAdapter = this.buildNameAdapter();
}
@Override
public void storeOn(Map<String, Object> map) {
super.storeOn(map);
map.put(NAME_PROPERTY, this.name);
this.name = null;
}
@Override
public void restoreFrom(Map<String, Object> map) {
super.restoreFrom(map);
this.setName((String) map.get(NAME_PROPERTY));
}
@Override
public void toString(StringBuilder sb) {
sb.append(this.name);
}
}