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