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