Commit 17eb7330 authored by Simon Barner's avatar Simon Barner
Browse files

Refactoring: Move anonymous classes to inner classes.

refs 2950
parent 84deaf36
......@@ -44,6 +44,7 @@ import org.conqat.ide.commons.ui.jface.TreeContentProviderBase;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
......@@ -51,7 +52,6 @@ import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
......@@ -200,21 +200,21 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends
Consumer<IHierarchicElement> modelSetter, Supplier<IHierarchicElement> modelGetter) {
super.modelSelectionChanged(model, modelSetter, modelGetter);
// For now update the entire table
updateTableViewer();
updateTreeViewer();
}
/** {@inheritDoc} */
@Override
protected void addEntity(IModelElement entity) {
// For now update the entire table
updateTableViewer();
updateTreeViewer();
}
/** {@inheritDoc} */
@Override
protected void removeEntity(IModelElement entity) {
// For now update the entire table
updateTableViewer();
updateTreeViewer();
}
/********************* Implementation of IWorkbenchPart *********************/
......@@ -233,30 +233,38 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends
editedObject.getTargetView(), editedObject::setTargetView,
editedObject::getTargetView);
gui.getCheckboxShowSourceModelHierarchy().addSelectionListener(new SelectionListener() {
/** {@inheritDoc} */
@Override
public void widgetSelected(SelectionEvent e) {
updateTreeViewerShowSourceModelHierarchy();
}
gui.getCheckboxShowSourceModelHierarchy().addSelectionListener(
new SourceModelHierarchyCheckBoxSelectionListener());
}
/** {@inheritDoc} */
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// Nothing to do
}
});
/** Selection listener to update how the {@link TreeViewer} displays the model's hierarchy. */
private class SourceModelHierarchyCheckBoxSelectionListener implements SelectionListener {
updateTreeViewerShowSourceModelHierarchy();
}
/** Constructor. */
public SourceModelHierarchyCheckBoxSelectionListener() {
updateTreeViewerShowSourceModelHierarchy();
}
/** Updates the {@link TreeViewer}'s model hierarchy. */
private void updateTreeViewerShowSourceModelHierarchy() {
// Update the TreeViewer's contents accordingly
updateTableViewer();
/** {@inheritDoc} */
@Override
public void widgetSelected(SelectionEvent e) {
updateTreeViewerShowSourceModelHierarchy();
}
if(gui.getCheckboxShowSourceModelHierarchy().getSelection()) {
gui.getTreeViewer().expandToLevel(ALL_LEVELS);
/** {@inheritDoc} */
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// Nothing to do
}
/** Updates how the {@link TreeViewer} displays the model's hierarchy. */
public void updateTreeViewerShowSourceModelHierarchy() {
// Update the TreeViewer's contents accordingly
updateTreeViewer();
if(gui.getCheckboxShowSourceModelHierarchy().getSelection()) {
gui.getTreeViewer().expandToLevel(ALL_LEVELS);
}
}
}
......@@ -377,7 +385,7 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends
// Update table in case the currently selected target or source view has just been added
// (required to restore table view after undoing a the removal of the source or target view)
if(editedObject.getSourceView() == model || editedObject.getTargetView() == model) {
updateTableViewer();
updateTreeViewer();
}
}
......@@ -392,99 +400,205 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends
// Update table in case the currently selected target or source view has been deleted
if(editedObject.getSourceView() == null || editedObject.getTargetView() == null) {
updateTableViewer();
updateTreeViewer();
}
}
/********************* Tree Viewer *********************/
/** Content provider to for allocation {@link TreeViewer}. */
private class TreeViewerContentProvider extends TreeContentProviderBase {
/** {@inheritDoc} */
@Override
public Object[] getChildren(Object parentElement) {
if(gui.getCheckboxShowSourceModelHierarchy().getSelection()) {
if(parentElement instanceof EObject) {
EObject node = (EObject)parentElement;
IModelElementHandler<EObject> nodeHandler =
IModelElementHandlerService.getInstance().getModelElementHandler(node);
List<EObject> rval = new ArrayList<EObject>();
// Add relevant structural elements: atomic sub-nodes that are of
// source-entity type, and nodes that can contain source-entity type
// elements in their offspring
for(EObject subNode : nodeHandler.getSubnodes(node)) {
IModelElementHandler<EObject> subNodeHandler =
IModelElementHandlerService.getInstance().getModelElementHandler(
subNode);
boolean isAtomicSourceEntity =
sourceEntityType.isAssignableFrom(subNode.getClass()) &&
subNodeHandler.getSubnodes(subNode).isEmpty();
boolean containsSourceEntity =
getFirstChildWithType(subNode, sourceEntityType) != null;
if(isAtomicSourceEntity || containsSourceEntity) {
rval.add(subNode);
}
}
// Add source-entity type connectors
rval.addAll(pickInstanceOf(sourceEntityType, nodeHandler.getConnectors(node)));
if(!rval.isEmpty()) {
return rval.toArray();
}
}
} else {
if(parentElement == editedObject.getSourceView()) {
return getSourceViewModelElements().toArray();
}
}
return new Object[0];
}
}
/** Label provider for {@link TreeViewer}'s first column. */
private class TreeViewerFirstColumnLabelProvider extends ColumnLabelProvider {
/** {@inheritDoc} */
@Override
public Image getImage(Object element) {
return AllocationTableEditor.getImage(element);
}
/** {@inheritDoc} */
@Override
public String getText(Object element) {
return getName(element);
}
/** {@inheritDoc} */
@Override
public String getToolTipText(Object object) {
return AllocationTableEditor.getToolTipText(object);
}
}
/** Label provider to display {@link AllocationTable}'s entries. */
private class TreeViewerCheckBoxLabelProvider extends CheckBoxLabelProvider {
/** Target element of {@link AllocationTable} entry. */
private IModelElement targetElement;
/** Constructor. */
public TreeViewerCheckBoxLabelProvider(IModelElement targetElement) {
this.targetElement = targetElement;
}
/** {@inheritDoc} */
@Override
protected boolean isChecked(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isAllocated(editedObject, allocationEntryType, sourceElement, targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
protected boolean isEnabled(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isModifiableAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
public Image getImage(Object element) {
if(element != null && sourceEntityType.isAssignableFrom(element.getClass())) {
return super.getImage(element);
}
return null;
}
}
/********************* Table Viewer *********************/
/** Editing support to edit {@link AllocationTable}'s entries. */
private class TreeViewerCheckBoxEditingSupport extends CheckBoxEditingSupport {
/** Target element of {@link AllocationTable} entry. */
private IModelElement targetElement;
/** Constructor. */
private TreeViewerCheckBoxEditingSupport(ColumnViewer viewer, IModelElement targetElement) {
super(viewer);
this.targetElement = targetElement;
}
/** {@inheritDoc} */
@Override
protected boolean canEdit(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isModifiableAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
protected Boolean getBooleanValue(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isAllocated(editedObject, allocationEntryType, sourceElement, targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
protected void setBooleanValue(Object element, Boolean value) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
if(value != isAllocated(editedObject, allocationEntryType, sourceElement,
targetElement)) {
runAsCommand(
editedObject,
() -> {
if(value) {
addAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
} else {
deleteAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
}
});
getViewer().refresh();
}
}
}
}
/**
* Updates (or creates) the {@link TableViewer} with the edited source/target entities and adds
* Updates (or creates) the {@link TreeViewer} with the edited source/target entities and adds
* the first (source) column to it.
*/
protected void updateTableViewer() {
protected void updateTreeViewer() {
// Dispose old tree. NOOP if it does not exist.
TreeViewer treeViewer = gui.getTreeViewer();
Tree tree = treeViewer.getTree();
tree.setRedraw(false);
for(TreeColumn column : tree.getColumns()) {
column.dispose();
}
treeViewer.setContentProvider(new TreeContentProviderBase() {
/** {@inheritDoc} */
@Override
public Object[] getChildren(Object parentElement) {
if(gui.getCheckboxShowSourceModelHierarchy().getSelection()) {
if(parentElement instanceof EObject) {
EObject node = (EObject)parentElement;
IModelElementHandler<EObject> nodeHandler =
IModelElementHandlerService.getInstance().getModelElementHandler(
node);
List<EObject> rval = new ArrayList<EObject>();
// Add relevant structural elements: atomic sub-nodes that are of
// source-entity type, and nodes that can contain source-entity type
// elements in their offspring
for(EObject subNode : nodeHandler.getSubnodes(node)) {
IModelElementHandler<EObject> subNodeHandler =
IModelElementHandlerService.getInstance()
.getModelElementHandler(subNode);
boolean isAtomicSourceEntity =
sourceEntityType.isAssignableFrom(subNode.getClass()) &&
subNodeHandler.getSubnodes(subNode).isEmpty();
boolean containsSourceEntity =
getFirstChildWithType(subNode, sourceEntityType) != null;
if(isAtomicSourceEntity || containsSourceEntity) {
rval.add(subNode);
}
}
// Add source-entity type connectors
rval.addAll(pickInstanceOf(sourceEntityType,
nodeHandler.getConnectors(node)));
if(!rval.isEmpty()) {
return rval.toArray();
}
}
} else {
if(parentElement == editedObject.getSourceView()) {
return getSourceViewModelElements().toArray();
}
}
return new Object[0];
}
});
// Setup tree viewer
treeViewer.setContentProvider(new TreeViewerContentProvider());
treeViewer.setInput(editedObject.getSourceView());
// Create first column (source elements)
TreeViewerColumn firstColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
TreeColumn tblclmnSrc = firstColumn.getColumn();
tblclmnSrc.setWidth(100);
tblclmnSrc.setText("\u2193 Src. | Tgt. \u2192"); // Down and right unicode arrow
firstColumn.setLabelProvider(new TreeViewerFirstColumnLabelProvider());
firstColumn.setLabelProvider(new ColumnLabelProvider() {
/** {@inheritDoc} */
@Override
public Image getImage(Object element) {
return AllocationTableEditor.getImage(element);
}
/** {@inheritDoc} */
@Override
public String getText(Object element) {
return getName(element);
}
/** {@inheritDoc} */
@Override
public String getToolTipText(Object object) {
return AllocationTableEditor.getToolTipText(object);
}
});
treeViewer.setInput(editedObject.getSourceView());
// Create entries' columns
List<? extends IModelElement> targetModelElements = getTargetViewModelElements();
for(IModelElement targetElement : targetModelElements) {
TreeViewerColumn treeViewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
......@@ -493,91 +607,12 @@ public abstract class AllocationTableEditor<T extends AllocationTable> extends
treeColumn.setText(getName(targetElement));
treeColumn.setToolTipText(getToolTipText(targetElement));
treeColumn.setImage(getImage(targetElement));
CheckBoxLabelProvider checkBoxlabelProvider = new CheckBoxLabelProvider() {
/** {@inheritDoc} */
@Override
protected boolean isChecked(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isAllocated(editedObject, allocationEntryType, sourceElement,
targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
protected boolean isEnabled(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isModifiableAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
public Image getImage(Object element) {
if(element != null && sourceEntityType.isAssignableFrom(element.getClass())) {
return super.getImage(element);
}
return null;
}
};
treeViewerColumn.setLabelProvider(checkBoxlabelProvider);
treeViewerColumn.setEditingSupport(new CheckBoxEditingSupport(treeViewer) {
/** {@inheritDoc} */
@Override
protected boolean canEdit(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isModifiableAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
protected Boolean getBooleanValue(Object element) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
return isAllocated(editedObject, allocationEntryType, sourceElement,
targetElement);
}
return false;
}
/** {@inheritDoc} */
@Override
protected void setBooleanValue(Object element, Boolean value) {
if(element instanceof IModelElement) {
IModelElement sourceElement = (IModelElement)element;
if(value != isAllocated(editedObject, allocationEntryType, sourceElement,
targetElement)) {
runAsCommand(
editedObject,
() -> {
if(value) {
addAllocationEntry(editedObject, allocationEntryType,
sourceElement, targetElement);
} else {
deleteAllocationEntry(editedObject,
allocationEntryType, sourceElement,
targetElement);
}
});
getViewer().refresh();
}
}
}
});
treeViewerColumn.setLabelProvider(new TreeViewerCheckBoxLabelProvider(targetElement));
treeViewerColumn.setEditingSupport(new TreeViewerCheckBoxEditingSupport(treeViewer,
targetElement));
}
// Update
treeViewer.refresh();
tree.setRedraw(true);
}
......
......@@ -19,7 +19,6 @@ package org.fortiss.af3.allocation.ui.editor;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.swt.SWT;
......@@ -50,7 +49,7 @@ public class AllocationTableEditorGUI extends Composite {
/** Checkbox used to select whether to show the source model elements' hierarchy. */
private Button checkboxShowSourceModelHierarchy;
/** {@link TableViewer} used to edit {@link AllocationTable}. */
/** {@link TreeViewer} used to edit {@link AllocationTable}. */
private TreeViewer treeViewer;
/** Constructor. */
......
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