| /******************************************************************************* |
| * Copyright (c) 2012, 2014 - Lunifera GmbH (Gross Enzersdorf, Austria), 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 API and implementation |
| * |
| *******************************************************************************/ |
| package org.eclipse.osbp.vaaclipse.addons.app.common; |
| |
| import org.osgi.service.component.ComponentInstance; |
| |
| import com.vaadin.server.SessionDestroyEvent; |
| import com.vaadin.server.SessionDestroyListener; |
| import com.vaadin.ui.UI; |
| |
| /** |
| * The Class OSGiUI. |
| */ |
| @SuppressWarnings("serial") |
| public abstract class OSGiUI extends UI implements SessionDestroyListener { |
| |
| /** The instance. */ |
| private ComponentInstance instance; |
| |
| /** |
| * Sets the component instance that can be used to dispose the instance of |
| * that UI. |
| * |
| * @param instance |
| * the new component instance |
| */ |
| public void setComponentInstance(ComponentInstance instance) { |
| if (this.instance != null) { |
| throw new IllegalArgumentException( |
| "Component instance may only be set onece!"); |
| } |
| this.instance = instance; |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.UI#attach() |
| */ |
| @Override |
| public void attach() { |
| super.attach(); |
| |
| getSession().getService().addSessionDestroyListener(this); |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.server.SessionDestroyListener#sessionDestroy(com.vaadin.server.SessionDestroyEvent) |
| */ |
| public void sessionDestroy(SessionDestroyEvent event) { |
| dispose(); |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.UI#detach() |
| */ |
| @Override |
| public void detach() { |
| getSession().getService().removeSessionDestroyListener(this); |
| |
| super.detach(); |
| |
| dispose(); |
| } |
| |
| /** |
| * Is called to remove the instance as an OSGi service and to cleanup the |
| * OSGi runtime. |
| */ |
| protected void dispose() { |
| if (instance != null) { |
| instance.dispose(); |
| instance = null; |
| } |
| } |
| |
| } |