How to visualize elements programatically [message #703969] |
Thu, 28 July 2011 02:03  |
Eclipse User |
|
|
|
Hi all,
this is slighty relate to the post from Neslepeaks "How to create a diagram
element programmatically", but in fact completely different. I'm seeking for
the easiest and most convenient way to visualize already existing elements
in a UML model on a diagram programatically - in a way as if someone would
have dragged the element from the model explorer and dropped it onto the
canvas.
I guess it is somehow related with changing the notation file, but actually,
I'm not sure and I have not found any information on this neither in the
Papyrus SVN nor in the web. Could someone give a hint where to search or how
to achieve this, please?
Timothy
|
|
|
Re: How to visualize elements programatically [message #713468 is a reply to message #703969] |
Mon, 08 August 2011 10:01   |
Eclipse User |
|
|
|
Here is a part of a class which should help you.
You may perform some refactoring, but all you need should be there.
Take care to respect the copyright.
/*****************************************************************************
* Copyright (c) 2010 Atos Origin.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Vincent Hemery (Atos Origin}) {vincent.hemery@atosorigin.com} - Initial API and implementation
*
*****************************************************************************/
package org.topcased.requirement.bundle.papyrus.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.core.modelsetquery.ModelSetQuery;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.diagram.common.util.DiagramEditPartsUtil;
import org.eclipse.papyrus.resource.IModel;
import org.eclipse.papyrus.resource.ModelSet;
import org.eclipse.papyrus.resource.uml.UmlModel;
import org.eclipse.papyrus.sasheditor.contentprovider.IPageMngr;
import org.eclipse.papyrus.sasheditor.editor.AbstractMultiPageSashEditor;
import org.eclipse.papyrus.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.ui.IEditorPart;
import org.topcased.requirement.bundle.papyrus.Activator;
import org.topcased.requirement.bundle.papyrus.internal.Messages;
import org.topcased.requirement.bundle.papyrus.resource.RequirementModel;
import org.topcased.requirement.core.extensions.IEditorServices;
import org.topcased.requirement.core.views.AbstractRequirementPage;
/**
* This class provides services for using requirements with the papyrus core editor. It is intended to be used only by
* extension point org.topcased.requirement.core.supportingEditors.
*
* @author vhemery
*/
public class PapyrusEditorServices implements IEditorServices
{
[...]
/**
* Get the currently active Papyrus diagram editor.
*
* @return the editor part or null.
*/
public IEditorPart getActiveEditor()
{
return EditorUtils.getMultiDiagramEditor();
}
[...]
/**
* Sets the selection state for the editor to reveal the position of the given EObject.<br>
* It searches in the different diagrams the occurrences of the EObject and ask the user to select the destination
* if the given EObject has several graphical representation.
*
* @param editor the editor or null for currently active one
* @param listOfEObjects list of eobjects to select
* @param openDiagramIfNeeded if true, when the object is not in current active diagram, a representing diagram will
* open
*/
public void gotoEObjects(IEditorPart editor, List<EObject> listOfEObjects, boolean openDiagramIfNeeded)
{
if (editor == null)
{
editor = getActiveEditor();
}
if (editor instanceof IMultiDiagramEditor)
{
List<EditPart> editPartsToSelect = new ArrayList<EditPart>(listOfEObjects.size());
// find appropriate viewer and edit parts to select
EditPartViewer currentViewer = getGraphicalViewer(editor);
Map< ? , ? > currentRegistry = currentViewer.getEditPartRegistry();
if (openDiagramIfNeeded)
{
/*
* Reference diagrams which represent these elements to know the best one (with most represented
* elements)
*/
Map<Diagram, Integer> numberOfRepresentedElementsByDiagram = new HashMap<Diagram, Integer>();
for (EObject toSelect : listOfEObjects)
{
Set<Diagram> diagramsRepresentingThisObject = new HashSet<Diagram>();
List< ? > views = DiagramEditPartsUtil.getEObjectViews(toSelect);
for (Object view : views)
{
if (view instanceof View && !(view instanceof Diagram))
{
Diagram diagram = ((View) view).getDiagram();
// test if a representation has already been counted for this diagram
if (!diagramsRepresentingThisObject.contains(diagram))
{
// the element is represented in this diagram
diagramsRepresentingThisObject.add(diagram);
if (!numberOfRepresentedElementsByDiagram.containsKey(diagram))
{
numberOfRepresentedElementsByDiagram.put(diagram, 0);
}
Integer num = numberOfRepresentedElementsByDiagram.get(diagram);
numberOfRepresentedElementsByDiagram.put(diagram, num + 1);
}
}
}
}
// find diagram where the largest number of elements are available
Diagram bestDiagram = null;
int numberOfRepresented = 0;
for (Entry<Diagram, Integer> entry : numberOfRepresentedElementsByDiagram.entrySet())
{
if (entry.getValue() > numberOfRepresented)
{
// take better diagram
bestDiagram = entry.getKey();
numberOfRepresented = entry.getValue();
}
else if (entry.getValue() == numberOfRepresented)
{
// take the currently opened diagram if it is as good
if (currentRegistry.get(entry.getKey()) != null)
{
bestDiagram = entry.getKey();
}
}
}
// change active diagram if necessary
if (bestDiagram != null && currentRegistry.get(bestDiagram) == null)
{
Object pageMngr = editor.getAdapter(IPageMngr.class);
if (pageMngr instanceof IPageMngr)
{
IPageMngr pageManager = (IPageMngr) pageMngr;
if (pageManager.isOpen(bestDiagram))
{
// bring to top
pageManager.closePage(bestDiagram);
pageManager.openPage(bestDiagram);
}
else
{
// open
pageManager.openPage(bestDiagram);
}
}
currentViewer = getGraphicalViewer(editor);
currentRegistry = currentViewer.getEditPartRegistry();
}
}
// select edit parts in the current viewer
for (EObject toSelect : listOfEObjects)
{
List< ? > views = DiagramEditPartsUtil.getEObjectViews(toSelect);
for (Object view : views)
{
if (currentRegistry.containsKey(view))
{
Object part = currentRegistry.get(view);
if (part instanceof EditPart)
{
editPartsToSelect.add((EditPart) part);
}
}
}
}
// select edit parts if selection has to change
if (!(editPartsToSelect.containsAll(currentViewer.getSelectedEditParts()) && currentViewer.getSelectedEditParts().contains(editPartsToSelect)))
{
boolean firstSelectedPartInViewer = true;
for (EditPart editPart : editPartsToSelect)
{
if (firstSelectedPartInViewer)
{
currentViewer.select(editPart);
firstSelectedPartInViewer = false;
}
else
{
currentViewer.appendSelection(editPart);
}
}
}
}
}
[...]
/**
* Get the graphical viewer of the editor.
*
* @param editor the editor or null for currently active one
* @return the graphical viewer.
*/
public EditPartViewer getGraphicalViewer(IEditorPart editor)
{
if (editor == null)
{
editor = getActiveEditor();
}
if (editor instanceof IMultiDiagramEditor)
{
Object viewer = editor.getAdapter(IDiagramGraphicalViewer.class);
if (viewer instanceof EditPartViewer)
{
return (EditPartViewer) viewer;
}
}
return null;
}
[...]
}
|
|
|
Re: How to visualize elements programatically [message #713544 is a reply to message #713468] |
Mon, 08 August 2011 11:27   |
Eclipse User |
|
|
|
Hi Vincent,
thanks for your answer. Will be quite hard to obtain a result by just having
this fragment. Please, may I ask you another question on this.
I suppose the method gotoEObject is the core concept for the solution of my
problem, however, it seems to me as if the method goToEObjects would open
the editor where a selected EObject is already present. Is this true? I
glanced over the code and I did not found anything that looked like adding
an EObject to the canvas. Maybe, if your time permits it, you could give me
some more details on how to customize this thing. It would be highly
appreciated, indeed.
Thanks in advance and have your day.
Thanks, Timothy
"Vincent" schrieb im Newsbeitrag news:j1opka$n9u$1@news.eclipse.org...
Here is a part of a class which should help you.
You may perform some refactoring, but all you need should be there.
Take care to respect the copyright.
/*****************************************************************************
* Copyright (c) 2010 Atos Origin.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Vincent Hemery (Atos Origin}) {vincent.hemery@atosorigin.com} - Initial
API and implementation
*
*****************************************************************************/
package org.topcased.requirement.bundle.papyrus.services;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.core.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.core.modelsetquery.ModelSetQuery;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.diagram.common.util.DiagramEditPartsUtil;
import org.eclipse.papyrus.resource.IModel;
import org.eclipse.papyrus.resource.ModelSet;
import org.eclipse.papyrus.resource.uml.UmlModel;
import org.eclipse.papyrus.sasheditor.contentprovider.IPageMngr;
import org.eclipse.papyrus.sasheditor.editor.AbstractMultiPageSashEditor;
import org.eclipse.papyrus.sasheditor.editor.ISashWindowsContainer;
import org.eclipse.ui.IEditorPart;
import org.topcased.requirement.bundle.papyrus.Activator;
import org.topcased.requirement.bundle.papyrus.internal.Messages;
import org.topcased.requirement.bundle.papyrus.resource.RequirementModel;
import org.topcased.requirement.core.extensions.IEditorServices;
import org.topcased.requirement.core.views.AbstractRequirementPage;
/**
* This class provides services for using requirements with the papyrus core
editor. It is intended to be used only by
* extension point org.topcased.requirement.core.supportingEditors.
*
* @author vhemery
*/
public class PapyrusEditorServices implements IEditorServices
{
[...]
/**
* Get the currently active Papyrus diagram editor.
*
* @return the editor part or null.
*/
public IEditorPart getActiveEditor()
{
return EditorUtils.getMultiDiagramEditor();
}
[...]
/**
* Sets the selection state for the editor to reveal the position of the
given EObject.<br>
* It searches in the different diagrams the occurrences of the EObject
and ask the user to select the destination
* if the given EObject has several graphical representation.
*
* @param editor the editor or null for currently active one
* @param listOfEObjects list of eobjects to select
* @param openDiagramIfNeeded if true, when the object is not in current
active diagram, a representing diagram will
* open
*/
public void gotoEObjects(IEditorPart editor, List<EObject>
listOfEObjects, boolean openDiagramIfNeeded)
{
if (editor == null)
{
editor = getActiveEditor();
}
if (editor instanceof IMultiDiagramEditor)
{
List<EditPart> editPartsToSelect = new
ArrayList<EditPart>(listOfEObjects.size());
// find appropriate viewer and edit parts to select
EditPartViewer currentViewer = getGraphicalViewer(editor);
Map< ? , ? > currentRegistry =
currentViewer.getEditPartRegistry();
if (openDiagramIfNeeded)
{
/*
* Reference diagrams which represent these elements to know
the best one (with most represented
* elements)
*/
Map<Diagram, Integer> numberOfRepresentedElementsByDiagram =
new HashMap<Diagram, Integer>();
for (EObject toSelect : listOfEObjects)
{
Set<Diagram> diagramsRepresentingThisObject = new
HashSet<Diagram>();
List< ? > views =
DiagramEditPartsUtil.getEObjectViews(toSelect);
for (Object view : views)
{
if (view instanceof View && !(view instanceof
Diagram))
{
Diagram diagram = ((View) view).getDiagram();
// test if a representation has already been
counted for this diagram
if
(!diagramsRepresentingThisObject.contains(diagram))
{
// the element is represented in this
diagram
diagramsRepresentingThisObject.add(diagram);
if
(!numberOfRepresentedElementsByDiagram.containsKey(diagram))
{
numberOfRepresentedElementsByDiagram.put(diagram,
0);
}
Integer num =
numberOfRepresentedElementsByDiagram.get(diagram);
numberOfRepresentedElementsByDiagram.put(diagram,
num + 1);
}
}
}
}
// find diagram where the largest number of elements are
available
Diagram bestDiagram = null;
int numberOfRepresented = 0;
for (Entry<Diagram, Integer> entry :
numberOfRepresentedElementsByDiagram.entrySet())
{
if (entry.getValue() > numberOfRepresented)
{
// take better diagram
bestDiagram = entry.getKey();
numberOfRepresented = entry.getValue();
}
else if (entry.getValue() == numberOfRepresented)
{
// take the currently opened diagram if it is as
good
if (currentRegistry.get(entry.getKey()) != null)
{
bestDiagram = entry.getKey();
}
}
}
// change active diagram if necessary
if (bestDiagram != null && currentRegistry.get(bestDiagram)
== null)
{
Object pageMngr = editor.getAdapter(IPageMngr.class);
if (pageMngr instanceof IPageMngr)
{
IPageMngr pageManager = (IPageMngr) pageMngr;
if (pageManager.isOpen(bestDiagram))
{
// bring to top
pageManager.closePage(bestDiagram);
pageManager.openPage(bestDiagram);
}
else
{
// open
pageManager.openPage(bestDiagram);
}
}
currentViewer = getGraphicalViewer(editor);
currentRegistry = currentViewer.getEditPartRegistry();
}
}
// select edit parts in the current viewer
for (EObject toSelect : listOfEObjects)
{
List< ? > views =
DiagramEditPartsUtil.getEObjectViews(toSelect);
for (Object view : views)
{
if (currentRegistry.containsKey(view))
{
Object part = currentRegistry.get(view);
if (part instanceof EditPart)
{
editPartsToSelect.add((EditPart) part);
}
}
}
}
// select edit parts if selection has to change
if
(!(editPartsToSelect.containsAll(currentViewer.getSelectedEditParts()) &&
currentViewer.getSelectedEditParts().contains(editPartsToSelect)))
{
boolean firstSelectedPartInViewer = true;
for (EditPart editPart : editPartsToSelect)
{
if (firstSelectedPartInViewer)
{
currentViewer.select(editPart);
firstSelectedPartInViewer = false;
}
else
{
currentViewer.appendSelection(editPart);
}
}
}
}
}
[...]
/**
* Get the graphical viewer of the editor.
*
* @param editor the editor or null for currently active one
* @return the graphical viewer.
*/
public EditPartViewer getGraphicalViewer(IEditorPart editor)
{
if (editor == null)
{
editor = getActiveEditor();
}
if (editor instanceof IMultiDiagramEditor)
{
Object viewer =
editor.getAdapter(IDiagramGraphicalViewer.class);
if (viewer instanceof EditPartViewer)
{
return (EditPartViewer) viewer;
}
}
return null;
}
[...]
}
|
|
|
|
|
|
|
|
Re: How to visualize elements programatically [message #883267 is a reply to message #870190] |
Fri, 08 June 2012 03:15  |
Eclipse User |
|
|
|
I have got a good tip to visualize elements programatically. If someone else wants to do this you can have a look at dev.eclipse.org/svnroot/modeling/org.eclipse.mdt.papyrus/branches/0.8.X/extraplugins/diagramtemplate . Here you can find the method showElementIn() in the oep.diagramtemplate.launcher.DiagramTemplateLauncher class. It is a part of org.eclipse.papyrus.diagramtemplate.editor. In this method you find all hints you'll need.
|
|
|
Powered by
FUDForum. Page generated in 0.04327 seconds