| /******************************************************************************* |
| * Copyright (c) 2006, 2011 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.ArrayList; |
| import java.util.List; |
| import org.eclipse.jface.viewers.ILabelProvider; |
| import org.eclipse.jface.viewers.LabelProvider; |
| import org.eclipse.jpt.common.ui.internal.util.PaneEnabler; |
| import org.eclipse.jpt.common.ui.internal.widgets.AddRemoveListPane; |
| import org.eclipse.jpt.common.ui.internal.widgets.Pane; |
| import org.eclipse.jpt.common.ui.internal.widgets.PostExecution; |
| import org.eclipse.jpt.common.ui.internal.widgets.AddRemovePane.AbstractAdapter; |
| import org.eclipse.jpt.common.ui.internal.widgets.AddRemovePane.Adapter; |
| import org.eclipse.jpt.common.utility.internal.iterables.ListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.SuperListIterableWrapper; |
| import org.eclipse.jpt.common.utility.internal.model.value.CompositeListValueModel; |
| import org.eclipse.jpt.common.utility.internal.model.value.ItemPropertyListValueModelAdapter; |
| import org.eclipse.jpt.common.utility.internal.model.value.ListAspectAdapter; |
| import org.eclipse.jpt.common.utility.internal.model.value.ListPropertyValueModelAdapter; |
| import org.eclipse.jpt.common.utility.internal.model.value.SimplePropertyValueModel; |
| import org.eclipse.jpt.common.utility.internal.model.value.swing.ObjectListSelectionModel; |
| import org.eclipse.jpt.common.utility.model.value.ListValueModel; |
| import org.eclipse.jpt.common.utility.model.value.WritablePropertyValueModel; |
| import org.eclipse.jpt.jpa.core.context.Entity; |
| import org.eclipse.jpt.jpa.core.context.PrimaryKeyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyBaseJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyNamedColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyPrimaryKeyJoinColumn; |
| import org.eclipse.jpt.jpa.ui.internal.JpaHelpContextIds; |
| import org.eclipse.osgi.util.NLS; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Group; |
| |
| /** |
| * Here the layout of this pane: |
| * <pre> |
| * ----------------------------------------------------------------------------- |
| * | - Join Columns ---------------------------------------------------------- | |
| * | | | | |
| * | | x Override Default | | |
| * | | | | |
| * | | --------------------------------------------------------------------- | | |
| * | | | | | | |
| * | | | AddRemoveListPane | | | |
| * | | | | | | |
| * | | --------------------------------------------------------------------- | | |
| * | ------------------------------------------------------------------------- | |
| * -----------------------------------------------------------------------------</pre> |
| * |
| * @see Entity |
| * |
| * @version 2.0 |
| * @since 2.0 |
| */ |
| public abstract class AbstractPrimaryKeyJoinColumnsComposite<T extends Entity> extends Pane<T> |
| { |
| protected WritablePropertyValueModel<PrimaryKeyJoinColumn> joinColumnHolder; |
| |
| public AbstractPrimaryKeyJoinColumnsComposite(Pane<? extends T> subjectHolder, |
| Composite parent) { |
| |
| super(subjectHolder, parent, false); |
| } |
| |
| void addJoinColumn(PrimaryKeyJoinColumnStateObject stateObject) { |
| PrimaryKeyJoinColumn joinColumn = getSubject().addSpecifiedPrimaryKeyJoinColumn(); |
| stateObject.updateJoinColumn(joinColumn); |
| } |
| |
| void addPrimaryKeyJoinColumn() { |
| |
| PrimaryKeyJoinColumnDialog dialog = new PrimaryKeyJoinColumnDialog( |
| getShell(), |
| getSubject(), |
| null |
| ); |
| |
| dialog.openDialog(buildAddPrimaryKeyJoinColumnPostExecution()); |
| } |
| |
| private PostExecution<PrimaryKeyJoinColumnDialog> buildAddPrimaryKeyJoinColumnPostExecution() { |
| return new PostExecution<PrimaryKeyJoinColumnDialog>() { |
| public void execute(PrimaryKeyJoinColumnDialog dialog) { |
| if (dialog.wasConfirmed()) { |
| addJoinColumn(dialog.getSubject()); |
| } |
| } |
| }; |
| } |
| |
| protected abstract ListValueModel<? extends ReadOnlyPrimaryKeyJoinColumn> buildDefaultJoinColumnsListHolder(); |
| |
| private PostExecution<PrimaryKeyJoinColumnDialog> buildEditPrimaryKeyJoinColumnPostExecution() { |
| return new PostExecution<PrimaryKeyJoinColumnDialog>() { |
| public void execute(PrimaryKeyJoinColumnDialog dialog) { |
| if (dialog.wasConfirmed()) { |
| editJoinColumn(dialog.getSubject()); |
| } |
| } |
| }; |
| } |
| |
| private WritablePropertyValueModel<PrimaryKeyJoinColumn> buildJoinColumnHolder() { |
| return new SimplePropertyValueModel<PrimaryKeyJoinColumn>(); |
| } |
| |
| String buildJoinColumnLabel(ReadOnlyPrimaryKeyJoinColumn joinColumn) { |
| if (joinColumn.isDefault()) { |
| return NLS.bind( |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_mappingBetweenTwoParamsDefault, |
| joinColumn.getName(), |
| joinColumn.getReferencedColumnName() |
| ); |
| } |
| |
| if (joinColumn.getSpecifiedName() == null) { |
| if (joinColumn.getSpecifiedReferencedColumnName() == null) { |
| return NLS.bind( |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_mappingBetweenTwoParamsBothDefault, |
| joinColumn.getName(), |
| joinColumn.getReferencedColumnName() |
| ); |
| } |
| |
| return NLS.bind( |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_mappingBetweenTwoParamsFirstDefault, |
| joinColumn.getName(), |
| joinColumn.getReferencedColumnName() |
| ); |
| } |
| |
| if (joinColumn.getSpecifiedReferencedColumnName() == null) { |
| return NLS.bind( |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_mappingBetweenTwoParamsSecDefault, |
| joinColumn.getName(), |
| joinColumn.getReferencedColumnName() |
| ); |
| } |
| |
| return NLS.bind( |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_mappingBetweenTwoParams, |
| joinColumn.getName(), |
| joinColumn.getReferencedColumnName() |
| ); |
| } |
| |
| private Adapter buildJoinColumnsAdapter() { |
| return new AbstractAdapter() { |
| public void addNewItem(ObjectListSelectionModel listSelectionModel) { |
| addPrimaryKeyJoinColumn(); |
| } |
| |
| @Override |
| public boolean hasOptionalButton() { |
| return true; |
| } |
| |
| @Override |
| public String optionalButtonText() { |
| return JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_edit; |
| } |
| |
| @Override |
| public void optionOnSelection(ObjectListSelectionModel listSelectionModel) { |
| editPrimaryKeyJoinColumn(listSelectionModel); |
| } |
| |
| public void removeSelectedItems(ObjectListSelectionModel listSelectionModel) { |
| |
| int[] selectedIndices = listSelectionModel.selectedIndices(); |
| Entity entity = getSubject(); |
| |
| for (int index = selectedIndices.length; --index >= 0; ) { |
| entity.removeSpecifiedPrimaryKeyJoinColumn(selectedIndices[index]); |
| } |
| } |
| }; |
| } |
| |
| private ILabelProvider buildJoinColumnsListLabelProvider() { |
| return new LabelProvider() { |
| @Override |
| public String getText(Object element) { |
| return buildJoinColumnLabel((ReadOnlyPrimaryKeyJoinColumn) element); |
| } |
| }; |
| } |
| |
| private WritablePropertyValueModel<Boolean> buildOverrideDefaultJoinColumnHolder() { |
| return new OverrideDefaultJoinColumnHolder(); |
| } |
| |
| private ListValueModel<ReadOnlyPrimaryKeyJoinColumn> buildPrimaryKeyJoinColumnsListHolder() { |
| List<ListValueModel<? extends ReadOnlyPrimaryKeyJoinColumn>> list = new ArrayList<ListValueModel<? extends ReadOnlyPrimaryKeyJoinColumn>>(); |
| list.add(buildSpecifiedJoinColumnsListHolder()); |
| list.add(buildDefaultJoinColumnsListHolder()); |
| return new CompositeListValueModel<ListValueModel<? extends ReadOnlyPrimaryKeyJoinColumn>, ReadOnlyPrimaryKeyJoinColumn>(list); |
| } |
| |
| private ListValueModel<ReadOnlyPrimaryKeyJoinColumn> buildPrimaryKeyJoinColumnsListModel() { |
| return new ItemPropertyListValueModelAdapter<ReadOnlyPrimaryKeyJoinColumn>( |
| buildPrimaryKeyJoinColumnsListHolder(), |
| ReadOnlyNamedColumn.SPECIFIED_NAME_PROPERTY, |
| ReadOnlyNamedColumn.DEFAULT_NAME_PROPERTY, |
| ReadOnlyBaseJoinColumn.SPECIFIED_REFERENCED_COLUMN_NAME_PROPERTY, |
| ReadOnlyBaseJoinColumn.DEFAULT_REFERENCED_COLUMN_NAME_PROPERTY |
| ); |
| } |
| |
| ListValueModel<PrimaryKeyJoinColumn> buildSpecifiedJoinColumnsListHolder() { |
| return new ListAspectAdapter<Entity, PrimaryKeyJoinColumn>(getSubjectHolder(), Entity.SPECIFIED_PRIMARY_KEY_JOIN_COLUMNS_LIST) { |
| @Override |
| protected ListIterable<PrimaryKeyJoinColumn> getListIterable() { |
| return new SuperListIterableWrapper<PrimaryKeyJoinColumn>(subject.getSpecifiedPrimaryKeyJoinColumns()); |
| } |
| |
| @Override |
| protected int size_() { |
| return subject.getSpecifiedPrimaryKeyJoinColumnsSize(); |
| } |
| }; |
| } |
| |
| void editJoinColumn(PrimaryKeyJoinColumnStateObject stateObject) { |
| stateObject.updateJoinColumn(stateObject.getJoinColumn()); |
| } |
| |
| void editPrimaryKeyJoinColumn(ObjectListSelectionModel listSelectionModel) { |
| |
| PrimaryKeyJoinColumn joinColumn = (PrimaryKeyJoinColumn) listSelectionModel.selectedValue(); |
| |
| PrimaryKeyJoinColumnDialog dialog = new PrimaryKeyJoinColumnDialog( |
| getShell(), |
| getSubject(), |
| joinColumn |
| ); |
| |
| dialog.openDialog(buildEditPrimaryKeyJoinColumnPostExecution()); |
| } |
| |
| /* |
| * (non-Javadoc) |
| */ |
| @Override |
| protected void initialize() { |
| super.initialize(); |
| joinColumnHolder = buildJoinColumnHolder(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| */ |
| @Override |
| protected void initializeLayout(Composite container) { |
| |
| // Primary Key Join Columns group pane |
| Group groupPane = addTitledGroup( |
| container, |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_primaryKeyJoinColumn |
| ); |
| |
| // Override Default Join Columns check box |
| addCheckBox( |
| addSubPane(groupPane, 8), |
| JptUiDetailsMessages.PrimaryKeyJoinColumnsComposite_overrideDefaultPrimaryKeyJoinColumns, |
| buildOverrideDefaultJoinColumnHolder(), |
| null |
| ); |
| |
| // Primary Key Join Columns list pane |
| AddRemoveListPane<Entity> joinColumnsListPane = new AddRemoveListPane<Entity>( |
| this, |
| groupPane, |
| buildJoinColumnsAdapter(), |
| buildPrimaryKeyJoinColumnsListModel(), |
| joinColumnHolder, |
| buildJoinColumnsListLabelProvider(), |
| JpaHelpContextIds.MAPPING_JOIN_TABLE_COLUMNS |
| ); |
| |
| installJoinColumnsListPaneEnabler(joinColumnsListPane); |
| } |
| |
| private void installJoinColumnsListPaneEnabler(AddRemoveListPane<Entity> pane) { |
| new PaneEnabler( |
| buildOverrideDefaultJoinColumnHolder(), |
| pane |
| ); |
| } |
| |
| void updateJoinColumns(boolean selected) { |
| |
| if (isPopulating()) { |
| return; |
| } |
| |
| setPopulating(true); |
| |
| try { |
| // Add a join column by creating a specified one using the default |
| // one if it exists |
| if (selected) { |
| |
| switchDefaultToSpecified(); |
| } |
| // Remove all the specified join columns |
| else { |
| for (int index = getSubject().getSpecifiedPrimaryKeyJoinColumnsSize(); --index >= 0; ) { |
| getSubject().removeSpecifiedPrimaryKeyJoinColumn(index); |
| } |
| } |
| } |
| finally { |
| setPopulating(false); |
| } |
| } |
| |
| protected abstract void switchDefaultToSpecified(); |
| |
| private class OverrideDefaultJoinColumnHolder extends ListPropertyValueModelAdapter<Boolean> |
| implements WritablePropertyValueModel<Boolean> { |
| |
| public OverrideDefaultJoinColumnHolder() { |
| super(buildSpecifiedJoinColumnsListHolder()); |
| } |
| |
| @Override |
| protected Boolean buildValue() { |
| return listHolder.size() > 0; |
| } |
| |
| public void setValue(Boolean value) { |
| updateJoinColumns(value.booleanValue()); |
| } |
| } |
| } |