Commit 0a23b325 authored by Simon Barner's avatar Simon Barner
Browse files

AllocationTableEditor applies user-provided predicate to filter list of source...

AllocationTableEditor applies user-provided predicate to filter list of source and target model elements
refs 2950
parent 12c53f9d
......@@ -21,14 +21,17 @@ import static org.fortiss.af3.allocation.utils.AllocationUtils.addAllocationEntr
import static org.fortiss.af3.allocation.utils.AllocationUtils.deleteAllocationEntry;
import static org.fortiss.af3.allocation.utils.AllocationUtils.isAllocated;
import static org.fortiss.af3.allocation.utils.AllocationUtils.isModifiableAllocationEntry;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.runAsCommand;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
......@@ -85,6 +88,17 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends G
*/
private Class<? extends IModelElement> targetEntityType;
/** Interface to define predicates to filter elements from source and target view. */
public interface ViewElementFilter extends Predicate<IModelElement> {
// Nothing to do
}
/** Predicate to filter elements of source view. */
private ViewElementFilter sourceViewElementFilter;
/** Predicate to filter elements of target view. */
private ViewElementFilter targetViewElementFilter;
/** The GUI element. */
private AllocationTableEditorGUI gui;
......@@ -93,12 +107,15 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends G
Class<? extends IProjectRootElement> sourceModelType,
Class<? extends IProjectRootElement> targetModelType,
Class<? extends IModelElement> sourceEntityType,
Class<? extends IModelElement> targetEntityType) {
Class<? extends IModelElement> targetEntityType,
ViewElementFilter sourceViewElementFilter, ViewElementFilter targetViewElementFilter) {
this.allocationEntryType = allocationEntryType;
this.sourceModelType = sourceModelType;
this.targetModelType = targetModelType;
this.sourceEntityType = sourceEntityType;
this.targetEntityType = targetEntityType;
this.sourceViewElementFilter = sourceViewElementFilter;
this.targetViewElementFilter = targetViewElementFilter;
}
/**
......@@ -210,14 +227,13 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends G
column.dispose();
}
EList<? extends IModelElement> sourceModelElements =
getChildrenWithType(editedObject.getSourceView(), sourceEntityType);
List<? extends IModelElement> sourceModelElements = getSourceViewModelElements();
gui.getTableViewer().setContentProvider(new ArrayContentProvider());
TableViewerColumn firstColumn = new TableViewerColumn(gui.getTableViewer(), SWT.NONE);
TableColumn tblclmnSrc = firstColumn.getColumn();
tblclmnSrc.setWidth(100);
tblclmnSrc.setText("Source");
tblclmnSrc.setText("\u2193 Src. | Tgt. \u2192"); // Down and right unicode arrow
firstColumn.setLabelProvider(new ColumnLabelProvider() {
/** {@inheritDoc} */
......@@ -229,8 +245,7 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends G
});
gui.getTableViewer().setInput(sourceModelElements);
EList<? extends IModelElement> targetModelElements =
getChildrenWithType(editedObject.getTargetView(), targetEntityType);
List<? extends IModelElement> targetModelElements = getTargetViewModelElements();
for(IModelElement targetElement : targetModelElements) {
TableViewerColumn tableViewerColumn =
new TableViewerColumn(gui.getTableViewer(), SWT.NONE);
......@@ -315,4 +330,44 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends G
}
gui.getTableViewer().refresh();
}
// TODO Move to LambdaUtils after release
/** Converts an {@link Iterator} to a (sequential) {@link Stream}. */
private static <T> Stream<T> asStream(Iterator<T> iterator) {
Iterable<T> iterable = new Iterable<T>() {
/** {@inheritDoc} */
@Override
public Iterator<T> iterator() {
return iterator;
}
};
return StreamSupport.stream(iterable.spliterator(), false);
}
/** Determines the list of model elements offered for a given {@code modelView}. */
private List<? extends IModelElement> getModelViewElements(IHierarchicElement modelView,
Class<? extends IModelElement> entityType, ViewElementFilter elementFilter) {
Stream<IModelElement> modelElements =
asStream(modelView.eAllContents()).filter(entityType::isInstance).map(
entityType::cast);
if(elementFilter != null) {
modelElements = modelElements.filter(elementFilter);
}
return modelElements.collect(Collectors.toList());
}
/** Determines the list of source model elements offered in this {@link AllocationTableEditor}. */
private List<? extends IModelElement> getSourceViewModelElements() {
return getModelViewElements(editedObject.getSourceView(), sourceEntityType,
sourceViewElementFilter);
}
/** Determines the list of target model elements offered in this {@link AllocationTableEditor}. */
private List<? extends IModelElement> getTargetViewModelElements() {
return getModelViewElements(editedObject.getTargetView(), targetEntityType,
targetViewElementFilter);
}
}
......@@ -37,7 +37,10 @@ public class LogicalArchitectureInputPortToPartitionArchitectureInputPortAllocat
/** Constructor. */
public LogicalArchitectureInputPortToPartitionArchitectureInputPortAllocationTableEditor() {
// Sources: Input ports (without those of hidden root component representing system inputs)
// Targets: Partition input ports
super(LogicalInPortToPartitionInPortAllocationEntry.class, ComponentArchitecture.class,
PartitionArchitecture.class, InputPort.class, PartitionInputPort.class);
PartitionArchitecture.class, InputPort.class, PartitionInputPort.class,
p -> ((InputPort)p).getComponent().getParentComponent() != null, null);
}
}
......@@ -37,7 +37,10 @@ public class LogicalArchitectureInputPortToPartitionArchitectureOutputPortAlloca
/** Constructor. */
public LogicalArchitectureInputPortToPartitionArchitectureOutputPortAllocationTableEditor() {
// Sources: Output ports (without those of hidden root component representing system output)
// Targets: Partition output ports
super(LogicalOutPortToPartitionOutPortAllocationEntry.class, ComponentArchitecture.class,
PartitionArchitecture.class, OutputPort.class, PartitionOutputPort.class);
PartitionArchitecture.class, OutputPort.class, PartitionOutputPort.class,
p -> ((OutputPort)p).getComponent().getParentComponent() != null, null);
}
}
......@@ -38,7 +38,10 @@ public class LogicalArchitectureToPartitionArchitectureAllocationTableEditor ext
/** Constructor. */
public LogicalArchitectureToPartitionArchitectureAllocationTableEditor() {
// Sources: Components (without hidden root component that represents the system)
// Targets: Partitions
super(LogicalComponentToPartitionAllocationEntry.class, ComponentArchitecture.class,
PartitionArchitecture.class, Component.class, Partition.class);
PartitionArchitecture.class, Component.class, Partition.class, c -> ((Component)c)
.getParentComponent() != null, null);
}
}
Supports Markdown
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