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