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