1 | /* |
2 | * Copyright 2007, SDQ, IPD, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.usage.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.ecore.util.EContentAdapter; |
15 | import org.eclipse.emf.transaction.RunnableWithResult; |
16 | import org.eclipse.gef.AccessibleEditPart; |
17 | import org.eclipse.gef.DragTracker; |
18 | import org.eclipse.gef.EditPolicy; |
19 | import org.eclipse.gef.Request; |
20 | import org.eclipse.gef.requests.DirectEditRequest; |
21 | import org.eclipse.gef.requests.SelectionRequest; |
22 | import org.eclipse.gef.tools.DirectEditManager; |
23 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; |
24 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; |
25 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; |
26 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions; |
27 | import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart; |
28 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
29 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
30 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
31 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.LabelDirectEditPolicy; |
32 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ListItemComponentEditPolicy; |
33 | import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry; |
34 | import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
35 | import org.eclipse.gmf.runtime.diagram.ui.tools.DragEditPartsTrackerEx; |
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.dialogs.usage.OpenVariableCharacterisationDialog; |
52 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.policies.PalladioComponentModelTextNonResizableEditPolicy; |
53 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
54 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.policies.VariableCharacterisationItemSemanticEditPolicy; |
55 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelVisualIDRegistry; |
56 | import de.uka.ipd.sdq.pcm.gmf.usage.providers.PalladioComponentModelElementTypes; |
57 | import de.uka.ipd.sdq.pcm.gmf.usage.providers.PalladioComponentModelParserProvider; |
58 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisation; |
59 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
60 | import de.uka.ipd.sdq.stoex.Expression; |
61 | |
62 | /** |
63 | * @generated |
64 | */ |
65 | public class VariableCharacterisationEditPart extends CompartmentEditPart |
66 | implements ITextAwareEditPart { |
67 | |
68 | /** |
69 | * @generated |
70 | */ |
71 | public static final int VISUAL_ID = 3013; |
72 | |
73 | /** |
74 | * @generated |
75 | */ |
76 | private DirectEditManager manager; |
77 | |
78 | /** |
79 | * @generated |
80 | */ |
81 | private IParser parser; |
82 | |
83 | /** |
84 | * @generated |
85 | */ |
86 | private List parserElements; |
87 | |
88 | /** |
89 | * @generated |
90 | */ |
91 | private String defaultText; |
92 | |
93 | /** |
94 | * @generated |
95 | */ |
96 | public VariableCharacterisationEditPart(View view) { |
97 | super(view); |
98 | } |
99 | |
100 | /** |
101 | * @generated |
102 | */ |
103 | public DragTracker getDragTracker(Request request) { |
104 | if (request instanceof SelectionRequest |
105 | && ((SelectionRequest) request).getLastButtonPressed() == 3) { |
106 | return null; |
107 | } |
108 | return new DragEditPartsTrackerEx(this); |
109 | } |
110 | |
111 | /** |
112 | * @generated |
113 | */ |
114 | protected void createDefaultEditPolicies() { |
115 | super.createDefaultEditPolicies(); |
116 | installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, |
117 | new VariableCharacterisationItemSemanticEditPolicy()); |
118 | installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, |
119 | new PalladioComponentModelTextNonResizableEditPolicy()); |
120 | installEditPolicy(EditPolicy.COMPONENT_ROLE, |
121 | new ListItemComponentEditPolicy()); |
122 | installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, |
123 | new LabelDirectEditPolicy()); |
124 | installEditPolicy(EditPolicyRoles.OPEN_ROLE, |
125 | new OpenVariableCharacterisationDialog()); |
126 | } |
127 | |
128 | /** |
129 | * @generated |
130 | */ |
131 | protected String getLabelTextHelper(IFigure figure) { |
132 | if (figure instanceof WrappingLabel) { |
133 | return ((WrappingLabel) figure).getText(); |
134 | } else { |
135 | return ((Label) figure).getText(); |
136 | } |
137 | } |
138 | |
139 | /** |
140 | * @generated |
141 | */ |
142 | protected void setLabelTextHelper(IFigure figure, String text) { |
143 | if (figure instanceof WrappingLabel) { |
144 | ((WrappingLabel) figure).setText(text); |
145 | } else { |
146 | ((Label) figure).setText(text); |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * @generated |
152 | */ |
153 | protected Image getLabelIconHelper(IFigure figure) { |
154 | if (figure instanceof WrappingLabel) { |
155 | return ((WrappingLabel) figure).getIcon(); |
156 | } else { |
157 | return ((Label) figure).getIcon(); |
158 | } |
159 | } |
160 | |
161 | /** |
162 | * @generated |
163 | */ |
164 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
165 | if (figure instanceof WrappingLabel) { |
166 | ((WrappingLabel) figure).setIcon(icon); |
167 | } else { |
168 | ((Label) figure).setIcon(icon); |
169 | } |
170 | } |
171 | |
172 | /** |
173 | * @generated |
174 | */ |
175 | public void setLabel(IFigure figure) { |
176 | unregisterVisuals(); |
177 | setFigure(figure); |
178 | defaultText = getLabelTextHelper(figure); |
179 | registerVisuals(); |
180 | refreshVisuals(); |
181 | } |
182 | |
183 | /** |
184 | * @generated |
185 | */ |
186 | protected List getModelChildren() { |
187 | return Collections.EMPTY_LIST; |
188 | } |
189 | |
190 | /** |
191 | * @generated |
192 | */ |
193 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
194 | return null; |
195 | } |
196 | |
197 | /** |
198 | * @generated |
199 | */ |
200 | protected EObject getParserElement() { |
201 | return resolveSemanticElement(); |
202 | } |
203 | |
204 | /** |
205 | * @generated |
206 | */ |
207 | protected Image getLabelIcon() { |
208 | EObject parserElement = getParserElement(); |
209 | if (parserElement == null) { |
210 | return null; |
211 | } |
212 | return PalladioComponentModelElementTypes.getImage(parserElement |
213 | .eClass()); |
214 | } |
215 | |
216 | /** |
217 | * @generated not |
218 | */ |
219 | protected String getLabelText() { |
220 | String text = null; |
221 | VariableCharacterisation vc = (VariableCharacterisation) this |
222 | .resolveSemanticElement(); |
223 | text = vc.getType().getLiteral() + " = "; |
224 | Expression expression = vc.getSpecification_VariableCharacterisation() |
225 | .getExpression(); |
226 | if (expression != null) |
227 | text += new PCMStoExPrettyPrintVisitor().prettyPrint(expression); |
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.VariableCharacterisation_3013, |
329 | getParserElement(), |
330 | PalladioComponentModelVisualIDRegistry |
331 | .getType(de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.VariableCharacterisationEditPart.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 | private EContentAdapter changeListener = null; |
493 | private EObject adaptedElement = null; |
494 | |
495 | /** |
496 | * @generated not |
497 | */ |
498 | protected void addSemanticListeners() { |
499 | EObject element = resolveSemanticElement(); |
500 | changeListener = new EContentAdapter() { |
501 | |
502 | @Override |
503 | public void notifyChanged(Notification notification) { |
504 | super.notifyChanged(notification); |
505 | VariableCharacterisationEditPart.this |
506 | .notifyChanged(notification); |
507 | } |
508 | |
509 | }; |
510 | adaptedElement = element; |
511 | element.eAdapters().add(changeListener); |
512 | addListenerFilter("SemanticModel", this, element); //$NON-NLS-1$ |
513 | } |
514 | |
515 | /** |
516 | * @generated not |
517 | */ |
518 | protected void removeSemanticListeners() { |
519 | removeListenerFilter("SemanticModel"); //$NON-NLS-1$ |
520 | adaptedElement.eAdapters().remove(changeListener); |
521 | } |
522 | |
523 | /** |
524 | * @generated |
525 | */ |
526 | protected AccessibleEditPart getAccessibleEditPart() { |
527 | if (accessibleEP == null) { |
528 | accessibleEP = new AccessibleGraphicalEditPart() { |
529 | |
530 | public void getName(AccessibleEvent e) { |
531 | e.result = getLabelTextHelper(getFigure()); |
532 | } |
533 | }; |
534 | } |
535 | return accessibleEP; |
536 | } |
537 | |
538 | /** |
539 | * @generated |
540 | */ |
541 | private View getFontStyleOwnerView() { |
542 | return getPrimaryView(); |
543 | } |
544 | |
545 | /** |
546 | * @generated |
547 | */ |
548 | protected void addNotationalListeners() { |
549 | super.addNotationalListeners(); |
550 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
551 | } |
552 | |
553 | /** |
554 | * @generated |
555 | */ |
556 | protected void removeNotationalListeners() { |
557 | super.removeNotationalListeners(); |
558 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
559 | } |
560 | |
561 | /** |
562 | * @generated not |
563 | */ |
564 | protected void handleNotificationEvent(Notification event) { |
565 | Object feature = event.getFeature(); |
566 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
567 | Integer c = (Integer) event.getNewValue(); |
568 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
569 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
570 | feature)) { |
571 | refreshUnderline(); |
572 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
573 | .equals(feature)) { |
574 | refreshStrikeThrough(); |
575 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
576 | feature) |
577 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
578 | feature) |
579 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
580 | .equals(feature) |
581 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
582 | feature)) { |
583 | refreshFont(); |
584 | } else { |
585 | refreshLabel(); |
586 | } |
587 | super.handleNotificationEvent(event); |
588 | } |
589 | |
590 | /** |
591 | * @generated |
592 | */ |
593 | protected IFigure createFigure() { |
594 | IFigure label = createFigurePrim(); |
595 | defaultText = getLabelTextHelper(label); |
596 | return label; |
597 | } |
598 | |
599 | /** |
600 | * @generated |
601 | */ |
602 | protected IFigure createFigurePrim() { |
603 | return new WrappingLabel(); |
604 | } |
605 | |
606 | } |