| /** |
| * |
| * 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 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: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.vaadin.emf.actions; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.emf.common.command.Command; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EStructuralFeature; |
| import org.eclipse.emf.edit.command.CommandParameter; |
| import org.eclipse.emf.edit.command.CreateChildCommand; |
| import org.eclipse.emf.edit.command.RemoveCommand; |
| import org.eclipse.emf.edit.domain.EditingDomain; |
| import org.eclipse.osbp.runtime.web.vaadin.common.resource.IResourceProvider; |
| import org.eclipse.osbp.vaadin.emf.api.IModelingContext; |
| import org.eclipse.osbp.vaadin.emf.data.EmfModelTreeContainer; |
| |
| import com.vaadin.event.Action; |
| import com.vaadin.server.Resource; |
| import com.vaadin.ui.AbstractSelect; |
| |
| /** |
| * A Vaadin ActionHandler that is aware about all containment references that |
| * can be created for a single EObject. |
| */ |
| @SuppressWarnings("serial") |
| public class EObjectActionHandler implements Action.Handler { |
| |
| private EditCallback callback; |
| private IModelingContext modelingContext; |
| private IResourceProvider resourceProvider; |
| |
| public EObjectActionHandler(IModelingContext modelingContext, |
| IResourceProvider resourceProvider, EditCallback callback) { |
| this.modelingContext = modelingContext; |
| this.resourceProvider = resourceProvider; |
| this.callback = callback; |
| } |
| |
| /** |
| * Gets the list of actions applicable to this handler. |
| * |
| * @param target |
| * the target handler to list actions for. For item containers |
| * this is the item id. |
| * @param sender |
| * the party that would be sending the actions. Most of this is |
| * the action container. |
| * @return the list of Action |
| */ |
| @SuppressWarnings("unchecked") |
| public Action[] getActions(Object target, Object sender) { |
| |
| List<CustomAction> result = new ArrayList<>(); |
| if (target != null) { |
| CustomAction editAction = new CustomAction(Commands.EDIT_DIALOG, |
| "Edit", null, null); |
| editAction.modelElement = (EObject) target; |
| result.add(editAction); |
| |
| result.add(new CustomAction(Commands.REFRESH_NODE, "Refresh", null, |
| null)); |
| |
| if (canDelete(target)) { |
| result.add(new CustomAction(Commands.DELETE, "Delete", null, |
| null)); |
| } |
| |
| if (modelingContext.getCommandStack().canUndo()) { |
| result.add(new CustomAction(Commands.UNDO, "Undo", null, null)); |
| } |
| |
| if (modelingContext.getCommandStack().canRedo()) { |
| result.add(new CustomAction(Commands.REDO, "Redo", null, null)); |
| } |
| } |
| |
| return result.toArray(new Action[result.size()]); |
| } |
| |
| private boolean canDelete(Object target) { |
| Command command = RemoveCommand.create( |
| modelingContext.getEditingDomain(), target); |
| return command.canExecute(); |
| } |
| |
| /** |
| * Handles an action for the given target. The handler method may just |
| * discard the action if it's not suitable. |
| * |
| * @param action |
| * the action to be handled. |
| * @param sender |
| * the sender of the action. This is most often the action |
| * container. |
| * @param target |
| * the target of the action. For item containers this is the item |
| * id. |
| */ |
| public void handleAction(Action action, Object sender, Object target) { |
| CustomAction customAction = (CustomAction) action; |
| |
| EditingDomain domain = modelingContext.getEditingDomain(); |
| if (customAction.getCommand() == Commands.CREATE_CHILD) { |
| CommandParameter executeParam = new CommandParameter(target, |
| customAction.getParam().getFeature(), |
| customAction.getParam()); |
| Command command = domain.createCommand(CreateChildCommand.class, |
| executeParam); |
| domain.getCommandStack().execute(command); |
| } else if (customAction.getCommand() == Commands.DELETE) { |
| domain.getCommandStack().execute( |
| RemoveCommand.create(domain, target)); |
| } else if (customAction.getCommand() == Commands.UNDO) { |
| domain.getCommandStack().undo(); |
| } else if (customAction.getCommand() == Commands.REDO) { |
| domain.getCommandStack().redo(); |
| } else if (customAction.getCommand() == Commands.REFRESH_NODE) { |
| AbstractSelect select = (AbstractSelect) sender; |
| EmfModelTreeContainer container = (EmfModelTreeContainer) select |
| .getContainerDataSource(); |
| container.refreshNode(target); |
| } else if (customAction.getCommand() == Commands.EDIT_DIALOG) { |
| callback.edit(customAction.modelElement); |
| } |
| } |
| |
| /** |
| * Custom action wraps CommandParameter. |
| */ |
| private static class CustomAction extends Action { |
| |
| private final Commands command; |
| private final CommandParameter param; |
| private final EStructuralFeature feature; |
| private EObject modelElement; |
| |
| public CustomAction(Commands command, String caption, Resource icon, |
| CommandParameter param) { |
| super(caption, icon); |
| this.command = command; |
| this.param = param; |
| this.feature = null; |
| } |
| |
| public CustomAction(Commands command, String caption, Resource icon, |
| CommandParameter param, EStructuralFeature feature) { |
| super(caption, icon); |
| this.command = command; |
| this.param = param; |
| this.feature = feature; |
| } |
| |
| public CustomAction(Commands command, String caption, Resource icon) { |
| super(caption, icon); |
| this.command = command; |
| this.param = null; |
| this.feature = null; |
| } |
| |
| public CommandParameter getParam() { |
| return param; |
| } |
| |
| public Commands getCommand() { |
| return command; |
| } |
| |
| public EStructuralFeature getFeature() { |
| return feature; |
| } |
| |
| } |
| |
| enum Commands { |
| EDIT_DIALOG, CREATE_CHILD, DELETE, UNDO, REDO, COPY, PASTE, REFRESH_NODE |
| } |
| |
| public interface EditCallback { |
| void edit(EObject model); |
| } |
| } |