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