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