Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a05a4c6263e4b4b86229b2d7e36a67c57580c70e (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
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2008, 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.ui.internal.details;

import java.util.Collection;
import org.eclipse.jpt.common.ui.internal.widgets.Pane;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.context.ReadOnlyPersistentAttribute;
import org.eclipse.jpt.jpa.ui.details.DefaultMappingUiDefinition;
import org.eclipse.jpt.jpa.ui.details.MappingUiDefinition;
import org.eclipse.jpt.jpa.ui.internal.details.orm.UnsupportedOrmAttributeMappingUiDefinition;
import org.eclipse.swt.widgets.Composite;

/**
 * This "Map As" composite is responsible for showing the mapping name and
 * mapping type for an attribute.
 */
public class PersistentAttributeMapAsComposite 
	extends MapAsComposite<ReadOnlyPersistentAttribute>
{
	/**
	 * Creates a new <code>PersistentAttributeMapAsComposite</code>.
	 *
	 * @param parentPane The parent pane of this one
	 * @param parent The parent container
	 */
	public PersistentAttributeMapAsComposite(
			Pane<? extends ReadOnlyPersistentAttribute> parentPane,
            Composite parent) {
		
		super(parentPane, parent);
	}

	public PersistentAttributeMapAsComposite(
			Pane<? extends ReadOnlyPersistentAttribute> parentPane,
	        Composite parent,
	        PropertyValueModel<Boolean> enabledModel) {

		super(parentPane, parent, enabledModel);
	}
	
	protected String getMappingKey() {
		return getSubject().getMappingKey();
	}

	@Override
	protected MappingChangeHandler buildMappingChangeHandler() {
		return new AttributeMappingChangeHandler();
	}

	protected class AttributeMappingChangeHandler
		implements MappingChangeHandler
	{
		public String getLabelText() {
			String mappingKey = getMappingKey();

			if (mappingKey != MappingKeys.NULL_ATTRIBUTE_MAPPING_KEY) {
				return JptUiDetailsMessages.MapAsComposite_mappedAttributeText;
			}
			if (getSubject().isVirtual()) {
				return JptUiDetailsMessages.MapAsComposite_virtualAttributeText;
			}

			return JptUiDetailsMessages.MapAsComposite_unmappedAttributeText;
		}

		public String getMappingText() {
			AttributeMapping mapping = getSubject().getMapping();
			String mappingKey = mapping.getKey();

			return mapping.isDefault() ? 
                          getDefaultDefinition(mappingKey).getLinkLabel() : 
                          getMappingUiDefinition().getLinkLabel();
		}
		
		public void morphMapping(MappingUiDefinition definition) {
			((PersistentAttribute) getSubject()).setMappingKey(definition.getKey());
		}
		
		public String getName() {
			return getSubject().getName();
		}
		
		public Iterable<? extends MappingUiDefinition<? extends ReadOnlyPersistentAttribute, ?>> getMappingUiDefinitions() {
			return getAttributeMappingUiDefinitions();
		}

		public MappingUiDefinition getMappingUiDefinition() {
			return getAttributeMappingUiDefinition();
		}
	}
	
	protected Iterable<? extends MappingUiDefinition<? extends ReadOnlyPersistentAttribute, ?>> getAttributeMappingUiDefinitions() {
		return getJpaPlatformUi().getAttributeMappingUiDefinitions(getSubject().getResourceType());
	}
	
	protected MappingUiDefinition<? extends ReadOnlyPersistentAttribute, ?> getAttributeMappingUiDefinition() {
		return getJpaPlatformUi().getAttributeMappingUiDefinition(getSubject().getResourceType(), getMappingKey());
	}
	
	@Override
	protected DefaultMappingUiDefinition getDefaultDefinition() {
		return getDefaultDefinition(getSubject().getDefaultMappingKey());
	}
	
	@Override
	protected DefaultMappingUiDefinition getDefaultDefinition(String mappingKey) {
		return getJpaPlatformUi().getDefaultAttributeMappingUiDefinition(getSubject().getMapping().getResourceType(), mappingKey);
	}

	@Override
	protected MappingUiDefinition getMappingUiDefinition() {
		MappingUiDefinition definition = super.getMappingUiDefinition();
		return (definition != null) ? definition : UnsupportedOrmAttributeMappingUiDefinition.instance();
	}
	
	@Override
	protected void addPropertyNames(Collection<String> propertyNames) {
		super.addPropertyNames(propertyNames);
		propertyNames.add(ReadOnlyPersistentAttribute.DEFAULT_MAPPING_KEY_PROPERTY);
		propertyNames.add(ReadOnlyPersistentAttribute.MAPPING_PROPERTY);
		propertyNames.add(ReadOnlyPersistentAttribute.NAME_PROPERTY);
	}

	@Override
	protected void propertyChanged(String propertyName) {
		super.propertyChanged(propertyName);

		if (propertyName == ReadOnlyPersistentAttribute.MAPPING_PROPERTY ||
		    propertyName == ReadOnlyPersistentAttribute.DEFAULT_MAPPING_KEY_PROPERTY   ||
		    propertyName == ReadOnlyPersistentAttribute.NAME_PROPERTY) {

			updateDescription();
		}
	}
}

Back to the top