| /* |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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: |
| * Florian Pirchner - Initial implementation |
| * Loetz GmbH&Co.KG |
| * |
| */ |
| package org.eclipse.osbp.vaadin.emf.views; |
| |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider; |
| import org.eclipse.osbp.vaadin.emf.actions.EObjectActionHandler; |
| import org.eclipse.osbp.vaadin.emf.actions.EmfTreeDndHandler; |
| import org.eclipse.osbp.vaadin.emf.api.IModelingContext; |
| import org.eclipse.osbp.vaadin.emf.data.EmfModelTreeContainer; |
| |
| import com.vaadin.event.ItemClickEvent.ItemClickListener; |
| import com.vaadin.ui.CustomComponent; |
| import com.vaadin.ui.Panel; |
| import com.vaadin.ui.Tree; |
| import com.vaadin.ui.Tree.TreeDragMode; |
| import com.vaadin.ui.UI; |
| import com.vaadin.ui.VerticalLayout; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * Shows any emfmodel in a tree viewer style. |
| * <p> |
| * ATTENTION: This viewer MUST be disposed! |
| */ |
| @SuppressWarnings("serial") |
| public class EmfModelTreeViewer extends CustomComponent { |
| |
| /** The tree. */ |
| private Tree tree; |
| |
| /** The container. */ |
| private EmfModelTreeContainer container; |
| |
| // private ContextMenu contextMenu; |
| |
| /** |
| * Instantiates a new emf model tree viewer. |
| * |
| * @param modelingContext |
| * the modeling context |
| * @param resourceProvider |
| * the resource provider |
| * @param callback |
| * the callback |
| */ |
| public EmfModelTreeViewer(IModelingContext modelingContext, |
| IResourceProvider resourceProvider, |
| EObjectActionHandler.EditCallback callback) { |
| Panel mainLayout = new Panel(); |
| mainLayout.setSizeFull(); |
| setCompositionRoot(mainLayout); |
| |
| VerticalLayout scrollArea = new VerticalLayout(); |
| mainLayout.setContent(scrollArea); |
| |
| tree = new Tree(); |
| tree.setSizeFull(); |
| |
| // contextMenu = new ContextMenu(tree, false); |
| // contextMenu.addContextMenuOpenListener(e -> contextMenuOpens(e)); |
| |
| tree.setDragMode(TreeDragMode.NODE); |
| tree.setDropHandler(new EmfTreeDndHandler(modelingContext, tree)); |
| |
| tree.addActionHandler(new EObjectActionHandler(modelingContext, |
| resourceProvider, callback)); |
| // resolve by ui request. Otherwise exception |
| tree.addExpandListener(e -> container.resolveChildren(e.getItemId())); |
| scrollArea.addComponent(tree); |
| |
| container = new EmfModelTreeContainer(modelingContext, |
| resourceProvider, new UiSync(UI.getCurrent())); |
| tree.setContainerDataSource(container); |
| |
| tree.setItemCaptionPropertyId("label"); |
| tree.setItemIconPropertyId("icon"); |
| } |
| |
| // private void contextMenuOpens(ContextMenuOpenEvent event) { |
| // event.getContextClickEvent().getComponent(); |
| // } |
| |
| /** |
| * Adds the item click listener. |
| * |
| * @param listener |
| * the listener |
| */ |
| public void addItemClickListener(ItemClickListener listener) { |
| tree.addItemClickListener(listener); |
| } |
| |
| /** |
| * Removes the item click listener. |
| * |
| * @param listener |
| * the listener |
| */ |
| public void removeItemClickListener(ItemClickListener listener) { |
| tree.removeItemClickListener(listener); |
| } |
| |
| /** |
| * Gets the selection. |
| * |
| * @return the selection |
| */ |
| public EObject getSelection() { |
| return (EObject) tree.getValue(); |
| } |
| |
| /** |
| * Sets the selection. |
| * |
| * @param selection |
| * the new selection |
| */ |
| public void setSelection(EObject selection) { |
| tree.setValue(selection); |
| } |
| |
| /** |
| * Gets the root element. |
| * |
| * @return the root element |
| */ |
| public EObject getRootElement() { |
| return container.getRootElement(); |
| } |
| |
| /** |
| * Sets the root element. |
| * |
| * @param rootElement |
| * the new root element |
| */ |
| public void setRootElement(EObject rootElement) { |
| container.setRootElement(rootElement); |
| } |
| |
| /** |
| * Expand. |
| * |
| * @param element |
| * the element |
| */ |
| public void expand(EObject element) { |
| if(element == null) { |
| return; |
| } |
| if (tree.containsId(element)) { |
| tree.expandItem(element); |
| |
| // expand parent up to the root |
| if (!tree.isRoot(element)) { |
| expand(element.eContainer()); |
| } |
| } else if (element.eContainer() != null) { |
| // expand parent |
| expand(element.eContainer()); |
| |
| // now expand child |
| if (tree.containsId(element)) { |
| tree.expandItem(element); |
| } |
| } |
| } |
| |
| /** |
| * Is required to be called. |
| */ |
| public void dispose() { |
| tree = null; |
| container.dispose(); |
| container = null; |
| } |
| |
| } |