Skip to main content
summaryrefslogtreecommitdiffstats
blob: 429c3281faa38f4ccf98ea4e9b8afd27fc9b479f (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 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.ChangeTracking;
import org.eclipse.jpt.eclipselink.core.context.ChangeTrackingType;
import org.eclipse.jpt.eclipselink.core.internal.EclipseLinkJpaFactory;
import org.eclipse.jpt.eclipselink.core.resource.java.ChangeTrackingAnnotation;

public class EclipseLinkJavaChangeTracking extends AbstractJavaJpaContextNode implements ChangeTracking
{
	protected JavaResourcePersistentType resourcePersistentType;
	
	protected ChangeTrackingType specifiedType;
	
	
	public EclipseLinkJavaChangeTracking(JavaTypeMapping parent) {
		super(parent);
	}
	
	@Override
	public JavaTypeMapping getParent() {
		return (JavaTypeMapping) super.getParent();
	}
	
	@Override
	protected EclipseLinkJpaFactory getJpaFactory() {
		return (EclipseLinkJpaFactory) super.getJpaFactory();
	}
	
	protected String getChangeTrackingAnnotationName() {
		return ChangeTrackingAnnotation.ANNOTATION_NAME;
	}
	
	protected ChangeTrackingAnnotation getChangeTrackingAnnotation() {
		return (ChangeTrackingAnnotation) this.resourcePersistentType.getSupportingAnnotation(getChangeTrackingAnnotationName());
	}
	
	protected void addChangeTrackingAnnotation() {
		this.resourcePersistentType.addSupportingAnnotation(getChangeTrackingAnnotationName());
	}
	
	protected void removeChangeTrackingAnnotation() {
		this.resourcePersistentType.removeSupportingAnnotation(getChangeTrackingAnnotationName());
	}
	
	public ChangeTrackingType getType() {
		return (this.getSpecifiedType() != null) ? this.getSpecifiedType() : this.getDefaultType();
	}
	
	public ChangeTrackingType getDefaultType() {
		return DEFAULT_TYPE;
	}
	
	public ChangeTrackingType getSpecifiedType() {
		return this.specifiedType;
	}
	
	public void setSpecifiedType(ChangeTrackingType newSpecifiedType) {
		if (this.specifiedType == newSpecifiedType) {
			return;
		}
		
		ChangeTrackingType oldSpecifiedType = this.specifiedType;
		this.specifiedType = newSpecifiedType;
		
		if (newSpecifiedType != null) {
			if (getChangeTrackingAnnotation() == null) {
				addChangeTrackingAnnotation();
			}
			getChangeTrackingAnnotation().setValue(ChangeTrackingType.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_(ChangeTrackingType newSpecifiedType) {
		ChangeTrackingType oldSpecifiedType = this.specifiedType;
		this.specifiedType = newSpecifiedType;
		firePropertyChanged(SPECIFIED_TYPE_PROPERTY, oldSpecifiedType, newSpecifiedType);
	}
	
	public void initialize(JavaResourcePersistentType resourcePersistentType) {
		this.resourcePersistentType = resourcePersistentType;
		ChangeTrackingAnnotation changeTrackingAnnotation = this.getChangeTrackingAnnotation();
		this.specifiedType = changeTrackingType(changeTrackingAnnotation);
	}
	
	public void update(JavaResourcePersistentType resourcePersistentType) {
		this.resourcePersistentType = resourcePersistentType;
		ChangeTrackingAnnotation changeTrackingAnnotation = this.getChangeTrackingAnnotation();
		this.setSpecifiedType_(changeTrackingType(changeTrackingAnnotation));
	}
	
	protected ChangeTrackingType changeTrackingType(ChangeTrackingAnnotation changeTracking) {
		if (changeTracking == null) {
			return null;
		}
		else if (changeTracking.getValue() == null) {
			return ChangeTracking.DEFAULT_TYPE;
		}
		else {
			return ChangeTrackingType.fromJavaResourceModel(changeTracking.getValue());
		}
	}
	
	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		ChangeTrackingAnnotation changeTrackingAnnotation = getChangeTrackingAnnotation();
		TextRange textRange = changeTrackingAnnotation == null ? null : changeTrackingAnnotation.getTextRange(astRoot);
		return (textRange != null) ? textRange : this.getParent().getValidationTextRange(astRoot);
	}
}

Back to the top