Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a4225b845f26d614a515b63eda9956f918d8b6e (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
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2012, 2015 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.context.java;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.jpa.core.context.Generator;
import org.eclipse.jpt.jpa.core.internal.jpa1.context.java.GenericJavaGeneratorContainer;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkGeneratorContainer;
import org.eclipse.jpt.jpa.eclipselink.core.context.java.EclipseLinkJavaUuidGenerator;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.UuidGeneratorAnnotation2_4;

public class EclipseLinkJavaGeneratorContainer
	extends GenericJavaGeneratorContainer
	implements EclipseLinkGeneratorContainer
{
	protected EclipseLinkJavaUuidGenerator uuidGenerator;


	public EclipseLinkJavaGeneratorContainer(Parent parentAdapter) {
		super(parentAdapter);
		this.uuidGenerator = this.buildUuidGenerator();
	}


	// ********** synchronize/update **********

	@Override
	public void synchronizeWithResourceModel() {
		super.synchronizeWithResourceModel();
		this.syncUuidGenerator();
	}

	@Override
	public void update(IProgressMonitor monitor) {
		super.update(monitor);
		if (this.uuidGenerator != null) {
			this.uuidGenerator.update(monitor);
		}
	}


	// ********** Uuid generator **********

	public EclipseLinkJavaUuidGenerator getUuidGenerator() {
		return this.uuidGenerator;
	}

	public EclipseLinkJavaUuidGenerator addUuidGenerator() {
		if (this.uuidGenerator != null) {
			throw new IllegalStateException("UUID generator already exists: " + this.uuidGenerator); //$NON-NLS-1$
		}
		UuidGeneratorAnnotation2_4 annotation = this.buildUuidGeneratorAnnotation();
		EclipseLinkJavaUuidGenerator generator = this.buildUuidGenerator(annotation);
		this.setUuidGenerator(generator);
		return generator;
	}

	protected UuidGeneratorAnnotation2_4 buildUuidGeneratorAnnotation() {
		return (UuidGeneratorAnnotation2_4) this.parent.getResourceAnnotatedElement().addAnnotation(UuidGeneratorAnnotation2_4.ANNOTATION_NAME);
	}

	public void removeUuidGenerator() {
		if (this.uuidGenerator == null) {
			throw new IllegalStateException("UUID generator does not exist"); //$NON-NLS-1$
		}
		this.parent.getResourceAnnotatedElement().removeAnnotation(UuidGeneratorAnnotation2_4.ANNOTATION_NAME);
		this.setUuidGenerator(null);
	}

	protected EclipseLinkJavaUuidGenerator buildUuidGenerator() {
		UuidGeneratorAnnotation2_4 annotation = this.getUuidGeneratorAnnotation();
		return (annotation == null) ? null : this.buildUuidGenerator(annotation);
	}

	protected UuidGeneratorAnnotation2_4 getUuidGeneratorAnnotation() {
		return (UuidGeneratorAnnotation2_4) this.parent.getResourceAnnotatedElement().getAnnotation(UuidGeneratorAnnotation2_4.ANNOTATION_NAME);
	}

	protected EclipseLinkJavaUuidGenerator buildUuidGenerator(UuidGeneratorAnnotation2_4 uuidGeneratorAnnotation) {
		return this.parent.supportsGenerators() ?
				new EclipseLinkJavaUuidGeneratorImpl(this, uuidGeneratorAnnotation) :
				null;
	}

	protected void syncUuidGenerator() {
		UuidGeneratorAnnotation2_4 annotation = this.getUuidGeneratorAnnotation();
		if (annotation == null) {
			if (this.uuidGenerator != null) {
				this.setUuidGenerator(null);
			}
		} else {
			if ((this.uuidGenerator != null) && (this.uuidGenerator.getGeneratorAnnotation() == annotation)) {
				this.uuidGenerator.synchronizeWithResourceModel();
			} else {
				this.setUuidGenerator(this.buildUuidGenerator(annotation));
			}
		}
	}

	protected void setUuidGenerator(EclipseLinkJavaUuidGenerator uuidGenerator) {
		EclipseLinkJavaUuidGenerator old = this.uuidGenerator;
		this.uuidGenerator = uuidGenerator;
		this.firePropertyChanged(UUID_GENERATOR_PROPERTY, old, uuidGenerator);
	}


	// ********** code completion **********

	@Override
	public Iterable<String> getCompletionProposals(int pos) {
		Iterable<String> result = super.getCompletionProposals(pos);
		if (result != null) {
			return result;
		}
		if (this.uuidGenerator != null) {
			result = this.uuidGenerator.getCompletionProposals(pos);
			if (result != null) {
				return result;
			}
		}
		return null;
	}


	// ********** misc **********

	@Override
	protected Iterable<Generator> getGenerators_() {
		return IterableTools.<Generator>iterable(this.sequenceGenerator, this.tableGenerator, this.uuidGenerator);
	}
}

Back to the top