Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1cff97cf34fd6919b6491ccba58a6b1ce3cad64e (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2008, 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.eclipselink.ui.internal.details;

import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jpt.common.ui.internal.widgets.DialogPane;
import org.eclipse.jpt.common.ui.internal.widgets.ValidatingDialog;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.StaticListValueModel;
import org.eclipse.jpt.common.utility.internal.transformer.TransformerAdapter;
import org.eclipse.jpt.common.utility.model.value.ListValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.common.utility.transformer.Transformer;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkConverter;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkConverterContainer;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkCustomConverter;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkObjectTypeConverter;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkStructConverter;
import org.eclipse.jpt.jpa.eclipselink.core.context.EclipseLinkTypeConverter;
import org.eclipse.jpt.jpa.eclipselink.ui.details.JptJpaEclipseLinkUiDetailsMessages;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class EclipseLinkConverterDialog
	extends ValidatingDialog<EclipseLinkConverterStateObject>
{
	/**
	 * The associated converter container
	 */
	EclipseLinkConverterContainer converterContainer;
	
	// ********** constructors **********

	/**
	 * Use this constructor to edit an <em>existing</em> conversion value.
	 */
	public EclipseLinkConverterDialog(
			Shell parentShell,
			ResourceManager resourceManager,
			EclipseLinkConverterContainer converterContainer) {
		super(parentShell, resourceManager, JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTER_DIALOG_ADD_CONVERTER);
		this.converterContainer = converterContainer;
	}

	@Override
	protected EclipseLinkConverterStateObject buildStateObject() {
		return new EclipseLinkConverterStateObject(this.converterContainer);
	}

	// ********** open **********

	@Override
	protected String getDescriptionTitle() {
		return JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTER_DIALOG_ADD_CONVERTER_DESCRIPTION_TITLE;
	}
	
	@Override
	protected String getDescription() {
		return JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTER_DIALOG_ADD_CONVERTER_DESCRIPTION;
	}
	
	@Override
	protected DialogPane<EclipseLinkConverterStateObject> buildLayout(Composite container) {
		return new ConversionValueDialogPane(this.getSubjectHolder(), container, this.resourceManager);
	}
	
	@Override
	public void create() {
		super.create();

		ConversionValueDialogPane pane = (ConversionValueDialogPane) getPane();
		pane.selectAll();

		getButton(OK).setEnabled(false);
	}


	// ********** public API **********

	/**
	 * Return the data value set in the text widget.
	 */
	public String getName() {
		return getSubject().getName();
	}

	/**
	 * Return the object value set in the text widget.
	 */
	public Class<? extends EclipseLinkConverter> getConverterType() {
		return getSubject().getConverterType();
	}
	
	static class ConversionValueDialogPane
		extends DialogPane<EclipseLinkConverterStateObject>
	{
		private Text nameText;

		ConversionValueDialogPane(
				PropertyValueModel<EclipseLinkConverterStateObject> subjectModel,
				Composite parentComposite,
				ResourceManager resourceManager) {
			super(subjectModel, parentComposite, resourceManager);
		}

		@Override
		protected Composite addComposite(Composite container) {
			return this.addSubPane(container, 2, 0, 0, 0, 0);
		}

		@Override
		protected void initializeLayout(Composite container) {
			this.addLabel(container, JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTER_DIALOG_NAME);
			this.nameText = this.addText(container, buildNameHolder());
			
			this.addLabel(container, JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTER_DIALOG_CONVERTER_TYPE);
			this.addCombo(
				container, 
				buildConverterTypeListHolder(), 
				buildConverterTypeHolder(), 
				buildConverterTypeLabelTransformer(),
				(String) null);
		}

		protected ListValueModel<Class<? extends EclipseLinkConverter>> buildConverterTypeListHolder() {
			return new StaticListValueModel<>(CONVERTER_TYPES);
		}

		@SuppressWarnings("unchecked")
		private static final Class<? extends EclipseLinkConverter>[] CONVERTER_TYPES =
			new Class[] {
				EclipseLinkCustomConverter.class,
				EclipseLinkObjectTypeConverter.class,
				EclipseLinkStructConverter.class,
				EclipseLinkTypeConverter.class
			};
		
		private Transformer<Class<? extends EclipseLinkConverter>, String> buildConverterTypeLabelTransformer() {
			return new ConverterTypeLabelTransformer();
		}

		static class ConverterTypeLabelTransformer
			extends TransformerAdapter<Class<? extends EclipseLinkConverter>, String>
		{
			@Override
			public String transform(Class<? extends EclipseLinkConverter> value) {
				if (value == null) {
					return null;
				}
				if (value == EclipseLinkCustomConverter.class) {
					return JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTERS_COMPOSITE_CUSTOM_CONVERTER;
				}
				if (value == EclipseLinkObjectTypeConverter.class) {
					return JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTERS_COMPOSITE_OBJECT_TYPE_CONVERTER;
				}
				if (value == EclipseLinkStructConverter.class) {
					return JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTERS_COMPOSITE_STRUCT_CONVERTER;
				}
				if (value == EclipseLinkTypeConverter.class) {
					return JptJpaEclipseLinkUiDetailsMessages.ECLIPSELINK_CONVERTERS_COMPOSITE_TYPE_CONVERTER;
				}
				return value.getSimpleName();
			}
		}
		
		private ModifiablePropertyValueModel<String> buildNameHolder() {
			return PropertyValueModelTools.modifiableSubjectModelAspectAdapter(
					this.getSubjectHolder(),
					EclipseLinkConverterStateObject.NAME_PROPERTY,
					m -> m.getName(),
					(m, value) -> m.setName(value)
				);
		}

		private ModifiablePropertyValueModel<Class<? extends EclipseLinkConverter>> buildConverterTypeHolder() {
			return PropertyValueModelTools.modifiableSubjectModelAspectAdapter(
					this.getSubjectHolder(),
					EclipseLinkConverterStateObject.CONVERTER_TYPE_PROPERTY,
					m -> m.getConverterType(),
					(m, value) -> m.setConverterType(value)
				);
		}

		void selectAll() {
			this.nameText.selectAll();
		}
	}
}

Back to the top