package com.nomagic.magicdraw.examples.mypatterns; import com.nomagic.magicdraw.core.Application; import com.nomagic.magicdraw.core.Project; import com.nomagic.magicdraw.openapi.uml.ReadOnlyElementException; import com.nomagic.magicdraw.patterns.AbstractPattern; import com.nomagic.magicdraw.patterns.AbstractPatternProperties; import com.nomagic.magicdraw.patterns.PatternHelper; import com.nomagic.magicdraw.patterns.Target; import com.nomagic.magicdraw.properties.Property; import com.nomagic.magicdraw.properties.PropertyManager; import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Operation; import com.nomagic.uml2.impl.ElementsFactory; public class MyPattern extends AbstractPattern { /** * Returns the categorized name of the pattern. * * @return name of the pattern {"Common", "MyPattern"}. */ @Override public String[] getCategorizedName() { return new String[]{"Common", "My Pattern"}; } /** * Applies design pattern to the target, using properties, passed as an argument. * * @param target Target, the pattern is applied for. * @param prop the pattern properties. */ @Override public void applyPattern(Target target, AbstractPatternProperties prop) throws ReadOnlyElementException { PropertyManager propManager = prop.getPropertyManager(); Project project = Application.getInstance().getProject(); if (project != null) { ElementsFactory ef = project.getElementsFactory(); Operation op = ef.createOperationInstance(); Property property = propManager.getProperty(MyPatternProperties.METHOD_NAME); if (property != null) { String methodName = property.getValue().toString(); op.setName(methodName); PatternHelper.addDistinctOperation(target.getTargetClassifier(), op); } } } }