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 OperationRequiredRoleEntityNameEditPart extends LabelEditPart |
57 | implements ITextAwareEditPart, IBorderItemEditPart { |
58 | |
59 | /** |
60 | * @generated |
61 | */ |
62 | public static final int VISUAL_ID = 5008; |
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.OperationRequiredRoleEntityNameEditPart.VISUAL_ID), |
91 | new Point(0, 0)); |
92 | } |
93 | |
94 | /** |
95 | * @generated |
96 | */ |
97 | public OperationRequiredRoleEntityNameEditPart(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 | return null; |
222 | } |
223 | |
224 | /** |
225 | * @generated |
226 | */ |
227 | protected String getLabelText() { |
228 | String text = null; |
229 | EObject parserElement = getParserElement(); |
230 | if (parserElement != null && getParser() != null) { |
231 | text = getParser().getPrintString( |
232 | new EObjectAdapter(parserElement), |
233 | getParserOptions().intValue()); |
234 | } |
235 | if (text == null || text.length() == 0) { |
236 | text = defaultText; |
237 | } |
238 | return text; |
239 | } |
240 | |
241 | /** |
242 | * @generated |
243 | */ |
244 | public void setLabelText(String text) { |
245 | setLabelTextHelper(getFigure(), text); |
246 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
247 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
248 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
249 | .refreshFeedback(); |
250 | } |
251 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
252 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
253 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
254 | .refreshFeedback(); |
255 | } |
256 | } |
257 | |
258 | /** |
259 | * @generated |
260 | */ |
261 | public String getEditText() { |
262 | if (getParserElement() == null || getParser() == null) { |
263 | return ""; //$NON-NLS-1$ |
264 | } |
265 | return getParser().getEditString( |
266 | new EObjectAdapter(getParserElement()), |
267 | getParserOptions().intValue()); |
268 | } |
269 | |
270 | /** |
271 | * @generated |
272 | */ |
273 | protected boolean isEditable() { |
274 | return false; |
275 | } |
276 | |
277 | /** |
278 | * @generated |
279 | */ |
280 | public ICellEditorValidator getEditTextValidator() { |
281 | return new ICellEditorValidator() { |
282 | |
283 | public String isValid(final Object value) { |
284 | if (value instanceof String) { |
285 | final EObject element = getParserElement(); |
286 | final IParser parser = getParser(); |
287 | try { |
288 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
289 | .runExclusive(new RunnableWithResult.Impl() { |
290 | |
291 | public void run() { |
292 | setResult(parser.isValidEditString( |
293 | new EObjectAdapter(element), |
294 | (String) value)); |
295 | } |
296 | }); |
297 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
298 | : valid.getMessage(); |
299 | } catch (InterruptedException ie) { |
300 | ie.printStackTrace(); |
301 | } |
302 | } |
303 | |
304 | // shouldn't get here |
305 | return null; |
306 | } |
307 | }; |
308 | } |
309 | |
310 | /** |
311 | * @generated |
312 | */ |
313 | public IContentAssistProcessor getCompletionProcessor() { |
314 | if (getParserElement() == null || getParser() == null) { |
315 | return null; |
316 | } |
317 | return getParser().getCompletionProcessor( |
318 | new EObjectAdapter(getParserElement())); |
319 | } |
320 | |
321 | /** |
322 | * @generated |
323 | */ |
324 | public ParserOptions getParserOptions() { |
325 | return ParserOptions.NONE; |
326 | } |
327 | |
328 | /** |
329 | * @generated |
330 | */ |
331 | public IParser getParser() { |
332 | if (parser == null) { |
333 | parser = PalladioComponentModelParserProvider |
334 | .getParser( |
335 | PalladioComponentModelElementTypes.OperationRequiredRole_3008, |
336 | getParserElement(), |
337 | PalladioComponentModelVisualIDRegistry |
338 | .getType(de.uka.ipd.sdq.pcm.gmf.composite.edit.parts.OperationRequiredRoleEntityNameEditPart.VISUAL_ID)); |
339 | } |
340 | return parser; |
341 | } |
342 | |
343 | /** |
344 | * @generated |
345 | */ |
346 | protected DirectEditManager getManager() { |
347 | if (manager == null) { |
348 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
349 | .getTextCellEditorClass(this), |
350 | PalladioComponentModelEditPartFactory |
351 | .getTextCellEditorLocator(this))); |
352 | } |
353 | return manager; |
354 | } |
355 | |
356 | /** |
357 | * @generated |
358 | */ |
359 | protected void setManager(DirectEditManager manager) { |
360 | this.manager = manager; |
361 | } |
362 | |
363 | /** |
364 | * @generated |
365 | */ |
366 | protected void performDirectEdit() { |
367 | getManager().show(); |
368 | } |
369 | |
370 | /** |
371 | * @generated |
372 | */ |
373 | protected void performDirectEdit(Point eventLocation) { |
374 | if (getManager().getClass() == TextDirectEditManager.class) { |
375 | ((TextDirectEditManager) getManager()).show(eventLocation |
376 | .getSWTPoint()); |
377 | } |
378 | } |
379 | |
380 | /** |
381 | * @generated |
382 | */ |
383 | private void performDirectEdit(char initialCharacter) { |
384 | if (getManager() instanceof TextDirectEditManager) { |
385 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
386 | } else { |
387 | performDirectEdit(); |
388 | } |
389 | } |
390 | |
391 | /** |
392 | * @generated |
393 | */ |
394 | protected void performDirectEditRequest(Request request) { |
395 | final Request theRequest = request; |
396 | try { |
397 | getEditingDomain().runExclusive(new Runnable() { |
398 | |
399 | public void run() { |
400 | if (isActive() && isEditable()) { |
401 | if (theRequest |
402 | .getExtendedData() |
403 | .get( |
404 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
405 | Character initialChar = (Character) theRequest |
406 | .getExtendedData() |
407 | .get( |
408 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
409 | performDirectEdit(initialChar.charValue()); |
410 | } else if ((theRequest instanceof DirectEditRequest) |
411 | && (getEditText().equals(getLabelText()))) { |
412 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
413 | performDirectEdit(editRequest.getLocation()); |
414 | } else { |
415 | performDirectEdit(); |
416 | } |
417 | } |
418 | } |
419 | }); |
420 | } catch (InterruptedException e) { |
421 | e.printStackTrace(); |
422 | } |
423 | } |
424 | |
425 | /** |
426 | * @generated |
427 | */ |
428 | protected void refreshVisuals() { |
429 | super.refreshVisuals(); |
430 | refreshLabel(); |
431 | refreshFont(); |
432 | refreshFontColor(); |
433 | refreshUnderline(); |
434 | refreshStrikeThrough(); |
435 | } |
436 | |
437 | /** |
438 | * @generated |
439 | */ |
440 | protected void refreshLabel() { |
441 | setLabelTextHelper(getFigure(), getLabelText()); |
442 | setLabelIconHelper(getFigure(), getLabelIcon()); |
443 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
444 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
445 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
446 | .refreshFeedback(); |
447 | } |
448 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
449 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
450 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
451 | .refreshFeedback(); |
452 | } |
453 | } |
454 | |
455 | /** |
456 | * @generated |
457 | */ |
458 | protected void refreshUnderline() { |
459 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
460 | NotationPackage.eINSTANCE.getFontStyle()); |
461 | if (style != null && getFigure() instanceof WrappingLabel) { |
462 | ((WrappingLabel) getFigure()).setTextUnderline(style.isUnderline()); |
463 | } |
464 | } |
465 | |
466 | /** |
467 | * @generated |
468 | */ |
469 | protected void refreshStrikeThrough() { |
470 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
471 | NotationPackage.eINSTANCE.getFontStyle()); |
472 | if (style != null && getFigure() instanceof WrappingLabel) { |
473 | ((WrappingLabel) getFigure()).setTextStrikeThrough(style |
474 | .isStrikeThrough()); |
475 | } |
476 | } |
477 | |
478 | /** |
479 | * @generated |
480 | */ |
481 | protected void refreshFont() { |
482 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
483 | NotationPackage.eINSTANCE.getFontStyle()); |
484 | if (style != null) { |
485 | FontData fontData = new FontData(style.getFontName(), style |
486 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
487 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
488 | setFont(fontData); |
489 | } |
490 | } |
491 | |
492 | /** |
493 | * @generated |
494 | */ |
495 | protected void setFontColor(Color color) { |
496 | getFigure().setForegroundColor(color); |
497 | } |
498 | |
499 | /** |
500 | * @generated |
501 | */ |
502 | protected void addSemanticListeners() { |
503 | if (getParser() instanceof ISemanticParser) { |
504 | EObject element = resolveSemanticElement(); |
505 | parserElements = ((ISemanticParser) getParser()) |
506 | .getSemanticElementsBeingParsed(element); |
507 | for (int i = 0; i < parserElements.size(); i++) { |
508 | addListenerFilter( |
509 | "SemanticModel" + i, this, (EObject) parserElements.get(i)); //$NON-NLS-1$ |
510 | } |
511 | } else { |
512 | super.addSemanticListeners(); |
513 | } |
514 | } |
515 | |
516 | /** |
517 | * @generated |
518 | */ |
519 | protected void removeSemanticListeners() { |
520 | if (parserElements != null) { |
521 | for (int i = 0; i < parserElements.size(); i++) { |
522 | removeListenerFilter("SemanticModel" + i); //$NON-NLS-1$ |
523 | } |
524 | } else { |
525 | super.removeSemanticListeners(); |
526 | } |
527 | } |
528 | |
529 | /** |
530 | * @generated |
531 | */ |
532 | protected AccessibleEditPart getAccessibleEditPart() { |
533 | if (accessibleEP == null) { |
534 | accessibleEP = new AccessibleGraphicalEditPart() { |
535 | |
536 | public void getName(AccessibleEvent e) { |
537 | e.result = getLabelTextHelper(getFigure()); |
538 | } |
539 | }; |
540 | } |
541 | return accessibleEP; |
542 | } |
543 | |
544 | /** |
545 | * @generated |
546 | */ |
547 | private View getFontStyleOwnerView() { |
548 | return getPrimaryView(); |
549 | } |
550 | |
551 | /** |
552 | * @generated |
553 | */ |
554 | protected void handleNotificationEvent(Notification event) { |
555 | Object feature = event.getFeature(); |
556 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
557 | Integer c = (Integer) event.getNewValue(); |
558 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
559 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
560 | feature)) { |
561 | refreshUnderline(); |
562 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
563 | .equals(feature)) { |
564 | refreshStrikeThrough(); |
565 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
566 | feature) |
567 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
568 | feature) |
569 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
570 | .equals(feature) |
571 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
572 | feature)) { |
573 | refreshFont(); |
574 | } else { |
575 | if (getParser() != null |
576 | && getParser().isAffectingEvent(event, |
577 | getParserOptions().intValue())) { |
578 | refreshLabel(); |
579 | } |
580 | if (getParser() instanceof ISemanticParser) { |
581 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
582 | if (modelParser.areSemanticElementsAffected(null, event)) { |
583 | removeSemanticListeners(); |
584 | if (resolveSemanticElement() != null) { |
585 | addSemanticListeners(); |
586 | } |
587 | refreshLabel(); |
588 | } |
589 | } |
590 | } |
591 | super.handleNotificationEvent(event); |
592 | } |
593 | |
594 | /** |
595 | * @generated |
596 | */ |
597 | protected IFigure createFigure() { |
598 | IFigure label = createFigurePrim(); |
599 | defaultText = getLabelTextHelper(label); |
600 | return label; |
601 | } |
602 | |
603 | /** |
604 | * @generated |
605 | */ |
606 | protected IFigure createFigurePrim() { |
607 | return new RoleLabelFigure(); |
608 | } |
609 | |
610 | /** |
611 | * @generated |
612 | */ |
613 | public class RoleLabelFigure extends WrappingLabel { |
614 | |
615 | /** |
616 | * @generated |
617 | */ |
618 | public RoleLabelFigure() { |
619 | this.setText("<IRole>"); |
620 | } |
621 | |
622 | } |
623 | |
624 | } |