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