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