| 1 | /* |
| 2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.resource.edit.parts; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.List; |
| 8 | |
| 9 | import org.eclipse.draw2d.IFigure; |
| 10 | import org.eclipse.draw2d.MarginBorder; |
| 11 | import org.eclipse.draw2d.RoundedRectangle; |
| 12 | import org.eclipse.draw2d.Shape; |
| 13 | import org.eclipse.draw2d.StackLayout; |
| 14 | import org.eclipse.draw2d.geometry.Dimension; |
| 15 | import org.eclipse.gef.EditPart; |
| 16 | import org.eclipse.gef.EditPolicy; |
| 17 | import org.eclipse.gef.Request; |
| 18 | import org.eclipse.gef.commands.Command; |
| 19 | import org.eclipse.gef.editpolicies.LayoutEditPolicy; |
| 20 | import org.eclipse.gef.editpolicies.NonResizableEditPolicy; |
| 21 | import org.eclipse.gef.requests.CreateRequest; |
| 22 | import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter; |
| 23 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 24 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart; |
| 25 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy; |
| 26 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
| 27 | import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest; |
| 28 | import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout; |
| 29 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; |
| 30 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
| 31 | import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure; |
| 32 | import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure; |
| 33 | import org.eclipse.gmf.runtime.notation.View; |
| 34 | import org.eclipse.swt.graphics.Color; |
| 35 | |
| 36 | import de.uka.ipd.sdq.pcm.gmf.resource.edit.policies.LinkingResource2ItemSemanticEditPolicy; |
| 37 | import de.uka.ipd.sdq.pcm.gmf.resource.part.PalladioComponentModelVisualIDRegistry; |
| 38 | import de.uka.ipd.sdq.pcm.gmf.resource.providers.PalladioComponentModelElementTypes; |
| 39 | |
| 40 | /** |
| 41 | * @generated |
| 42 | */ |
| 43 | public class LinkingResource2EditPart extends ShapeNodeEditPart { |
| 44 | |
| 45 | /** |
| 46 | * @generated |
| 47 | */ |
| 48 | public static final int VISUAL_ID = 2003; |
| 49 | |
| 50 | /** |
| 51 | * @generated |
| 52 | */ |
| 53 | protected IFigure contentPane; |
| 54 | |
| 55 | /** |
| 56 | * @generated |
| 57 | */ |
| 58 | protected IFigure primaryShape; |
| 59 | |
| 60 | /** |
| 61 | * @generated |
| 62 | */ |
| 63 | public LinkingResource2EditPart(View view) { |
| 64 | super(view); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @generated |
| 69 | */ |
| 70 | protected void createDefaultEditPolicies() { |
| 71 | installEditPolicy(EditPolicyRoles.CREATION_ROLE, |
| 72 | new CreationEditPolicy()); |
| 73 | super.createDefaultEditPolicies(); |
| 74 | installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, |
| 75 | new LinkingResource2ItemSemanticEditPolicy()); |
| 76 | installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy()); |
| 77 | // XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies |
| 78 | // removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @generated |
| 83 | */ |
| 84 | protected LayoutEditPolicy createLayoutEditPolicy() { |
| 85 | LayoutEditPolicy lep = new LayoutEditPolicy() { |
| 86 | |
| 87 | protected EditPolicy createChildEditPolicy(EditPart child) { |
| 88 | EditPolicy result = child |
| 89 | .getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
| 90 | if (result == null) { |
| 91 | result = new NonResizableEditPolicy(); |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | protected Command getMoveChildrenCommand(Request request) { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | protected Command getCreateCommand(CreateRequest request) { |
| 101 | return null; |
| 102 | } |
| 103 | }; |
| 104 | return lep; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @generated |
| 109 | */ |
| 110 | protected IFigure createNodeShape() { |
| 111 | NetworkSwitchFigure figure = new NetworkSwitchFigure(); |
| 112 | return primaryShape = figure; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @generated |
| 117 | */ |
| 118 | public NetworkSwitchFigure getPrimaryShape() { |
| 119 | return (NetworkSwitchFigure) primaryShape; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @generated |
| 124 | */ |
| 125 | protected boolean addFixedChild(EditPart childEditPart) { |
| 126 | if (childEditPart instanceof LinkingResourceEntityNameEditPart) { |
| 127 | ((LinkingResourceEntityNameEditPart) childEditPart) |
| 128 | .setLabel(getPrimaryShape() |
| 129 | .getFigureNetworkSwitchNameFigure()); |
| 130 | return true; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @generated |
| 137 | */ |
| 138 | protected boolean removeFixedChild(EditPart childEditPart) { |
| 139 | if (childEditPart instanceof LinkingResourceEntityNameEditPart) { |
| 140 | return true; |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @generated |
| 147 | */ |
| 148 | protected void addChildVisual(EditPart childEditPart, int index) { |
| 149 | if (addFixedChild(childEditPart)) { |
| 150 | return; |
| 151 | } |
| 152 | super.addChildVisual(childEditPart, -1); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @generated |
| 157 | */ |
| 158 | protected void removeChildVisual(EditPart childEditPart) { |
| 159 | if (removeFixedChild(childEditPart)) { |
| 160 | return; |
| 161 | } |
| 162 | super.removeChildVisual(childEditPart); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @generated |
| 167 | */ |
| 168 | protected IFigure getContentPaneFor(IGraphicalEditPart editPart) { |
| 169 | return getContentPane(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @generated |
| 174 | */ |
| 175 | protected NodeFigure createNodePlate() { |
| 176 | DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(250, 50); |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Creates figure for this edit part. |
| 182 | * |
| 183 | * Body of this method does not depend on settings in generation model |
| 184 | * so you may safely remove <i>generated</i> tag and modify it. |
| 185 | * |
| 186 | * @generated |
| 187 | */ |
| 188 | protected NodeFigure createNodeFigure() { |
| 189 | NodeFigure figure = createNodePlate(); |
| 190 | figure.setLayoutManager(new StackLayout()); |
| 191 | IFigure shape = createNodeShape(); |
| 192 | figure.add(shape); |
| 193 | contentPane = setupContentPane(shape); |
| 194 | return figure; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Default implementation treats passed figure as content pane. |
| 199 | * Respects layout one may have set for generated figure. |
| 200 | * @param nodeShape instance of generated figure class |
| 201 | * @generated |
| 202 | */ |
| 203 | protected IFigure setupContentPane(IFigure nodeShape) { |
| 204 | if (nodeShape.getLayoutManager() == null) { |
| 205 | ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout(); |
| 206 | layout.setSpacing(5); |
| 207 | nodeShape.setLayoutManager(layout); |
| 208 | } |
| 209 | return nodeShape; // use nodeShape itself as contentPane |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @generated |
| 214 | */ |
| 215 | public IFigure getContentPane() { |
| 216 | if (contentPane != null) { |
| 217 | return contentPane; |
| 218 | } |
| 219 | return super.getContentPane(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @generated |
| 224 | */ |
| 225 | protected void setForegroundColor(Color color) { |
| 226 | if (primaryShape != null) { |
| 227 | primaryShape.setForegroundColor(color); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * @generated |
| 233 | */ |
| 234 | protected void setBackgroundColor(Color color) { |
| 235 | if (primaryShape != null) { |
| 236 | primaryShape.setBackgroundColor(color); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @generated |
| 242 | */ |
| 243 | protected void setLineWidth(int width) { |
| 244 | if (primaryShape instanceof Shape) { |
| 245 | ((Shape) primaryShape).setLineWidth(width); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @generated |
| 251 | */ |
| 252 | protected void setLineType(int style) { |
| 253 | if (primaryShape instanceof Shape) { |
| 254 | ((Shape) primaryShape).setLineStyle(style); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @generated |
| 260 | */ |
| 261 | public EditPart getPrimaryChildEditPart() { |
| 262 | return getChildBySemanticHint(PalladioComponentModelVisualIDRegistry |
| 263 | .getType(LinkingResourceEntityNameEditPart.VISUAL_ID)); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @generated |
| 268 | */ |
| 269 | public List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/getMARelTypesOnSource() { |
| 270 | List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/types = new ArrayList/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/(); |
| 271 | types |
| 272 | .add(PalladioComponentModelElementTypes.LinkingResourceConnectedResourceContainers_LinkingResource_4002); |
| 273 | return types; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @generated |
| 278 | */ |
| 279 | public List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/getMARelTypesOnSourceAndTarget( |
| 280 | IGraphicalEditPart targetEditPart) { |
| 281 | List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/types = new ArrayList/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/(); |
| 282 | if (targetEditPart instanceof ResourceContainerEditPart) { |
| 283 | types |
| 284 | .add(PalladioComponentModelElementTypes.LinkingResourceConnectedResourceContainers_LinkingResource_4002); |
| 285 | } |
| 286 | return types; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @generated |
| 291 | */ |
| 292 | public List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/getMATypesForTarget( |
| 293 | IElementType relationshipType) { |
| 294 | List/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/types = new ArrayList/*<org.eclipse.gmf.runtime.emf.type.core.IElementType>*/(); |
| 295 | if (relationshipType == PalladioComponentModelElementTypes.LinkingResourceConnectedResourceContainers_LinkingResource_4002) { |
| 296 | types |
| 297 | .add(PalladioComponentModelElementTypes.ResourceContainer_2001); |
| 298 | } |
| 299 | return types; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * @generated |
| 304 | */ |
| 305 | public EditPart getTargetEditPart(Request request) { |
| 306 | if (request instanceof CreateViewAndElementRequest) { |
| 307 | CreateElementRequestAdapter adapter = ((CreateViewAndElementRequest) request) |
| 308 | .getViewAndElementDescriptor() |
| 309 | .getCreateElementRequestAdapter(); |
| 310 | IElementType type = (IElementType) adapter |
| 311 | .getAdapter(IElementType.class); |
| 312 | if (type == PalladioComponentModelElementTypes.CommunicationLinkResourceSpecification_3002) { |
| 313 | return getChildBySemanticHint(PalladioComponentModelVisualIDRegistry |
| 314 | .getType(LinkingResourceNetworkSwitchCompartmentEditPart.VISUAL_ID)); |
| 315 | } |
| 316 | } |
| 317 | return super.getTargetEditPart(request); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @generated |
| 322 | */ |
| 323 | public class NetworkSwitchFigure extends RoundedRectangle { |
| 324 | |
| 325 | /** |
| 326 | * @generated |
| 327 | */ |
| 328 | private WrappingLabel fFigureNetworkSwitchNameFigure; |
| 329 | |
| 330 | /** |
| 331 | * @generated |
| 332 | */ |
| 333 | public NetworkSwitchFigure() { |
| 334 | this.setCornerDimensions(new Dimension(getMapMode().DPtoLP(27), |
| 335 | getMapMode().DPtoLP(27))); |
| 336 | this.setLineWidth(1); |
| 337 | this.setSize(getMapMode().DPtoLP(200), getMapMode().DPtoLP(40)); |
| 338 | |
| 339 | this.setBorder(new MarginBorder(getMapMode().DPtoLP(10), |
| 340 | getMapMode().DPtoLP(10), getMapMode().DPtoLP(10), |
| 341 | getMapMode().DPtoLP(10))); |
| 342 | createContents(); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * @generated |
| 347 | */ |
| 348 | private void createContents() { |
| 349 | |
| 350 | fFigureNetworkSwitchNameFigure = new WrappingLabel(); |
| 351 | fFigureNetworkSwitchNameFigure.setText(""); |
| 352 | |
| 353 | this.add(fFigureNetworkSwitchNameFigure); |
| 354 | |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @generated |
| 359 | */ |
| 360 | private boolean myUseLocalCoordinates = false; |
| 361 | |
| 362 | /** |
| 363 | * @generated |
| 364 | */ |
| 365 | protected boolean useLocalCoordinates() { |
| 366 | return myUseLocalCoordinates; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @generated |
| 371 | */ |
| 372 | protected void setUseLocalCoordinates(boolean useLocalCoordinates) { |
| 373 | myUseLocalCoordinates = useLocalCoordinates; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * @generated |
| 378 | */ |
| 379 | public WrappingLabel getFigureNetworkSwitchNameFigure() { |
| 380 | return fFigureNetworkSwitchNameFigure; |
| 381 | } |
| 382 | |
| 383 | } |
| 384 | |
| 385 | } |