blob: feb4e1c09e79e67edb9220e745c00bc9ea33ab6d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 Rushan R. Gilmullin and others.
* 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:
* Rushan R. Gilmullin - initial API and implementation
*******************************************************************************/
package org.eclipse.osbp.vaaclipse.presentation.renderers;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.model.application.ui.MContext;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.workbench.IPresentationEngine;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.UIEvents.EventTags;
import org.eclipse.osbp.vaaclipse.presentation.utils.HierarchyUtils;
import org.eclipse.osbp.vaaclipse.widgets.StackWidget;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import com.vaadin.ui.Component;
import com.vaadin.ui.VerticalLayout;
/**
* @author rushan
*
*/
public class PlaceholderRenderer extends VaadinRenderer {
@Inject
IPresentationEngine renderingEngine;
@Inject
private IEventBroker eventBroker;
@Override
public void createWidget(MUIElement element,
MElementContainer<MUIElement> parent) {
MPlaceholder ph = (MPlaceholder) element;
final MUIElement ref = ph.getRef();
ref.setCurSharedRef(ph);
VerticalLayout phComp = new VerticalLayout();
phComp.setSizeFull();
phComp.setMargin(false);
ph.setWidget(phComp);
Component refWidget = (Component) ref.getWidget();
if (refWidget == null) {
ref.setToBeRendered(true);
refWidget = (Component) renderingEngine.createGui(ref);
}
if (refWidget.getParent() != phComp) {
phComp.addComponent(refWidget);
}
if (ref instanceof MContext) {
IEclipseContext context = ((MContext) ref).getContext();
IEclipseContext newParentContext = getContext(ph);
if (context.getParent() != newParentContext) {
context.setParent(newParentContext);
}
}
}
EventHandler tagListener = new EventHandler() {
@Override
public void handleEvent(Event event) {
Object changedObj = event.getProperty(EventTags.ELEMENT);
String eventType = (String) event
.getProperty(UIEvents.EventTags.TYPE);
String tag = (String) event
.getProperty(UIEvents.EventTags.NEW_VALUE);
if (!(changedObj instanceof MPlaceholder)) {
return;
}
final MPlaceholder ph = (MPlaceholder) changedObj;
MPartStack topLeftStack = HierarchyUtils.findTopLeftFolder(ph
.getRef());
if (topLeftStack != null) {
StackWidget stackWidget = (StackWidget) topLeftStack
.getWidget();
if (UIEvents.EventTypes.ADD.equals(eventType)) {
if (IPresentationEngine.MINIMIZED.equals(tag)) {
stackWidget.setState(-1);
} else if (IPresentationEngine.MAXIMIZED.equals(tag)) {
stackWidget.setState(1);
}
} else if (UIEvents.EventTypes.REMOVE.equals(eventType)) {
stackWidget.setState(0);
}
}
}
};
@PostConstruct
void postConstruct() {
eventBroker.subscribe(UIEvents.ApplicationElement.TOPIC_TAGS,
tagListener);
}
@PreDestroy
void preDestroy() {
eventBroker.unsubscribe(tagListener);
}
}