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