blob: d8e0e28d7091572fab365c7bd4f340893618d623 [file] [log] [blame]
/**
*
*/
package org.eclipse.epf.authoring.gef.viewer;
import java.util.Iterator;
import java.util.List;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.DelegatingLayout;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayeredPane;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.epf.diagram.core.bridge.BridgeHelper;
import org.eclipse.epf.diagram.core.services.DiagramManager;
import org.eclipse.epf.diagram.model.LinkedObject;
import org.eclipse.epf.diagram.model.NamedNode;
import org.eclipse.epf.diagram.model.Node;
import org.eclipse.epf.diagram.model.NodeContainer;
import org.eclipse.epf.diagramming.edit.parts.UMLEditPartFactory;
import org.eclipse.epf.library.edit.IFilter;
import org.eclipse.epf.library.edit.util.Suppression;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.epf.library.layout.diagram.DiagramInfo;
import org.eclipse.epf.library.util.ResourceHelper;
import org.eclipse.epf.uma.Activity;
import org.eclipse.epf.uma.DiagramElement;
import org.eclipse.epf.uma.MethodElement;
import org.eclipse.epf.uma.VariabilityElement;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramRootEditPart;
import org.eclipse.gmf.runtime.diagram.ui.image.ImageFileFormat;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramGraphicalViewer;
import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.widgets.Composite;
/**
*
* @author Shashidhar Kannoori
*/
public abstract class NewAbstractDiagramGraphicalViewer extends
AbstractDiagramGraphicalViewer {
protected GraphicalEditPart part = null;
protected Diagram diagram;
protected DiagramManager dmgr;
public NewAbstractDiagramGraphicalViewer(Composite parent, Object wrapper) {
super(parent, wrapper);
// TODO Auto-generated constructor stub
}
/**
* Creates diagram for given object (only for {@link Activity} and packs the bounds.
*
* @param wrapper {@link Object}
* @param needReset boolean
* @param filter {@link IFilter}
* @param sup {@link Suppression}
*/
public EditPart loadDiagram(Object wrapper, boolean needReset,
IFilter filter, Suppression sup) {
// initialize the viewer with the edit part
EditPart editPart = createEditPart(wrapper, filter, sup);
this.graphicalViewer.setContents(editPart);
// ask for immediate update of the control so that the diagram figure
// will be computed
parent.pack(true);
// This check is needed for browsing, in order to render activity contributing's
// children. In processAuthoring we don't do realization, in order to displayed
// realized elements, we have to cleanup ADD diagram and recreate. needtoReset or cleanupdiagram.
// Only for activity detail diagram.
Object o = TngUtil.unwrap(wrapper);
if(o instanceof VariabilityElement && getDiagramType() != null &&
getDiagramType().equalsIgnoreCase(ResourceHelper.DIAGRAM_TYPE_ACTIVITY_DETAIL)){
if(TngUtil.hasContributor((VariabilityElement)o)){
needReset = true;
}
}
if (needReset) {
cleanUpDiagram();
}
return editPart;
}
/**
* returns {@link DiagramInfo}
*
*/
public DiagramInfo getDiagramInfo() {
DiagramInfo diagramInfo = null;
Object element = part.getModel();
if (element instanceof LinkedObject) {
element = ((LinkedObject) element).getObject();
}
if(element instanceof View){
element = ((View)element).getElement();
if(element != null && element instanceof EModelElement){
element = BridgeHelper.getMethodElement((EModelElement)element);
}
if(element instanceof Node){
element = ((Node)element).getLinkedElement();
}
}
if (element instanceof MethodElement) {
diagramInfo = new DiagramInfo(getDiagramType(),
(MethodElement) element);
loadDiagramInfo(part, diagramInfo);
}
return diagramInfo;
}
public void createDiagramImage(ImageFileFormat imageFileFormat){
// CopyToImageUtil im = new CopyToImageUtil();
//im.copyToImage(part, destination, format, monitor)
}
/* (non-Javadoc)
* @see org.eclipse.epf.authoring.gef.viewer.AbstractDiagramGraphicalViewer#createEditPartFactory()
*/
@Override
protected EditPartFactory createEditPartFactory() {
return EditPartService.getInstance();
}
/**
*
*/
protected void configureGraphicalViewer() {
getGraphicalViewer().getControl().setBackground(
ColorConstants.listBackground);
DiagramGraphicalViewer viewer = (DiagramGraphicalViewer) getGraphicalViewer();
DiagramRootEditPart root = (DiagramRootEditPart) EditPartService
.getInstance().createRootEditPart(getDiagram());
LayeredPane printableLayers = (LayeredPane) root
.getLayer(LayerConstants.PRINTABLE_LAYERS);
FreeformLayer extLabelsLayer = new FreeformLayer();
extLabelsLayer.setLayoutManager(new DelegatingLayout());
printableLayers.addLayerAfter(extLabelsLayer,
UMLEditPartFactory.EXTERNAL_NODE_LABELS_LAYER,
LayerConstants.PRIMARY_LAYER);
LayeredPane scalableLayers = (LayeredPane) root
.getLayer(LayerConstants.SCALABLE_LAYERS);
FreeformLayer scaledFeedbackLayer = new FreeformLayer();
scaledFeedbackLayer.setEnabled(false);
scalableLayers.addLayerAfter(scaledFeedbackLayer,
LayerConstants.SCALED_FEEDBACK_LAYER,
DiagramRootEditPart.DECORATION_UNPRINTABLE_LAYER);
viewer.setRootEditPart(root);
viewer.setEditPartFactory(createEditPartFactory());
}
/*
*
*/
protected Diagram getDiagram(){
try{
if(diagram == null){
Object o = TngUtil.unwrap(wrapper);
if (o instanceof Activity) {
Activity act = (Activity) o;
dmgr = DiagramManager.getInstance(TngUtil
.getOwningProcess(act));
List list = dmgr.getDiagrams(act, ActivityDiagramService
.getIntType(getDiagramType()));
if (!list.isEmpty()) {
diagram = (Diagram) list.get(0);
} else {
System.out.println("Diagram is empty for " + act);
}
}
}
return diagram;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
@Override
protected void createGraphicalViewer() {
this.graphicalViewer = new DiagramGraphicalViewer();
this.graphicalViewer.createControl(parent);
configureGraphicalViewer();
}
protected void loadDiagramInfo(GraphicalEditPart part, DiagramInfo diagramInfo) {
List children = part.getChildren();
for (Iterator it = children.iterator(); it.hasNext();) {
Object o = it.next();
if (o instanceof GraphicalEditPart) {
part = (GraphicalEditPart) o;
Object model = part.getModel();
Object e = model;
if (e instanceof LinkedObject) {
e = ((LinkedObject) e).getObject();
}
if(e instanceof View){
e = ((View)e).getElement();
if(e instanceof Node){
if(e instanceof NodeContainer){
loadDiagramInfo((GraphicalEditPart)o, diagramInfo);
}
e = ((Node)e).getLinkedElement();
}
if(e != null && e instanceof EModelElement){
e = BridgeHelper.getMethodElement((EModelElement)e);
}
}
if (e instanceof DiagramElement) {
continue;
}
if (e instanceof MethodElement) {
boolean suppressed;
if (model instanceof NamedNode) {
suppressed = ((NamedNode) model).isSuppressed();
} else {
suppressed = ((MethodElement) e).getSuppressed()
.booleanValue();
}
IFigure f = part.getFigure();
Rectangle bounds = f.getBounds();
String altTag = null;
if (f instanceof WrapLabel) {
altTag = ((WrapLabel) f).getText();
}
diagramInfo.addArea((MethodElement) e, bounds.x,
bounds.y, bounds.width, bounds.height, altTag, suppressed);
}
} else {
}
}
}
@Override
public void dispose() {
if(dmgr != null){
dmgr.release();
}
super.dispose();
}
}