Home » Modeling » Graphiti » Creating a new Diagram
Creating a new Diagram [message #1072355] |
Mon, 22 July 2013 15:29 |
Thomas Thonhofer Messages: 25 Registered: July 2013 |
Junior Member |
|
|
Hello
I have created an editor with Graphiti. So far i have only tested it with the default Graphiti Wizard, creating the standard .diagram file. Now I want to use my own wizard to create a custom file, containing a diagram and open my editor. I already have a wizard that creates a file. What I don't know is, how I should create a new diagram of my existing diagram type, use the file as a resource for it and open my editor. I already have a subclass of DiagramEditor, but I have no idea how to use it for creating my custom diagram. I didn't find any real help so far.
Below is the performFinish method of my wizard. The diagram should be created within the createDiagram method of the DiagramCreator class. I just don't know what this method should look like.
public boolean performFinish() {
final String fileName = page1.getFileName();
final IResource container = page1.getDiagramContainer();
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException {
try {
IPath path = container.getFullPath().append(fileName);
URI uri = URI.createPlatformResourceURI(path.toString(), true);
DiagramCreator.createDiagram(uri, targetNamespace);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
getContainer().run(true, false, op);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
MessageDialog.openError(getShell(), "Error", realException.getMessage());
return false;
}
return true;
}
I really hope someone can help me out. If possible, some code snippets or a comprehensible example would be very appreciated.
Thank you.
|
|
| | | | | |
Re: Creating a new Diagram [message #1077353 is a reply to message #1072355] |
Thu, 01 August 2013 15:26 |
Thomas Thonhofer Messages: 25 Registered: July 2013 |
Junior Member |
|
|
I use a normal add method for my pictogram elements, similar to the graphiti tutorial. It is called from the create method when I add a new Element.
@Override
public PictogramElement add(IAddContext context) {
final Activity addedClass = (Activity) context.getNewObject();
final Diagram targetDiagram = (Diagram) context.getTargetContainer();
if (addedClass.getId() == null){
Activator activator = Activator.getDefault();
Shell shell = activator.getWorkbench().getActiveWorkbenchWindow().getShell();
InputDialog n = new InputDialog(shell, "ID input", "Please insert ID", "", null);
n.open();
addedClass.setId(n.getValue());
}
// CONTAINER SHAPE WITH ROUNDED RECTANGLE
final IPeCreateService peCreateService = Graphiti.getPeCreateService();
final ContainerShape containerShape = peCreateService.createContainerShape(targetDiagram, true);
// check whether the context has a size (e.g. from a create feature)
// otherwise define a default size for the shape
final int width = context.getWidth() <= 0 ? 120 : context.getWidth();
final int height = context.getHeight() <= 0 ? 70 : context.getHeight();
IGaService gaService = Graphiti.getGaService();
RoundedRectangle roundedRectangle; // need to access it later
{
// create and set graphics algorithm
roundedRectangle = gaService.createRoundedRectangle(containerShape, 5, 5);
roundedRectangle.setForeground(manageColor(FOREGROUND));
roundedRectangle.setBackground(manageColor(BACKGROUND));
roundedRectangle.setLineWidth(2);
gaService.setLocationAndSize(roundedRectangle, context.getX(), context.getY(), width, height);
// create link and wire it
link(containerShape, addedClass);
// SHAPE WITH LINE
{
// create shape for line
Shape shape = peCreateService.createShape(containerShape, false);
// create and set graphics algorithm
Polyline polyline = gaService.createPolyline(shape, new int[] { 0, 20, width, 20 });
polyline.setForeground(manageColor(FOREGROUND));
polyline.setLineWidth(2);
}
{
// create shape for text
Shape shape = peCreateService.createShape(containerShape, false);
// create and set text graphics algorithm
Text text = gaService.createText(shape, addedClass.getId());
text.setForeground(manageColor(TEXT_FOREGROUND));
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER );
// vertical alignment has as default value "center"
text.setFont(gaService.manageDefaultFont(getDiagram(), false, true));
gaService.setLocationAndSize(text, -40, 20, width, 20);
// create link and wire it
link(shape, addedClass);
}
// SHAPE WITH TEXT
{
// create shape for text
Shape shape = peCreateService.createShape(containerShape, false);
// create and set text graphics algorithm
Text text = gaService.createText(shape, "Activity");
text.setForeground(manageColor(TEXT_FOREGROUND));
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER );
// vertical alignment has as default value "center"
text.setFont(gaService.manageDefaultFont(getDiagram(), false, true));
gaService.setLocationAndSize(text, 0, 0, width, 20);
// create link and wire it
link(shape, addedClass);
}
ChopboxAnchor ca = peCreateService.createChopboxAnchor(containerShape);
link(ca, addedClass);
// call the layout feature
layoutPictogramElement(containerShape);
return containerShape;
}
The method basically works when I use the default graphiti wizard. The problem of the pictogram elements not being added to the canvas, only occurs when I create and open my own diagram and file.
|
|
| |
Re: Creating a new Diagram [message #1079905 is a reply to message #1072355] |
Mon, 05 August 2013 08:13 |
Thomas Thonhofer Messages: 25 Registered: July 2013 |
Junior Member |
|
|
For opening my editor I created my own PersistencyBehavior class and a loadDiagram method. For now I don't want to save the diagram persistently. I only save the business model and create a new diagram and its pictogram elements on opening the editor. So I create the new diagram within the loadDiagram Method. After that I create the root object of my business model (called EMFContainer) and link it with the diagram.
@Override
public Diagram loadDiagram(URI uri) {
Diagram diagram = Graphiti.getPeCreateService().createDiagram("infinica_process_engine_designer", uri.toString(), true);
EMFContainer c = ContainerFactory.eINSTANCE.createEMFContainer();
PictogramsFactory pictogramsFactory = PictogramsFactory.eINSTANCE;
PictogramLink link = pictogramsFactory.createPictogramLink();
diagram.setLink(link);
link.getBusinessObjects().add(c);
This method is executed correctly and the editor opens, but the pictogram elements I add afterwards, don't appear. This is only a temporary solution, but I don't see, why it should not work.
Here are also my DiagramEditor, DiagramEditorInput and DiagramBehavior classes, if thats any help.
public class InfinicaEditor extends DiagramEditor
{
public static final String DIAGRAMID = "infinica_process_engine_designer.InfinicaDiagramType";
public static final String EDITOR_ID = "infinica-process-engine-designer.editor.InfinicaEditor";
@Override
protected DiagramEditorInput convertToDiagramEditorInput(IEditorInput input)
throws PartInitException
{
if(input instanceof IURIEditorInput)
{
return createNewInfinicaEditorInput((IEditorSite) getSite(), (IURIEditorInput) input);
}
else
{
return super.convertToDiagramEditorInput(input);
}
}
private DiagramEditorInput createNewInfinicaEditorInput(IEditorSite site,
IURIEditorInput input)
{
URI uri = URI.createURI(input.getURI().toString());
InfinicaDiagramEditorInput infinput = new InfinicaDiagramEditorInput(uri);
return infinput;
}
@Override
protected InfinicaDiagramBehavior createDiagramBehavior()
{
return new InfinicaDiagramBehavior(this);
}
}
public final class InfinicaDiagramEditorInput extends DiagramEditorInput {
private URI uri;
public InfinicaDiagramEditorInput(URI uri)
{
super(uri, InfinicaEditor.DIAGRAMID);
this.uri = uri;
}
@Override
public URI getUri()
{
return uri;
}
@Override
public void updateUri(URI uri)
{
this.uri = uri;
}
}
public class InfinicaDiagramBehavior extends DiagramBehavior
{
public InfinicaDiagramBehavior(IDiagramContainerUI diagramContainer)
{
super(diagramContainer);
}
@Override
protected DefaultPersistencyBehavior createPersistencyBehavior() {
return new InfinicaPersistencyBehavior(this);
}
}
|
|
| |
Re: Creating a new Diagram [message #1080878 is a reply to message #1072355] |
Tue, 06 August 2013 13:39 |
Thomas Thonhofer Messages: 25 Registered: July 2013 |
Junior Member |
|
|
Thank you very much Michael,
I finnally found the cause of my problem. "a resource the editor knows" was the tip I needed. My diagram was added to a resource and an editing domain, but they had no connection to the editor. I fixed that and now it works fine.
After this problem is out of the way, I already have another one:
I used the method "link(PictogramElement, BusinessObject)" of the feature provider in a few places, in order to link my Pictogram Elements. Now I realised that it is not working anymore and I have no idea why. The method is called and executed correctly as far as I can tell, but the link, that should be created doesn't exist afterwards.
I need this method because I have non-EMF objects and it uses an independenceSolver for linking them.
So I either need to find out, why the link method of my featureProvider isn't working or I need a new method that is able to link a PictogramElement with a non-EMF object (with or without an independenceSolver)
I hope someone can help me out with this.
Thank you.
[Updated on: Tue, 06 August 2013 13:41] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Wed Sep 25 15:41:45 GMT 2024
Powered by FUDForum. Page generated in 0.04188 seconds
|