blob: 78dadc38bf3825df91838727c4406e7082bc63ec [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:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*
* This copyright notice shows up in the generated Java code
*
*/
package org.eclipse.osbp.xtext.action.jvmmodel
import java.util.concurrent.ExecutorService
import javax.inject.Inject
import org.eclipse.e4.core.contexts.IEclipseContext
import org.eclipse.e4.core.di.annotations.CanExecute
import org.eclipse.e4.core.di.annotations.Execute
import org.eclipse.e4.core.services.events.IEventBroker
import org.eclipse.e4.ui.model.application.ui.menu.MToolBar
import org.eclipse.e4.ui.workbench.IPresentationEngine
import org.eclipse.osbp.persistence.IPersistenceService
import org.eclipse.osbp.ui.api.themes.IThemeResourceService
import org.eclipse.osbp.ui.api.user.IUser
import org.eclipse.osbp.utils.constants.GeneratorConstants
import org.eclipse.osbp.xtext.action.ActionButton
import org.eclipse.osbp.xtext.action.ActionChart
import org.eclipse.osbp.xtext.action.ActionDatainterchange
import org.eclipse.osbp.xtext.action.ActionDialog
import org.eclipse.osbp.xtext.action.ActionGeneric
import org.eclipse.osbp.xtext.action.ActionPackage
import org.eclipse.osbp.xtext.action.ActionReport
import org.eclipse.osbp.xtext.action.ActionSelectWorkload
import org.eclipse.osbp.xtext.action.ActionTask
import org.eclipse.osbp.xtext.action.ActionToolbar
import org.eclipse.osbp.xtext.action.ActionWorkflow
import org.eclipse.osbp.xtext.action.common.IToolbarAction
import org.eclipse.osbp.xtext.datainterchange.DataInterchange
import org.eclipse.osbp.xtext.datainterchange.jvmmodel.DataDSLJvmModelInferrer
import org.eclipse.osbp.xtext.functionlibrarydsl.FunctionLibraryPackage
import org.eclipse.osbp.xtext.functionlibrarydsl.util.Library.Context
import org.eclipse.xtext.common.types.JvmDeclaredType
import org.eclipse.xtext.common.types.JvmField
import org.eclipse.xtext.common.types.JvmGenericType
import org.eclipse.xtext.common.types.JvmVisibility
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.slf4j.Logger
/**
* <p>Infers a JVM model from the source model.</p>
*
* <p>The JVM model should contain all elements that would appear in the Java code
* which is generated from the source model. Other models link against the JVM model rather than the source model.</p>
*/
class ActionDSLJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject extension DataDSLJvmModelInferrer data
def dispatch void infer(ActionPackage pkg, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
for(toolbar:pkg.toolbars) {
var toolbarClass = toolbar.toClass(toolbar.className)
acceptor.accept(toolbarClass,
[
documentation = GeneratorConstants.GENERATED_CLASSES_DOCUMENTATION
superTypes += _typeReferenceBuilder.typeRef(IToolbarAction)
it.toToolbarConstructor(toolbar)
it.toToolbarFields(toolbar)
it.toToolbarOperations(toolbar)
])
for(action: toolbar.actions) {
var handlerClass = toolbar.toClass(toolbar.className(action))
acceptor.accept(handlerClass,
[
documentation = GeneratorConstants.GENERATED_CLASSES_DOCUMENTATION
it.toHandlerFields(action)
it.toHandlerOperations(toolbar, action)
it.toHandlerConstructor(action)
])
}
}
}
/**
* <p>build the constructors including the uuid.</p>
*
*/
def void toToolbarConstructor(JvmGenericType type, ActionToolbar toolbar){
type.members += toolbar.toConstructor(
[
parameters += toolbar.toParameter("uuid", _typeReferenceBuilder.typeRef(String))
body = [
append(
'''
super();
this.uuid = uuid;''')]
])
}
def String className(ActionToolbar toolbar) {
return className(toolbar, null)
}
def String className(ActionToolbar toolbar, ActionButton action) {
var actionId = ""
var pkg = toolbar.eContainer as ActionPackage
if (action != null) {
switch action.actionType {
ActionTask: {
actionId = (action.actionType as ActionTask).action.literal
}
ActionSelectWorkload: {
actionId = (action.actionType as ActionSelectWorkload).action.literal
}
ActionDialog: {
actionId = (action.actionType as ActionDialog).action.literal
}
ActionReport: {
actionId = (action.actionType as ActionReport).action.literal
}
ActionChart: {
actionId = (action.actionType as ActionChart).action.literal
}
ActionWorkflow: {
actionId = (action.actionType as ActionWorkflow).action.literal
}
ActionDatainterchange: {
actionId = (action.actionType as ActionDatainterchange).action.literal
}
ActionGeneric: {
actionId = (action.actionType as ActionGeneric).action
}
}
}
return pkg.name.toString.concat(".").concat(toolbar.name.toFirstUpper).concat(actionId.toFirstUpper).concat("Action")
}
def void toToolbarOperations(JvmGenericType type, ActionToolbar toolbar) {
// create a toolbar
type.members += toolbar.toMethod("createToolbar", _typeReferenceBuilder.typeRef(Void::TYPE), [
parameters += toolbar.toParameter("eclipseContext", _typeReferenceBuilder.typeRef(IEclipseContext))
parameters += toolbar.toParameter("renderingEngine", _typeReferenceBuilder.typeRef(IPresentationEngine))
parameters += toolbar.toParameter("themeResourceService", _typeReferenceBuilder.typeRef(IThemeResourceService))
visibility = JvmVisibility.PUBLIC
annotations += _annotationTypesBuilder.annotationRef(Override)
body = [append('''«toolbar.createToolbar»''')]
])
// get toolbar
type.members += toolbar.toMethod("getToolBar", _typeReferenceBuilder.typeRef(MToolBar), [
annotations += _annotationTypesBuilder.annotationRef(Override)
body = [ append('''return toolbar;''')]
])
}
def String createToolbar(ActionToolbar toolbar) {
var body = ""
body = '''
«body»
this.eclipseContext = eclipseContext;
this.themeResourceService = themeResourceService;
MenuFactoryImpl factory = MenuFactoryImpl.eINSTANCE;
MDirectToolItem toolItem = null;
MPart part = (MPart)eclipseContext.get(MPart.class);
if(part != null) {
toolbar = part.getToolbar();
if (toolbar == null) {
toolbar = factory.createToolBar();
toolbar.setElementId(uuid);
eclipseContext.set("«toolbar.name»_TOOLBAR_ID", toolbar.getElementId());
toolbar.setToBeRendered(true);
part.setToolbar(toolbar);
Panel panel = (Panel) part.getWidget();
if (panel != null) { // it is already visible in terms of vaadin
//create toolbar area
VerticalLayout rootContainer = (VerticalLayout)panel.getContent();
CssLayout toolbarArea = new CssLayout();
toolbarArea.setStyleName(EnumCssClass.MPARTTOOLBARAREA.toString());
toolbarArea.setSizeUndefined();
toolbarArea.setWidth("100%");
rootContainer.addComponentAsFirst(toolbarArea);
//create toolbar
Component toolbarWidget = (Component) renderingEngine.createGui(toolbar);
((AbstractLayout)toolbarWidget).setSizeUndefined();
toolbarWidget.setStyleName(EnumCssClass.MPARTTOOLBAR.toString());
toolbarArea.addComponent(toolbarWidget);
}
}
else {
toolbar.getChildren().removeIf(c -> c.isToBeRendered());
}
'''
for(action : toolbar.actions) {
body = '''«body»«toolbar.createToolItem(action)»'''
}
body = '''
«body»
}'''
return body
}
def String createToolItem(ActionToolbar toolbar, ActionButton action) {
var body = ""
body = '''
«body»
toolItem = factory.createDirectToolItem();
toolItem.setType(ItemType.PUSH);
toolItem.setTooltip("«action.name»");
toolItem.setIconURI(themeResourceService.getThemeURI("«action.iconURI»", ThemeResourceType.ICON));
toolItem.setEnabled(true);
toolItem.setToBeRendered(true);
toolItem.setVisible(true);
toolItem.setContributionURI("bundleclass://"+FrameworkUtil.getBundle(«toolbar.className(action)».class).getSymbolicName()+"/«toolbar.className(action)»");
toolbar.getChildren().add(toolItem);
«IF action.actionType instanceof ActionReport»
toolItem.getTags().add("«(action.actionType as ActionReport).action.literal»");
«ENDIF»
'''
return body
}
def void toToolbarFields(JvmGenericType type, ActionToolbar toolbar) {
var JvmField field = null
// create logger
field = toolbar.toField("log", _typeReferenceBuilder.typeRef(Logger))[setInitializer([append('''org.slf4j.LoggerFactory.getLogger("toolbar")''')])]
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
// create UUID
field = toolbar.toField("uuid", _typeReferenceBuilder.typeRef(String))
field.visibility = JvmVisibility::PRIVATE
type.members += field
field = toolbar.toField("eclipseContext", _typeReferenceBuilder.typeRef(IEclipseContext))
type.members += field
field = toolbar.toField("themeResourceService", _typeReferenceBuilder.typeRef(IThemeResourceService))
type.members += field
field = toolbar.toField("toolbar", _typeReferenceBuilder.typeRef(MToolBar))
type.members += field
}
// handler stuff
def void toHandlerFields(JvmGenericType type, ActionButton action) {
var JvmField field = null
// create logger
field = action.toField("log", _typeReferenceBuilder.typeRef(Logger))[setInitializer([append('''org.slf4j.LoggerFactory.getLogger("action")''')])]
field.static = true
field.visibility = JvmVisibility::PRIVATE
type.members += field
field = action.toField("eventBroker", _typeReferenceBuilder.typeRef(IEventBroker)) [annotations += _annotationTypesBuilder.annotationRef(Inject)]
type.members += field
field = action.toField("user", _typeReferenceBuilder.typeRef(IUser)) [annotations += _annotationTypesBuilder.annotationRef(Inject)]
type.members += field
field = action.toField("eclipseContext", _typeReferenceBuilder.typeRef(IEclipseContext)) [annotations += _annotationTypesBuilder.annotationRef(Inject)]
type.members += field
field = action.toField("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService)) [annotations += _annotationTypesBuilder.annotationRef(Inject)]
type.members += field
field = action.toField("executorService", _typeReferenceBuilder.typeRef(ExecutorService))
field.static = true
type.members += field
}
/**
* <p>build the constructors to be used by an e4 application.</p>
*
*/
def void toHandlerConstructor(JvmDeclaredType type, ActionButton action) {
type.members += action.toConstructor([
body = [ append('''«action.handlerConstructor»''')]
])
}
def handlerConstructor(ActionButton action) {
'''
executorService = Executors.newFixedThreadPool(10);
'''
}
/**
* <p>build the methods to be used as toolbar handlers by an e4 application.</p>
*
*/
def void toHandlerOperations(JvmDeclaredType type, ActionToolbar toolbar, ActionButton action) {
// create getContent
type.members += action.toMethod("getContent", _typeReferenceBuilder.typeRef(Context), [
visibility = JvmVisibility::PRIVATE
body = [ append('''«action.getContent»''')]
])
// create canExecute
type.members += action.toMethod("canExecute", _typeReferenceBuilder.typeRef(boolean), [
annotations += _annotationTypesBuilder.annotationRef(CanExecute)
body = [ append('''«action.canExecute»''')]
])
// create execute
type.members += action.toMethod("execute", _typeReferenceBuilder.typeRef(Void::TYPE), [
annotations += _annotationTypesBuilder.annotationRef(Execute)
parameters += action.toParameter("eventBroker", _typeReferenceBuilder.typeRef(IEventBroker))
body = [ append('''«action.execute(toolbar)»''')]
])
}
def execute(ActionButton action, ActionToolbar toolbar) {
var packageFQN = ""
if(action.group != null) {
var FunctionLibraryPackage pkg = action.group.eContainer as FunctionLibraryPackage
packageFQN = pkg.fullyQualifiedName.toString
}
switch action.actionType {
ActionTask: (action.actionType as ActionTask).action.literal.doBroadcast(action, toolbar, packageFQN)
ActionSelectWorkload: (action.actionType as ActionSelectWorkload).action.literal.doBroadcast(action, toolbar, packageFQN)
ActionDialog: (action.actionType as ActionDialog).action.literal.doBroadcast(action, toolbar, packageFQN)
ActionReport: (action.actionType as ActionReport).action.literal.doBroadcast(action, toolbar, packageFQN)
ActionChart: (action.actionType as ActionChart).action.literal.doBroadcast(action, toolbar, packageFQN)
ActionWorkflow: (action.actionType as ActionWorkflow).action.literal.doBroadcast(action, toolbar, packageFQN)
ActionDatainterchange: (action.actionType as ActionDatainterchange).dataRef?.doInterchange(action)
ActionGeneric: (action.actionType as ActionGeneric).action.doBroadcast(action, toolbar, packageFQN)
}
}
def doInterchange(DataInterchange dataInterchange, ActionButton action)
'''
«data.getBasicRunConfiguration(dataInterchange, true, data.getFileURL(dataInterchange))»
«dataInterchange.getDefaultVariableName()».setDirection(WorkerThreadRunnable.Direction.«(action.actionType as ActionDatainterchange).action.literal.toUpperCase»);
«dataInterchange.getDefaultVariableName()».setEventBroker(eventBroker);
executorService.execute(«dataInterchange.getDefaultVariableName()»);'''
def doBroadcast(String identifier, ActionButton action, ActionToolbar toolbar, String packageFQN)
'''
log.debug("action execute called for «action.name»");
String uuid = (String)eclipseContext.get("«toolbar.name»_TOOLBAR_ID");
eventBroker.send(EventBrokerMsg.ACTION_BUTTON_EXECUTE_PREFIX+"«toolbar.name»Action"+uuid, new EventBrokerMsg(-1, "«identifier»", «IF action.group==null || action.executeparameter==null»0L«ELSE»«packageFQN».«action.group.name.toString.toFirstUpper».«action.executeparameter.name.toString»(getContent())«ENDIF»));'''
def canExecute(ActionButton action) {
if (action != null) {
if (action.group != null) {
var pkg = action.group.eContainer as FunctionLibraryPackage
'''
«IF action.group==null»return true;«ELSE»return «pkg.fullyQualifiedName.toString».«action.group.name.toString.toFirstUpper».«action.canexecute.name.toString»(getContent());«ENDIF»'''
}
else
{
'''return true;'''
}
}
else
{
'''return true;'''
}
}
def getContent(ActionButton action) {
'''
Context result = new Context();
result.put(Context.USER, user);
MContext context = eclipseContext.get(MContext.class);
if (context instanceof MPart && ((MPart)context).getTransientData().containsKey("tablePositionInfo")) {
result.put(Context.TABLE, ((MPart)context).getTransientData().get("tablePositionInfo"));
result.put(Context.TASKSUMMARY, ((MPart)context).getTransientData().get("taskSummary"));
}
else if (context instanceof MTrimmedWindow) {
if (((MTrimmedWindow)context).getSelectedElement()!=null && ((MTrimmedWindow)context).getSelectedElement() instanceof MPerspectiveStack) {
MPerspectiveStack perspectiveStack = (MPerspectiveStack)((MTrimmedWindow)context).getSelectedElement();
if (perspectiveStack.getSelectedElement() != null && perspectiveStack.getSelectedElement() instanceof MPerspective) {
MPerspective perspective = (MPerspective)perspectiveStack.getSelectedElement();
if (perspective.getTransientData().containsKey("taskSummary")) {
result.put(Context.TASKSUMMARY, perspective.getTransientData().get("taskSummary"));
}
}
}
}
return result;'''
}
}