1 | /* |
2 | * Copyright 2007, IPD, SDQ, University of Karlsruhe |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.repository.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.repository.OpenCapacityDialog; |
52 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.policies.PalladioComponentModelTextNonResizableEditPolicy; |
53 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
54 | import de.uka.ipd.sdq.pcm.gmf.repository.edit.policies.PassiveResourceItemSemanticEditPolicy; |
55 | import de.uka.ipd.sdq.pcm.gmf.repository.part.PalladioComponentModelVisualIDRegistry; |
56 | import de.uka.ipd.sdq.pcm.gmf.repository.providers.PalladioComponentModelElementTypes; |
57 | import de.uka.ipd.sdq.pcm.gmf.repository.providers.PalladioComponentModelParserProvider; |
58 | import de.uka.ipd.sdq.pcm.repository.PassiveResource; |
59 | import de.uka.ipd.sdq.pcm.stochasticexpressions.PCMStoExPrettyPrintVisitor; |
60 | |
61 | /** |
62 | * @generated |
63 | */ |
64 | public class PassiveResourceEditPart extends CompartmentEditPart implements |
65 | ITextAwareEditPart { |
66 | |
67 | /** |
68 | * @generated |
69 | */ |
70 | public static final int VISUAL_ID = 3103; |
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 PassiveResourceEditPart(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 PassiveResourceItemSemanticEditPolicy()); |
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 OpenCapacityDialog()); |
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 stoex = null, text = null; |
219 | if (resolveSemanticElement() instanceof PassiveResource) { |
220 | PassiveResource pr = (PassiveResource) resolveSemanticElement(); |
221 | if (pr.getCapacity_PassiveResource() != null) { |
222 | stoex = new PCMStoExPrettyPrintVisitor().prettyPrint(pr |
223 | .getCapacity_PassiveResource().getExpression()); |
224 | } |
225 | text = pr.getEntityName(); |
226 | if (stoex == null) |
227 | stoex = "not set"; |
228 | text += " <Capacity: " + stoex + ">"; |
229 | |
230 | } |
231 | if (text == null || text.length() == 0) { |
232 | text = defaultText; |
233 | } |
234 | return text; |
235 | } |
236 | |
237 | /** |
238 | * @generated |
239 | */ |
240 | public void setLabelText(String text) { |
241 | setLabelTextHelper(getFigure(), text); |
242 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
243 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
244 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
245 | .refreshFeedback(); |
246 | } |
247 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
248 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
249 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
250 | .refreshFeedback(); |
251 | } |
252 | } |
253 | |
254 | /** |
255 | * @generated |
256 | */ |
257 | public String getEditText() { |
258 | if (getParserElement() == null || getParser() == null) { |
259 | return ""; //$NON-NLS-1$ |
260 | } |
261 | return getParser().getEditString( |
262 | new EObjectAdapter(getParserElement()), |
263 | getParserOptions().intValue()); |
264 | } |
265 | |
266 | /** |
267 | * @generated |
268 | */ |
269 | protected boolean isEditable() { |
270 | return getParser() != null; |
271 | } |
272 | |
273 | /** |
274 | * @generated |
275 | */ |
276 | public ICellEditorValidator getEditTextValidator() { |
277 | return new ICellEditorValidator() { |
278 | |
279 | public String isValid(final Object value) { |
280 | if (value instanceof String) { |
281 | final EObject element = getParserElement(); |
282 | final IParser parser = getParser(); |
283 | try { |
284 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
285 | .runExclusive(new RunnableWithResult.Impl() { |
286 | |
287 | public void run() { |
288 | setResult(parser.isValidEditString( |
289 | new EObjectAdapter(element), |
290 | (String) value)); |
291 | } |
292 | }); |
293 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
294 | : valid.getMessage(); |
295 | } catch (InterruptedException ie) { |
296 | ie.printStackTrace(); |
297 | } |
298 | } |
299 | |
300 | // shouldn't get here |
301 | return null; |
302 | } |
303 | }; |
304 | } |
305 | |
306 | /** |
307 | * @generated |
308 | */ |
309 | public IContentAssistProcessor getCompletionProcessor() { |
310 | if (getParserElement() == null || getParser() == null) { |
311 | return null; |
312 | } |
313 | return getParser().getCompletionProcessor( |
314 | new EObjectAdapter(getParserElement())); |
315 | } |
316 | |
317 | /** |
318 | * @generated |
319 | */ |
320 | public ParserOptions getParserOptions() { |
321 | return ParserOptions.NONE; |
322 | } |
323 | |
324 | /** |
325 | * @generated |
326 | */ |
327 | public IParser getParser() { |
328 | if (parser == null) { |
329 | parser = PalladioComponentModelParserProvider |
330 | .getParser( |
331 | PalladioComponentModelElementTypes.PassiveResource_3103, |
332 | getParserElement(), |
333 | PalladioComponentModelVisualIDRegistry |
334 | .getType(de.uka.ipd.sdq.pcm.gmf.repository.edit.parts.PassiveResourceEditPart.VISUAL_ID)); |
335 | } |
336 | return parser; |
337 | } |
338 | |
339 | /** |
340 | * @generated |
341 | */ |
342 | protected DirectEditManager getManager() { |
343 | if (manager == null) { |
344 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
345 | .getTextCellEditorClass(this), |
346 | PalladioComponentModelEditPartFactory |
347 | .getTextCellEditorLocator(this))); |
348 | } |
349 | return manager; |
350 | } |
351 | |
352 | /** |
353 | * @generated |
354 | */ |
355 | protected void setManager(DirectEditManager manager) { |
356 | this.manager = manager; |
357 | } |
358 | |
359 | /** |
360 | * @generated |
361 | */ |
362 | protected void performDirectEdit() { |
363 | getManager().show(); |
364 | } |
365 | |
366 | /** |
367 | * @generated |
368 | */ |
369 | protected void performDirectEdit(Point eventLocation) { |
370 | if (getManager().getClass() == TextDirectEditManager.class) { |
371 | ((TextDirectEditManager) getManager()).show(eventLocation |
372 | .getSWTPoint()); |
373 | } |
374 | } |
375 | |
376 | /** |
377 | * @generated |
378 | */ |
379 | private void performDirectEdit(char initialCharacter) { |
380 | if (getManager() instanceof TextDirectEditManager) { |
381 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
382 | } else { |
383 | performDirectEdit(); |
384 | } |
385 | } |
386 | |
387 | /** |
388 | * @generated |
389 | */ |
390 | protected void performDirectEditRequest(Request request) { |
391 | final Request theRequest = request; |
392 | try { |
393 | getEditingDomain().runExclusive(new Runnable() { |
394 | |
395 | public void run() { |
396 | if (isActive() && isEditable()) { |
397 | if (theRequest |
398 | .getExtendedData() |
399 | .get( |
400 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
401 | Character initialChar = (Character) theRequest |
402 | .getExtendedData() |
403 | .get( |
404 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
405 | performDirectEdit(initialChar.charValue()); |
406 | } else if ((theRequest instanceof DirectEditRequest) |
407 | && (getEditText().equals(getLabelText()))) { |
408 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
409 | performDirectEdit(editRequest.getLocation()); |
410 | } else { |
411 | performDirectEdit(); |
412 | } |
413 | } |
414 | } |
415 | }); |
416 | } catch (InterruptedException e) { |
417 | e.printStackTrace(); |
418 | } |
419 | } |
420 | |
421 | /** |
422 | * @generated |
423 | */ |
424 | protected void refreshVisuals() { |
425 | super.refreshVisuals(); |
426 | refreshLabel(); |
427 | refreshFont(); |
428 | refreshFontColor(); |
429 | refreshUnderline(); |
430 | refreshStrikeThrough(); |
431 | } |
432 | |
433 | /** |
434 | * @generated |
435 | */ |
436 | protected void refreshLabel() { |
437 | setLabelTextHelper(getFigure(), getLabelText()); |
438 | setLabelIconHelper(getFigure(), getLabelIcon()); |
439 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
440 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
441 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
442 | .refreshFeedback(); |
443 | } |
444 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
445 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
446 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
447 | .refreshFeedback(); |
448 | } |
449 | } |
450 | |
451 | /** |
452 | * @generated |
453 | */ |
454 | protected void refreshUnderline() { |
455 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
456 | NotationPackage.eINSTANCE.getFontStyle()); |
457 | if (style != null && getFigure() instanceof WrappingLabel) { |
458 | ((WrappingLabel) getFigure()).setTextUnderline(style.isUnderline()); |
459 | } |
460 | } |
461 | |
462 | /** |
463 | * @generated |
464 | */ |
465 | protected void refreshStrikeThrough() { |
466 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
467 | NotationPackage.eINSTANCE.getFontStyle()); |
468 | if (style != null && getFigure() instanceof WrappingLabel) { |
469 | ((WrappingLabel) getFigure()).setTextStrikeThrough(style |
470 | .isStrikeThrough()); |
471 | } |
472 | } |
473 | |
474 | /** |
475 | * @generated |
476 | */ |
477 | protected void refreshFont() { |
478 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
479 | NotationPackage.eINSTANCE.getFontStyle()); |
480 | if (style != null) { |
481 | FontData fontData = new FontData(style.getFontName(), style |
482 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
483 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
484 | setFont(fontData); |
485 | } |
486 | } |
487 | |
488 | /** |
489 | * @generated |
490 | */ |
491 | protected void setFontColor(Color color) { |
492 | getFigure().setForegroundColor(color); |
493 | } |
494 | |
495 | private EContentAdapter changeListener = null; |
496 | private EObject adaptedElement = null; |
497 | |
498 | /** |
499 | * @generated not |
500 | */ |
501 | protected void addSemanticListeners() { |
502 | PassiveResource element = (PassiveResource) resolveSemanticElement(); |
503 | changeListener = new EContentAdapter() { |
504 | |
505 | @Override |
506 | public void notifyChanged(Notification notification) { |
507 | super.notifyChanged(notification); |
508 | refreshLabel(); |
509 | } |
510 | |
511 | }; |
512 | adaptedElement = element; |
513 | element.eAdapters().add(changeListener); |
514 | } |
515 | |
516 | /** |
517 | * @generated not |
518 | */ |
519 | protected void removeSemanticListeners() { |
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 |
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 | if (getParser() != null |
586 | && getParser().isAffectingEvent(event, |
587 | getParserOptions().intValue())) { |
588 | refreshLabel(); |
589 | } |
590 | if (getParser() instanceof ISemanticParser) { |
591 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
592 | if (modelParser.areSemanticElementsAffected(null, event)) { |
593 | removeSemanticListeners(); |
594 | if (resolveSemanticElement() != null) { |
595 | addSemanticListeners(); |
596 | } |
597 | refreshLabel(); |
598 | } |
599 | } |
600 | } |
601 | super.handleNotificationEvent(event); |
602 | } |
603 | |
604 | /** |
605 | * @generated |
606 | */ |
607 | protected IFigure createFigure() { |
608 | IFigure label = createFigurePrim(); |
609 | defaultText = getLabelTextHelper(label); |
610 | return label; |
611 | } |
612 | |
613 | /** |
614 | * @generated |
615 | */ |
616 | protected IFigure createFigurePrim() { |
617 | return new PassiveResourceLabellFigure(); |
618 | } |
619 | |
620 | /** |
621 | * @generated |
622 | */ |
623 | public class PassiveResourceLabellFigure extends WrappingLabel { |
624 | |
625 | /** |
626 | * @generated |
627 | */ |
628 | public PassiveResourceLabellFigure() { |
629 | this.setText("PassiveResource"); |
630 | } |
631 | |
632 | } |
633 | } |