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