IBM Support

How to programmatically clone shape appearance from one view including its size

Question & Answer


Question

This technote shows an example of how you can create a pluglet using IBM Rational Software Modeler and IBM Rational Software Architect that will clone the appearance properties including its size from the first selected view in a diagram

Cause

The Apply Appearance Properties feature (available from the toolbar or the right click menu) does not clone the dimension of the selected views, as it is not part of the appearance properties.

Answer

Attached to this technote is a zip called AppearanceCloner.zip which contains a pluglet example which shows how you can apply Appearance properties and dimension using a pluglet:

To see how to import and run the attached pluglet project, refer to the technote 1382031--Importing and using pluglets.

To see how to create a pluglet that modifies a model, refer to the technote 1257492--How to create a pluglet that modifies a model.

The class com.ibm.rational.support.CloneEditPartAppearanceProperties.java contains the code for you to review:

Disclaimer

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


/*


 * Licensed Materials - Use restricted, please refer to terms
 * and conditions in the IBM International Program License Agreement.
 *
 * © Copyright IBM Corporation 2003, 2004. All Rights Reserved.
 */
package com.ibm.rational.support;

import java.util.Hashtable;
import java.util.Iterator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.gmf.runtime.diagram.ui.services.layout.ILayoutNode;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.Style;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.NamedElement;

import com.ibm.xtools.modeler.ui.UMLModeler;
import com.ibm.xtools.pluglets.Pluglet;

