SubscriberEditPart.java
Go to the documentation of this file.
00001 package org.ros.model.ros.diagram.edit.parts;
00002 
00003 import java.util.ArrayList;
00004 import java.util.HashMap;
00005 import java.util.LinkedList;
00006 import java.util.List;
00007 
00008 import org.eclipse.draw2d.ColorConstants;
00009 import org.eclipse.draw2d.FlowLayout;
00010 import org.eclipse.draw2d.Graphics;
00011 import org.eclipse.draw2d.IFigure;
00012 import org.eclipse.draw2d.MarginBorder;
00013 import org.eclipse.draw2d.RectangleFigure;
00014 import org.eclipse.draw2d.ScalablePolygonShape;
00015 import org.eclipse.draw2d.Shape;
00016 import org.eclipse.draw2d.StackLayout;
00017 import org.eclipse.draw2d.geometry.Dimension;
00018 import org.eclipse.draw2d.geometry.Point;
00019 import org.eclipse.draw2d.geometry.PrecisionPoint;
00020 import org.eclipse.draw2d.geometry.Rectangle;
00021 import org.eclipse.gef.EditPart;
00022 import org.eclipse.gef.EditPolicy;
00023 import org.eclipse.gef.commands.Command;
00024 import org.eclipse.gef.editpolicies.LayoutEditPolicy;
00025 import org.eclipse.gef.requests.CreateRequest;
00026 import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart;
00027 import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
00028 import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
00029 import org.eclipse.gmf.runtime.diagram.ui.editpolicies.FlowLayoutEditPolicy;
00030 import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
00031 import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
00032 import org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil;
00033 import org.eclipse.gmf.runtime.emf.type.core.IElementType;
00034 import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
00035 import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
00036 import org.eclipse.gmf.runtime.notation.View;
00037 import org.eclipse.swt.graphics.Color;
00038 import org.ros.model.ros.diagram.DefaultSizeNodeFigureWithFixedAnchors;
00039 import org.ros.model.ros.diagram.edit.policies.SubscriberItemSemanticEditPolicy;
00040 import org.ros.model.ros.diagram.part.RosVisualIDRegistry;
00041 import org.ros.model.ros.diagram.providers.RosElementTypes;
00042 
00046 public class SubscriberEditPart extends AbstractBorderItemEditPart {
00047 
00051         public static final int VISUAL_ID = 3004;
00052 
00056         protected IFigure contentPane;
00057 
00061         protected IFigure primaryShape;
00062 
00066         public SubscriberEditPart(View view) {
00067                 super(view);
00068         }
00069 
00073         protected void createDefaultEditPolicies() {
00074                 super.createDefaultEditPolicies();
00075                 installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE,
00076                                 getPrimaryDragEditPolicy());
00077                 installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
00078                                 new SubscriberItemSemanticEditPolicy());
00079                 installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
00080                 // XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
00081                 // removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
00082         }
00083 
00087         protected LayoutEditPolicy createLayoutEditPolicy() {
00088 
00089                 FlowLayoutEditPolicy lep = new FlowLayoutEditPolicy() {
00090 
00091                         protected Command createAddCommand(EditPart child, EditPart after) {
00092                                 return null;
00093                         }
00094 
00095                         protected Command createMoveChildCommand(EditPart child,
00096                                         EditPart after) {
00097                                 return null;
00098                         }
00099 
00100                         protected Command getCreateCommand(CreateRequest request) {
00101                                 return null;
00102                         }
00103                 };
00104                 return lep;
00105         }
00106 
00110         protected IFigure createNodeShape() {
00111                 return primaryShape = new Subscriber2Figure();
00112         }
00113 
00117         public Subscriber2Figure getPrimaryShape() {
00118                 return (Subscriber2Figure) primaryShape;
00119         }
00120 
00124         protected boolean addFixedChild(EditPart childEditPart) {
00125                 if (childEditPart instanceof SubscriberNameEditPart) {
00126                         ((SubscriberNameEditPart) childEditPart).setLabel(getPrimaryShape()
00127                                         .getFigureSubscriberNameFigure());
00128                         return true;
00129                 }
00130                 return false;
00131         }
00132 
00136         protected boolean removeFixedChild(EditPart childEditPart) {
00137                 if (childEditPart instanceof SubscriberNameEditPart) {
00138                         return true;
00139                 }
00140                 return false;
00141         }
00142 
00146         protected void addChildVisual(EditPart childEditPart, int index) {
00147                 if (addFixedChild(childEditPart)) {
00148                         return;
00149                 }
00150                 super.addChildVisual(childEditPart, -1);
00151         }
00152 
00156         protected void removeChildVisual(EditPart childEditPart) {
00157                 if (removeFixedChild(childEditPart)) {
00158                         return;
00159                 }
00160                 super.removeChildVisual(childEditPart);
00161         }
00162 
00166         protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
00167                 return getContentPane();
00168         }
00169 
00173         protected NodeFigure createNodePlate() {
00174                 HashMap<String, PrecisionPoint> anchorLocations = new HashMap<String, PrecisionPoint>();
00175                 anchorLocations.put("WEST", new PrecisionPoint(0.2d, 0.5d));
00176                 //anchorLocations.put("EAST", new PrecisionPoint(1d, 0.5d));
00177                 //anchorLocations.put("NORTH", new PrecisionPoint(0.5d, 0));
00178                 //anchorLocations.put("SOUTH", new PrecisionPoint(0.5d, 1d));
00179                 DefaultSizeNodeFigureWithFixedAnchors result = new DefaultSizeNodeFigureWithFixedAnchors(
00180                                 60, 20, anchorLocations);
00181                 //FIXME: workaround for #154536
00182                 result.getBounds().setSize(result.getPreferredSize());
00183                 return result;
00184         }
00185 
00194         protected NodeFigure createNodeFigure() {
00195                 NodeFigure figure = createNodePlate();
00196                 figure.setLayoutManager(new StackLayout());
00197                 IFigure shape = createNodeShape();
00198                 figure.add(shape);
00199                 contentPane = setupContentPane(shape);
00200                 return figure;
00201         }
00202 
00209         protected IFigure setupContentPane(IFigure nodeShape) {
00210                 if (nodeShape.getLayoutManager() == null) {
00211                         ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
00212                         layout.setSpacing(5);
00213                         nodeShape.setLayoutManager(layout);
00214                 }
00215                 return nodeShape; // use nodeShape itself as contentPane
00216         }
00217 
00221         public IFigure getContentPane() {
00222                 if (contentPane != null) {
00223                         return contentPane;
00224                 }
00225                 return super.getContentPane();
00226         }
00227 
00231         protected void setForegroundColor(Color color) {
00232                 if (primaryShape != null) {
00233                         primaryShape.setForegroundColor(color);
00234                 }
00235         }
00236 
00240         protected void setBackgroundColor(Color color) {
00241                 if (primaryShape != null) {
00242                         primaryShape.setBackgroundColor(color);
00243                 }
00244         }
00245 
00249         protected void setLineWidth(int width) {
00250                 if (primaryShape instanceof Shape) {
00251                         ((Shape) primaryShape).setLineWidth(width);
00252                 }
00253         }
00254 
00258         protected void setLineType(int style) {
00259                 if (primaryShape instanceof Shape) {
00260                         ((Shape) primaryShape).setLineStyle(style);
00261                 }
00262         }
00263 
00267         public EditPart getPrimaryChildEditPart() {
00268                 return getChildBySemanticHint(RosVisualIDRegistry
00269                                 .getType(SubscriberNameEditPart.VISUAL_ID));
00270         }
00271 
00275         public List<IElementType> getMARelTypesOnTarget() {
00276                 ArrayList<IElementType> types = new ArrayList<IElementType>(1);
00277                 types.add(RosElementTypes.Topic_4001);
00278                 return types;
00279         }
00280 
00284         public List<IElementType> getMATypesForSource(IElementType relationshipType) {
00285                 LinkedList<IElementType> types = new LinkedList<IElementType>();
00286                 if (relationshipType == RosElementTypes.Topic_4001) {
00287                         types.add(RosElementTypes.Publisher_3003);
00288                 }
00289                 return types;
00290         }
00291 
00295         public class Subscriber2Figure extends ScalablePolygonShape {
00296                 public static final int BLUR_SHADOW_WIDTH = 5;
00297 
00298                 @Override
00299                 public void paintFigure(Graphics graphics) {
00300                         drawBlurredShadow(graphics);
00301                         super.paintFigure(graphics);
00302                 }
00303 
00304                 private void drawBlurredShadow(Graphics graphics) {
00305                         // draw the shadow...
00306                         graphics.pushState();
00307 
00308                         int size = MapModeUtil.getMapMode(this).DPtoLP(BLUR_SHADOW_WIDTH);
00309                         int step = MapModeUtil.getMapMode(this).DPtoLP(-1);
00310 
00311                         graphics.setForegroundColor(ColorConstants.gray);
00312                         graphics.setLineWidth(MapModeUtil.getMapMode(this).DPtoLP(2));
00313                         graphics.translate(size, size);
00314                         graphics.setClip(graphics.getClip(new Rectangle(getBounds()))
00315                                         .expand(size, size));
00316                         graphics.setAlpha(20);
00317                         outlineShape(graphics);
00318                         graphics.translate(step, step);
00319                         graphics.setAlpha(30);
00320                         outlineShape(graphics);
00321                         graphics.translate(step, step);
00322                         graphics.setAlpha(60);
00323                         outlineShape(graphics);
00324                         graphics.translate(step, step);
00325                         graphics.setAlpha(100);
00326                         outlineShape(graphics);
00327                         graphics.translate(step, step);
00328                         graphics.setAlpha(150);
00329                         outlineShape(graphics);
00330 
00331                         graphics.popState();
00332                 }
00333 
00337                 private WrappingLabel fFigureSubscriberNameFigure;
00338 
00342                 public Subscriber2Figure() {
00343 
00344                         FlowLayout layoutThis = new FlowLayout();
00345                         layoutThis.setStretchMinorAxis(false);
00346                         layoutThis.setMinorAlignment(FlowLayout.ALIGN_CENTER);
00347 
00348                         layoutThis.setMajorAlignment(FlowLayout.ALIGN_CENTER);
00349                         layoutThis.setMajorSpacing(5);
00350                         layoutThis.setMinorSpacing(5);
00351                         layoutThis.setHorizontal(false);
00352 
00353                         this.setLayoutManager(layoutThis);
00354 
00355                         this.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode()
00356                                         .DPtoLP(0)));
00357                         this.addPoint(new Point(getMapMode().DPtoLP(40), getMapMode()
00358                                         .DPtoLP(0)));
00359                         this.addPoint(new Point(getMapMode().DPtoLP(40), getMapMode()
00360                                         .DPtoLP(40)));
00361                         this.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode()
00362                                         .DPtoLP(40)));
00363                         this.addPoint(new Point(getMapMode().DPtoLP(10), getMapMode()
00364                                         .DPtoLP(20)));
00365                         this.setFill(true);
00366                         this.setForegroundColor(THIS_FORE);
00367                         this.setBackgroundColor(THIS_BACK);
00368                         this.setPreferredSize(new Dimension(getMapMode().DPtoLP(80),
00369                                         getMapMode().DPtoLP(20)));
00370 
00371                         this.setBorder(new MarginBorder(getMapMode().DPtoLP(2),
00372                                         getMapMode().DPtoLP(22), getMapMode().DPtoLP(2),
00373                                         getMapMode().DPtoLP(2)));
00374                         createContents();
00375                 }
00376 
00380                 private void createContents() {
00381 
00382                         fFigureSubscriberNameFigure = new WrappingLabel();
00383 
00384                         fFigureSubscriberNameFigure.setText("<...>");
00385 
00386                         this.add(fFigureSubscriberNameFigure);
00387 
00388                 }
00389 
00393                 public WrappingLabel getFigureSubscriberNameFigure() {
00394                         return fFigureSubscriberNameFigure;
00395                 }
00396 
00397         }
00398 
00402         static final Color THIS_FORE = new Color(null, 211, 72, 54);
00403 
00407         static final Color THIS_BACK = new Color(null, 211, 72, 54);
00408 
00409 }


bride_plugin_source
Author(s): Alexander Bubeck
autogenerated on Sun Oct 5 2014 22:38:34