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