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