| /** |
| * Copyright (c) 2011, 2015 - 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 implementation |
| */ |
| package org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.Date; |
| import java.util.List; |
| |
| import org.eclipse.osbp.ecview.core.common.dnd.IDropTargetStrategy; |
| import org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart; |
| import org.eclipse.osbp.ecview.core.common.editpart.ILayoutEditpart; |
| import org.eclipse.osbp.ecview.core.common.model.core.CoreModelFactory; |
| import org.eclipse.osbp.ecview.core.common.model.core.YElement; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddable; |
| import org.eclipse.osbp.ecview.core.common.model.core.YEmbeddableEvent; |
| import org.eclipse.osbp.ecview.core.common.model.core.YLayout; |
| import org.eclipse.osbp.ecview.core.common.model.core.YMarginable; |
| import org.eclipse.osbp.ecview.core.common.model.core.YSpacingable; |
| import org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation; |
| import org.eclipse.osbp.runtime.designer.api.IDesignerService.DesignEvent; |
| import org.eclipse.osbp.runtime.designer.api.IDesignerService.EventType; |
| import org.eclipse.osbp.runtime.designer.api.IDesignerService.IDesignListener; |
| import org.eclipse.osbp.runtime.designer.api.IWidgetDesignConfigurator; |
| |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.HasComponents.ComponentAttachDetachNotifier; |
| import com.vaadin.ui.Layout.MarginHandler; |
| import com.vaadin.ui.Layout.SpacingHandler; |
| |
| import fi.jasoft.dragdroplayouts.client.ui.LayoutDragMode; |
| import fi.jasoft.dragdroplayouts.interfaces.DragFilter; |
| import fi.jasoft.dragdroplayouts.interfaces.LayoutDragSource; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * An abstract base class implementing {@link ILayoutPresentation}. |
| * |
| * @param <A> |
| * the generic type |
| */ |
| public abstract class AbstractLayoutPresenter<A extends Component> extends AbstractVaadinWidgetPresenter<A> |
| implements ILayoutPresentation<A>, IDesignListener { |
| |
| /** The children. */ |
| private List<IEmbeddableEditpart> children; |
| |
| /** The render lock. */ |
| private boolean renderLock; |
| |
| /** |
| * Instantiates a new abstract layout presenter. |
| * |
| * @param editpart |
| * the editpart |
| */ |
| public AbstractLayoutPresenter(ILayoutEditpart editpart) { |
| super(editpart); |
| } |
| |
| /** |
| * Returns the editpart the presenter will render for. |
| * |
| * @return the editpart |
| */ |
| protected ILayoutEditpart getEditpart() { |
| return (ILayoutEditpart) super.getEditpart(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#getModel() |
| */ |
| @Override |
| public YLayout getModel() { |
| return (YLayout) getEditpart().getModel(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation# |
| * getChildren() |
| */ |
| @Override |
| public List<IEmbeddableEditpart> getChildren() { |
| return children != null ? Collections.unmodifiableList(children) : Collections.<IEmbeddableEditpart>emptyList(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation# |
| * contains(org.eclipse.osbp.ecview.core.common.editpart. |
| * IEmbeddableEditpart) |
| */ |
| @Override |
| public boolean contains(IEmbeddableEditpart presentation) { |
| return children != null && children.contains(presentation); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation#add( |
| * org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart) |
| */ |
| @Override |
| public void add(IEmbeddableEditpart editPart) { |
| ensureChildren(); |
| |
| if (!children.contains(editPart)) { |
| children.add(editPart); |
| |
| if (!renderLock) { |
| internalAdd(editPart); |
| } |
| } |
| } |
| |
| /** |
| * This method is called after the editpart was successfully added to the |
| * children collection.<br> |
| * Subclasses should handle the add of the UI element in that method. |
| * |
| * @param presentation |
| * the presentation |
| */ |
| protected void internalAdd(IEmbeddableEditpart presentation) { |
| |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation# |
| * remove(org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart) |
| */ |
| @Override |
| public void remove(IEmbeddableEditpart editPart) { |
| if (children == null) { |
| return; |
| } |
| |
| if (children.remove(editPart) && !renderLock) { |
| internalRemove(editPart); |
| } |
| } |
| |
| /** |
| * This method is called after the editpart was successfully removed from |
| * the children collection.<br> |
| * Subclasses should handle the removal of the UI element in that method. |
| * |
| * @param presentation |
| * The presentation to be removed |
| */ |
| protected void internalRemove(IEmbeddableEditpart presentation) { |
| |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation# |
| * insert(org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart, |
| * int) |
| */ |
| @Override |
| public void insert(IEmbeddableEditpart editPart, int index) { |
| ensureChildren(); |
| |
| int currentIndex = children.indexOf(editPart); |
| if (currentIndex > -1 && currentIndex != index) { |
| throw new RuntimeException( |
| String.format("Insert at index %d not possible since presentation already contained at index %d", |
| index, currentIndex)); |
| } |
| |
| children.add(index, editPart); |
| |
| if (!renderLock) { |
| internalInsert(editPart, index); |
| } |
| } |
| |
| /** |
| * This method is called after the editpart was successfully inserted to the |
| * children collection.<br> |
| * Subclasses should handle the insert of the UI element in that method. |
| * |
| * @param editpart |
| * The editpart to be inserted |
| * @param index |
| * The index where the editpart should be inserted |
| */ |
| protected void internalInsert(IEmbeddableEditpart editpart, int index) { |
| |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation#move |
| * (org.eclipse.osbp.ecview.core.common.editpart.IEmbeddableEditpart, int) |
| */ |
| @Override |
| public void move(IEmbeddableEditpart editpart, int index) { |
| if (children == null) { |
| throw new RuntimeException("Move not possible. No children present."); |
| } |
| |
| if (!children.contains(editpart)) { |
| throw new RuntimeException( |
| String.format("Move to index %d not possible since presentation not added yet!", index)); |
| } |
| |
| int currentIndex = children.indexOf(editpart); |
| children.remove(editpart); |
| children.add(index, editpart); |
| |
| if (!renderLock) { |
| internalMove(editpart, currentIndex, index); |
| } |
| } |
| |
| /** |
| * This method is called after the presentation was successfully moved |
| * inside the children collection.<br> |
| * Subclasses should handle the move of the UI element in that method. |
| * |
| * @param presentation |
| * The presentation to be moved. |
| * @param oldIndex |
| * The old index where the control was located. |
| * @param newIndex |
| * The new index where the control should be located after the |
| * move operation. |
| */ |
| protected void internalMove(IEmbeddableEditpart presentation, int oldIndex, int newIndex) { |
| |
| } |
| |
| protected void initialize(Component component, YElement model) { |
| |
| super.initialize(component, model); |
| |
| if (component instanceof ComponentAttachDetachNotifier && model instanceof YLayout) { |
| ((ComponentAttachDetachNotifier) component).addComponentAttachListener(e -> { |
| YEmbeddableEvent event = CoreModelFactory.eINSTANCE.createYEmbeddableEvent(); |
| event.setTime(new Date()); |
| event.setEmbeddable((YEmbeddable) getModel()); |
| event.setRawEvent(e); |
| ((YLayout) model).setLastComponentAttach(event); |
| }); |
| ((ComponentAttachDetachNotifier) component).addComponentDetachListener(e -> { |
| YEmbeddableEvent event = CoreModelFactory.eINSTANCE.createYEmbeddableEvent(); |
| event.setTime(new Date()); |
| event.setEmbeddable((YEmbeddable) getModel()); |
| event.setRawEvent(e); |
| ((YLayout) model).setLastComponentDetach(event); |
| }); |
| } |
| } |
| |
| /** |
| * Returns true, if rendering should not be done. |
| * |
| * @return true, if is render lock |
| */ |
| public boolean isRenderLock() { |
| return renderLock; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation# |
| * updateCellStyle(org.eclipse.osbp.ecview.core.common.model.core. |
| * YEmbeddable) |
| */ |
| @Override |
| public void updateCellStyle(YEmbeddable child) { |
| // can be overridden by sub classes |
| } |
| |
| /** |
| * True, if rendering should not be done. |
| * |
| * @param renderLock |
| * the new render lock |
| */ |
| public void setRenderLock(boolean renderLock) { |
| this.renderLock = renderLock; |
| } |
| |
| /** |
| * Ensures, that the children collection exists. |
| */ |
| protected void ensureChildren() { |
| if (children == null) { |
| children = new ArrayList<IEmbeddableEditpart>(); |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#notify(org.eclipse.osbp.runtime.designer. |
| * api.IDesignerService.DesignEvent) |
| */ |
| @Override |
| public void notify(DesignEvent event) { |
| super.notify(event); |
| |
| if (getWidget() != null && getWidget() instanceof LayoutDragSource) { |
| LayoutDragSource dragSource = (LayoutDragSource) getWidget(); |
| if (event.getType() == EventType.ENABLED) { |
| dragSource.setDragMode(getDragMode()); |
| dragSource.setDragFilter(DragFilter.ALL); |
| } else { |
| dragSource.setDragMode(LayoutDragMode.NONE); |
| dragSource.setDragFilter(DragFilter.NONE); |
| } |
| } |
| |
| IWidgetDesignConfigurator service = getViewContext().getService(IWidgetDesignConfigurator.class.getName()); |
| if (service != null) { |
| if (event.getType() == EventType.ENABLED) { |
| service.configure(getWidget(), (YEmbeddable) getCastedModel(), true); |
| } else { |
| service.configure(getWidget(), (YEmbeddable) getCastedModel(), false); |
| } |
| } |
| } |
| |
| /** |
| * Is called to initialize the newly created component. |
| * |
| * @param component |
| * the component |
| * @param model |
| * the model |
| */ |
| protected void setupComponent(Component component, YElement model) { |
| super.setupComponent(component, model); |
| |
| if (component instanceof LayoutDragSource) { |
| LayoutDragSource source = (LayoutDragSource) component; |
| LayoutDragMode mode = getDragMode(); |
| source.setDragMode(getDragMode()); |
| if (mode == LayoutDragMode.NONE) { |
| source.setDragFilter(DragFilter.NONE); |
| } else { |
| source.setDragFilter(DragFilter.ALL); |
| } |
| } |
| |
| setupDropTarget(component, (YLayout) model); |
| |
| initialize(component, model); |
| } |
| |
| /** |
| * Creates a UI-Kit specific drop handler for the component. |
| * |
| * @param component |
| * the component |
| * @param yLayout |
| * the y layout |
| */ |
| protected void setupDropTarget(Component component, YLayout yLayout) { |
| IDropTargetStrategy strategy = getViewContext().getService(IDropTargetStrategy.class.getName()); |
| if (strategy != null) { |
| strategy.setupDropTarget(getViewContext(), component, yLayout); |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.osbp.ecview.core.common.presentation.ILayoutPresentation# |
| * updateSpacings() |
| */ |
| @Override |
| public void updateSpacings() { |
| if (getModel() instanceof YSpacingable) { |
| ((SpacingHandler) getWidget()).setSpacing(((YSpacingable) getModel()).isSpacing()); |
| } |
| if (getModel() instanceof YMarginable) { |
| ((MarginHandler) getWidget()).setMargin(((YMarginable) getModel()).isMargin()); |
| } |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.osbp.runtime.web.ecview.presentation.vaadin.common. |
| * AbstractVaadinWidgetPresenter#internalDispose() |
| */ |
| @Override |
| protected void internalDispose() { |
| try { |
| if (children != null) { |
| children.clear(); |
| children = null; |
| } |
| } finally { |
| super.internalDispose(); |
| } |
| } |
| } |