kmoore | c9c9e2b | 2011-02-06 02:07:28 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
bvosburgh | 91d39dc | 2011-04-28 02:00:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2009, 2011 Oracle. All rights reserved. |
kmoore | c9c9e2b | 2011-02-06 02:07:28 +0000 | [diff] [blame] | 3 | * This program and the accompanying materials are made available under the |
| 4 | * terms of the Eclipse Public License v1.0, which accompanies this distribution |
| 5 | * and is available at http://www.eclipse.org/legal/epl-v10.html. |
| 6 | * |
| 7 | * Contributors: |
| 8 | * Oracle - initial API and implementation |
| 9 | ******************************************************************************/ |
| 10 | package org.eclipse.jpt.jpa.core.internal.jpa2.context.java; |
| 11 | |
| 12 | import java.util.List; |
| 13 | import org.eclipse.jdt.core.dom.CompilationUnit; |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 14 | import org.eclipse.jpt.common.core.resource.java.JavaResourceAttribute; |
kmoore | c9c9e2b | 2011-02-06 02:07:28 +0000 | [diff] [blame] | 15 | import org.eclipse.jpt.common.core.utility.TextRange; |
| 16 | import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaJpaContextNode; |
| 17 | import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaDerivedIdentity2_0; |
| 18 | import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaIdDerivedIdentityStrategy2_0; |
| 19 | import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaSingleRelationshipMapping2_0; |
| 20 | import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation; |
kmoore | c9c9e2b | 2011-02-06 02:07:28 +0000 | [diff] [blame] | 21 | import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| 22 | import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| 23 | |
| 24 | public class GenericJavaIdDerivedIdentityStrategy2_0 |
| 25 | extends AbstractJavaJpaContextNode |
| 26 | implements JavaIdDerivedIdentityStrategy2_0 |
| 27 | { |
| 28 | protected boolean value; |
| 29 | |
| 30 | |
| 31 | public GenericJavaIdDerivedIdentityStrategy2_0(JavaDerivedIdentity2_0 parent) { |
| 32 | super(parent); |
| 33 | this.value = this.buildValue(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | // ********** synchronize/update ********** |
| 38 | |
| 39 | @Override |
| 40 | public void synchronizeWithResourceModel() { |
| 41 | super.synchronizeWithResourceModel(); |
| 42 | this.setValue_(this.buildValue()); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | // ********** value ********** |
| 47 | |
| 48 | public boolean getValue() { |
| 49 | return this.value; |
| 50 | } |
| 51 | |
| 52 | public void setValue(boolean value) { |
| 53 | if (value != this.value) { |
| 54 | if (value) { |
| 55 | this.addAnnotation(); |
| 56 | } else { |
| 57 | this.removeAnnotation(); |
| 58 | } |
| 59 | this.setValue_(value); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | protected void setValue_(boolean value) { |
| 64 | boolean old = this.value; |
| 65 | this.value = value; |
| 66 | this.firePropertyChanged(VALUE_PROPERTY, old, value); |
| 67 | } |
| 68 | |
| 69 | protected boolean buildValue() { |
| 70 | return this.getAnnotation() != null; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | // ********** annotation ********** |
| 75 | |
| 76 | protected IdAnnotation getAnnotation() { |
| 77 | return (IdAnnotation) this.getResourceAttribute().getAnnotation(this.getAnnotationName()); |
| 78 | } |
| 79 | |
| 80 | protected void addAnnotation() { |
| 81 | this.getResourceAttribute().addAnnotation(this.getAnnotationName()); |
| 82 | } |
| 83 | |
| 84 | protected void removeAnnotation() { |
| 85 | this.getResourceAttribute().removeAnnotation(this.getAnnotationName()); |
| 86 | } |
| 87 | |
| 88 | protected String getAnnotationName() { |
| 89 | return IdAnnotation.ANNOTATION_NAME; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // ********** misc ********** |
| 94 | |
| 95 | @Override |
| 96 | public JavaDerivedIdentity2_0 getParent() { |
| 97 | return (JavaDerivedIdentity2_0) super.getParent(); |
| 98 | } |
| 99 | |
| 100 | protected JavaDerivedIdentity2_0 getDerivedIdentity() { |
| 101 | return this.getParent(); |
| 102 | } |
| 103 | |
| 104 | protected JavaSingleRelationshipMapping2_0 getMapping() { |
| 105 | return this.getDerivedIdentity().getMapping(); |
| 106 | } |
| 107 | |
kmoore | c7a0867 | 2011-08-01 15:10:18 +0000 | [diff] [blame] | 108 | protected JavaResourceAttribute getResourceAttribute() { |
| 109 | return this.getMapping().getPersistentAttribute().getResourceAttribute(); |
kmoore | c9c9e2b | 2011-02-06 02:07:28 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public boolean isSpecified() { |
| 113 | return this.value; |
| 114 | } |
| 115 | |
| 116 | public void addStrategy() { |
| 117 | this.setValue(true); |
| 118 | } |
| 119 | |
| 120 | public void removeStrategy() { |
| 121 | this.setValue(false); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | // ********** validation ********** |
| 126 | |
| 127 | @Override |
| 128 | public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) { |
| 129 | super.validate(messages, reporter, astRoot); |
| 130 | // no validation rules |
| 131 | } |
| 132 | |
| 133 | public TextRange getValidationTextRange(CompilationUnit astRoot) { |
bvosburgh | 91d39dc | 2011-04-28 02:00:43 +0000 | [diff] [blame] | 134 | TextRange textRange = this.getAnnotationTextRange(astRoot); |
| 135 | return (textRange != null) ? textRange : this.getDerivedIdentity().getValidationTextRange(astRoot); |
| 136 | } |
| 137 | |
| 138 | protected TextRange getAnnotationTextRange(CompilationUnit astRoot) { |
kmoore | c9c9e2b | 2011-02-06 02:07:28 +0000 | [diff] [blame] | 139 | IdAnnotation annotation = this.getAnnotation(); |
| 140 | return (annotation == null) ? null : annotation.getTextRange(astRoot); |
| 141 | } |
| 142 | } |