/******************************************************************************* * Copyright (c) 2007, 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.ui.internal.mappings.details; import java.util.Collection; import java.util.Iterator; import org.eclipse.jpt.core.internal.context.base.INonOwningMapping; import org.eclipse.jpt.ui.internal.IJpaHelpContextIds; import org.eclipse.jpt.ui.internal.mappings.JptUiMappingsMessages; import org.eclipse.jpt.ui.internal.widgets.AbstractFormPane; import org.eclipse.jpt.ui.internal.widgets.IWidgetFactory; import org.eclipse.jpt.utility.internal.CollectionTools; import org.eclipse.jpt.utility.internal.StringTools; import org.eclipse.jpt.utility.internal.model.value.PropertyValueModel; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; /** * Here the layout of this pane: *
 * -----------------------------------------------------------------------------
 * |            -------------------------------------------------------------- |
 * | Mapped By: |                                                          |v| |
 * |            -------------------------------------------------------------- |
 * -----------------------------------------------------------------------------
* * @see INonOwningMapping * @see ManyToManyMappingComposite - A container of this pane * @see OneToManyMappingComposite - A container of this pane * @see OneToOneMappingComposite - A container of this pane * * @version 2.0 * @since 1.0 */ @SuppressWarnings("nls") public class MappedByComposite extends AbstractFormPane { private CCombo combo; /** * Creates a new MappedByComposite. * * @param parentPane The parent container of this one * @param parent The parent container */ public MappedByComposite(AbstractFormPane parentPane, Composite parent) { super(parentPane, parent); } /** * Creates a new MappedByComposite. * * @param subjectHolder The holder of the subject INonOwningMapping * @param parent The parent container * @param widgetFactory The factory used to create various common widgets */ public MappedByComposite(PropertyValueModel subjectHolder, Composite parent, IWidgetFactory widgetFactory) { super(subjectHolder, parent, widgetFactory); } /* * (non-Javadoc) */ @Override protected void addPropertyNames(Collection propertyNames) { super.addPropertyNames(propertyNames); propertyNames.add(INonOwningMapping.MAPPED_BY_PROPERTY); } private ModifyListener buildComboModifyListener() { return new ModifyListener() { public void modifyText(ModifyEvent e) { if (!isPopulating()) { CCombo combo = (CCombo) e.widget; valueChanged(combo.getText()); } } }; } private FocusListener buildFocusListener() { return new FocusListener() { public void focusGained(FocusEvent e) { setPopulating(true); try { if (combo.getSelectionIndex() == 0) { combo.setText(""); } } finally { setPopulating(false); } } public void focusLost(FocusEvent e) { setPopulating(true); try { if (combo.getText().length() == 0) { combo.select(0); } } finally { setPopulating(false); } } }; } /* * (non-Javadoc) */ @Override protected void doPopulate() { super.doPopulate(); populateCombo(); } /* * (non-Javadoc) */ @Override protected void initializeLayout(Composite container) { combo = buildLabeledEditableCCombo( container, JptUiMappingsMessages.NonOwningMapping_mappedByLabel, buildComboModifyListener(), IJpaHelpContextIds.MAPPING_MAPPED_BY ); combo.addFocusListener(buildFocusListener()); } private void populateCombo() { combo.removeAll(); combo.add(JptUiMappingsMessages.NoneSelected); INonOwningMapping subject = subject(); if (subject != null) { Iterator iter = subject.candidateMappedByAttributeNames(); for (iter = CollectionTools.sort(iter); iter.hasNext(); ) { combo.add(iter.next()); } } updateSelectedItem(); } /* * (non-Javadoc) */ @Override protected void propertyChanged(String propertyName) { super.propertyChanged(propertyName); if (propertyName == INonOwningMapping.MAPPED_BY_PROPERTY) { populateCombo(); } } /** * Updates the selected item by selected the current value, if not * null, or select the default value if one is available, * otherwise remove the selection. *

* Note: It seems the text can be shown as truncated, changing the * selection to (0, 0) makes the entire text visible. */ private void updateSelectedItem() { INonOwningMapping subject = subject(); String value = (subject != null) ? subject.getMappedBy() : null; if (value != null) { combo.setText(value); } else { combo.select(0); } combo.setSelection(new Point(0, 0)); } private void valueChanged(String value) { INonOwningMapping subject = subject(); String oldValue = (subject != null) ? subject.getMappedBy() : null; // Check for null value if (StringTools.stringIsEmpty(value)) { value = null; if (StringTools.stringIsEmpty(oldValue)) { return; } } // The default value if (value == JptUiMappingsMessages.NoneSelected) { value = null; } // Nothing to change if ((oldValue == value) && value == null) { return; } // Set the new value if ((value != null) && (oldValue == null) || ((oldValue != null) && !oldValue.equals(value))) { setPopulating(true); try { subject.setMappedBy(value); } finally { setPopulating(false); } } } }