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.LineBorder;
00013 import org.eclipse.draw2d.MarginBorder;
00014 import org.eclipse.draw2d.RectangleFigure;
00015 import org.eclipse.draw2d.ScalablePolygonShape;
00016 import org.eclipse.draw2d.Shape;
00017 import org.eclipse.draw2d.StackLayout;
00018 import org.eclipse.draw2d.geometry.Dimension;
00019 import org.eclipse.draw2d.geometry.Point;
00020 import org.eclipse.draw2d.geometry.PrecisionPoint;
00021 import org.eclipse.draw2d.geometry.Rectangle;
00022 import org.eclipse.gef.EditPart;
00023 import org.eclipse.gef.EditPolicy;
00024 import org.eclipse.gef.commands.Command;
00025 import org.eclipse.gef.editpolicies.LayoutEditPolicy;
00026 import org.eclipse.gef.requests.CreateRequest;
00027 import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart;
00028 import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
00029 import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
00030 import org.eclipse.gmf.runtime.diagram.ui.editpolicies.FlowLayoutEditPolicy;
00031 import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
00032 import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
00033 import org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil;
00034 import org.eclipse.gmf.runtime.emf.type.core.IElementType;
00035 import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
00036 import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
00037 import org.eclipse.gmf.runtime.notation.View;
00038 import org.eclipse.swt.graphics.Color;
00039 import org.ros.model.ros.diagram.DefaultSizeNodeFigureWithFixedAnchors;
00040 import org.ros.model.ros.diagram.edit.policies.PublisherItemSemanticEditPolicy;
00041 import org.ros.model.ros.diagram.part.RosVisualIDRegistry;
00042 import org.ros.model.ros.diagram.providers.RosElementTypes;
00043
00047 public class PublisherEditPart extends AbstractBorderItemEditPart {
00048
00052 public static final int VISUAL_ID = 3003;
00053
00057 protected IFigure contentPane;
00058
00062 protected IFigure primaryShape;
00063
00067 public PublisherEditPart(View view) {
00068 super(view);
00069 }
00070
00074 protected void createDefaultEditPolicies() {
00075 super.createDefaultEditPolicies();
00076 installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE,
00077 getPrimaryDragEditPolicy());
00078 installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
00079 new PublisherItemSemanticEditPolicy());
00080 installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
00081
00082
00083 }
00084
00088 protected LayoutEditPolicy createLayoutEditPolicy() {
00089
00090 FlowLayoutEditPolicy lep = new FlowLayoutEditPolicy() {
00091
00092 protected Command createAddCommand(EditPart child, EditPart after) {
00093 return null;
00094 }
00095
00096 protected Command createMoveChildCommand(EditPart child,
00097 EditPart after) {
00098 return null;
00099 }
00100
00101 protected Command getCreateCommand(CreateRequest request) {
00102 return null;
00103 }
00104 };
00105 return lep;
00106 }
00107
00111 protected IFigure createNodeShape() {
00112 return primaryShape = new Publisher2Figure();
00113 }
00114
00118 public Publisher2Figure getPrimaryShape() {
00119 return (Publisher2Figure) primaryShape;
00120 }
00121
00125 protected boolean addFixedChild(EditPart childEditPart) {
00126 if (childEditPart instanceof PublisherNameEditPart) {
00127 ((PublisherNameEditPart) childEditPart).setLabel(getPrimaryShape()
00128 .getFigurePublisherNameFigure());
00129 return true;
00130 }
00131 return false;
00132 }
00133
00137 protected boolean removeFixedChild(EditPart childEditPart) {
00138 if (childEditPart instanceof PublisherNameEditPart) {
00139 return true;
00140 }
00141 return false;
00142 }
00143
00147 protected void addChildVisual(EditPart childEditPart, int index) {
00148 if (addFixedChild(childEditPart)) {
00149 return;
00150 }
00151 super.addChildVisual(childEditPart, -1);
00152 }
00153
00157 protected void removeChildVisual(EditPart childEditPart) {
00158 if (removeFixedChild(childEditPart)) {
00159 return;
00160 }
00161 super.removeChildVisual(childEditPart);
00162 }
00163
00167 protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
00168 return getContentPane();
00169 }
00170
00174 protected NodeFigure createNodePlate() {
00175 HashMap<String, PrecisionPoint> anchorLocations = new HashMap<String, PrecisionPoint>();
00176
00177 anchorLocations.put("EAST", new PrecisionPoint(1d, 0.5d));
00178
00179
00180 DefaultSizeNodeFigureWithFixedAnchors result = new DefaultSizeNodeFigureWithFixedAnchors(
00181 60, 20, anchorLocations);
00182
00183 result.getBounds().setSize(result.getPreferredSize());
00184 return result;
00185 }
00186
00195 protected NodeFigure createNodeFigure() {
00196 NodeFigure figure = createNodePlate();
00197 figure.setLayoutManager(new StackLayout());
00198 IFigure shape = createNodeShape();
00199 figure.add(shape);
00200 contentPane = setupContentPane(shape);
00201 return figure;
00202 }
00203
00210 protected IFigure setupContentPane(IFigure nodeShape) {
00211 if (nodeShape.getLayoutManager() == null) {
00212 ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
00213 layout.setSpacing(5);
00214 nodeShape.setLayoutManager(layout);
00215 }
00216 return nodeShape;
00217 }
00218
00222 public IFigure getContentPane() {
00223 if (contentPane != null) {
00224 return contentPane;
00225 }
00226 return super.getContentPane();
00227 }
00228
00232 protected void setForegroundColor(Color color) {
00233 if (primaryShape != null) {
00234 primaryShape.setForegroundColor(color);
00235 }
00236 }
00237
00241 protected void setBackgroundColor(Color color) {
00242 if (primaryShape != null) {
00243 primaryShape.setBackgroundColor(color);
00244 }
00245 }
00246
00250 protected void setLineWidth(int width) {
00251 if (primaryShape instanceof Shape) {
00252 ((Shape) primaryShape).setLineWidth(width);
00253 }
00254 }
00255
00259 protected void setLineType(int style) {
00260 if (primaryShape instanceof Shape) {
00261 ((Shape) primaryShape).setLineStyle(style);
00262 }
00263 }
00264
00268 public EditPart getPrimaryChildEditPart() {
00269 return getChildBySemanticHint(RosVisualIDRegistry
00270 .getType(PublisherNameEditPart.VISUAL_ID));
00271 }
00272
00276 public List<IElementType> getMARelTypesOnSource() {
00277 ArrayList<IElementType> types = new ArrayList<IElementType>(1);
00278 types.add(RosElementTypes.Topic_4001);
00279 return types;
00280 }
00281
00285 public List<IElementType> getMARelTypesOnSourceAndTarget(
00286 IGraphicalEditPart targetEditPart) {
00287 LinkedList<IElementType> types = new LinkedList<IElementType>();
00288 if (targetEditPart instanceof SubscriberEditPart) {
00289 types.add(RosElementTypes.Topic_4001);
00290 }
00291 return types;
00292 }
00293
00297 public List<IElementType> getMATypesForTarget(IElementType relationshipType) {
00298 LinkedList<IElementType> types = new LinkedList<IElementType>();
00299 if (relationshipType == RosElementTypes.Topic_4001) {
00300 types.add(RosElementTypes.Subscriber_3004);
00301 }
00302 return types;
00303 }
00304
00308 public class Publisher2Figure extends ScalablePolygonShape {
00309 public static final int BLUR_SHADOW_WIDTH = 5;
00310
00311 @Override
00312 public void paintFigure(Graphics graphics) {
00313 drawBlurredShadow(graphics);
00314 super.paintFigure(graphics);
00315 }
00316
00317 private void drawBlurredShadow(Graphics graphics) {
00318
00319 graphics.pushState();
00320
00321 int size = MapModeUtil.getMapMode(this).DPtoLP(BLUR_SHADOW_WIDTH);
00322 int step = MapModeUtil.getMapMode(this).DPtoLP(-1);
00323
00324 graphics.setForegroundColor(ColorConstants.gray);
00325 graphics.setLineWidth(MapModeUtil.getMapMode(this).DPtoLP(2));
00326 graphics.translate(size, size);
00327 graphics.setClip(graphics.getClip(new Rectangle(getBounds()))
00328 .expand(size, size));
00329 graphics.setAlpha(20);
00330 outlineShape(graphics);
00331 graphics.translate(step, step);
00332 graphics.setAlpha(30);
00333 outlineShape(graphics);
00334 graphics.translate(step, step);
00335 graphics.setAlpha(60);
00336 outlineShape(graphics);
00337 graphics.translate(step, step);
00338 graphics.setAlpha(100);
00339 outlineShape(graphics);
00340 graphics.translate(step, step);
00341 graphics.setAlpha(150);
00342 outlineShape(graphics);
00343
00344 graphics.popState();
00345 }
00346
00350 private WrappingLabel fFigurePublisherNameFigure;
00351
00355 public Publisher2Figure() {
00356
00357 FlowLayout layoutThis = new FlowLayout();
00358 layoutThis.setStretchMinorAxis(false);
00359 layoutThis.setMinorAlignment(FlowLayout.ALIGN_CENTER);
00360
00361 layoutThis.setMajorAlignment(FlowLayout.ALIGN_CENTER);
00362 layoutThis.setMajorSpacing(5);
00363 layoutThis.setMinorSpacing(5);
00364 layoutThis.setHorizontal(false);
00365
00366 this.setLayoutManager(layoutThis);
00367
00368 this.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode()
00369 .DPtoLP(0)));
00370 this.addPoint(new Point(getMapMode().DPtoLP(30), getMapMode()
00371 .DPtoLP(0)));
00372 this.addPoint(new Point(getMapMode().DPtoLP(40), getMapMode()
00373 .DPtoLP(20)));
00374 this.addPoint(new Point(getMapMode().DPtoLP(30), getMapMode()
00375 .DPtoLP(40)));
00376 this.addPoint(new Point(getMapMode().DPtoLP(0), getMapMode()
00377 .DPtoLP(40)));
00378 this.setFill(true);
00379 this.setForegroundColor(THIS_FORE);
00380 this.setBackgroundColor(THIS_BACK);
00381 this.setPreferredSize(new Dimension(getMapMode().DPtoLP(80),
00382 getMapMode().DPtoLP(20)));
00383
00384 this.setBorder(new MarginBorder(getMapMode().DPtoLP(2),
00385 getMapMode().DPtoLP(2), getMapMode().DPtoLP(2),
00386 getMapMode().DPtoLP(2)));
00387 createContents();
00388 }
00389
00393 private void createContents() {
00394
00395 fFigurePublisherNameFigure = new WrappingLabel();
00396
00397 fFigurePublisherNameFigure.setText("<...>");
00398
00399 this.add(fFigurePublisherNameFigure);
00400
00401 }
00402
00406 public WrappingLabel getFigurePublisherNameFigure() {
00407 return fFigurePublisherNameFigure;
00408 }
00409
00410 }
00411
00415 static final Color THIS_FORE = new Color(null, 211, 72, 54);
00416
00420 static final Color THIS_BACK = new Color(null, 211, 72, 54);
00421
00422 }