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.LabelDirectEditPolicy; |
34 | import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry; |
35 | import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
36 | import org.eclipse.gmf.runtime.diagram.ui.tools.TextDirectEditManager; |
37 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; |
38 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
39 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser; |
40 | import org.eclipse.gmf.runtime.notation.FontStyle; |
41 | import org.eclipse.gmf.runtime.notation.NotationPackage; |
42 | import org.eclipse.gmf.runtime.notation.View; |
43 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
44 | import org.eclipse.jface.viewers.ICellEditorValidator; |
45 | import org.eclipse.swt.SWT; |
46 | import org.eclipse.swt.accessibility.AccessibleEvent; |
47 | import org.eclipse.swt.graphics.Color; |
48 | import org.eclipse.swt.graphics.FontData; |
49 | import org.eclipse.swt.graphics.Image; |
50 | |
51 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
52 | import de.uka.ipd.sdq.pcm.gmf.seff.part.PalladioComponentModelVisualIDRegistry; |
53 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelElementTypes; |
54 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelParserProvider; |
55 | import de.uka.ipd.sdq.pcm.parameter.VariableUsage; |
56 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
57 | |
58 | /** |
59 | * @generated |
60 | */ |
61 | public class VariableUsage5LabelEditPart extends CompartmentEditPart implements |
62 | ITextAwareEditPart { |
63 | |
64 | /** |
65 | * @generated |
66 | */ |
67 | public static final int VISUAL_ID = 5050; |
68 | |
69 | /** |
70 | * @generated |
71 | */ |
72 | private DirectEditManager manager; |
73 | |
74 | /** |
75 | * @generated |
76 | */ |
77 | private IParser parser; |
78 | |
79 | /** |
80 | * @generated |
81 | */ |
82 | private List parserElements; |
83 | |
84 | /** |
85 | * @generated |
86 | */ |
87 | private String defaultText; |
88 | |
89 | /** |
90 | * @generated |
91 | */ |
92 | public VariableUsage5LabelEditPart(View view) { |
93 | super(view); |
94 | } |
95 | |
96 | /** |
97 | * @generated |
98 | */ |
99 | protected void createDefaultEditPolicies() { |
100 | super.createDefaultEditPolicies(); |
101 | installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, |
102 | new PalladioComponentModelTextSelectionEditPolicy()); |
103 | installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, |
104 | new LabelDirectEditPolicy()); |
105 | installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, |
106 | new NonResizableEditPolicy() { |
107 | |
108 | protected List createSelectionHandles() { |
109 | List handles = new ArrayList(); |
110 | NonResizableHandleKit.addMoveHandle( |
111 | (GraphicalEditPart) getHost(), handles); |
112 | ((MoveHandle) handles.get(0)).setBorder(null); |
113 | return handles; |
114 | } |
115 | |
116 | public Command getCommand(Request request) { |
117 | return null; |
118 | } |
119 | |
120 | public boolean understandsRequest(Request request) { |
121 | return false; |
122 | } |
123 | }); |
124 | } |
125 | |
126 | /** |
127 | * @generated |
128 | */ |
129 | protected String getLabelTextHelper(IFigure figure) { |
130 | if (figure instanceof WrappingLabel) { |
131 | return ((WrappingLabel) figure).getText(); |
132 | } else { |
133 | return ((Label) figure).getText(); |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * @generated |
139 | */ |
140 | protected void setLabelTextHelper(IFigure figure, String text) { |
141 | if (figure instanceof WrappingLabel) { |
142 | ((WrappingLabel) figure).setText(text); |
143 | } else { |
144 | ((Label) figure).setText(text); |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * @generated |
150 | */ |
151 | protected Image getLabelIconHelper(IFigure figure) { |
152 | if (figure instanceof WrappingLabel) { |
153 | return ((WrappingLabel) figure).getIcon(); |
154 | } else { |
155 | return ((Label) figure).getIcon(); |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * @generated |
161 | */ |
162 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
163 | if (figure instanceof WrappingLabel) { |
164 | ((WrappingLabel) figure).setIcon(icon); |
165 | } else { |
166 | ((Label) figure).setIcon(icon); |
167 | } |
168 | } |
169 | |
170 | /** |
171 | * @generated |
172 | */ |
173 | public void setLabel(WrappingLabel figure) { |
174 | unregisterVisuals(); |
175 | setFigure(figure); |
176 | defaultText = getLabelTextHelper(figure); |
177 | registerVisuals(); |
178 | refreshVisuals(); |
179 | } |
180 | |
181 | /** |
182 | * @generated |
183 | */ |
184 | protected List getModelChildren() { |
185 | return Collections.EMPTY_LIST; |
186 | } |
187 | |
188 | /** |
189 | * @generated |
190 | */ |
191 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
192 | return null; |
193 | } |
194 | |
195 | /** |
196 | * @generated |
197 | */ |
198 | protected EObject getParserElement() { |
199 | return resolveSemanticElement(); |
200 | } |
201 | |
202 | /** |
203 | * @generated |
204 | */ |
205 | protected Image getLabelIcon() { |
206 | EObject parserElement = getParserElement(); |
207 | if (parserElement == null) { |
208 | return null; |
209 | } |
210 | return PalladioComponentModelElementTypes.getImage(parserElement |
211 | .eClass()); |
212 | } |
213 | |
214 | /**Displays the name of the referenced variable. |
215 | * |
216 | * @generated not |
217 | */ |
218 | protected String getLabelText() { |
219 | String text = null; |
220 | EObject parserElement = getParserElement(); |
221 | if (parserElement != null) { |
222 | if (parserElement instanceof VariableUsage) { |
223 | // customized |
224 | VariableUsage usage = (VariableUsage) this |
225 | .resolveSemanticElement(); |
226 | if (usage.getNamedReference__VariableUsage() != null) { |
227 | text = new PCMStoExPrettyPrintVisitor().prettyPrint(usage |
228 | .getNamedReference__VariableUsage()); |
229 | } |
230 | } else { |
231 | if (parserElement != null && getParser() != null) { |
232 | text = getParser().getPrintString( |
233 | new EObjectAdapter(parserElement), |
234 | getParserOptions().intValue()); |
235 | } |
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 false; |
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.VariableUsage_3054, |
339 | getParserElement(), |
340 | PalladioComponentModelVisualIDRegistry |
341 | .getType(de.uka.ipd.sdq.pcm.gmf.seff.edit.parts.VariableUsage5LabelEditPart.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 | /** |
571 | * @generated |
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 | if (getParser() != null |
595 | && getParser().isAffectingEvent(event, |
596 | getParserOptions().intValue())) { |
597 | refreshLabel(); |
598 | } |
599 | if (getParser() instanceof ISemanticParser) { |
600 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
601 | if (modelParser.areSemanticElementsAffected(null, event)) { |
602 | removeSemanticListeners(); |
603 | if (resolveSemanticElement() != null) { |
604 | addSemanticListeners(); |
605 | } |
606 | refreshLabel(); |
607 | } |
608 | } |
609 | } |
610 | super.handleNotificationEvent(event); |
611 | } |
612 | |
613 | /** |
614 | * @generated |
615 | */ |
616 | protected IFigure createFigure() { |
617 | // Parent should assign one using setLabel() method |
618 | return null; |
619 | } |
620 | |
621 | } |