| /* |
| * |
| * 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 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| * Loetz GmbH&Co.KG |
| * |
| */ |
| package org.eclipse.osbp.vaadin.emf.data; |
| |
| import java.net.URL; |
| import java.util.Set; |
| |
| import org.eclipse.emf.common.notify.AdapterFactory; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.edit.domain.EditingDomain; |
| import org.eclipse.emf.edit.provider.IItemLabelProvider; |
| import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider; |
| import org.eclipse.osbp.vaadin.emf.api.IModelingContext; |
| import org.eclipse.xtext.common.types.JvmDeclaredType; |
| |
| import com.vaadin.data.Item; |
| import com.vaadin.data.util.IndexedContainer; |
| import com.vaadin.server.Resource; |
| |
| /** |
| * The base container to provide SubTypes for a given type |
| * <p> |
| * ATTENTION: This container MUST be disposed! |
| */ |
| @SuppressWarnings("serial") |
| public class JvmSubTypesContainer extends IndexedContainer { |
| |
| /** The Constant ICON. */ |
| public static final String ICON = "icon"; |
| |
| /** The Constant KEY. */ |
| public static final String LABEL = "LABEL"; |
| |
| /** The root element. */ |
| private JvmDeclaredType rootElement; |
| |
| /** The editing domain. */ |
| @SuppressWarnings("unused") |
| private final EditingDomain editingDomain; |
| |
| /** The adapter factory. */ |
| private final AdapterFactory adapterFactory; |
| |
| private final IResourceProvider resourceProvider; |
| |
| private final SubTypesCallback callback; |
| |
| /** |
| * Instantiates a new emf properties container. |
| * |
| * @param modelingContext |
| * the modeling context |
| * @param resourceProvider |
| * the resource provider |
| */ |
| public JvmSubTypesContainer(IModelingContext modelingContext, |
| IResourceProvider resourceProvider, SubTypesCallback callback) { |
| this.editingDomain = modelingContext.getEditingDomain(); |
| this.adapterFactory = modelingContext.getAdapterFactory(); |
| this.resourceProvider = resourceProvider; |
| this.callback = callback; |
| |
| addContainerProperty(LABEL, String.class, ""); |
| addContainerProperty(ICON, Resource.class, null); |
| } |
| |
| /** |
| * Gets the root element. |
| * |
| * @return the root element |
| */ |
| public EObject getRootElement() { |
| return rootElement; |
| } |
| |
| /** |
| * Sets the root element. |
| * |
| * @param rootElement |
| * the new root element |
| */ |
| @SuppressWarnings("unchecked") |
| public void setRootElement(JvmDeclaredType rootElement) { |
| if (this.rootElement == rootElement) { |
| return; |
| } |
| |
| this.rootElement = rootElement; |
| |
| removeAllItems(); |
| |
| if (rootElement != null) { |
| for (JvmDeclaredType subType : callback.getSubTypes(rootElement)) { |
| IItemLabelProvider labelProvider = (IItemLabelProvider) adapterFactory |
| .adapt(subType, IItemLabelProvider.class); |
| Item item = addItem(subType); |
| item.getItemProperty(LABEL).setValue( |
| labelProvider.getText(subType)); |
| URL url = ((URL) labelProvider.getImage(subType)); |
| item.getItemProperty(ICON).setValue( |
| resourceProvider.getResource(url.toString())); |
| } |
| } |
| } |
| |
| /** |
| * Returns a set with sub types for the given root type. |
| */ |
| public interface SubTypesCallback { |
| |
| /** |
| * Returns a set with sub types for the given root type. |
| * |
| * @param type |
| * @return |
| */ |
| Set<JvmDeclaredType> getSubTypes(JvmDeclaredType type); |
| |
| } |
| } |