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.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.MoveHandle; |
23 | import org.eclipse.gef.handles.NonResizableHandleKit; |
24 | import org.eclipse.gef.requests.DirectEditRequest; |
25 | import org.eclipse.gef.tools.DirectEditManager; |
26 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; |
27 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; |
28 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; |
29 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions; |
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.EditPolicyRoles; |
34 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.LabelDirectEditPolicy; |
35 | import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry; |
36 | import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
37 | import org.eclipse.gmf.runtime.diagram.ui.tools.TextDirectEditManager; |
38 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; |
39 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
40 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser; |
41 | import org.eclipse.gmf.runtime.notation.FontStyle; |
42 | import org.eclipse.gmf.runtime.notation.NotationPackage; |
43 | import org.eclipse.gmf.runtime.notation.View; |
44 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
45 | import org.eclipse.jface.viewers.ICellEditorValidator; |
46 | import org.eclipse.swt.SWT; |
47 | import org.eclipse.swt.accessibility.AccessibleEvent; |
48 | import org.eclipse.swt.graphics.Color; |
49 | import org.eclipse.swt.graphics.FontData; |
50 | import org.eclipse.swt.graphics.Image; |
51 | |
52 | import de.uka.ipd.sdq.pcm.dialogs.seff.OpenInfrastructureCallNumberOfCallsDialog; |
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.seff.ExternalCallAction; |
58 | import de.uka.ipd.sdq.pcm.seff.seff_performance.InfrastructureCall; |
59 | |
60 | /** |
61 | * @generated |
62 | */ |
63 | public class InfrastructureCallTargetLabelEditPart extends CompartmentEditPart |
64 | implements ITextAwareEditPart { |
65 | |
66 | /** |
67 | * @generated |
68 | */ |
69 | public static final int VISUAL_ID = 5052; |
70 | |
71 | /** |
72 | * @generated |
73 | */ |
74 | private DirectEditManager manager; |
75 | |
76 | /** |
77 | * @generated |
78 | */ |
79 | private IParser parser; |
80 | |
81 | /** |
82 | * @generated |
83 | */ |
84 | private List parserElements; |
85 | |
86 | /** |
87 | * @generated |
88 | */ |
89 | private String defaultText; |
90 | |
91 | /** |
92 | * @generated |
93 | */ |
94 | public InfrastructureCallTargetLabelEditPart(View view) { |
95 | super(view); |
96 | } |
97 | |
98 | /** |
99 | * @generated |
100 | */ |
101 | protected void createDefaultEditPolicies() { |
102 | super.createDefaultEditPolicies(); |
103 | installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, |
104 | new PalladioComponentModelTextSelectionEditPolicy()); |
105 | installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, |
106 | new LabelDirectEditPolicy()); |
107 | installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, |
108 | new NonResizableEditPolicy() { |
109 | |
110 | protected List createSelectionHandles() { |
111 | List handles = new ArrayList(); |
112 | NonResizableHandleKit.addMoveHandle( |
113 | (GraphicalEditPart) getHost(), handles); |
114 | ((MoveHandle) handles.get(0)).setBorder(null); |
115 | return handles; |
116 | } |
117 | |
118 | public Command getCommand(Request request) { |
119 | return null; |
120 | } |
121 | |
122 | public boolean understandsRequest(Request request) { |
123 | return false; |
124 | } |
125 | }); |
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 | /**Displays required role and signature. |
217 | * @generated not |
218 | */ |
219 | protected String getLabelText() { |
220 | String text = null; |
221 | EObject parserElement = getParserElement(); |
222 | if (parserElement instanceof InfrastructureCall) { |
223 | InfrastructureCall eca = (InfrastructureCall) resolveSemanticElement(); |
224 | text = ""; |
225 | if (eca.getRequiredRole__InfrastructureCall() != null) { |
226 | text += eca.getRequiredRole__InfrastructureCall() |
227 | .getEntityName(); |
228 | } else { |
229 | text += "?"; |
230 | } |
231 | text += "."; |
232 | if (eca.getSignature__InfrastructureCall() != null) { |
233 | text += eca.getSignature__InfrastructureCall().getEntityName(); |
234 | } else { |
235 | text += "?"; |
236 | } |
237 | } |
238 | if (text == null || text.length() == 0) { |
239 | text = defaultText; |
240 | } |
241 | return text; |
242 | } |
243 | |
244 | /** |
245 | * @generated |
246 | */ |
247 | public void setLabelText(String text) { |
248 | setLabelTextHelper(getFigure(), text); |
249 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
250 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
251 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
252 | .refreshFeedback(); |
253 | } |
254 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
255 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
256 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
257 | .refreshFeedback(); |
258 | } |
259 | } |
260 | |
261 | /** |
262 | * @generated |
263 | */ |
264 | public String getEditText() { |
265 | if (getParserElement() == null || getParser() == null) { |
266 | return ""; //$NON-NLS-1$ |
267 | } |
268 | return getParser().getEditString( |
269 | new EObjectAdapter(getParserElement()), |
270 | getParserOptions().intValue()); |
271 | } |
272 | |
273 | /** |
274 | * @generated |
275 | */ |
276 | protected boolean isEditable() { |
277 | return getParser() != null; |
278 | } |
279 | |
280 | /** |
281 | * @generated |
282 | */ |
283 | public ICellEditorValidator getEditTextValidator() { |
284 | return new ICellEditorValidator() { |
285 | |
286 | public String isValid(final Object value) { |
287 | if (value instanceof String) { |
288 | final EObject element = getParserElement(); |
289 | final IParser parser = getParser(); |
290 | try { |
291 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
292 | .runExclusive(new RunnableWithResult.Impl() { |
293 | |
294 | public void run() { |
295 | setResult(parser.isValidEditString( |
296 | new EObjectAdapter(element), |
297 | (String) value)); |
298 | } |
299 | }); |
300 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
301 | : valid.getMessage(); |
302 | } catch (InterruptedException ie) { |
303 | ie.printStackTrace(); |
304 | } |
305 | } |
306 | |
307 | // shouldn't get here |
308 | return null; |
309 | } |
310 | }; |
311 | } |
312 | |
313 | /** |
314 | * @generated |
315 | */ |
316 | public IContentAssistProcessor getCompletionProcessor() { |
317 | if (getParserElement() == null || getParser() == null) { |
318 | return null; |
319 | } |
320 | return getParser().getCompletionProcessor( |
321 | new EObjectAdapter(getParserElement())); |
322 | } |
323 | |
324 | /** |
325 | * @generated |
326 | */ |
327 | public ParserOptions getParserOptions() { |
328 | return ParserOptions.NONE; |
329 | } |
330 | |
331 | /** |
332 | * @generated |
333 | */ |
334 | public IParser getParser() { |
335 | if (parser == null) { |
336 | parser = PalladioComponentModelParserProvider |
337 | .getParser( |
338 | PalladioComponentModelElementTypes.InfrastructureCall_3053, |
339 | getParserElement(), |
340 | PalladioComponentModelVisualIDRegistry |
341 | .getType(de.uka.ipd.sdq.pcm.gmf.seff.edit.parts.InfrastructureCallTargetLabelEditPart.VISUAL_ID)); |
342 | } |
343 | return parser; |
344 | } |
345 | |
346 | /** |
347 | * @generated |
348 | */ |
349 | protected DirectEditManager getManager() { |
350 | if (manager == null) { |
351 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
352 | .getTextCellEditorClass(this), |
353 | PalladioComponentModelEditPartFactory |
354 | .getTextCellEditorLocator(this))); |
355 | } |
356 | return manager; |
357 | } |
358 | |
359 | /** |
360 | * @generated |
361 | */ |
362 | protected void setManager(DirectEditManager manager) { |
363 | this.manager = manager; |
364 | } |
365 | |
366 | /** |
367 | * @generated |
368 | */ |
369 | protected void performDirectEdit() { |
370 | getManager().show(); |
371 | } |
372 | |
373 | /** |
374 | * @generated |
375 | */ |
376 | protected void performDirectEdit(Point eventLocation) { |
377 | if (getManager().getClass() == TextDirectEditManager.class) { |
378 | ((TextDirectEditManager) getManager()).show(eventLocation |
379 | .getSWTPoint()); |
380 | } |
381 | } |
382 | |
383 | /** |
384 | * @generated |
385 | */ |
386 | private void performDirectEdit(char initialCharacter) { |
387 | if (getManager() instanceof TextDirectEditManager) { |
388 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
389 | } else { |
390 | performDirectEdit(); |
391 | } |
392 | } |
393 | |
394 | /** |
395 | * @generated |
396 | */ |
397 | protected void performDirectEditRequest(Request request) { |
398 | final Request theRequest = request; |
399 | try { |
400 | getEditingDomain().runExclusive(new Runnable() { |
401 | |
402 | public void run() { |
403 | if (isActive() && isEditable()) { |
404 | if (theRequest |
405 | .getExtendedData() |
406 | .get( |
407 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
408 | Character initialChar = (Character) theRequest |
409 | .getExtendedData() |
410 | .get( |
411 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
412 | performDirectEdit(initialChar.charValue()); |
413 | } else if ((theRequest instanceof DirectEditRequest) |
414 | && (getEditText().equals(getLabelText()))) { |
415 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
416 | performDirectEdit(editRequest.getLocation()); |
417 | } else { |
418 | performDirectEdit(); |
419 | } |
420 | } |
421 | } |
422 | }); |
423 | } catch (InterruptedException e) { |
424 | e.printStackTrace(); |
425 | } |
426 | } |
427 | |
428 | /** |
429 | * @generated |
430 | */ |
431 | protected void refreshVisuals() { |
432 | super.refreshVisuals(); |
433 | refreshLabel(); |
434 | refreshFont(); |
435 | refreshFontColor(); |
436 | refreshUnderline(); |
437 | refreshStrikeThrough(); |
438 | } |
439 | |
440 | /** |
441 | * @generated |
442 | */ |
443 | protected void refreshLabel() { |
444 | setLabelTextHelper(getFigure(), getLabelText()); |
445 | setLabelIconHelper(getFigure(), getLabelIcon()); |
446 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
447 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
448 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
449 | .refreshFeedback(); |
450 | } |
451 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
452 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
453 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
454 | .refreshFeedback(); |
455 | } |
456 | } |
457 | |
458 | /** |
459 | * @generated |
460 | */ |
461 | protected void refreshUnderline() { |
462 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
463 | NotationPackage.eINSTANCE.getFontStyle()); |
464 | if (style != null && getFigure() instanceof WrappingLabel) { |
465 | ((WrappingLabel) getFigure()).setTextUnderline(style.isUnderline()); |
466 | } |
467 | } |
468 | |
469 | /** |
470 | * @generated |
471 | */ |
472 | protected void refreshStrikeThrough() { |
473 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
474 | NotationPackage.eINSTANCE.getFontStyle()); |
475 | if (style != null && getFigure() instanceof WrappingLabel) { |
476 | ((WrappingLabel) getFigure()).setTextStrikeThrough(style |
477 | .isStrikeThrough()); |
478 | } |
479 | } |
480 | |
481 | /** |
482 | * @generated |
483 | */ |
484 | protected void refreshFont() { |
485 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
486 | NotationPackage.eINSTANCE.getFontStyle()); |
487 | if (style != null) { |
488 | FontData fontData = new FontData(style.getFontName(), style |
489 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
490 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
491 | setFont(fontData); |
492 | } |
493 | } |
494 | |
495 | /** |
496 | * @generated |
497 | */ |
498 | protected void setFontColor(Color color) { |
499 | getFigure().setForegroundColor(color); |
500 | } |
501 | |
502 | /** |
503 | * @generated |
504 | */ |
505 | protected void addSemanticListeners() { |
506 | if (getParser() instanceof ISemanticParser) { |
507 | EObject element = resolveSemanticElement(); |
508 | parserElements = ((ISemanticParser) getParser()) |
509 | .getSemanticElementsBeingParsed(element); |
510 | for (int i = 0; i < parserElements.size(); i++) { |
511 | addListenerFilter( |
512 | "SemanticModel" + i, this, (EObject) parserElements.get(i)); //$NON-NLS-1$ |
513 | } |
514 | } else { |
515 | super.addSemanticListeners(); |
516 | } |
517 | } |
518 | |
519 | /** |
520 | * @generated |
521 | */ |
522 | protected void removeSemanticListeners() { |
523 | if (parserElements != null) { |
524 | for (int i = 0; i < parserElements.size(); i++) { |
525 | removeListenerFilter("SemanticModel" + i); //$NON-NLS-1$ |
526 | } |
527 | } else { |
528 | super.removeSemanticListeners(); |
529 | } |
530 | } |
531 | |
532 | /** |
533 | * @generated |
534 | */ |
535 | protected AccessibleEditPart getAccessibleEditPart() { |
536 | if (accessibleEP == null) { |
537 | accessibleEP = new AccessibleGraphicalEditPart() { |
538 | |
539 | public void getName(AccessibleEvent e) { |
540 | e.result = getLabelTextHelper(getFigure()); |
541 | } |
542 | }; |
543 | } |
544 | return accessibleEP; |
545 | } |
546 | |
547 | /** |
548 | * @generated |
549 | */ |
550 | private View getFontStyleOwnerView() { |
551 | return getPrimaryView(); |
552 | } |
553 | |
554 | /** |
555 | * @generated |
556 | */ |
557 | protected void addNotationalListeners() { |
558 | super.addNotationalListeners(); |
559 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
560 | } |
561 | |
562 | /** |
563 | * @generated |
564 | */ |
565 | protected void removeNotationalListeners() { |
566 | super.removeNotationalListeners(); |
567 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
568 | } |
569 | |
570 | /**Handles refresh of custommized text. |
571 | * @generated not |
572 | */ |
573 | protected void handleNotificationEvent(Notification event) { |
574 | Object feature = event.getFeature(); |
575 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
576 | Integer c = (Integer) event.getNewValue(); |
577 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
578 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
579 | feature)) { |
580 | refreshUnderline(); |
581 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
582 | .equals(feature)) { |
583 | refreshStrikeThrough(); |
584 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
585 | feature) |
586 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
587 | feature) |
588 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
589 | .equals(feature) |
590 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
591 | feature)) { |
592 | refreshFont(); |
593 | } else { |
594 | refreshLabel(); |
595 | } |
596 | super.handleNotificationEvent(event); |
597 | } |
598 | |
599 | /** |
600 | * @generated |
601 | */ |
602 | protected IFigure createFigure() { |
603 | // Parent should assign one using setLabel() method |
604 | return null; |
605 | } |
606 | |
607 | } |