blob: 0da397ff448210df4a11d927368ef506ee2fbb0c [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009, 2012 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.eclipselink.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.internal.utility.jdt.AnnotatedElementAnnotationElementAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.ConversionDeclarationAnnotationElementAdapter;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement;
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.AnnotationElementAdapter;
import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationElementAdapter;
import org.eclipse.jpt.common.core.utility.jdt.IndexedAnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.IndexedDeclarationAnnotationAdapter;
import org.eclipse.jpt.jpa.core.resource.java.OverrideAnnotation;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLinkNamedConverterAnnotation;
/**
* <code>
* <ul>
* <li>org.eclipse.persistence.annotations.Converter
* <li>org.eclipse.persistence.annotations.StructConverter
* <li>org.eclipse.persistence.annotations.TypeConverter
* <li>org.eclipse.persistence.annotations.ObjectTypeConverter
* </ul>
* </code>
*/
abstract class SourceEclipseLinkNamedConverterAnnotation
extends SourceAnnotation
implements EclipseLinkNamedConverterAnnotation
{
final DeclarationAnnotationElementAdapter<String> nameDeclarationAdapter;
final AnnotationElementAdapter<String> nameAdapter;
String name;
TextRange nameTextRange;
// ********** construction/initialization **********
SourceEclipseLinkNamedConverterAnnotation(
JavaResourceAnnotatedElement parent,
AnnotatedElement element,
IndexedDeclarationAnnotationAdapter daa,
IndexedAnnotationAdapter annotationAdapter) {
super(parent, element, daa, annotationAdapter);
this.nameDeclarationAdapter = ConversionDeclarationAnnotationElementAdapter.forStrings(daa, this.getNameElementName());
this.nameAdapter = new AnnotatedElementAnnotationElementAdapter<String>(this.annotatedElement, this.nameDeclarationAdapter);
}
public void initialize(CompilationUnit astRoot) {
this.name = this.buildName(astRoot);
this.nameTextRange = this.buildNameTextRange(astRoot);
}
public void synchronizeWith(CompilationUnit astRoot) {
this.syncName(this.buildName(astRoot));
this.nameTextRange = this.buildNameTextRange(astRoot);
}
@Override
public boolean isUnset() {
return super.isUnset() &&
(this.name == null);
}
@Override
public void toString(StringBuilder sb) {
sb.append(this.name);
}
// ********** NamedConverterAnnotation 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(OverrideAnnotation.NAME_PROPERTY, old, astName);
}
private String buildName(CompilationUnit astRoot) {
return this.nameAdapter.getValue(astRoot);
}
public TextRange getNameTextRange() {
return this.nameTextRange;
}
private TextRange buildNameTextRange(CompilationUnit astRoot) {
return this.getElementTextRange(this.nameDeclarationAdapter, astRoot);
}
public boolean nameTouches(int pos, CompilationUnit astRoot) {
return this.elementTouches(this.nameDeclarationAdapter, pos, astRoot);
}
abstract String getNameElementName();
}