| /******************************************************************************* |
| * Copyright (c) 2008 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.eclipselink.ui.internal.orm.details; |
| |
| import java.util.ArrayList; |
| import java.util.ListIterator; |
| import org.eclipse.jpt.eclipselink.core.context.Convert; |
| import org.eclipse.jpt.eclipselink.core.internal.context.persistence.EclipseLinkPersistenceUnit; |
| import org.eclipse.jpt.eclipselink.ui.internal.mappings.EclipseLinkUiMappingsMessages; |
| import org.eclipse.jpt.ui.WidgetFactory; |
| import org.eclipse.jpt.ui.internal.mappings.JptUiMappingsMessages; |
| import org.eclipse.jpt.ui.internal.util.PaneEnabler; |
| import org.eclipse.jpt.ui.internal.util.SWTUtil; |
| import org.eclipse.jpt.ui.internal.widgets.FormPane; |
| import org.eclipse.jpt.utility.internal.CollectionTools; |
| import org.eclipse.jpt.utility.internal.StringConverter; |
| import org.eclipse.jpt.utility.internal.model.value.CompositeListValueModel; |
| import org.eclipse.jpt.utility.internal.model.value.ListAspectAdapter; |
| import org.eclipse.jpt.utility.internal.model.value.PropertyAspectAdapter; |
| import org.eclipse.jpt.utility.internal.model.value.PropertyListValueModelAdapter; |
| import org.eclipse.jpt.utility.internal.model.value.StaticListValueModel; |
| import org.eclipse.jpt.utility.internal.model.value.TransformationPropertyValueModel; |
| import org.eclipse.jpt.utility.model.value.ListValueModel; |
| import org.eclipse.jpt.utility.model.value.PropertyValueModel; |
| import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel; |
| import org.eclipse.osgi.util.NLS; |
| import org.eclipse.swt.custom.CCombo; |
| import org.eclipse.swt.widgets.Composite; |
| |
| /** |
| * Here the layout of this pane: |
| * <pre> |
| * ----------------------------------------------------------------------------- |
| * | -------------------------------------------------------------- | |
| * | Converter: | |v| | |
| * | -------------------------------------------------------------- | |
| * -----------------------------------------------------------------------------</pre> |
| * |
| * @see EclipseLinkConvert |
| * @see EclipseLinkBasicMappingComposite - A container of this widget |
| * |
| * @version 2.1 |
| * @since 2.1 |
| */ |
| |
| //Temporary to remove the Define Converters section from orm basic, id, version mapping. |
| //This is supported in EclipseLink in version 1.1, but not 1.0 |
| public class OrmConvertComposite extends FormPane<Convert> |
| { |
| |
| /** |
| * A key used to represent the default value, this is required to convert |
| * the selected item from a combo to <code>null</code>. This key is most |
| * likely never typed the user and it will help to convert the value to |
| * <code>null</code> when it's time to set the new selected value into the |
| * model. |
| */ |
| protected static String DEFAULT_KEY = "?!#!?#?#?default?#?!#?!#?"; //$NON-NLS-1$ |
| |
| |
| /** |
| * Creates a new <code>EnumTypeComposite</code>. |
| * |
| * @param parentPane The parent container of this one |
| * @param parent The parent container |
| */ |
| public OrmConvertComposite(PropertyValueModel<? extends Convert> subjectHolder, |
| Composite parent, |
| WidgetFactory widgetFactory) { |
| |
| super(subjectHolder, parent, widgetFactory); |
| } |
| |
| @Override |
| protected void initializeLayout(Composite container) { |
| CCombo combo = addLabeledEditableCCombo( |
| container, |
| EclipseLinkUiMappingsMessages.ConvertComposite_converterNameLabel, |
| buildConvertNameListHolder(), |
| buildConvertNameHolder(), |
| buildNameConverter(), |
| null |
| ); |
| SWTUtil.attachDefaultValueHandler(combo); |
| |
| new PaneEnabler(buildBooleanHolder(), this); |
| } |
| |
| protected final WritablePropertyValueModel<String> buildConvertNameHolder() { |
| return new PropertyAspectAdapter<Convert, String>(getSubjectHolder(), Convert.SPECIFIED_CONVERTER_NAME_PROPERTY) { |
| @Override |
| protected String buildValue_() { |
| return this.subject.getSpecifiedConverterName(); |
| } |
| |
| @Override |
| protected void setValue_(String value) { |
| // Convert the default value or an empty string to null |
| if ((value != null) && |
| ((value.length() == 0) || value.startsWith(DEFAULT_KEY))) { |
| |
| value = null; |
| } |
| this.subject.setSpecifiedConverterName(value); |
| } |
| }; |
| } |
| private ListValueModel<String> buildConvertNameListHolder() { |
| java.util.List<ListValueModel<String>> list = new ArrayList<ListValueModel<String>>(); |
| list.add(buildDefaultNameListHolder()); |
| list.add(buildReservedConverterNameListHolder()); |
| list.add(buildConverterNameListHolder()); |
| return new CompositeListValueModel<ListValueModel<String>, String>(list); |
| } |
| |
| protected ListValueModel<String> buildDefaultNameListHolder() { |
| return new PropertyListValueModelAdapter<String>( |
| buildDefaultNameHolder() |
| ); |
| } |
| |
| private WritablePropertyValueModel<String> buildDefaultNameHolder() { |
| return new PropertyAspectAdapter<Convert, String>(getSubjectHolder(), Convert.DEFAULT_CONVERTER_NAME_PROPERTY) { |
| @Override |
| protected String buildValue_() { |
| String name = this.subject.getDefaultConverterName(); |
| |
| if (name == null) { |
| name = DEFAULT_KEY; |
| } |
| else { |
| name = DEFAULT_KEY + name; |
| } |
| |
| return name; |
| } |
| }; |
| } |
| |
| private StringConverter<String> buildNameConverter() { |
| return new StringConverter<String>() { |
| public String convertToString(String value) { |
| |
| if (getSubject() == null) { |
| return value; |
| } |
| |
| if (value == null) { |
| value = getSubject().getDefaultConverterName(); |
| |
| if (value != null) { |
| value = DEFAULT_KEY + value; |
| } |
| else { |
| value = DEFAULT_KEY; |
| } |
| } |
| |
| if (value.startsWith(DEFAULT_KEY)) { |
| String defaultName = value.substring(DEFAULT_KEY.length()); |
| |
| if (defaultName.length() > 0) { |
| value = NLS.bind( |
| JptUiMappingsMessages.DefaultWithValue, |
| defaultName |
| ); |
| } |
| else { |
| value = JptUiMappingsMessages.DefaultWithoutValue; |
| } |
| } |
| |
| return value; |
| } |
| }; |
| } |
| |
| protected ListValueModel<String> buildReservedConverterNameListHolder() { |
| return new StaticListValueModel<String>(CollectionTools.list(Convert.RESERVED_CONVERTER_NAMES)); |
| } |
| |
| protected ListValueModel<String> buildConverterNameListHolder() { |
| return new ListAspectAdapter<EclipseLinkPersistenceUnit, String>( |
| buildPersistenceUnitHolder(), |
| EclipseLinkPersistenceUnit.CONVERTERS_LIST)//TODO need EclipseLinkPersistenceUnit interface |
| { |
| @Override |
| protected ListIterator<String> listIterator_() { |
| return CollectionTools.listIterator(CollectionTools.sort(this.subject.uniqueConverterNames())); |
| } |
| }; |
| } |
| |
| protected PropertyValueModel<EclipseLinkPersistenceUnit> buildPersistenceUnitHolder() { |
| return new PropertyAspectAdapter<Convert, EclipseLinkPersistenceUnit>(getSubjectHolder()) { |
| @Override |
| protected EclipseLinkPersistenceUnit buildValue_() { |
| return (EclipseLinkPersistenceUnit) getSubject().getPersistenceUnit(); |
| } |
| }; |
| } |
| |
| protected PropertyValueModel<Boolean> buildBooleanHolder() { |
| return new TransformationPropertyValueModel<Convert, Boolean>(getSubjectHolder()) { |
| @Override |
| protected Boolean transform(Convert value) { |
| if (getSubject() != null && getSubject().getParent().getPersistentAttribute().isVirtual()) { |
| return Boolean.FALSE; |
| } |
| return Boolean.valueOf(value != null); |
| } |
| }; |
| } |
| } |