Skip to content
Snippets Groups Projects
Commit fb330019 authored by Alexander Diewald's avatar Alexander Diewald
Browse files

JavaFX: Base classes for AF3/JavaFX TreeViews.

* Add a TreeViewer that constructs a DynamicTreeViewer whose UI provider
  uses the methods and kernel services for IHierarchicElements and
  NamedElements.
* The Viewer uses a composition principle where a client must provide
  TreeView (JavaFX), a root element (AF3), and a content provider that
  selects the elements to be displayed.
* Add two basic content providers that a) list all children below the
  given root element and b) list all children of a given type.

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


Signed-off-by: default avatarAlexander Diewald <diewald@fortiss.org>
parent 1b1673dd
No related branches found
No related tags found
1 merge request!43209 javafx
......@@ -33,6 +33,9 @@ Export-Package: org.fortiss.tooling.base.ui,
org.fortiss.tooling.base.ui.editpart.request,
org.fortiss.tooling.base.ui.extension.base,
org.fortiss.tooling.base.ui.fieldassist,
org.fortiss.tooling.base.ui.internal.command,
org.fortiss.tooling.base.ui.javafx.control.contentprovider,
org.fortiss.tooling.base.ui.javafx.control.treetableview,
org.fortiss.tooling.base.ui.layout,
org.fortiss.tooling.base.ui.layout.auto,
org.fortiss.tooling.base.ui.library,
......
ChildrenContentProvider.java 01bc9b3232e4209f4e97355762b620c59eddc677 YELLOW
SameTypeContentProvider.java f6c0381a3264dc62dd9b3d3a21e0b2cf5fb00dc1 YELLOW
/*-------------------------------------------------------------------------+
| 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.base.ui.javafx.control.contentprovider;
import static org.fortiss.tooling.common.util.LambdaUtils.filterType;
import java.util.Collection;
import org.fortiss.tooling.base.model.element.IHierarchicElement;
import org.fortiss.tooling.common.ui.javafx.control.treetableview.DynamicTreeContentProviderBase;
import org.fortiss.tooling.kernel.model.INamedElement;
/**
* Content provider that collects all children of the given type from an externally defined root
* node.
*
* @author diewald
*/
public class ChildrenContentProvider<T extends IHierarchicElement & INamedElement> extends
DynamicTreeContentProviderBase<T> {
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
protected Collection<? extends T> getChildren(T current) {
// The cast is safe by the construction of the Parameter 'T'.
return (Collection<? extends T>)filterType(current.getContainedElements(),
INamedElement.class);
}
}
/*-------------------------------------------------------------------------+
| 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.base.ui.javafx.control.contentprovider;
import static org.fortiss.tooling.common.util.LambdaUtils.filterStream;
import java.util.Collection;
import java.util.stream.Collectors;
import org.eclipse.emf.ecore.EClass;
import org.fortiss.tooling.base.model.element.IHierarchicElement;
import org.fortiss.tooling.common.ui.javafx.control.treetableview.DynamicTreeContentProviderBase;
import org.fortiss.tooling.kernel.model.INamedElement;
/**
* Content provider that collects all children of the given type from an externally defined root
* node. Nodes that have a different type are not traversed further even if they have children of
* the given type.
*
* @author diewald
*/
public class SameTypeContentProvider<T extends IHierarchicElement & INamedElement> extends
DynamicTreeContentProviderBase<T> {
/** EObject type to check against. */
EClass type;
/** Constructor. */
public SameTypeContentProvider(EClass type) {
this.type = type;
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
protected Collection<? extends T> getChildren(T current) {
// The cast is safe by the construction of the Parameter 'T'.
// @CodeFormatterOff
Collection<? extends T> children = (Collection<? extends T>)
filterStream(current.getContainedElements(), e -> e instanceof INamedElement)
.filter(e -> (e.getClass().equals(type)) || (type.isInstance(e)))
.collect(Collectors.toList());
// @CodeFormatterOn
return children;
}
}
HierarchicModelElementTreeViewer.java 8662b201c3a459f3ebe3620970652f7a3f90270e YELLOW
/*-------------------------------------------------------------------------+
| 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.base.ui.javafx.control.treetableview;
import javafx.embed.swt.SWTFXUtils;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.fortiss.tooling.base.model.element.IHierarchicElement;
import org.fortiss.tooling.common.ui.javafx.control.treetableview.DynamicTreeContentProviderBase;
import org.fortiss.tooling.common.ui.javafx.control.treetableview.DynamicTreeUIProviderBase;
import org.fortiss.tooling.common.ui.javafx.control.treetableview.DynamicTreeViewer;
import org.fortiss.tooling.kernel.model.INamedCommentedElement;
import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
/**
* TreeViewer that constructs a DynamicTreeViewer whose UI provider uses the methods and kernel
* services for {@link IHierarchicElement}s and {@link INamedCommentedElement}s.
* The Viewer uses a composition principle where a client must provide {@link TreeView} (JavaFX), a
* root element (AF3), and a content provider that selects the elements to be displayed.
*
* @author diewald
*/
public class HierarchicModelElementTreeViewer<T extends IHierarchicElement & INamedCommentedElement> {
/** References the constructed {@link DynamicTreeViewer} "controller". */
protected DynamicTreeViewer<T> dynTreeViewer;
/** Constructor. */
public HierarchicModelElementTreeViewer(TreeView<T> treeView, T modelRoot,
DynamicTreeContentProviderBase<T> contentProvider) {
DynamicTreeUIProviderBase<T> uiProvider = createContentUIProvider();
dynTreeViewer =
new DynamicTreeViewer<T>(treeView, modelRoot, true, 0, contentProvider, uiProvider);
}
/**
* Creates a default UI provider for {@link DynamicTreeViewer}s that present
* {@link IHierarchicElement}s. It also uses the {@link IModelElementHandlerService} for a nicer
* visual appearance.
* <p>
* Externalize this method into a class when subclassing seems appropriate.
*
* @return The constructed UI Provider.
*/
protected DynamicTreeUIProviderBase<T> createContentUIProvider() {
return new DynamicTreeUIProviderBase<T>() {
/** {@inheritDoc} */
@Override
public String getLabel(T element) {
return element.getName();
}
/** {@inheritDoc} */
@Override
public Node getIconNode(T element) {
org.eclipse.swt.graphics.Image icon =
IModelElementHandlerService.getInstance().getIcon(element);
if(icon != null) {
Image fxImage = SWTFXUtils.toFXImage(icon.getImageData(), null);
return new ImageView(fxImage);
}
return null;
}
/** {@inheritDoc} */
@Override
public ContextMenu createContextMenu(T element) {
ContextMenu menu = new ContextMenu();
// TODO (3209): Add sensible default operations for AF3 elements, if they are
// needed. Otherwise, use a list provided by subclasses that defines the menu items
// and their order. The code below is taken from SystemFOCUS and shall serve as a
// reference.
// MenuItem item = new MenuItem("Delete '" + getLabel(element) + "'.");
// item.setOnAction(event -> {
// new DeleteOperation(element).delete();
// viewer.update();
// });
// menu.getItems().add(item);
return menu;
}
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment