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