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