Changes for page Graphical Modeling with Graphiti
Last modified by msp on 2025/01/30 12:03
Summary
-
Page properties (1 modified, 0 added, 0 removed)
-
Objects (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -62,7 +62,7 @@ 62 62 1. Include the new plugin in your Eclipse run configuration and start it. Create a Turing Machine diagram with your new wizard: //File//→ //New//→ //Other...//→ //Other//→ //Turing Machine//. This opens a Graphiti diagram editor for the new file, but you cannot do anything in that editor, since the palette is still empty. 63 63 1. In order to open a previously created {{code language="none"}}tudi{{/code}} file, right-click it → //Open With//→ //Other...//→ //Graphiti Diagram Editor//. This setting for {{code language="none"}}tudi{{/code}} files will be saved in your workspace preferences. 64 64 65 -= Creating and Adding Elements =65 += Creating and Adding Shapes = 66 66 67 67 The next step is to write so-called //features// for creating and adding elements to the diagrams. Each type of graphical element requires a //create// feature for the creation of corresponding meta model (//business model//) elements, and an //add// feature for adding a specific graphical representation to the diagram. A graphical representation is modeled with a so-called //pictogram element//, which contains a structure of //graphics algorithms// that specify how the element is rendered. 68 68 ... ... @@ -142,7 +142,7 @@ 142 142 {{/code}} 143 143 ))) 144 144 1. ((( 145 -Add the following feature class to your plugin and implement the {{code language="none"}}add{{/code}} method: 145 +Add the following feature class to your plugin and implement the {{code language="none"}}add{{/code}} method at the TODO note: 146 146 147 147 {{code theme="Eclipse" language="java"}} 148 148 import org.eclipse.graphiti.features.IFeatureProvider; ... ... @@ -188,8 +188,9 @@ 188 188 ContainerShape containerShape = Graphiti.getPeCreateService().createContainerShape( 189 189 context.getTargetContainer(), true); 190 190 191 - // TODO specify the concrete representation by adding at least one graphics algorithm 192 - 191 + // TODO specify the concrete representation by adding at least one graphics algorithm to the shape 192 + 193 + Graphiti.getPeCreateService().createChopboxAnchor(containerShape); 193 193 link(containerShape, context.getNewObject()); 194 194 return containerShape; 195 195 } ... ... @@ -218,3 +218,167 @@ 218 218 {{/code}} 219 219 ))) 220 220 1. Test the features in the diagram editor. You should now be able to select "State" in the editor's palette. Use this to create a few states. Note that if you create a state and later change the graphical representation in StateAddFeature, the previously created state will still look the same, since the add feature code is only applied to new states created from the palette. 222 + 223 += Creating and Adding Connections = 224 + 225 +While states can be represented as simple shapes, transitions are connections between two shapes. The process of creating such connections is similar to that for shapes. 226 + 227 +1. ((( 228 +Add the following feature class to your plugin, adapting references to meta model elements to your Turing Machine definition and implementing the TODO part: 229 + 230 +{{code theme="Eclipse" language="java"}} 231 +import org.eclipse.graphiti.features.IFeatureProvider; 232 +import org.eclipse.graphiti.features.context.ICreateConnectionContext; 233 +import org.eclipse.graphiti.features.context.impl.AddConnectionContext; 234 +import org.eclipse.graphiti.features.impl.AbstractCreateConnectionFeature; 235 +import org.eclipse.graphiti.mm.pictograms.Anchor; 236 +import org.eclipse.graphiti.mm.pictograms.Connection; 237 + 238 +import de.cau.cs.rtprak.login.turingmodel.State; 239 +import de.cau.cs.rtprak.login.turingmodel.Transition; 240 +import de.cau.cs.rtprak.login.turingmodel.TuringFactory; 241 + 242 +/** 243 + * A create feature for Turing Machine transitions. 244 + * 245 + * @author msp 246 + */ 247 +public class TransitionCreateFeature extends AbstractCreateConnectionFeature { 248 + 249 + /** 250 + * Constructor for a transition create feature. 251 + * 252 + * @param fp the feature provider for which the feature is created 253 + */ 254 + public TransitionCreateFeature(IFeatureProvider fp) { 255 + super(fp, "Transition", "Create a Transition"); 256 + } 257 + 258 + /** 259 + * Retrieve the state linked with the given anchor's parent. 260 + * 261 + * @param anchor an anchor for the source or target of the new connection 262 + * @return the corresponding state, or {@code null} if there is none 263 + */ 264 + private State getState(Anchor anchor) { 265 + if (anchor != null) { 266 + Object object = getBusinessObjectForPictogramElement(anchor.getParent()); 267 + if (object instanceof State) { 268 + return (State) object; 269 + } 270 + } 271 + return null; 272 + } 273 + 274 + /** 275 + * {@inheritDoc} 276 + */ 277 + public boolean canStartConnection(ICreateConnectionContext context) { 278 + return getState(context.getSourceAnchor()) != null; 279 + } 280 + 281 + /** 282 + * {@inheritDoc} 283 + */ 284 + public boolean canCreate(ICreateConnectionContext context) { 285 + return getState(context.getSourceAnchor()) != null 286 + && getState(context.getTargetAnchor()) != null; 287 + } 288 + 289 + /** 290 + * {@inheritDoc} 291 + */ 292 + public Connection create(ICreateConnectionContext context) { 293 + State source = getState(context.getSourceAnchor()); 294 + State target = getState(context.getTargetAnchor()); 295 + if (source == null || target == null) { 296 + throw new IllegalStateException("Cannot retrieve the source or target."); 297 + } 298 + 299 + // TODO create new transition with the specified source and target state 300 + 301 + AddConnectionContext addContext = new AddConnectionContext(context.getSourceAnchor(), 302 + context.getTargetAnchor()); 303 + addContext.setNewObject(transition); 304 + return (Connection) getFeatureProvider().addIfPossible(addContext); 305 + } 306 + 307 +} 308 +{{/code}} 309 +))) 310 +1. ((( 311 +Add the following method to {{code language="none"}}TuringFeatureProvider{{/code}}, adding more content to the diagram editor's palette: 312 + 313 +{{code theme="Eclipse" language="java"}} 314 +/** 315 + * {@inheritDoc} 316 + */ 317 +@Override 318 +public ICreateConnectionFeature[] getCreateConnectionFeatures() { 319 + return new ICreateConnectionFeature[] { new TransitionCreateFeature(this) }; 320 +} 321 +{{/code}} 322 +))) 323 +1. ((( 324 +Add the following feature class to your plugin and implement the {{code language="none"}}add{{/code}} method at the TODO note: 325 + 326 +{{code theme="Eclipse" language="java"}} 327 +import org.eclipse.graphiti.features.IFeatureProvider; 328 +import org.eclipse.graphiti.features.context.IAddConnectionContext; 329 +import org.eclipse.graphiti.features.context.IAddContext; 330 +import org.eclipse.graphiti.features.impl.AbstractAddFeature; 331 +import org.eclipse.graphiti.mm.pictograms.Connection; 332 +import org.eclipse.graphiti.mm.pictograms.PictogramElement; 333 +import org.eclipse.graphiti.services.Graphiti; 334 +import org.eclipse.graphiti.services.IGaService; 335 + 336 +import de.cau.cs.rtprak.login.turingmodel.Transition; 337 + 338 +/** 339 + * An add feature for Turing Machine transitions. 340 + * 341 + * @author msp 342 + */ 343 +public class TransitionAddFeature extends AbstractAddFeature { 344 + 345 + /** 346 + * Constructor for a transition add feature. 347 + * 348 + * @param fp the feature provider for which the feature is created 349 + */ 350 + public TransitionAddFeature(IFeatureProvider fp) { 351 + super(fp); 352 + } 353 + 354 + /** 355 + * {@inheritDoc} 356 + */ 357 + public boolean canAdd(IAddContext context) { 358 + return context instanceof IAddConnectionContext 359 + && context.getNewObject() instanceof Transition; 360 + } 361 + 362 + /** 363 + * {@inheritDoc} 364 + */ 365 + public PictogramElement add(IAddContext context) { 366 + IAddConnectionContext addConnContext = (IAddConnectionContext) context; 367 + Connection connection = Graphiti.getPeCreateService().createFreeFormConnection(getDiagram()); 368 + connection.setStart(addConnContext.getSourceAnchor()); 369 + connection.setEnd(addConnContext.getTargetAnchor()); 370 + 371 + // TODO specify the concrete representation by adding at least one graphics algorithm to the connection 372 + 373 + link(connection, context.getNewObject()); 374 + return connection; 375 + } 376 + 377 +} 378 +{{/code}} 379 + 380 +Use {{code language="none"}}Graphiti.getGaService(){{/code}} to get a service class for creating graphics algorithms and modifying their properties. You are free to design your model elements as you like. Find more information on how to solve this task in the official [[Graphiti Tutorial>>url:http://help.eclipse.org/juno/nav/23_1||shape="rect"]]. 381 +))) 382 +1. Register the {{code language="none"}}TransitionAddFeature{{/code}} in the {{code language="none"}}getAddFeature{{/code}} method of the {{code language="none"}}TuringFeatureProvider{{/code}} in the same way as done before for the {{code language="none"}}StateAddFeature{{/code}}. 383 +1. Test the features in the diagram editor. The palette should now contain an entry for creating transitions. 384 + 385 +\\
- Confluence.Code.ConfluencePageClass[0]
-
- Id
-
... ... @@ -1,1 +1,1 @@ 1 -360458 31 +3604585 - URL
-
... ... @@ -1,1 +1,1 @@ 1 -https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/360458 3/Graphical Modeling with Graphiti1 +https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/3604585/Graphical Modeling with Graphiti