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