blob: 17d03e35122fa8b0701084430b9b256be1497429 [file] [log] [blame]
/*****************************************************************************
* 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.classtextualedition.classcompartments;
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.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.uml.diagram.clazz.lf.classtextualedition.messages.Messages;
//import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassEditPart;
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 UmlClassResizer singleton;
private static String RESIZE = Messages.UmlClassResizer_RESIZE;
private UmlClassResizer() {
}
public static UmlClassResizer getInstance() {
if (singleton == null) {
singleton = new UmlClassResizer();
}
return singleton;
}
/**
* 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
*/
public void resizeHeight(ClassEditPart umlClassEditPart, int height) throws ServiceException {
if (umlClassEditPart.getModel() instanceof org.eclipse.gmf.runtime.notation.Node) {
resizeHeight(Node.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(Node 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
*/
protected 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(), Integer.valueOf(height));
}
}