Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9abd28d1d2a9bb836b4de7020dc6ecc61b538126 (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
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2006, 2016 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.HashMap;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jpt.common.ui.WidgetFactory;
import org.eclipse.jpt.common.ui.internal.swt.bindings.SWTBindingTools;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyAspectAdapter;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools;
import org.eclipse.jpt.common.utility.internal.predicate.CriterionPredicate;
import org.eclipse.jpt.common.utility.internal.transformer.AbstractTransformer;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.predicate.Predicate;
import org.eclipse.jpt.common.utility.transformer.Transformer;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.ui.JpaPlatformUi;
import org.eclipse.jpt.jpa.ui.details.JpaComposite;
import org.eclipse.jpt.jpa.ui.internal.plugin.JptJpaUiPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.part.PageBook;

public abstract class PersistentAttributeDetailsPageManager<A extends PersistentAttribute>
	extends AbstractJpaDetailsPageManager<A>
{
	private final HashMap<String, JpaComposite> mappingComposites = new HashMap<>();
	private PageBook mappingPageBook;

	private PropertyValueModel<AttributeMapping> mappingModel;
	
	protected PersistentAttributeDetailsPageManager(Composite parent, WidgetFactory widgetFactory, ResourceManager resourceManager) {
		super(parent, widgetFactory, resourceManager);
	}

	@Override
	protected void initialize() {
		super.initialize();
	}

	@Override
	protected void initializeLayout(Composite container) {
		this.mappingPageBook = this.buildMappingPageBook(container);
	}

	private PageBook buildMappingPageBook(Composite parent) {
		PageBook book = new PageBook(parent, SWT.NONE);

		GridData gridData = new GridData();
		gridData.horizontalAlignment       = SWT.FILL;
		gridData.verticalAlignment         = SWT.TOP;
		gridData.verticalIndent = 5;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace   = true;
		book.setLayoutData(gridData);
		
		this.mappingModel = this.buildMappingModel();
		SWTBindingTools.bind(this.mappingModel, this.buildPaneTransformer(), book);

		return book;
	}

	private Transformer<AttributeMapping, Control> buildPaneTransformer() {
		return new PaneTransformer();
	}

	protected class PaneTransformer
		extends AbstractTransformer<AttributeMapping, Control>
	{
		@Override
		public Control transform_(AttributeMapping attributeMapping) {
			return getMappingComposite(attributeMapping.getKey()).getControl();
		}
	}

	protected JpaComposite getMappingComposite(String key) {
		JpaComposite composite = this.mappingComposites.get(key);
		if (composite == null) {
			composite = this.buildMappingComposite(this.mappingPageBook, key);
			if (composite != null) {
				this.mappingComposites.put(key, composite);
			}
		}
		return composite;
	}

	protected JpaComposite buildMappingComposite(PageBook pageBook, String key) {
		JpaPlatformUi ui = this.getJpaPlatformUi();
		return (ui == null) ? null : ui.buildAttributeMappingComposite(
				this.getSubject().getResourceType(),
				key,
				pageBook,
				this.buildMappingModel(key),
				this.getMappingCompositeEnabledModel(),
				this.getWidgetFactory(),
				this.getResourceManager()
			);
	}

	protected abstract PropertyValueModel<Boolean> getMappingCompositeEnabledModel();

	private PropertyValueModel<AttributeMapping> buildMappingModel(String key) {
		return PropertyValueModelTools.filter_(this.mappingModel, this.buildKeyEquals(key));
	}

	private Predicate<AttributeMapping> buildKeyEquals(String mappingKey) {
		return new KeyEquals(mappingKey);
	}


	private ModifiablePropertyValueModel<AttributeMapping> buildMappingModel() {
		return new PropertyAspectAdapter<A, AttributeMapping>(
			getSubjectHolder(),
			PersistentAttribute.MAPPING_PROPERTY)
		{
			@Override
			protected AttributeMapping buildValue_() {
				return this.subject.getMapping();
			}
		};
	}

	@Override
	public void controlDisposed() {
		JptJpaUiPlugin.instance().trace(TRACE_OPTION, "dispose"); //$NON-NLS-1$

		this.mappingComposites.clear();
		super.controlDisposed();
	}

	private static final String TRACE_OPTION = PersistentAttributeDetailsPageManager.class.getSimpleName();

	private class KeyEquals
		extends CriterionPredicate<AttributeMapping, String>
	{
		KeyEquals(String mappingKey) {
			super(mappingKey);
		}
		public boolean evaluate(AttributeMapping mapping) {
			return ((mapping == null) || (this.criterion == null)) || this.criterion.equals(mapping.getKey());
		}
	}
}

Back to the top