diff options
Diffstat (limited to 'jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/mappings/details/JoinColumnsComposite.java')
-rw-r--r-- | jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/mappings/details/JoinColumnsComposite.java | 476 |
1 files changed, 183 insertions, 293 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/mappings/details/JoinColumnsComposite.java b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/mappings/details/JoinColumnsComposite.java index 886e200383..c85332a246 100644 --- a/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/mappings/details/JoinColumnsComposite.java +++ b/jpa/plugins/org.eclipse.jpt.ui/src/org/eclipse/jpt/ui/internal/mappings/details/JoinColumnsComposite.java @@ -1,348 +1,238 @@ /******************************************************************************* - * Copyright (c) 2007 Oracle. All rights reserved. + * 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.Iterator; -import java.util.List; -import org.eclipse.emf.common.command.CommandStack; -import org.eclipse.emf.common.notify.Adapter; -import org.eclipse.emf.common.notify.Notification; -import org.eclipse.emf.common.notify.impl.AdapterImpl; -import org.eclipse.emf.ecore.EObject; -import org.eclipse.jface.viewers.IContentProvider; +import java.util.ListIterator; import org.eclipse.jface.viewers.ILabelProvider; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.ISelectionChangedListener; -import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.LabelProvider; -import org.eclipse.jface.viewers.ListViewer; -import org.eclipse.jface.viewers.SelectionChangedEvent; -import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.jface.viewers.Viewer; -import org.eclipse.jpt.core.internal.mappings.IAbstractColumn; -import org.eclipse.jpt.core.internal.mappings.IAbstractJoinColumn; -import org.eclipse.jpt.core.internal.mappings.IJoinColumn; -import org.eclipse.jpt.core.internal.mappings.INamedColumn; -import org.eclipse.jpt.core.internal.mappings.JpaCoreMappingsPackage; +import org.eclipse.jpt.core.internal.IJpaNode; +import org.eclipse.jpt.core.internal.context.base.IJoinColumn; import org.eclipse.jpt.ui.internal.IJpaHelpContextIds; -import org.eclipse.jpt.ui.internal.details.BaseJpaComposite; import org.eclipse.jpt.ui.internal.mappings.JptUiMappingsMessages; +import org.eclipse.jpt.ui.internal.widgets.AbstractFormPane; +import org.eclipse.jpt.ui.internal.widgets.AddRemoveListPane; +import org.eclipse.jpt.ui.internal.widgets.AddRemovePane.AbstractAdapter; +import org.eclipse.jpt.ui.internal.widgets.AddRemovePane.Adapter; +import org.eclipse.jpt.utility.internal.model.value.ListAspectAdapter; +import org.eclipse.jpt.utility.internal.model.value.ListValueModel; +import org.eclipse.jpt.utility.internal.model.value.PropertyValueModel; +import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel; +import org.eclipse.jpt.utility.internal.model.value.SortedListValueModelAdapter; +import org.eclipse.jpt.utility.internal.model.value.WritablePropertyValueModel; +import org.eclipse.jpt.utility.internal.model.value.swing.ObjectListSelectionModel; import org.eclipse.osgi.util.NLS; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Group; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory; -public class JoinColumnsComposite extends BaseJpaComposite +/** + * Here the layout of this pane: + * <pre> + * ----------------------------------------------------------------------------- + * | ------------------------------------------------------------------------- | + * | | | | + * | | AddRemoveListPane | | + * | | | | + * | ------------------------------------------------------------------------- | + * -----------------------------------------------------------------------------</pre> + * + * @see JoinTableComposite - A container of this pane + * @see OverridesComposite - A container of this pane + * + * @version 2.0 + * @since 2.0 + */ +public class JoinColumnsComposite<T extends IJpaNode> extends AbstractFormPane<T> { - - private Owner owner; - private final Adapter joinColumnsOwnerListener; + private IJoinColumnsEditor<T> joinColumnsEditor; - private ListViewer joinColumnsListViewer; - - private final Adapter joinColumnListener; - - private Group joinColumnsGroup; - private Button joinColumnsAddButton; - private Button joinColumnsRemoveButton; - private Button joinColumnsEditButton; - + /** + * Creates a new <code>JoinColumnsComposite</code>. + * + * @param parentPane The parent controller of this one + * @param parent The parent container + * @param widgetFactory The factory used to create various common widgets + */ + public JoinColumnsComposite(AbstractFormPane<? extends T> parentPane, + Composite parent, + IJoinColumnsEditor<T> joinColumnsEditor) { - public JoinColumnsComposite(Composite parent, CommandStack commandStack, TabbedPropertySheetWidgetFactory widgetFactory, String groupTitle) { - super(parent, SWT.NULL, commandStack, widgetFactory); - this.joinColumnsOwnerListener = buildJoinColumnsOwnerListener(); - this.joinColumnListener = buildJoinColumnListener(); - this.joinColumnsGroup.setText(groupTitle); + super(parentPane, parent); + this.joinColumnsEditor = joinColumnsEditor; + initializeLayout2(); } - - private Adapter buildJoinColumnsOwnerListener() { - return new AdapterImpl() { - public void notifyChanged(Notification notification) { - joinColumnsOwnerChanged(notification); - } - }; + + /** + * Creates a new <code>JoinColumnsComposite</code>. + * + * @param parentPane The parent controller of this one + * @param subjectHolder The holder of this pane's subject + * @param parent The parent container + * @param widgetFactory The factory used to create various common widgets + */ + public JoinColumnsComposite(AbstractFormPane<?> parentPane, + PropertyValueModel<? extends T> subjectHolder, + Composite parent, + IJoinColumnsEditor<T> joinColumnsEditor) { + + super(parentPane, subjectHolder, parent); + this.joinColumnsEditor = joinColumnsEditor; + initializeLayout2(); } - - private Adapter buildJoinColumnListener() { - return new AdapterImpl() { - public void notifyChanged(Notification notification) { - joinColumnChanged(notification, joinColumnsListViewer); - } - }; + + /** + * Creates a new <code>JoinColumnsComposite</code>. + * + * @param subjectHolder The holder of this pane's subject + * @param parent The parent container + * @param widgetFactory The factory used to create various common widgets + */ + public JoinColumnsComposite(PropertyValueModel<? extends T> subjectHolder, + Composite parent, + IWidgetFactory widgetFactory, + IJoinColumnsEditor<T> joinColumnsEditor) { + + super(subjectHolder, parent, widgetFactory); + this.joinColumnsEditor = joinColumnsEditor; + initializeLayout2(); } - - @Override - protected void initializeLayout(Composite composite) { - GridLayout layout = new GridLayout(2, false); - layout.marginWidth = 0; - composite.setLayout(layout); - this.joinColumnsGroup = - getWidgetFactory().createGroup( - composite, - ""); - this.joinColumnsGroup.setLayout(new GridLayout(2, false)); - GridData gridData = new GridData(); - gridData.horizontalAlignment = GridData.FILL; - gridData.verticalAlignment = GridData.FILL; - gridData.grabExcessHorizontalSpace = true; - gridData.grabExcessVerticalSpace = true; - gridData.horizontalSpan = 2; - this.joinColumnsGroup.setLayoutData(gridData); - - this.joinColumnsListViewer = new ListViewer(this.joinColumnsGroup, SWT.BORDER | SWT.MULTI); - this.joinColumnsListViewer.setContentProvider(buildJoinColumnsListContentProvider()); - this.joinColumnsListViewer.setLabelProvider(buildJoinColumnsListLabelProvider()); - gridData = new GridData(); - gridData.horizontalAlignment = GridData.FILL; - gridData.verticalAlignment = GridData.FILL; - gridData.verticalSpan = 3; - gridData.grabExcessHorizontalSpace = true; - gridData.grabExcessVerticalSpace = true; - this.joinColumnsListViewer.getList().setLayoutData(gridData); - PlatformUI.getWorkbench().getHelpSystem().setHelp(this.joinColumnsListViewer.getList(), IJpaHelpContextIds.MAPPING_JOIN_TABLE_COLUMNS); - - this.joinColumnsAddButton = getWidgetFactory().createButton(this.joinColumnsGroup, JptUiMappingsMessages.JoinTableComposite_add, SWT.NONE); - gridData = new GridData(); - gridData.horizontalAlignment = GridData.FILL; - this.joinColumnsAddButton.setLayoutData(gridData); - this.joinColumnsAddButton.addSelectionListener(new SelectionListener() { - public void widgetDefaultSelected(SelectionEvent e) { - } - - public void widgetSelected(SelectionEvent e) { - owner.addJoinColumn(); + private String buildDefaultJoinColumnLabel(IJoinColumn joinColumn) { + return NLS.bind( + JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsDefault, + joinColumn.getName(), + joinColumn.getReferencedColumnName() + ); + } + + private WritablePropertyValueModel<IJoinColumn> buildJoinColumnHolder() { + return new SimplePropertyValueModel<IJoinColumn>(); + } + + private String buildJoinColumnLabel(IJoinColumn joinColumn) { + + if (joinColumn.getSpecifiedName() == null) { + + if (joinColumn.getSpecifiedReferencedColumnName() == null) { + return NLS.bind( + JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsBothDefault, + joinColumn.getName(), + joinColumn.getReferencedColumnName() + ); } - }); - - this.joinColumnsEditButton = getWidgetFactory().createButton(this.joinColumnsGroup, JptUiMappingsMessages.JoinTableComposite_edit, SWT.NONE); - this.joinColumnsEditButton.addSelectionListener(new SelectionListener() { - public void widgetDefaultSelected(SelectionEvent e) { + + return NLS.bind( + JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsFirstDefault, + joinColumn.getName(), + joinColumn.getReferencedColumnName() + ); + } + + if (joinColumn.getSpecifiedReferencedColumnName() == null) { + return NLS.bind( + JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsSecDefault, + joinColumn.getName(), + joinColumn.getReferencedColumnName() + ); + } + + return NLS.bind( + JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParams, + joinColumn.getName(), + joinColumn.getReferencedColumnName() + ); + } + + private Adapter buildJoinColumnsAdapter() { + return new AbstractAdapter() { + + public void addNewItem(ObjectListSelectionModel listSelectionModel) { + joinColumnsEditor.addJoinColumn(subject()); } - - public void widgetSelected(SelectionEvent e) { - editJoinColumn(); + + @Override + public boolean hasOptionalButton() { + return true; } - }); - gridData = new GridData(); - gridData.horizontalAlignment = GridData.FILL; - this.joinColumnsEditButton.setLayoutData(gridData); - this.joinColumnsRemoveButton = getWidgetFactory().createButton(this.joinColumnsGroup, JptUiMappingsMessages.JoinTableComposite_remove, SWT.NONE); - gridData = new GridData(); - gridData.horizontalAlignment = GridData.FILL; - gridData.verticalAlignment = GridData.BEGINNING; - this.joinColumnsRemoveButton.setLayoutData(gridData); - this.joinColumnsRemoveButton.addSelectionListener(new SelectionListener() { - public void widgetDefaultSelected(SelectionEvent e) { + @Override + public String optionalButtonText() { + return JptUiMappingsMessages.JoinColumnComposite_edit; } - - public void widgetSelected(SelectionEvent e) { - removeJoinColumn(); + + @Override + public void optionOnSelection(ObjectListSelectionModel listSelectionModel) { + IJoinColumn joinColumn = (IJoinColumn) listSelectionModel.selectedValue(); + joinColumnsEditor.editJoinColumn(subject(), joinColumn); } - }); - - this.joinColumnsListViewer.addSelectionChangedListener(new ISelectionChangedListener() { - public void selectionChanged(SelectionChangedEvent event) { - updateJoinColumnsEnablement(); + + public void removeSelectedItems(ObjectListSelectionModel listSelectionModel) { + joinColumnsEditor.removeJoinColumns(subject(), listSelectionModel.selectedIndices()); } - }); + }; } - private IContentProvider buildJoinColumnsListContentProvider() { - return new IStructuredContentProvider(){ - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - } - - public void dispose() { - - } - - public Object[] getElements(Object inputElement) { - return ((Owner) inputElement).getJoinColumns().toArray(); + private ListValueModel<IJoinColumn> buildJoinColumnsListHolder() { + return new ListAspectAdapter<T, IJoinColumn>(getSubjectHolder(), joinColumnsEditor.propertyNames()) { + @Override + protected ListIterator<IJoinColumn> listIterator_() { + return joinColumnsEditor.joinColumns(subject); } }; } - + private ILabelProvider buildJoinColumnsListLabelProvider() { return new LabelProvider() { + @Override public String getText(Object element) { IJoinColumn joinColumn = (IJoinColumn) element; - return (JoinColumnsComposite.this.owner.containsSpecifiedJoinColumns()) ? - buildJoinColumnLabel(joinColumn) - : + + return joinColumnsEditor.hasSpecifiedJoinColumns(subject()) ? + buildJoinColumnLabel(joinColumn) : buildDefaultJoinColumnLabel(joinColumn); } }; } - - private String buildDefaultJoinColumnLabel(IJoinColumn joinColumn) { - return NLS.bind(JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsDefault, joinColumn.getName(), joinColumn.getReferencedColumnName()); - } - - private String buildJoinColumnLabel(IJoinColumn joinColumn) { - if (joinColumn.getSpecifiedName() == null) { - if (joinColumn.getSpecifiedReferencedColumnName() == null) { - return NLS.bind(JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsBothDefault, joinColumn.getName(),joinColumn.getReferencedColumnName()); - } - return NLS.bind(JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsFirstDefault, joinColumn.getName(), joinColumn.getReferencedColumnName()); - } - else if (joinColumn.getSpecifiedReferencedColumnName() == null) { - return NLS.bind(JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParamsSecDefault, joinColumn.getName(), joinColumn.getReferencedColumnName()); - } - else { - return NLS.bind(JptUiMappingsMessages.JoinTableComposite_mappingBetweenTwoParams, joinColumn.getName(), joinColumn.getReferencedColumnName()); - } - } - void editJoinColumn() { - this.owner.editJoinColumn(getSelectedJoinColumn()); - } - - void removeJoinColumn() { - ISelection selection = this.joinColumnsListViewer.getSelection(); - if (selection instanceof StructuredSelection) { - for (Iterator stream = ((StructuredSelection) selection).iterator(); stream.hasNext(); ) { - this.owner.getJoinColumns().remove(stream.next()); - } - } - } - - private IJoinColumn getSelectedJoinColumn() { - return (IJoinColumn) ((StructuredSelection) this.joinColumnsListViewer.getSelection()).getFirstElement(); - } - - protected void joinColumnsOwnerChanged(Notification notification) { - if (notification.getFeatureID(owner.owningFeatureClass()) == owner.specifiedJoinColumnsFeatureId()) { - if (notification.getEventType() == Notification.ADD) { - ((IJoinColumn) notification.getNewValue()).eAdapters().add(this.joinColumnListener); - } - else if (notification.getEventType() == Notification.REMOVE) { - ((IJoinColumn) notification.getOldValue()).eAdapters().remove(this.joinColumnListener); - } - Display.getDefault().asyncExec(new Runnable() { - public void run() { - if (getControl().isDisposed()) { - return; - } - joinColumnsListViewer.refresh(); - updateJoinColumnsEnablement(); - } - }); - } - } - - private void enableGroup(Group group, boolean enabled) { - group.setEnabled(enabled); - for (int i = 0; i < group.getChildren().length; i++) { - group.getChildren()[i].setEnabled(enabled); - } - } - protected void joinColumnChanged(Notification notification, final ListViewer listViewer) { - if (notification.getFeatureID(INamedColumn.class) == JpaCoreMappingsPackage.INAMED_COLUMN__SPECIFIED_NAME - || notification.getFeatureID(INamedColumn.class) == JpaCoreMappingsPackage.INAMED_COLUMN__DEFAULT_NAME - || notification.getFeatureID(IAbstractColumn.class) == JpaCoreMappingsPackage.IABSTRACT_COLUMN__SPECIFIED_TABLE - || notification.getFeatureID(IAbstractColumn.class) == JpaCoreMappingsPackage.IABSTRACT_COLUMN__DEFAULT_TABLE - || notification.getFeatureID(IAbstractJoinColumn.class) == JpaCoreMappingsPackage.IABSTRACT_JOIN_COLUMN__SPECIFIED_REFERENCED_COLUMN_NAME - || notification.getFeatureID(IAbstractJoinColumn.class) == JpaCoreMappingsPackage.IABSTRACT_JOIN_COLUMN__DEFAULT_REFERENCED_COLUMN_NAME) { - Display.getDefault().asyncExec(new Runnable() { - public void run() { - if (getControl().isDisposed()) { - return; - } - listViewer.refresh(); - } - }); - } - } - - protected void engageListeners() { - if (this.owner != null) { - this.owner.getEObject().eAdapters().add(this.joinColumnsOwnerListener); - for (IJoinColumn joinColumn : this.owner.getJoinColumns()) { - joinColumn.eAdapters().add(this.joinColumnListener); - } - } - } - - protected void disengageListeners() { - if (this.owner != null) { - for (IJoinColumn joinColumn : this.owner.getJoinColumns()) { - joinColumn.eAdapters().remove(this.joinColumnListener); - } - this.owner.getEObject().eAdapters().remove(this.joinColumnsOwnerListener); - } - } - - public void doPopulate(EObject obj) { - this.owner = (Owner) obj; - if (this.owner == null) { - this.joinColumnsListViewer.setInput(null); - return; - } - - this.joinColumnsListViewer.setInput(this.owner); - - updateEnablement(); + private ListValueModel<IJoinColumn> buildSortedJoinColumnsListHolder() { + return new SortedListValueModelAdapter<IJoinColumn>( + buildJoinColumnsListHolder() + ); } + /* + * (non-Javadoc) + */ @Override - protected void doPopulate() { + protected void initializeLayout(Composite container) { } - private void updateEnablement() { - updateJoinColumnsEnablement(); - } - - void updateJoinColumnsEnablement() { - boolean groupEnabledState = this.owner.containsSpecifiedJoinColumns(); - enableGroup(this.joinColumnsGroup, groupEnabledState); + private void initializeLayout2() { - this.joinColumnsRemoveButton.setEnabled(groupEnabledState && !((StructuredSelection) this.joinColumnsListViewer.getSelection()).isEmpty()); - this.joinColumnsEditButton.setEnabled(groupEnabledState && ((StructuredSelection) this.joinColumnsListViewer.getSelection()).size() == 1); - } - - protected void enableWidgets(boolean enabled) { - enableGroup(this.joinColumnsGroup, enabled); - this.joinColumnsAddButton.setEnabled(enabled); - this.joinColumnsEditButton.setEnabled(enabled); - this.joinColumnsRemoveButton.setEnabled(enabled); - this.joinColumnsListViewer.getList().setEnabled(enabled); + // Join Columns list pane + new AddRemoveListPane<T>( + this, + getControl(), + buildJoinColumnsAdapter(), + buildSortedJoinColumnsListHolder(), + buildJoinColumnHolder(), + buildJoinColumnsListLabelProvider(), + IJpaHelpContextIds.MAPPING_JOIN_TABLE_COLUMNS + ); } - public interface Owner { - int specifiedJoinColumnsFeatureId(); - - Class owningFeatureClass(); + public static interface IJoinColumnsEditor<T> { - EObject getEObject(); - - boolean containsSpecifiedJoinColumns(); - - List<IJoinColumn> getJoinColumns(); - - List<IJoinColumn> getSpecifiedJoinColumns(); - - IJoinColumn createJoinColumn(int index); - - void addJoinColumn(); - - void editJoinColumn(IJoinColumn joinColumn); + void addJoinColumn(T subject); + void editJoinColumn(T subject, IJoinColumn joinColumn); + boolean hasSpecifiedJoinColumns(T subject); + ListIterator<IJoinColumn> joinColumns(T subject); + String[] propertyNames(); + void removeJoinColumns(T subject, int[] selectedIndices); } - }
\ No newline at end of file |