blob: 96b7b9fc97285bcfb0f8e37ebab4a704ef26caa8 [file] [log] [blame]
/*
*
* 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.xtext.common.types.JvmGenericType;
import org.eclipse.xtext.common.types.access.IJvmTypeProvider;
import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider;
import org.eclipse.osbp.vaadin.emf.api.IModelingContext;
import org.eclipse.osbp.vaadin.emf.data.JvmTypePropertiesTreeContainer;
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 {@link JvmGenericType} in a tree viewer style. Only properties
* (getter and setter available) are shown as details.
* <p>
* ATTENTION: This viewer MUST be disposed!
*/
@SuppressWarnings({ "serial", "restriction" })
public class JvmTypePropertiesTreeViewer extends CustomComponent {
/** The tree. */
private Tree tree;
/** The container. */
private JvmTypePropertiesTreeContainer container;
/** The type provider. */
private IJvmTypeProvider typeProvider;
/**
* Instantiates a new jvm type properties tree viewer.
*
* @param modelingContext
* the modeling context
* @param resourceProvider
* the resource provider
* @param typeProvider
* the type provider
*/
public JvmTypePropertiesTreeViewer(IModelingContext modelingContext,
IResourceProvider resourceProvider, IJvmTypeProvider typeProvider) {
this.typeProvider = typeProvider;
Panel mainLayout = new Panel();
mainLayout.setSizeFull();
setCompositionRoot(mainLayout);
VerticalLayout scrollArea = new VerticalLayout();
scrollArea.setMargin(true);
mainLayout.setContent(scrollArea);
tree = new Tree();
tree.setSizeFull();
tree.setDragMode(TreeDragMode.NODE);
// tree.setDropHandler(new EmfTreeDndHandler(modelingContext, tree));
// tree.addActionHandler(new EObjectActionHandler(modelingContext,
// resourceProvider));
// resolve by ui request. Otherwise exception
tree.addExpandListener(e -> container.resolveChildren(e.getItemId()));
scrollArea.addComponent(tree);
container = new JvmTypePropertiesTreeContainer(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 value.
*
* @param selection
* the new value
*/
public void setValue(EObject selection) {
tree.setValue(selection);
}
/**
* Gets the root element.
*
* @return the root element
*/
public JvmGenericType getRootElement() {
return (JvmGenericType) container.getRootElement();
}
/**
* Sets the root element.
*
* @param rootElement
* the new root element
*/
public void setRootElement(JvmGenericType rootElement) {
container.setRootElement(rootElement);
}
/**
* Sets the root element.
*
* @param rootElement
* the new root element
*/
public void setRootElement(Class<?> rootElement) {
container.setRootElement((JvmGenericType) typeProvider
.findTypeByName(rootElement.getName()));
}
/**
* Sets the root element.
*
* @param className
* the new root element
*/
public void setRootElement(String className) {
container.setRootElement((JvmGenericType) typeProvider
.findTypeByName(className));
}
/**
* Expand.
*
* @param element
* the element
*/
public void expand(EObject element) {
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;
}
}