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