| /** |
| * |
| * 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 |
| */ |
| package org.eclipse.osbp.vaaclipse.addons.softwarefactory.maintoolbar; |
| |
| import java.util.UUID; |
| |
| import javax.annotation.PostConstruct; |
| import javax.inject.Inject; |
| |
| import org.apache.commons.lang3.StringUtils; |
| import org.eclipse.e4.core.contexts.IEclipseContext; |
| import org.eclipse.e4.ui.model.application.MApplication; |
| import org.eclipse.e4.ui.model.application.ui.SideValue; |
| import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar; |
| import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement; |
| import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow; |
| import org.eclipse.e4.ui.model.application.ui.basic.impl.BasicFactoryImpl; |
| import org.eclipse.e4.ui.model.application.ui.menu.ItemType; |
| import org.eclipse.e4.ui.model.application.ui.menu.MDirectToolItem; |
| import org.eclipse.e4.ui.model.application.ui.menu.MToolBar; |
| import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService; |
| import org.eclipse.osbp.ui.api.themes.IThemeResourceService; |
| import org.eclipse.osbp.ui.api.themes.IThemeResourceService.ThemeResourceType; |
| import org.eclipse.osbp.utils.theme.EnumCssClass; |
| import org.eclipse.osbp.vaaclipse.addons.softwarefactory.perspective.PerspectiveConstants; |
| import org.eclipse.osbp.vaaclipse.presentation.engine.GenericPresentationEngine; |
| import org.eclipse.osbp.xtext.action.ActionButton; |
| import org.eclipse.osbp.xtext.action.ActionChart; |
| 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.ActionTask; |
| import org.eclipse.osbp.xtext.action.ActionToolbar; |
| import org.eclipse.osbp.xtext.action.ActionWorkflow; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.vaadin.ui.AbstractLayout; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.CssLayout; |
| |
| /** |
| * The Class MainToolbarProvider. |
| */ |
| @SuppressWarnings("restriction") |
| public class MainToolbarProvider implements IMainToolbarProvider { |
| |
| /** The log. */ |
| private static Logger log = LoggerFactory.getLogger(MainToolbarProvider.class); |
| |
| /** The app model. */ |
| @Inject |
| private MApplication appModel; |
| |
| /** The eclipse context. */ |
| @Inject |
| private IEclipseContext eclipseContext; |
| |
| /** The theme resource service. */ |
| @Inject |
| private IThemeResourceService themeResourceService; |
| |
| /** The dsl metadata service. */ |
| @Inject |
| private IDSLMetadataService dslMetadataService; |
| |
| /** The trimmed window. */ |
| private MTrimmedWindow trimmedWindow; |
| |
| /** The trim bar. */ |
| private MTrimBar trimBar; |
| |
| /** The menu factory. */ |
| private MenuFactoryImpl menuFactory; |
| |
| /** The basic factory. */ |
| private BasicFactoryImpl basicFactory; |
| |
| /** The presentation engine. */ |
| private GenericPresentationEngine presentationEngine; |
| |
| /** The unique toolbar id. */ |
| private String toolbarId; |
| |
| /** |
| * Instantiates a new main toolbar provider. It is injected later via E4ContextFunction. |
| */ |
| @Inject |
| public MainToolbarProvider() { |
| } |
| |
| /** |
| * Inits the maintoolbar provider. |
| */ |
| @PostConstruct |
| public void init() { |
| log.debug("init MainToolbarProvider"); |
| menuFactory = MenuFactoryImpl.init(); |
| basicFactory = BasicFactoryImpl.init(); |
| presentationEngine = eclipseContext.get(GenericPresentationEngine.class); |
| if (appModel != null) { |
| trimmedWindow = null; |
| if (appModel.getChildren().isEmpty()) { |
| trimmedWindow = BasicFactoryImpl.eINSTANCE.createTrimmedWindow(); |
| trimmedWindow.setToBeRendered(true); |
| trimmedWindow.setVisible(true); |
| appModel.getChildren().add(trimmedWindow); |
| } else { |
| trimmedWindow = (MTrimmedWindow) appModel.getChildren().get(0); |
| } |
| trimBar = null; |
| if (trimmedWindow.getTrimBars().isEmpty()) { |
| // create trimBar |
| trimBar = basicFactory.createTrimBar(); |
| trimBar.setElementId(PerspectiveConstants.E4Constants.TRIMBAR); |
| trimBar.setToBeRendered(true); |
| trimBar.setVisible(true); |
| trimBar.setSide(SideValue.TOP); |
| trimmedWindow.getTrimBars().add(trimBar); |
| } else { |
| trimBar = trimmedWindow.getTrimBars().get(0); |
| } |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.vaaclipse.addons.softwarefactory.maintoolbar.IMainToolbarProvider#addToolbar(java.lang.String, org.eclipse.osbp.xtext.action.ActionToolbar, |
| * java.lang.String) |
| */ |
| @Override |
| public void addToolbar(String perspectiveId, ActionToolbar toolBarModel) { |
| log.debug("addToolbar for perspectiveId " + perspectiveId); |
| MToolBar toolBar = createToolbar(perspectiveId, toolBarModel); |
| createToolbarArea(toolBar); |
| } |
| |
| /** |
| * Creates the toolbar area. |
| * |
| * @param toolBar |
| * the tool bar |
| */ |
| void createToolbarArea(MToolBar toolBar) { |
| // set os style |
| CssLayout toolBarArea = (CssLayout) trimBar.getWidget(); |
| toolBarArea.setStyleName(EnumCssClass.MPARTTOOLBARAREA.toString()); |
| |
| Component toolBarWidget = null; |
| for (int idx = 0; idx < toolBarArea.getComponentCount(); idx++) { |
| Component test = toolBarArea.getComponent(idx); |
| AbstractLayout hl = (AbstractLayout) test; |
| if (hl.getData() instanceof MToolBar) { |
| toolBarWidget = (Component) toolBarArea.getComponent(idx); |
| break; |
| } |
| } |
| if (toolBarWidget == null) { |
| // create toolBar |
| toolBarWidget = (Component) presentationEngine.createGui(toolBar); |
| ((AbstractLayout) toolBarWidget).setSizeUndefined(); |
| } |
| toolBarWidget.setStyleName(EnumCssClass.MPARTTOOLBAR.toString()); |
| toolBarArea.addComponent(toolBarWidget); |
| } |
| |
| /** |
| * Creates the toolbar. |
| * |
| * @param perspectiveId |
| * the perspective id |
| * @param toolBarModel |
| * the tool bar model |
| * @param actionUri |
| * the action uri |
| * @return the m tool bar |
| */ |
| MToolBar createToolbar(String perspectiveId, ActionToolbar toolBarModel) { |
| MToolBar toolBar = null; |
| toolBar = menuFactory.createToolBar(); |
| toolBar.setElementId(PerspectiveConstants.E4Constants.TOOLBARCONTROLS + "." + perspectiveId); |
| toolBar.setToBeRendered(true); |
| addToolbarItems(toolBar, toolBarModel); |
| // set handle |
| toolBar.getTransientData().put("perspectiveId", perspectiveId); |
| trimBar.getChildren().add(toolBar); |
| return toolBar; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.vaaclipse.addons.softwarefactory.maintoolbar.IMainToolbarProvider#removeToolbar(java.lang.String) |
| */ |
| @Override |
| public void removeToolbar(String perspectiveId) { |
| log.debug("removeToolbar for perspectiveId " + perspectiveId); |
| MTrimElement elementToRemove = null; |
| for (MTrimElement element : trimBar.getChildren()) { |
| if (element instanceof MToolBar) { |
| if (perspectiveId.equals(((MToolBar) element).getTransientData().get("perspectiveId"))) { |
| removeToolbarItems((MToolBar) element); |
| elementToRemove = element; |
| } |
| } |
| } |
| trimBar.getChildren().remove(elementToRemove); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.vaaclipse.addons.softwarefactory.maintoolbar.IMainToolbarProvider#setActive(java.lang.String, boolean) |
| */ |
| @Override |
| public void setActive(String perspectiveId, boolean active) { |
| for (MTrimElement element : trimBar.getChildren()) { |
| if (element instanceof MToolBar) { |
| if (perspectiveId.equals(((MToolBar) element).getTransientData().get("perspectiveId"))) { |
| ((MToolBar) element).setVisible(active); |
| } |
| } |
| } |
| } |
| |
| /** |
| * Add toolBar items. |
| * |
| * @param toolBar |
| * the tool bar |
| * @param toolBarModel |
| * the tool bar model |
| * @param actionUri |
| * the action uri |
| */ |
| private void addToolbarItems(MToolBar toolBar, ActionToolbar toolBarModel) { |
| for (ActionButton buttonModel : toolBarModel.getActions()) { |
| addToolbarItem(buttonModel, toolBar); |
| } |
| } |
| |
| /** |
| * Adds the toolbar item. |
| * |
| * @param buttonModel |
| * the button model |
| * @param toolBar |
| * the tool bar |
| * @param actionUri |
| * the action uri |
| */ |
| private void addToolbarItem(ActionButton buttonModel, MToolBar toolBar) { |
| MDirectToolItem toolItem = menuFactory.createDirectToolItem(); |
| toolItem.setType(ItemType.PUSH); |
| |
| toolItem.setTooltip(buttonModel.getName()); |
| toolItem.setIconURI(themeResourceService.getThemeURI(buttonModel.getIconURI(), ThemeResourceType.ICON)); |
| toolItem.setEnabled(true); |
| toolItem.setToBeRendered(true); |
| toolItem.setVisible(true); |
| |
| toolItem.setContributionURI(getToolItemURI(buttonModel)); |
| toolItem.setElementId(PerspectiveConstants.E4Constants.TOOLBARITEM_PREFIX + buttonModel.getName()); |
| toolBar.getChildren().add(toolItem); |
| } |
| |
| /** |
| * Removes toolBar items. |
| * |
| * @param toolBar |
| * the tool bar |
| */ |
| private void removeToolbarItems(MToolBar toolBar) { |
| toolBar.getChildren().clear(); |
| } |
| |
| /** |
| * Gets the tool item uri. |
| * |
| * @param buttonModel |
| * the button model |
| * @param actionUri |
| * the action uri |
| * @return the tool item uri |
| */ |
| private String getToolItemURI(ActionButton buttonModel) { // NOSONAR |
| log.debug("add toolitemuri " + buttonModel.getName()); |
| String pkgName = null; |
| String toolBarName = null; |
| EObject eObj = buttonModel.eContainer(); |
| while (eObj != null) { |
| if (eObj instanceof ActionToolbar) { |
| toolBarName = ((ActionToolbar) eObj).getName(); |
| } |
| if (eObj instanceof ActionPackage) { |
| pkgName = ((ActionPackage) eObj).getName(); |
| } |
| eObj = eObj.eContainer(); |
| } |
| // create unique id for broadcast message identification and put to context |
| String uuid = UUID.randomUUID().toString(); |
| toolbarId = toolBarName + "Action" + uuid; |
| eclipseContext.set(toolBarName + "_TOOLBAR_ID", uuid); |
| |
| String actionId = null; |
| if (buttonModel.getActionType() instanceof ActionTask) { |
| actionId = ((ActionTask) buttonModel.getActionType()).getAction().getLiteral(); |
| } |
| if (buttonModel.getActionType() instanceof ActionDialog) { |
| actionId = ((ActionDialog) buttonModel.getActionType()).getAction().getLiteral(); |
| } |
| if (buttonModel.getActionType() instanceof ActionReport) { |
| actionId = ((ActionReport) buttonModel.getActionType()).getAction().getLiteral(); |
| } |
| if (buttonModel.getActionType() instanceof ActionChart) { |
| actionId = ((ActionChart) buttonModel.getActionType()).getAction().getLiteral(); |
| } |
| if (buttonModel.getActionType() instanceof ActionWorkflow) { |
| actionId = ((ActionWorkflow) buttonModel.getActionType()).getAction().getLiteral(); |
| } |
| if (buttonModel.getActionType() instanceof ActionGeneric) { |
| actionId = ((ActionGeneric) buttonModel.getActionType()).getAction(); |
| } |
| return dslMetadataService.getClassURI(pkgName + "." + StringUtils.capitalize(toolBarName) + StringUtils.capitalize(actionId) + "Action", "action"); |
| } |
| |
| @Override |
| public String getToolbarId() { |
| return toolbarId; |
| } |
| |
| } |