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.Collections; |
7 | import java.util.List; |
8 | |
9 | import org.eclipse.draw2d.IFigure; |
10 | import org.eclipse.draw2d.Label; |
11 | import org.eclipse.draw2d.geometry.Point; |
12 | import org.eclipse.emf.common.notify.Notification; |
13 | import org.eclipse.emf.ecore.EObject; |
14 | import org.eclipse.emf.transaction.RunnableWithResult; |
15 | import org.eclipse.gef.AccessibleEditPart; |
16 | import org.eclipse.gef.DragTracker; |
17 | import org.eclipse.gef.EditPolicy; |
18 | import org.eclipse.gef.Request; |
19 | import org.eclipse.gef.requests.DirectEditRequest; |
20 | import org.eclipse.gef.requests.SelectionRequest; |
21 | import org.eclipse.gef.tools.DirectEditManager; |
22 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; |
23 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; |
24 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; |
25 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions; |
26 | import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart; |
27 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
28 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
29 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
30 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.LabelDirectEditPolicy; |
31 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ListItemComponentEditPolicy; |
32 | import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry; |
33 | import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
34 | import org.eclipse.gmf.runtime.diagram.ui.tools.DragEditPartsTrackerEx; |
35 | import org.eclipse.gmf.runtime.diagram.ui.tools.TextDirectEditManager; |
36 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel; |
37 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
38 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser; |
39 | import org.eclipse.gmf.runtime.notation.FontStyle; |
40 | import org.eclipse.gmf.runtime.notation.NotationPackage; |
41 | import org.eclipse.gmf.runtime.notation.View; |
42 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
43 | import org.eclipse.jface.viewers.ICellEditorValidator; |
44 | import org.eclipse.swt.SWT; |
45 | import org.eclipse.swt.accessibility.AccessibleEvent; |
46 | import org.eclipse.swt.graphics.Color; |
47 | import org.eclipse.swt.graphics.FontData; |
48 | import org.eclipse.swt.graphics.Image; |
49 | |
50 | import de.uka.ipd.sdq.pcm.dialogs.OpenStoExDialog; |
51 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.PalladioComponentModelTextNonResizableEditPolicy; |
52 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
53 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.VariableCharacterisationItemSemanticEditPolicy; |
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.parameter.VariableCharacterisation; |
58 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
59 | import de.uka.ipd.sdq.stoex.Expression; |
60 | |
61 | /** |
62 | * @generated |
63 | */ |
64 | public class VariableCharacterisationEditPart extends CompartmentEditPart |
65 | implements ITextAwareEditPart { |
66 | |
67 | /** |
68 | * @generated |
69 | */ |
70 | public static final int VISUAL_ID = 3033; |
71 | |
72 | /** |
73 | * @generated |
74 | */ |
75 | private DirectEditManager manager; |
76 | |
77 | /** |
78 | * @generated |
79 | */ |
80 | private IParser parser; |
81 | |
82 | /** |
83 | * @generated |
84 | */ |
85 | private List parserElements; |
86 | |
87 | /** |
88 | * @generated |
89 | */ |
90 | private String defaultText; |
91 | |
92 | /** |
93 | * @generated |
94 | */ |
95 | public VariableCharacterisationEditPart(View view) { |
96 | super(view); |
97 | } |
98 | |
99 | /** |
100 | * @generated |
101 | */ |
102 | public DragTracker getDragTracker(Request request) { |
103 | if (request instanceof SelectionRequest |
104 | && ((SelectionRequest) request).getLastButtonPressed() == 3) { |
105 | return null; |
106 | } |
107 | return new DragEditPartsTrackerEx(this); |
108 | } |
109 | |
110 | /** |
111 | * @generated |
112 | */ |
113 | protected void createDefaultEditPolicies() { |
114 | super.createDefaultEditPolicies(); |
115 | installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, |
116 | new VariableCharacterisationItemSemanticEditPolicy()); |
117 | installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, |
118 | new PalladioComponentModelTextNonResizableEditPolicy()); |
119 | installEditPolicy(EditPolicy.COMPONENT_ROLE, |
120 | new ListItemComponentEditPolicy()); |
121 | installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, |
122 | new LabelDirectEditPolicy()); |
123 | installEditPolicy(EditPolicyRoles.OPEN_ROLE, new OpenStoExDialog()); |
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(IFigure 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 = null; |
219 | VariableCharacterisation vc = (VariableCharacterisation) this |
220 | .resolveSemanticElement(); |
221 | if (vc != null) { |
222 | text = vc.getType().getLiteral() + " = "; |
223 | if (vc.getSpecification_VariableCharacterisation() != null) { |
224 | Expression expression = vc |
225 | .getSpecification_VariableCharacterisation() |
226 | .getExpression(); |
227 | if (expression != null) |
228 | text += new PCMStoExPrettyPrintVisitor() |
229 | .prettyPrint(expression); |
230 | } else |
231 | text = null; |
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 false; |
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.VariableCharacterisation_3033, |
334 | getParserElement(), |
335 | PalladioComponentModelVisualIDRegistry |
336 | .getType(de.uka.ipd.sdq.pcm.gmf.seff.edit.parts.VariableCharacterisationEditPart.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 not |
499 | */ |
500 | protected void addSemanticListeners() { |
501 | VariableCharacterisation characterisation = (VariableCharacterisation) resolveSemanticElement(); |
502 | addListenerFilter("SemanticModel", this, characterisation |
503 | .getSpecification_VariableCharacterisation()); |
504 | addListenerFilter("SemanticModel2", this, characterisation); |
505 | } |
506 | |
507 | /** |
508 | * @generated not |
509 | */ |
510 | protected void removeSemanticListeners() { |
511 | removeListenerFilter("SemanticModel"); |
512 | removeListenerFilter("SemanticModel2"); |
513 | } |
514 | |
515 | /** |
516 | * @generated |
517 | */ |
518 | protected AccessibleEditPart getAccessibleEditPart() { |
519 | if (accessibleEP == null) { |
520 | accessibleEP = new AccessibleGraphicalEditPart() { |
521 | |
522 | public void getName(AccessibleEvent e) { |
523 | e.result = getLabelTextHelper(getFigure()); |
524 | } |
525 | }; |
526 | } |
527 | return accessibleEP; |
528 | } |
529 | |
530 | /** |
531 | * @generated |
532 | */ |
533 | private View getFontStyleOwnerView() { |
534 | return getPrimaryView(); |
535 | } |
536 | |
537 | /** |
538 | * @generated |
539 | */ |
540 | protected void addNotationalListeners() { |
541 | super.addNotationalListeners(); |
542 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
543 | } |
544 | |
545 | /** |
546 | * @generated |
547 | */ |
548 | protected void removeNotationalListeners() { |
549 | super.removeNotationalListeners(); |
550 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
551 | } |
552 | |
553 | /** |
554 | * @generated not |
555 | */ |
556 | protected void handleNotificationEvent(Notification event) { |
557 | Object feature = event.getFeature(); |
558 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
559 | Integer c = (Integer) event.getNewValue(); |
560 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
561 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
562 | feature)) { |
563 | refreshUnderline(); |
564 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
565 | .equals(feature)) { |
566 | refreshStrikeThrough(); |
567 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
568 | feature) |
569 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
570 | feature) |
571 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
572 | .equals(feature) |
573 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
574 | feature)) { |
575 | refreshFont(); |
576 | } else { |
577 | refreshLabel(); |
578 | } |
579 | super.handleNotificationEvent(event); |
580 | } |
581 | |
582 | /** |
583 | * @generated |
584 | */ |
585 | protected IFigure createFigure() { |
586 | IFigure label = createFigurePrim(); |
587 | defaultText = getLabelTextHelper(label); |
588 | return label; |
589 | } |
590 | |
591 | /** |
592 | * @generated |
593 | */ |
594 | protected IFigure createFigurePrim() { |
595 | return new WrappingLabel(); |
596 | } |
597 | } |