| 1 | /* |
| 2 | * Copyright 2007, SDQ, IPD, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.usage.edit.parts; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collections; |
| 8 | import java.util.List; |
| 9 | |
| 10 | import org.eclipse.draw2d.IFigure; |
| 11 | import org.eclipse.draw2d.Label; |
| 12 | import org.eclipse.draw2d.geometry.Point; |
| 13 | import org.eclipse.emf.common.notify.Notification; |
| 14 | import org.eclipse.emf.ecore.EObject; |
| 15 | import org.eclipse.emf.transaction.RunnableWithResult; |
| 16 | import org.eclipse.gef.AccessibleEditPart; |
| 17 | import org.eclipse.gef.EditPolicy; |
| 18 | import org.eclipse.gef.GraphicalEditPart; |
| 19 | import org.eclipse.gef.Request; |
| 20 | import org.eclipse.gef.commands.Command; |
| 21 | import org.eclipse.gef.editpolicies.NonResizableEditPolicy; |
| 22 | import org.eclipse.gef.handles.NonResizableHandleKit; |
| 23 | import org.eclipse.gef.requests.DirectEditRequest; |
| 24 | import org.eclipse.gef.tools.DirectEditManager; |
| 25 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; |
| 26 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; |
| 27 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; |
| 28 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions; |
| 29 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService; |
| 30 | import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart; |
| 31 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 32 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
| 33 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.LabelDirectEditPolicy; |
| 34 | import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry; |
| 35 | import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
| 36 | import org.eclipse.gmf.runtime.diagram.ui.tools.TextDirectEditManager; |
| 37 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel; |
| 38 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
| 39 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
| 40 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser; |
| 41 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ParserHintAdapter; |
| 42 | import org.eclipse.gmf.runtime.notation.FontStyle; |
| 43 | import org.eclipse.gmf.runtime.notation.NotationPackage; |
| 44 | import org.eclipse.gmf.runtime.notation.View; |
| 45 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
| 46 | import org.eclipse.jface.viewers.ICellEditorValidator; |
| 47 | import org.eclipse.swt.SWT; |
| 48 | import org.eclipse.swt.accessibility.AccessibleEvent; |
| 49 | import org.eclipse.swt.graphics.Color; |
| 50 | import org.eclipse.swt.graphics.FontData; |
| 51 | import org.eclipse.swt.graphics.Image; |
| 52 | |
| 53 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
| 54 | import de.uka.ipd.sdq.pcm.gmf.usage.providers.PalladioComponentModelElementTypes; |
| 55 | |
| 56 | /** |
| 57 | * @generated |
| 58 | */ |
| 59 | public class ScenarioBehaviourId2EditPart extends CompartmentEditPart implements |
| 60 | ITextAwareEditPart { |
| 61 | |
| 62 | /** |
| 63 | * @generated |
| 64 | */ |
| 65 | public static final int VISUAL_ID = 5005; |
| 66 | |
| 67 | /** |
| 68 | * @generated |
| 69 | */ |
| 70 | private DirectEditManager manager; |
| 71 | |
| 72 | /** |
| 73 | * @generated |
| 74 | */ |
| 75 | private IParser parser; |
| 76 | |
| 77 | /** |
| 78 | * @generated |
| 79 | */ |
| 80 | private List parserElements; |
| 81 | |
| 82 | /** |
| 83 | * @generated |
| 84 | */ |
| 85 | private String defaultText; |
| 86 | |
| 87 | /** |
| 88 | * @generated |
| 89 | */ |
| 90 | public ScenarioBehaviourId2EditPart(View view) { |
| 91 | super(view); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @generated |
| 96 | */ |
| 97 | protected void createDefaultEditPolicies() { |
| 98 | super.createDefaultEditPolicies(); |
| 99 | installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, |
| 100 | new LabelDirectEditPolicy()); |
| 101 | installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, |
| 102 | new NonResizableEditPolicy() { |
| 103 | |
| 104 | protected List createSelectionHandles() { |
| 105 | List handles = new ArrayList(); |
| 106 | NonResizableHandleKit.addMoveHandle( |
| 107 | (GraphicalEditPart) getHost(), handles); |
| 108 | return handles; |
| 109 | } |
| 110 | |
| 111 | public Command getCommand(Request request) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | public boolean understandsRequest(Request request) { |
| 116 | return false; |
| 117 | } |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @generated |
| 123 | */ |
| 124 | protected String getLabelTextHelper(IFigure figure) { |
| 125 | if (figure instanceof WrapLabel) { |
| 126 | return ((WrapLabel) figure).getText(); |
| 127 | } else { |
| 128 | return ((Label) figure).getText(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @generated |
| 134 | */ |
| 135 | protected void setLabelTextHelper(IFigure figure, String text) { |
| 136 | if (figure instanceof WrapLabel) { |
| 137 | ((WrapLabel) figure).setText(text); |
| 138 | } else { |
| 139 | ((Label) figure).setText(text); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @generated |
| 145 | */ |
| 146 | protected Image getLabelIconHelper(IFigure figure) { |
| 147 | if (figure instanceof WrapLabel) { |
| 148 | return ((WrapLabel) figure).getIcon(); |
| 149 | } else { |
| 150 | return ((Label) figure).getIcon(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @generated |
| 156 | */ |
| 157 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
| 158 | if (figure instanceof WrapLabel) { |
| 159 | ((WrapLabel) figure).setIcon(icon); |
| 160 | } else { |
| 161 | ((Label) figure).setIcon(icon); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @generated |
| 167 | */ |
| 168 | public void setLabel(WrapLabel figure) { |
| 169 | unregisterVisuals(); |
| 170 | setFigure(figure); |
| 171 | defaultText = getLabelTextHelper(figure); |
| 172 | registerVisuals(); |
| 173 | refreshVisuals(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @generated |
| 178 | */ |
| 179 | protected List getModelChildren() { |
| 180 | return Collections.EMPTY_LIST; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @generated |
| 185 | */ |
| 186 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
| 187 | return null; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @generated |
| 192 | */ |
| 193 | protected EObject getParserElement() { |
| 194 | EObject element = resolveSemanticElement(); |
| 195 | return element != null ? element : (View) getModel(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @generated |
| 200 | */ |
| 201 | protected Image getLabelIcon() { |
| 202 | EObject parserElement = getParserElement(); |
| 203 | if (parserElement == null) { |
| 204 | return null; |
| 205 | } |
| 206 | return PalladioComponentModelElementTypes.getImage(parserElement |
| 207 | .eClass()); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @generated |
| 212 | */ |
| 213 | protected String getLabelText() { |
| 214 | String text = null; |
| 215 | if (getParser() != null) { |
| 216 | text = getParser().getPrintString( |
| 217 | new EObjectAdapter(getParserElement()), |
| 218 | getParserOptions().intValue()); |
| 219 | } |
| 220 | if (text == null || text.length() == 0) { |
| 221 | text = defaultText; |
| 222 | } |
| 223 | return text; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @generated |
| 228 | */ |
| 229 | public void setLabelText(String text) { |
| 230 | setLabelTextHelper(getFigure(), text); |
| 231 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
| 232 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
| 233 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
| 234 | .refreshFeedback(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @generated |
| 240 | */ |
| 241 | public String getEditText() { |
| 242 | if (getParser() == null) { |
| 243 | return ""; //$NON-NLS-1$ |
| 244 | } |
| 245 | return getParser().getEditString( |
| 246 | new EObjectAdapter(getParserElement()), |
| 247 | getParserOptions().intValue()); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @generated |
| 252 | */ |
| 253 | protected boolean isEditable() { |
| 254 | return getParser() != null; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @generated |
| 259 | */ |
| 260 | public ICellEditorValidator getEditTextValidator() { |
| 261 | return new ICellEditorValidator() { |
| 262 | |
| 263 | public String isValid(final Object value) { |
| 264 | if (value instanceof String) { |
| 265 | final EObject element = getParserElement(); |
| 266 | final IParser parser = getParser(); |
| 267 | try { |
| 268 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
| 269 | .runExclusive(new RunnableWithResult.Impl() { |
| 270 | |
| 271 | public void run() { |
| 272 | setResult(parser.isValidEditString( |
| 273 | new EObjectAdapter(element), |
| 274 | (String) value)); |
| 275 | } |
| 276 | }); |
| 277 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
| 278 | : valid.getMessage(); |
| 279 | } catch (InterruptedException ie) { |
| 280 | ie.printStackTrace(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // shouldn't get here |
| 285 | return null; |
| 286 | } |
| 287 | }; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @generated |
| 292 | */ |
| 293 | public IContentAssistProcessor getCompletionProcessor() { |
| 294 | if (getParser() == null) { |
| 295 | return null; |
| 296 | } |
| 297 | return getParser().getCompletionProcessor( |
| 298 | new EObjectAdapter(getParserElement())); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @generated |
| 303 | */ |
| 304 | public ParserOptions getParserOptions() { |
| 305 | return ParserOptions.NONE; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @generated |
| 310 | */ |
| 311 | public IParser getParser() { |
| 312 | if (parser == null) { |
| 313 | String parserHint = ((View) getModel()).getType(); |
| 314 | ParserHintAdapter hintAdapter = new ParserHintAdapter( |
| 315 | getParserElement(), parserHint) { |
| 316 | |
| 317 | public Object getAdapter(Class adapter) { |
| 318 | if (IElementType.class.equals(adapter)) { |
| 319 | return PalladioComponentModelElementTypes.ScenarioBehaviour_3007; |
| 320 | } |
| 321 | return super.getAdapter(adapter); |
| 322 | } |
| 323 | }; |
| 324 | parser = ParserService.getInstance().getParser(hintAdapter); |
| 325 | } |
| 326 | return parser; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @generated |
| 331 | */ |
| 332 | protected DirectEditManager getManager() { |
| 333 | if (manager == null) { |
| 334 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
| 335 | .getTextCellEditorClass(this), |
| 336 | PalladioComponentModelEditPartFactory |
| 337 | .getTextCellEditorLocator(this))); |
| 338 | } |
| 339 | return manager; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @generated |
| 344 | */ |
| 345 | protected void setManager(DirectEditManager manager) { |
| 346 | this.manager = manager; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @generated |
| 351 | */ |
| 352 | protected void performDirectEdit() { |
| 353 | getManager().show(); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * @generated |
| 358 | */ |
| 359 | protected void performDirectEdit(Point eventLocation) { |
| 360 | if (getManager().getClass() == TextDirectEditManager.class) { |
| 361 | ((TextDirectEditManager) getManager()).show(eventLocation |
| 362 | .getSWTPoint()); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @generated |
| 368 | */ |
| 369 | private void performDirectEdit(char initialCharacter) { |
| 370 | if (getManager() instanceof TextDirectEditManager) { |
| 371 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
| 372 | } else { |
| 373 | performDirectEdit(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * @generated |
| 379 | */ |
| 380 | protected void performDirectEditRequest(Request request) { |
| 381 | final Request theRequest = request; |
| 382 | try { |
| 383 | getEditingDomain().runExclusive(new Runnable() { |
| 384 | |
| 385 | public void run() { |
| 386 | if (isActive() && isEditable()) { |
| 387 | if (theRequest |
| 388 | .getExtendedData() |
| 389 | .get( |
| 390 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
| 391 | Character initialChar = (Character) theRequest |
| 392 | .getExtendedData() |
| 393 | .get( |
| 394 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
| 395 | performDirectEdit(initialChar.charValue()); |
| 396 | } else if ((theRequest instanceof DirectEditRequest) |
| 397 | && (getEditText().equals(getLabelText()))) { |
| 398 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
| 399 | performDirectEdit(editRequest.getLocation()); |
| 400 | } else { |
| 401 | performDirectEdit(); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | }); |
| 406 | } catch (InterruptedException e) { |
| 407 | e.printStackTrace(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * @generated |
| 413 | */ |
| 414 | protected void refreshVisuals() { |
| 415 | super.refreshVisuals(); |
| 416 | refreshLabel(); |
| 417 | refreshFont(); |
| 418 | refreshFontColor(); |
| 419 | refreshUnderline(); |
| 420 | refreshStrikeThrough(); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @generated |
| 425 | */ |
| 426 | protected void refreshLabel() { |
| 427 | setLabelTextHelper(getFigure(), getLabelText()); |
| 428 | setLabelIconHelper(getFigure(), getLabelIcon()); |
| 429 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
| 430 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
| 431 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
| 432 | .refreshFeedback(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * @generated |
| 438 | */ |
| 439 | protected void refreshUnderline() { |
| 440 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
| 441 | NotationPackage.eINSTANCE.getFontStyle()); |
| 442 | if (style != null && getFigure() instanceof WrapLabel) { |
| 443 | ((WrapLabel) getFigure()).setTextUnderline(style.isUnderline()); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * @generated |
| 449 | */ |
| 450 | protected void refreshStrikeThrough() { |
| 451 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
| 452 | NotationPackage.eINSTANCE.getFontStyle()); |
| 453 | if (style != null && getFigure() instanceof WrapLabel) { |
| 454 | ((WrapLabel) getFigure()).setTextStrikeThrough(style |
| 455 | .isStrikeThrough()); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * @generated |
| 461 | */ |
| 462 | protected void refreshFont() { |
| 463 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
| 464 | NotationPackage.eINSTANCE.getFontStyle()); |
| 465 | if (style != null) { |
| 466 | FontData fontData = new FontData(style.getFontName(), style |
| 467 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
| 468 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
| 469 | setFont(fontData); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * @generated |
| 475 | */ |
| 476 | protected void setFontColor(Color color) { |
| 477 | getFigure().setForegroundColor(color); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @generated |
| 482 | */ |
| 483 | protected void addSemanticListeners() { |
| 484 | if (getParser() instanceof ISemanticParser) { |
| 485 | EObject element = resolveSemanticElement(); |
| 486 | parserElements = ((ISemanticParser) getParser()) |
| 487 | .getSemanticElementsBeingParsed(element); |
| 488 | for (int i = 0; i < parserElements.size(); i++) { |
| 489 | addListenerFilter( |
| 490 | "SemanticModel" + i, this, (EObject) parserElements.get(i)); //$NON-NLS-1$ |
| 491 | } |
| 492 | } else { |
| 493 | super.addSemanticListeners(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * @generated |
| 499 | */ |
| 500 | protected void removeSemanticListeners() { |
| 501 | if (parserElements != null) { |
| 502 | for (int i = 0; i < parserElements.size(); i++) { |
| 503 | removeListenerFilter("SemanticModel" + i); //$NON-NLS-1$ |
| 504 | } |
| 505 | } else { |
| 506 | super.removeSemanticListeners(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * @generated |
| 512 | */ |
| 513 | protected AccessibleEditPart getAccessibleEditPart() { |
| 514 | if (accessibleEP == null) { |
| 515 | accessibleEP = new AccessibleGraphicalEditPart() { |
| 516 | |
| 517 | public void getName(AccessibleEvent e) { |
| 518 | e.result = getLabelTextHelper(getFigure()); |
| 519 | } |
| 520 | }; |
| 521 | } |
| 522 | return accessibleEP; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * @generated |
| 527 | */ |
| 528 | private View getFontStyleOwnerView() { |
| 529 | return getPrimaryView(); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * @generated |
| 534 | */ |
| 535 | protected void addNotationalListeners() { |
| 536 | super.addNotationalListeners(); |
| 537 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * @generated |
| 542 | */ |
| 543 | protected void removeNotationalListeners() { |
| 544 | super.removeNotationalListeners(); |
| 545 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * @generated |
| 550 | */ |
| 551 | protected void handleNotificationEvent(Notification event) { |
| 552 | Object feature = event.getFeature(); |
| 553 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
| 554 | Integer c = (Integer) event.getNewValue(); |
| 555 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
| 556 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
| 557 | feature)) { |
| 558 | refreshUnderline(); |
| 559 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
| 560 | .equals(feature)) { |
| 561 | refreshStrikeThrough(); |
| 562 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
| 563 | feature) |
| 564 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
| 565 | feature) |
| 566 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
| 567 | .equals(feature) |
| 568 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
| 569 | feature)) { |
| 570 | refreshFont(); |
| 571 | } else { |
| 572 | if (getParser() != null |
| 573 | && getParser().isAffectingEvent(event, |
| 574 | getParserOptions().intValue())) { |
| 575 | refreshLabel(); |
| 576 | } |
| 577 | if (getParser() instanceof ISemanticParser) { |
| 578 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
| 579 | if (modelParser.areSemanticElementsAffected(null, event)) { |
| 580 | removeSemanticListeners(); |
| 581 | if (resolveSemanticElement() != null) { |
| 582 | addSemanticListeners(); |
| 583 | } |
| 584 | refreshLabel(); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | super.handleNotificationEvent(event); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * @generated |
| 593 | */ |
| 594 | protected IFigure createFigure() { |
| 595 | // Parent should assign one using setLabel() method |
| 596 | return null; |
| 597 | } |
| 598 | } |