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