Commit 506532a0 authored by Simon Barner's avatar Simon Barner
Browse files

Add property section base class for editing EReference} lists.

* Uses EReferencePropertySectionBase's combobox to select model element
  to be added to the list
  * See ProvideEReferencePropertySectionBase::{get,set}ModelValue().
    Declare are because of special semantics
  * Derived classes must implement getValues() (and the abstract
    methods defined in this class EReferenceListPropertySectionBase)

Issue-Ref: 3470
Issue-Url: https://af3-developer.fortiss.org/issues/3470

Signed-off-by: Simon Barner's avatarSimon Barner <barner@fortiss.org>
parent a127d8fe
ConstraintUIBases.java 3676a600e0866091db9798763c6eee97eec5b55b GREEN
ContextMenuSubMenuContributorBase.java 6275d96fe8690d9d4744bcbaef3c7d14ba8e30ff GREEN
EObjectActionBase.java 4ef9f8be59e64d4838acc9e268d418ba5d94fa1a GREEN
EReferenceListPropertySectionBase.java b43469e81cf9f959230e9b2353460c49d8d6a7d3 YELLOW
EReferencePropertySectionBase.java 3347e99b2fc135dd4b3117407179900ef757092c GREEN
EditorBase.java 9c09fff92945256bb8680992ae7bb2c78f47b150 GREEN
ModelEditorBindingBase.java 4c5ac569c0b6e7678fc8191096b26dfd09fdcb98 GREEN
......
/*-------------------------------------------------------------------------+
| Copyright 2018 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.extension.base;
import static org.fortiss.tooling.kernel.ui.util.SelectionUtils.checkAndPickFirst;
import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.runAsCommand;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
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.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.fortiss.tooling.kernel.ui.extension.base.EReferencePropertySectionBase;
import org.fortiss.tooling.kernel.ui.presentation.ModelElementLabelProvider;
/**
* Base class for property sections for editing {@link EReference} lists.
*
* @author barner
*/
public abstract class EReferenceListPropertySectionBase<I extends EObject, R extends EObject>
extends EReferencePropertySectionBase<I, R> {
/**
* Element that has been selected the combo box provided by the base class
* {@link EReferencePropertySectionBase}. It is added to the model using the "add" button.
*/
private R selectedElement;
/** {@link ListViewer} to show edited {@link EReference} list. */
private ListViewer elementListViewer;
/** The edited object (updated in {@link #setSectionInput(Object)}). */
private I input = null;
/** Constructor . */
public EReferenceListPropertySectionBase(final String label) {
super(label);
}
/** {@inheritDoc} */
@Override
public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
super.createControls(parent, aTabbedPropertySheetPage);
TabbedPropertySheetWidgetFactory wf = getWidgetFactory();
Composite listComposite = wf.createComposite(composite, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = 0;
listComposite.setLayout(layout);
elementListViewer = new ListViewer(listComposite, SWT.BORDER | SWT.V_SCROLL);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.grabExcessHorizontalSpace = true;
gd.heightHint = 48;
elementListViewer.getList().setLayoutData(gd);
elementListViewer.setContentProvider(new ArrayContentProvider());
elementListViewer.setLabelProvider(new ModelElementLabelProvider());
Composite buttonComposite = wf.createComposite(listComposite, SWT.NONE);
buttonComposite.setLayout(new FillLayout(SWT.VERTICAL));
Button addButton = wf.createButton(buttonComposite, "Add", SWT.NONE);
addButton.addSelectionListener(new SelectionAdapter() {
/** {@inheritDoc} */
@Override
public void widgetSelected(SelectionEvent e) {
if(selectedElement != null &&
!getModelListElements(input).contains(selectedElement)) {
runAsCommand(input, () -> addModelListElement(input, selectedElement));
refresh();
}
}
});
Button removeButton = wf.createButton(buttonComposite, "Remove", SWT.NONE);
removeButton.addSelectionListener(new SelectionAdapter() {
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public void widgetSelected(SelectionEvent e) {
runAsCommand(input, () -> removeModelListElement(input,
(R)checkAndPickFirst(elementListViewer.getSelection(), EObject.class)));
refresh();
}
});
createFormEntry(listComposite, "");
}
/** {@inheritDoc} */
@Override
public void refresh() {
super.refresh();
if(input == null) {
return;
}
elementListViewer.setInput(getModelListElements(input));
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
protected void setSectionInput(Object input) {
super.setSectionInput(input);
this.input = (I)input;
}
/** {@inheritDoc} */
@Override
protected final EObject getModelValue(I input) {
List<R> elements = getValues(input);
// The value selected in the combo box is not directly stored in the model (see
// setModelValue()). Hence, instead of retrieving a value for the model from the combobox,
// by default the first element is selected.
selectedElement = elements.isEmpty() ? null : elements.get(0);
return selectedElement;
}
/** {@inheritDoc} */
@Override
protected final void setModelValue(I input, R newValue) {
// Store the element selected in the combobox (provided by the base class) as input
// for possible "add" operation.
selectedElement = newValue;
}
/** Returns the {@link List} of elements in the edited {@link EReference} list. */
protected abstract List<R> getModelListElements(I input);
/** Adds the given {@code element} to the edited {@link EReference} list. */
protected abstract void addModelListElement(I input, R element);
/** Removes the given {@code element} from the edited {@link EReference} list. */
protected abstract void removeModelListElement(I input, R element);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment