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