public class CloneEditPartAppearanceProperties extends Pluglet {

/**
* Walk the selected objects and log them to the console
*/
public void plugletmain(String[] args) {

IWorkbenchPart aWPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
ISelection selection = aWPart.getSite().getPage().getSelection();

if (aWPart instanceof DiagramEditor) {
DiagramEditor anEditor = (DiagramEditor) aWPart;
TransactionalEditingDomain editDomain = anEditor.getEditingDomain();
Diagram theDiag = null;
StructuredSelection selected = (StructuredSelection) selection;
            if (selected.size()>1){
IGraphicalEditPart aSourcePart = (IGraphicalEditPart) selected.getFirstElement();
out.println("Source View:\n -> "
+ ((NamedElement) aSourcePart.getNotationView().getElement()).getQualifiedName());
for (Iterator iter = selected.iterator(); iter.hasNext();) {
Object obj = iter.next();

if (obj instanceof IGraphicalEditPart) {
if (!selected.getFirstElement().equals(obj)) {
IGraphicalEditPart aClonePart = (IGraphicalEditPart) obj;
processCloning(editDomain, aClonePart, aSourcePart, anEditor);
theDiag = aClonePart.getNotationView().getDiagram();
}
}
}
out.println("\nSaved editor for diagram: " + theDiag.getName());
anEditor.doSave(null);
aWPart.getSite().getPage().closeEditor(anEditor, true);
UMLModeler.getUMLDiagramHelper().openDiagramEditor(theDiag);

            } else {            
    MessageBox msgBoxEnd = new MessageBox(Display.getDefault().getActiveShell(), SWT.END);
    msgBoxEnd.setMessage("Select more than one view in the diagram\nto process with the pluglet !");
    msgBoxEnd.setText("Wrong selection of views !");
    msgBoxEnd.open();
            }
           
} else {
MessageBox msgBoxEnd = new MessageBox(Display.getDefault().getActiveShell(), SWT.END);
msgBoxEnd.setMessage("Diagram editor needs to be opened \nto process with the pluglet !");
msgBoxEnd.setText("An UML diagram Editor not active !");
msgBoxEnd.open();
}
out.println("--- Pulglet ended ----");
}

/**
* @param ted
* @param aClonePart
* @param aSourcePart
* @param anEditor
* 
*/
public void processCloning(final TransactionalEditingDomain ted, final IGraphicalEditPart aClonePart,
final IGraphicalEditPart aSourcePart, final DiagramEditor anEditor) {
ted.getCommandStack().execute(new RecordingCommand(ted) {
protected void doExecute() {
View aSrcView = aSourcePart.getNotationView();
View aCloneView = aClonePart.getNotationView();
out.println("Applying source properties to View:\n -> "+ ((NamedElement) aCloneView.getElement()).getQualifiedName());
if (aCloneView instanceof Node && aSrcView instanceof Node) {
Style aStyle2Copy = getShapeStyle2Copy(aSrcView);
ILayoutNode aNodeLayout2copy = getLayout2Copy(aSrcView);
Hashtable compartments2Copy = getCompartments2Show(aSrcView);
// style
setStyle(aStyle2Copy, aCloneView);
// dimension
setDimension(aNodeLayout2copy, aCloneView);
// compartments
setCompartmentVisbility(compartments2Copy, aCloneView);
}
}
});
//out.println("---> Done with processing of view: \n      "
//+ ((NamedElement) ClonePart.getNotationView().getElement()).getQualifiedName());
}

/**
* @param firstSelected
* @return Style
       *
       * Method to get the style of the view to copy
*/
private Style getShapeStyle2Copy(Object firstSelected) {
if (firstSelected instanceof View) {
View viewToCopyFrom = (View) firstSelected;
EList<Style> viewToCopyFromStyles = viewToCopyFrom.getStyles();
for (Iterator<Style> iter = viewToCopyFromStyles.iterator(); iter.hasNext();) {
Object aView2CopyObj = iter.next();
if (aView2CopyObj instanceof Style) {
Style aView2CopyStyle = (Style) aView2CopyObj;
return aView2CopyStyle;
}
}
}
return null;
}

/**
* @param Style2Copy
* @param View
* 
* Method to set the style of first view to the clone view
* 
*/
private void setStyle(Style aStyle2Copy, View aView) {
aView.getStyles().clear();
Style newStyle4View = (Style) EcoreUtil.copy(aStyle2Copy);
aView.getStyles().add(newStyle4View);
}

/**
* @param firstSelected
* @return ILayoutNode 
       * Method to get the layout / dimension of the view to copy
*
*/
private ILayoutNode getLayout2Copy(Object firstSelected) {
if (firstSelected instanceof Node) {
Node viewToCopyFrom = (Node) firstSelected;
ILayoutNode layoutNode2Copy = UMLModeler.getUMLDiagramHelper().getLayoutNode(viewToCopyFrom);
return layoutNode2Copy;
}
return null;
}

/**
* 
* @param aNodeLayout2copy
* @param aView
* 
* Method to set the dimension to the clone view
* 
*/
private void setDimension(ILayoutNode aNodeLayout2copy, View aView) {
LayoutConstraint layoutConstraint = ((Node) aView).getLayoutConstraint();
if (layoutConstraint instanceof Bounds) {
Bounds bounds = (Bounds) layoutConstraint;
bounds.setHeight(aNodeLayout2copy.getHeight());
bounds.setWidth(aNodeLayout2copy.getWidth());
}
}

/**
* 
* @param compartments2Copy
* @param aView
* 
* Method to set the Compartment visibility to the clone view
* 
*/
private void setCompartmentVisbility(Hashtable compartments2Copy, View aView) {
for (Iterator anIter = aView.getChildren().iterator(); anIter.hasNext();) {
Object childObj = anIter.next();
Node childView = (Node) childObj;
if (compartments2Copy.get(childView.getType()) != null) {
childView.setVisible((Boolean) compartments2Copy.get(childView.getType()));
}
}
}

/**
* 
* @param firstSelected
* @return Hashtable
* 
*         Method to get the list of compartment which are visible
*/
private Hashtable<String, Boolean> getCompartments2Show(Object firstSelected) {
Hashtable<String, Boolean> compartments = new Hashtable<String, Boolean>();
if (firstSelected instanceof Node) {
Node viewToCopyFrom = (Node) firstSelected;
viewToCopyFrom.getType();
for (Iterator anIter = viewToCopyFrom.getChildren().iterator(); anIter.hasNext();) {
Object childrenObj = anIter.next();
Node childNode = (Node) childrenObj;
compartments.put(childNode.getType(), childNode.isVisible());

}
return compartments;
}
return null;
}
}


AppearanceCloner.zip

[{"Product":{"code":"SSCLKU","label":"Rational Software Modeler"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Extensibility","Platform":[{"code":"PF033","label":"Windows"}],"Version":"7.5.5;7.5.4;7.5.3;7.5.2;7.5.1;7.5","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SS4JE2","label":"Rational Software Architect Standard Edition"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Extensibility","Platform":[{"code":"PF033","label":"Windows"}],"Version":"7.5;7.5.1;7.5.2;7.5.3;7.5.4;7.5.5","Edition":"","Line of Business":{"code":"LOB36","label":"IBM Automation"}},{"Product":{"code":"SS4JCV","label":"Rational Software Architect for WebSphere Software"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Extensibility","Platform":[{"code":"PF033","label":"Windows"}],"Version":"7.5;7.5.1;7.5.2;7.5.3;7.5.4;7.5.5","Edition":"","Line of Business":{"code":"LOB15","label":"Integration"}}]

Document Information

Modified date:
23 June 2018

UID

swg21409788