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