Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1a8428fa9976c4e9bc3d30c408ab16f80c2d2f4f (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 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.eclipselink.core.internal.context.java;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.core.context.java.JavaTypeMapping;
import org.eclipse.jpt.core.internal.context.java.AbstractJavaJpaContextNode;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.core.utility.TextRange;
import org.eclipse.jpt.eclipselink.core.context.EclipseLinkChangeTracking;
import org.eclipse.jpt.eclipselink.core.context.EclipseLinkChangeTrackingType;
import org.eclipse.jpt.eclipselink.core.internal.EclipseLinkJpaFactory;
import org.eclipse.jpt.eclipselink.core.resource.java.EclipseLinkChangeTrackingAnnotation;

public class JavaEclipseLinkChangeTracking extends AbstractJavaJpaContextNode implements EclipseLinkChangeTracking
{
	protected JavaResourcePersistentType resourcePersistentType;
	
	protected EclipseLinkChangeTrackingType specifiedType;
	
	
	public JavaEclipseLinkChangeTracking(JavaTypeMapping parent) {
		super(parent);
	}
	
	@Override
	public JavaTypeMapping getParent() {
		return (JavaTypeMapping) super.getParent();
	}
	
	@Override
	protected EclipseLinkJpaFactory getJpaFactory() {
		return (EclipseLinkJpaFactory) super.getJpaFactory();
	}
	
	protected String getChangeTrackingAnnotationName() {
		return EclipseLinkChangeTrackingAnnotation.ANNOTATION_NAME;
	}
	
	protected EclipseLinkChangeTrackingAnnotation getChangeTrackingAnnotation() {
		return (EclipseLinkChangeTrackingAnnotation) this.resourcePersistentType.getAnnotation(getChangeTrackingAnnotationName());
	}
	
	protected void addChangeTrackingAnnotation() {
		this.resourcePersistentType.addAnnotation(getChangeTrackingAnnotationName());
	}
	
	protected void removeChangeTrackingAnnotation() {
		this.resourcePersistentType.removeAnnotation(getChangeTrackingAnnotationName());
	}
	
	public EclipseLinkChangeTrackingType getType() {
		return (this.getSpecifiedType() != null) ? this.getSpecifiedType() : this.getDefaultType();
	}
	
	public EclipseLinkChangeTrackingType getDefaultType() {
		return DEFAULT_TYPE;
	}
	
	public EclipseLinkChangeTrackingType getSpecifiedType() {
		return this.specifiedType;
	}
	
	public void setSpecifiedType(EclipseLinkChangeTrackingType newSpecifiedType) {
		if (this.specifiedType == newSpecifiedType) {
			return;
		}
		
		EclipseLinkChangeTrackingType oldSpecifiedType = this.specifiedType;
		this.specifiedType = newSpecifiedType;
		
		if (newSpecifiedType != null) {
			if (getChangeTrackingAnnotation() == null) {
				addChangeTrackingAnnotation();
			}
			getChangeTrackingAnnotation().setValue(EclipseLinkChangeTrackingType.toJavaResourceModel(newSpecifiedType));
		}
		else {
			if (getChangeTrackingAnnotation() != null) {
				removeChangeTrackingAnnotation();
			}
		}
		firePropertyChanged(SPECIFIED_TYPE_PROPERTY, oldSpecifiedType, newSpecifiedType);
	}
	
	/**
	 * internal setter used only for updating from the resource model.
	 * There were problems with InvalidThreadAccess exceptions in the UI
	 * when you set a value from the UI and the annotation doesn't exist yet.
	 * Adding the annotation causes an update to occur and then the exception.
	 */
	protected void setSpecifiedType_(EclipseLinkChangeTrackingType newSpecifiedType) {
		EclipseLinkChangeTrackingType oldSpecifiedType = this.specifiedType;
		this.specifiedType = newSpecifiedType;
		firePropertyChanged(SPECIFIED_TYPE_PROPERTY, oldSpecifiedType, newSpecifiedType);
	}
	
	public void initialize(JavaResourcePersistentType resourcePersistentType) {
		this.resourcePersistentType = resourcePersistentType;
		EclipseLinkChangeTrackingAnnotation changeTrackingAnnotation = this.getChangeTrackingAnnotation();
		this.specifiedType = changeTrackingType(changeTrackingAnnotation);
	}
	
	public void update(JavaResourcePersistentType resourcePersistentType) {
		this.resourcePersistentType = resourcePersistentType;
		EclipseLinkChangeTrackingAnnotation changeTrackingAnnotation = this.getChangeTrackingAnnotation();
		this.setSpecifiedType_(changeTrackingType(changeTrackingAnnotation));
	}
	
	protected EclipseLinkChangeTrackingType changeTrackingType(EclipseLinkChangeTrackingAnnotation changeTracking) {
		if (changeTracking == null) {
			return null;
		}
		else if (changeTracking.getValue() == null) {
			return EclipseLinkChangeTracking.DEFAULT_TYPE;
		}
		else {
			return EclipseLinkChangeTrackingType.fromJavaResourceModel(changeTracking.getValue());
		}
	}
	
	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		EclipseLinkChangeTrackingAnnotation changeTrackingAnnotation = getChangeTrackingAnnotation();
		TextRange textRange = changeTrackingAnnotation == null ? null : changeTrackingAnnotation.getTextRange(astRoot);
		return (textRange != null) ? textRange : this.getParent().getValidationTextRange(astRoot);
	}
}

Back to the top