| /***************************************************************************** |
| * Copyright (c) 2019 CEA LIST. |
| * |
| * |
| * 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: |
| * Xavier Le Pallec (for CEA LIST) xlepallec@lilo.org - Bug 558456 |
| * |
| *****************************************************************************/ |
| |
| package org.eclipse.papyrus.uml.diagram.clazz.lf.autosizeableclasses.edit.policies.utils; |
| |
| import org.eclipse.emf.transaction.RecordingCommand; |
| import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| import org.eclipse.gmf.runtime.notation.LayoutConstraint; |
| import org.eclipse.gmf.runtime.notation.NotationPackage; |
| import org.eclipse.gmf.runtime.notation.Shape; |
| import org.eclipse.papyrus.infra.core.services.ServiceException; |
| import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject; |
| import org.eclipse.papyrus.uml.diagram.common.editparts.ClassEditPart; |
| |
| /** |
| * The class UmlClassResizer allows to propose a static-like method |
| * "resizeHeight" without using static methods. Mechanisms well fitted to unit |
| * tests. The class uses an instance to do this. |
| * |
| */ |
| public class UmlClassResizer { |
| |
| private static final UmlClassResizer INSTANCE = new UmlClassResizer(); |
| |
| /** |
| * @return |
| */ |
| public static UmlClassResizer getInstance() { |
| return INSTANCE; |
| } |
| |
| /** |
| * See |
| * {@link UmlClassResizer#resizeHeight(LayoutConstraint, TransactionalEditingDomain, int)} |
| * for full description of meaning of resizeHeight. The method here replaces the |
| * transactional editing domain by the global one and the layoutConstraint |
| * parameter by the class (editPart - GEF level) including it. |
| * |
| * @param umlClassEditPart the class (editPart - GEF level) that includes |
| * indirectly the layoutConstraint |
| * @param height the new height of the class (notation level) |
| * @throws ServiceException if the transaction cannot be done |
| */ |
| protected void resizeHeight(ClassEditPart umlClassEditPart, int height) throws ServiceException { |
| resizeHeight(Shape.class.cast(umlClassEditPart.getModel()), height); |
| } |
| |
| /** |
| * See |
| * {@link UmlClassResizer#resizeHeight(LayoutConstraint, TransactionalEditingDomain, int)} |
| * for full description of meaning of resizeHeight. The method here replaces the |
| * transactional editing domain by the global one and the layoutConstraint |
| * parameter by the class (notation level) including it. |
| * |
| * @param umlClass the class (notation level) that includes the layoutConstraint |
| * @param height the new height of the class (notation level) |
| * @throws ServiceException if the transaction cannot be done |
| */ |
| protected void resizeHeight(Shape umlClass, int height) throws ServiceException { |
| TransactionalEditingDomain editingDomain = ServiceUtilsForEObject.getInstance() |
| .getTransactionalEditingDomain(umlClass); |
| resizeHeight(umlClass.getLayoutConstraint(), editingDomain, height); |
| } |
| |
| /** |
| * See |
| * {@link UmlClassResizer#resizeHeight(LayoutConstraint, TransactionalEditingDomain, int)} |
| * for full description of meaning of resizeHeight. The method here replaces the |
| * transactional editing domain by the global one. |
| * |
| * @param layoutConstraint the layoutConstraint associated to the class |
| * @param height the new height of the class (notation level) |
| * @throws ServiceException if the transaction cannot be done |
| */ |
| public void resizeHeight(LayoutConstraint layoutConstraint, int height) throws ServiceException { |
| TransactionalEditingDomain editingDomain = ServiceUtilsForEObject.getInstance() |
| .getTransactionalEditingDomain(layoutConstraint); |
| resizeHeight(layoutConstraint, editingDomain, height); |
| } |
| |
| /** |
| * Changing the height of a class is done by setting the height of the |
| * layoutConstraint associated to the class (notation level). This setting |
| * operation must be done through a transaction and so needs a |
| * TransactionalEditingDomain. These are the 3 required parameters: |
| * layoutConstraint, editingDomain and a height. |
| * |
| * @param layoutConstraint the layoutConstraint associated to the class |
| * @param editingDomain the transactional editing Domain required to perform |
| * a command |
| * @param height the new height of the class (notation level) |
| * @throws ServiceException if the transaction cannot be done |
| */ |
| protected void resizeHeight(LayoutConstraint layoutConstraint, TransactionalEditingDomain editingDomain, int height) |
| throws ServiceException { |
| editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain, "Resize") { |
| |
| @Override |
| protected void doExecute() { |
| realResizeHeight(layoutConstraint, height); |
| } |
| }); |
| } |
| |
| /** |
| * This method is called by the transaction. It includes the real action to |
| * performed. So it only needs layoutConstraint and the new height. |
| * |
| * @param layoutConstraint the layoutConstraint associated to the class |
| * @param height the new height of the class (notation level) |
| */ |
| protected void realResizeHeight(LayoutConstraint layoutConstraint, int height) { |
| layoutConstraint.eSet(NotationPackage.eINSTANCE.getSize_Height(), height); |
| } |
| } |