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.Dimension; |
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.ecore.util.EContentAdapter; |
16 | import org.eclipse.emf.transaction.RunnableWithResult; |
17 | import org.eclipse.gef.AccessibleEditPart; |
18 | import org.eclipse.gef.DragTracker; |
19 | import org.eclipse.gef.EditPolicy; |
20 | import org.eclipse.gef.Request; |
21 | import org.eclipse.gef.requests.DirectEditRequest; |
22 | import org.eclipse.gef.requests.SelectionRequest; |
23 | import org.eclipse.gef.tools.DirectEditManager; |
24 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; |
25 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; |
26 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; |
27 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions; |
28 | import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart; |
29 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
30 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
31 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
32 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.LabelDirectEditPolicy; |
33 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ListItemComponentEditPolicy; |
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.DragEditPartsTrackerEx; |
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.OpenStoExDialog; |
53 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.PalladioComponentModelTextNonResizableEditPolicy; |
54 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
55 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.ParametricResourceDemandItemSemanticEditPolicy; |
56 | import de.uka.ipd.sdq.pcm.gmf.seff.part.PalladioComponentModelVisualIDRegistry; |
57 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelElementTypes; |
58 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelParserProvider; |
59 | import de.uka.ipd.sdq.pcm.seff.seff_performance.ParametricResourceDemand; |
60 | import de.uka.ipd.sdq.pcm.seff.seff_performance.Seff_performancePackage; |
61 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
62 | |
63 | /** |
64 | * @generated |
65 | */ |
66 | public class ParametricResourceDemandEditPart extends CompartmentEditPart |
67 | implements ITextAwareEditPart { |
68 | |
69 | /** |
70 | * @generated |
71 | */ |
72 | public static final int VISUAL_ID = 3051; |
73 | |
74 | /** |
75 | * @generated |
76 | */ |
77 | private DirectEditManager manager; |
78 | |
79 | /** |
80 | * @generated |
81 | */ |
82 | private IParser parser; |
83 | |
84 | /** |
85 | * @generated |
86 | */ |
87 | private List parserElements; |
88 | |
89 | /** |
90 | * @generated |
91 | */ |
92 | private String defaultText; |
93 | |
94 | /** |
95 | * @generated |
96 | */ |
97 | public ParametricResourceDemandEditPart(View view) { |
98 | super(view); |
99 | } |
100 | |
101 | /** |
102 | * @generated |
103 | */ |
104 | public DragTracker getDragTracker(Request request) { |
105 | if (request instanceof SelectionRequest |
106 | && ((SelectionRequest) request).getLastButtonPressed() == 3) { |
107 | return null; |
108 | } |
109 | return new DragEditPartsTrackerEx(this); |
110 | } |
111 | |
112 | /** |
113 | * @generated not |
114 | */ |
115 | protected void createDefaultEditPolicies() { |
116 | super.createDefaultEditPolicies(); |
117 | installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, |
118 | new ParametricResourceDemandItemSemanticEditPolicy()); |
119 | installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, |
120 | new PalladioComponentModelTextNonResizableEditPolicy()); |
121 | installEditPolicy(EditPolicy.COMPONENT_ROLE, |
122 | new ListItemComponentEditPolicy()); |
123 | installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, |
124 | new LabelDirectEditPolicy()); |
125 | installEditPolicy( |
126 | EditPolicyRoles.OPEN_ROLE, |
127 | new OpenStoExDialog( |
128 | Seff_performancePackage.eINSTANCE |
129 | .getParametricResourceDemand_Specification_ParametericResourceDemand())); |
130 | } |
131 | |
132 | /** |
133 | * @generated |
134 | */ |
135 | protected String getLabelTextHelper(IFigure figure) { |
136 | if (figure instanceof WrappingLabel) { |
137 | return ((WrappingLabel) figure).getText(); |
138 | } else { |
139 | return ((Label) figure).getText(); |
140 | } |
141 | } |
142 | |
143 | /** |
144 | * @generated |
145 | */ |
146 | protected void setLabelTextHelper(IFigure figure, String text) { |
147 | if (figure instanceof WrappingLabel) { |
148 | ((WrappingLabel) figure).setText(text); |
149 | } else { |
150 | ((Label) figure).setText(text); |
151 | } |
152 | } |
153 | |
154 | /** |
155 | * @generated |
156 | */ |
157 | protected Image getLabelIconHelper(IFigure figure) { |
158 | if (figure instanceof WrappingLabel) { |
159 | return ((WrappingLabel) figure).getIcon(); |
160 | } else { |
161 | return ((Label) figure).getIcon(); |
162 | } |
163 | } |
164 | |
165 | /** |
166 | * @generated |
167 | */ |
168 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
169 | if (figure instanceof WrappingLabel) { |
170 | ((WrappingLabel) figure).setIcon(icon); |
171 | } else { |
172 | ((Label) figure).setIcon(icon); |
173 | } |
174 | } |
175 | |
176 | /** |
177 | * @generated |
178 | */ |
179 | public void setLabel(IFigure figure) { |
180 | unregisterVisuals(); |
181 | setFigure(figure); |
182 | defaultText = getLabelTextHelper(figure); |
183 | registerVisuals(); |
184 | refreshVisuals(); |
185 | } |
186 | |
187 | /** |
188 | * @generated |
189 | */ |
190 | protected List getModelChildren() { |
191 | return Collections.EMPTY_LIST; |
192 | } |
193 | |
194 | /** |
195 | * @generated |
196 | */ |
197 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
198 | return null; |
199 | } |
200 | |
201 | /** |
202 | * @generated |
203 | */ |
204 | protected EObject getParserElement() { |
205 | return resolveSemanticElement(); |
206 | } |
207 | |
208 | /** |
209 | * @generated |
210 | */ |
211 | protected Image getLabelIcon() { |
212 | EObject parserElement = getParserElement(); |
213 | if (parserElement == null) { |
214 | return null; |
215 | } |
216 | return PalladioComponentModelElementTypes.getImage(parserElement |
217 | .eClass()); |
218 | } |
219 | |
220 | /** |
221 | * @generated not |
222 | */ |
223 | protected String getLabelText() { |
224 | String text = null; |
225 | if (resolveSemanticElement() instanceof ParametricResourceDemand) { |
226 | ParametricResourceDemand demand = (ParametricResourceDemand) resolveSemanticElement(); |
227 | if (demand.getRequiredResource_ParametricResourceDemand() != null) { |
228 | text = new PCMStoExPrettyPrintVisitor().prettyPrint(demand |
229 | .getSpecification_ParametericResourceDemand() |
230 | .getExpression()); |
231 | if (text == null) |
232 | text = ""; |
233 | else |
234 | text += " "; |
235 | text += "<" |
236 | + demand.getRequiredResource_ParametricResourceDemand() |
237 | .getEntityName() + ">"; |
238 | } |
239 | } |
240 | if (text == null || text.length() == 0) { |
241 | text = defaultText; |
242 | } |
243 | return text; |
244 | } |
245 | |
246 | /** |
247 | * @generated |
248 | */ |
249 | public void setLabelText(String text) { |
250 | setLabelTextHelper(getFigure(), text); |
251 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
252 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
253 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
254 | .refreshFeedback(); |
255 | } |
256 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
257 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
258 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
259 | .refreshFeedback(); |
260 | } |
261 | } |
262 | |
263 | /** |
264 | * @generated |
265 | */ |
266 | public String getEditText() { |
267 | if (getParserElement() == null || getParser() == null) { |
268 | return ""; //$NON-NLS-1$ |
269 | } |
270 | return getParser().getEditString( |
271 | new EObjectAdapter(getParserElement()), |
272 | getParserOptions().intValue()); |
273 | } |
274 | |
275 | /** |
276 | * @generated |
277 | */ |
278 | protected boolean isEditable() { |
279 | return false; |
280 | } |
281 | |
282 | /** |
283 | * @generated |
284 | */ |
285 | public ICellEditorValidator getEditTextValidator() { |
286 | return new ICellEditorValidator() { |
287 | |
288 | public String isValid(final Object value) { |
289 | if (value instanceof String) { |
290 | final EObject element = getParserElement(); |
291 | final IParser parser = getParser(); |
292 | try { |
293 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
294 | .runExclusive(new RunnableWithResult.Impl() { |
295 | |
296 | public void run() { |
297 | setResult(parser.isValidEditString( |
298 | new EObjectAdapter(element), |
299 | (String) value)); |
300 | } |
301 | }); |
302 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
303 | : valid.getMessage(); |
304 | } catch (InterruptedException ie) { |
305 | ie.printStackTrace(); |
306 | } |
307 | } |
308 | |
309 | // shouldn't get here |
310 | return null; |
311 | } |
312 | }; |
313 | } |
314 | |
315 | /** |
316 | * @generated |
317 | */ |
318 | public IContentAssistProcessor getCompletionProcessor() { |
319 | if (getParserElement() == null || getParser() == null) { |
320 | return null; |
321 | } |
322 | return getParser().getCompletionProcessor( |
323 | new EObjectAdapter(getParserElement())); |
324 | } |
325 | |
326 | /** |
327 | * @generated |
328 | */ |
329 | public ParserOptions getParserOptions() { |
330 | return ParserOptions.NONE; |
331 | } |
332 | |
333 | /** |
334 | * @generated |
335 | */ |
336 | public IParser getParser() { |
337 | if (parser == null) { |
338 | parser = PalladioComponentModelParserProvider |
339 | .getParser( |
340 | PalladioComponentModelElementTypes.ParametricResourceDemand_3051, |
341 | getParserElement(), |
342 | PalladioComponentModelVisualIDRegistry |
343 | .getType(de.uka.ipd.sdq.pcm.gmf.seff.edit.parts.ParametricResourceDemandEditPart.VISUAL_ID)); |
344 | } |
345 | return parser; |
346 | } |
347 | |
348 | /** |
349 | * @generated |
350 | */ |
351 | protected DirectEditManager getManager() { |
352 | if (manager == null) { |
353 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
354 | .getTextCellEditorClass(this), |
355 | PalladioComponentModelEditPartFactory |
356 | .getTextCellEditorLocator(this))); |
357 | } |
358 | return manager; |
359 | } |
360 | |
361 | /** |
362 | * @generated |
363 | */ |
364 | protected void setManager(DirectEditManager manager) { |
365 | this.manager = manager; |
366 | } |
367 | |
368 | /** |
369 | * @generated |
370 | */ |
371 | protected void performDirectEdit() { |
372 | getManager().show(); |
373 | } |
374 | |
375 | /** |
376 | * @generated |
377 | */ |
378 | protected void performDirectEdit(Point eventLocation) { |
379 | if (getManager().getClass() == TextDirectEditManager.class) { |
380 | ((TextDirectEditManager) getManager()).show(eventLocation |
381 | .getSWTPoint()); |
382 | } |
383 | } |
384 | |
385 | /** |
386 | * @generated |
387 | */ |
388 | private void performDirectEdit(char initialCharacter) { |
389 | if (getManager() instanceof TextDirectEditManager) { |
390 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
391 | } else { |
392 | performDirectEdit(); |
393 | } |
394 | } |
395 | |
396 | /** |
397 | * @generated |
398 | */ |
399 | protected void performDirectEditRequest(Request request) { |
400 | final Request theRequest = request; |
401 | try { |
402 | getEditingDomain().runExclusive(new Runnable() { |
403 | |
404 | public void run() { |
405 | if (isActive() && isEditable()) { |
406 | if (theRequest |
407 | .getExtendedData() |
408 | .get( |
409 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
410 | Character initialChar = (Character) theRequest |
411 | .getExtendedData() |
412 | .get( |
413 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
414 | performDirectEdit(initialChar.charValue()); |
415 | } else if ((theRequest instanceof DirectEditRequest) |
416 | && (getEditText().equals(getLabelText()))) { |
417 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
418 | performDirectEdit(editRequest.getLocation()); |
419 | } else { |
420 | performDirectEdit(); |
421 | } |
422 | } |
423 | } |
424 | }); |
425 | } catch (InterruptedException e) { |
426 | e.printStackTrace(); |
427 | } |
428 | } |
429 | |
430 | /** |
431 | * @generated |
432 | */ |
433 | protected void refreshVisuals() { |
434 | super.refreshVisuals(); |
435 | refreshLabel(); |
436 | refreshFont(); |
437 | refreshFontColor(); |
438 | refreshUnderline(); |
439 | refreshStrikeThrough(); |
440 | } |
441 | |
442 | /** |
443 | * @generated |
444 | */ |
445 | protected void refreshLabel() { |
446 | setLabelTextHelper(getFigure(), getLabelText()); |
447 | setLabelIconHelper(getFigure(), getLabelIcon()); |
448 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
449 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
450 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
451 | .refreshFeedback(); |
452 | } |
453 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
454 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
455 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
456 | .refreshFeedback(); |
457 | } |
458 | } |
459 | |
460 | /** |
461 | * @generated |
462 | */ |
463 | protected void refreshUnderline() { |
464 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
465 | NotationPackage.eINSTANCE.getFontStyle()); |
466 | if (style != null && getFigure() instanceof WrappingLabel) { |
467 | ((WrappingLabel) getFigure()).setTextUnderline(style.isUnderline()); |
468 | } |
469 | } |
470 | |
471 | /** |
472 | * @generated |
473 | */ |
474 | protected void refreshStrikeThrough() { |
475 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
476 | NotationPackage.eINSTANCE.getFontStyle()); |
477 | if (style != null && getFigure() instanceof WrappingLabel) { |
478 | ((WrappingLabel) getFigure()).setTextStrikeThrough(style |
479 | .isStrikeThrough()); |
480 | } |
481 | } |
482 | |
483 | /** |
484 | * @generated |
485 | */ |
486 | protected void refreshFont() { |
487 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
488 | NotationPackage.eINSTANCE.getFontStyle()); |
489 | if (style != null) { |
490 | FontData fontData = new FontData(style.getFontName(), style |
491 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
492 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
493 | setFont(fontData); |
494 | } |
495 | } |
496 | |
497 | /** |
498 | * @generated |
499 | */ |
500 | protected void setFontColor(Color color) { |
501 | getFigure().setForegroundColor(color); |
502 | } |
503 | |
504 | private EContentAdapter changeListener = null; |
505 | private EObject adaptedElement = null; |
506 | |
507 | /** |
508 | * @generated not |
509 | */ |
510 | protected void addSemanticListeners() { |
511 | ParametricResourceDemand element = (ParametricResourceDemand) resolveSemanticElement(); |
512 | changeListener = new EContentAdapter() { |
513 | |
514 | @Override |
515 | public void notifyChanged(Notification notification) { |
516 | super.notifyChanged(notification); |
517 | refreshLabel(); |
518 | } |
519 | |
520 | }; |
521 | adaptedElement = element; |
522 | element.eAdapters().add(changeListener); |
523 | } |
524 | |
525 | /** |
526 | * @generated not |
527 | */ |
528 | protected void removeSemanticListeners() { |
529 | adaptedElement.eAdapters().remove(changeListener); |
530 | } |
531 | |
532 | /** |
533 | * @generated |
534 | */ |
535 | protected AccessibleEditPart getAccessibleEditPart() { |
536 | if (accessibleEP == null) { |
537 | accessibleEP = new AccessibleGraphicalEditPart() { |
538 | |
539 | public void getName(AccessibleEvent e) { |
540 | e.result = getLabelTextHelper(getFigure()); |
541 | } |
542 | }; |
543 | } |
544 | return accessibleEP; |
545 | } |
546 | |
547 | /** |
548 | * @generated |
549 | */ |
550 | private View getFontStyleOwnerView() { |
551 | return getPrimaryView(); |
552 | } |
553 | |
554 | /** |
555 | * @generated |
556 | */ |
557 | protected void addNotationalListeners() { |
558 | super.addNotationalListeners(); |
559 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
560 | } |
561 | |
562 | /** |
563 | * @generated |
564 | */ |
565 | protected void removeNotationalListeners() { |
566 | super.removeNotationalListeners(); |
567 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
568 | } |
569 | |
570 | /** |
571 | * @generated |
572 | */ |
573 | protected void handleNotificationEvent(Notification event) { |
574 | Object feature = event.getFeature(); |
575 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
576 | Integer c = (Integer) event.getNewValue(); |
577 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
578 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
579 | feature)) { |
580 | refreshUnderline(); |
581 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
582 | .equals(feature)) { |
583 | refreshStrikeThrough(); |
584 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
585 | feature) |
586 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
587 | feature) |
588 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
589 | .equals(feature) |
590 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
591 | feature)) { |
592 | refreshFont(); |
593 | } else { |
594 | if (getParser() != null |
595 | && getParser().isAffectingEvent(event, |
596 | getParserOptions().intValue())) { |
597 | refreshLabel(); |
598 | } |
599 | if (getParser() instanceof ISemanticParser) { |
600 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
601 | if (modelParser.areSemanticElementsAffected(null, event)) { |
602 | removeSemanticListeners(); |
603 | if (resolveSemanticElement() != null) { |
604 | addSemanticListeners(); |
605 | } |
606 | refreshLabel(); |
607 | } |
608 | } |
609 | } |
610 | super.handleNotificationEvent(event); |
611 | } |
612 | |
613 | /** |
614 | * @generated |
615 | */ |
616 | protected IFigure createFigure() { |
617 | IFigure label = createFigurePrim(); |
618 | defaultText = getLabelTextHelper(label); |
619 | return label; |
620 | } |
621 | |
622 | /** |
623 | * @generated |
624 | */ |
625 | protected IFigure createFigurePrim() { |
626 | return new ParametricResourceDemandFigure(); |
627 | } |
628 | |
629 | /** |
630 | * @generated |
631 | */ |
632 | public class ParametricResourceDemandFigure extends WrappingLabel { |
633 | |
634 | /** |
635 | * @generated |
636 | */ |
637 | public ParametricResourceDemandFigure() { |
638 | this.setText("<...>"); |
639 | this.setMinimumSize(new Dimension(getMapMode().DPtoLP(0), |
640 | getMapMode().DPtoLP(0))); |
641 | } |
642 | |
643 | } |
644 | |
645 | } |