1 | /* |
2 | * Copyright 2007, SDQ, IPD, U KA |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Collections; |
8 | import java.util.List; |
9 | |
10 | import org.eclipse.core.runtime.IAdaptable; |
11 | import org.eclipse.draw2d.IFigure; |
12 | import org.eclipse.draw2d.Label; |
13 | import org.eclipse.draw2d.geometry.Point; |
14 | import org.eclipse.emf.common.notify.Notification; |
15 | import org.eclipse.emf.ecore.EObject; |
16 | import org.eclipse.emf.transaction.RunnableWithResult; |
17 | import org.eclipse.gef.AccessibleEditPart; |
18 | import org.eclipse.gef.EditPolicy; |
19 | import org.eclipse.gef.GraphicalEditPart; |
20 | import org.eclipse.gef.Request; |
21 | import org.eclipse.gef.commands.Command; |
22 | import org.eclipse.gef.editpolicies.NonResizableEditPolicy; |
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.common.ui.services.parser.ParserService; |
31 | import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart; |
32 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
33 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
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.WrapLabel; |
39 | import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
40 | import org.eclipse.gmf.runtime.emf.type.core.IElementType; |
41 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser; |
42 | import org.eclipse.gmf.runtime.emf.ui.services.parser.ParserHintAdapter; |
43 | import org.eclipse.gmf.runtime.notation.FontStyle; |
44 | import org.eclipse.gmf.runtime.notation.NotationPackage; |
45 | import org.eclipse.gmf.runtime.notation.View; |
46 | import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
47 | import org.eclipse.jface.viewers.ICellEditorValidator; |
48 | import org.eclipse.swt.SWT; |
49 | import org.eclipse.swt.accessibility.AccessibleEvent; |
50 | import org.eclipse.swt.graphics.Color; |
51 | import org.eclipse.swt.graphics.FontData; |
52 | import org.eclipse.swt.graphics.Image; |
53 | |
54 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
55 | import de.uka.ipd.sdq.pcm.gmf.allocation.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
56 | import de.uka.ipd.sdq.pcm.gmf.allocation.providers.PalladioComponentModelElementTypes; |
57 | import de.uka.ipd.sdq.pcm.gmf.allocation.providers.PalladioComponentModelParserProvider; |
58 | |
59 | /** |
60 | * @generated |
61 | */ |
62 | public class AllocationComponentLabelEditPart extends CompartmentEditPart |
63 | implements ITextAwareEditPart { |
64 | |
65 | /** |
66 | * @generated |
67 | */ |
68 | public static final int VISUAL_ID = 5005; |
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 AllocationComponentLabelEditPart(View view) { |
94 | super(view); |
95 | } |
96 | |
97 | /** |
98 | * @generated |
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 | } |
123 | |
124 | /** |
125 | * @generated |
126 | */ |
127 | protected String getLabelTextHelper(IFigure figure) { |
128 | if (figure instanceof WrapLabel) { |
129 | return ((WrapLabel) figure).getText(); |
130 | } else { |
131 | return ((Label) figure).getText(); |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * @generated |
137 | */ |
138 | protected void setLabelTextHelper(IFigure figure, String text) { |
139 | if (figure instanceof WrapLabel) { |
140 | ((WrapLabel) figure).setText(text); |
141 | } else { |
142 | ((Label) figure).setText(text); |
143 | } |
144 | } |
145 | |
146 | /** |
147 | * @generated |
148 | */ |
149 | protected Image getLabelIconHelper(IFigure figure) { |
150 | if (figure instanceof WrapLabel) { |
151 | return ((WrapLabel) figure).getIcon(); |
152 | } else { |
153 | return ((Label) figure).getIcon(); |
154 | } |
155 | } |
156 | |
157 | /** |
158 | * @generated |
159 | */ |
160 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
161 | if (figure instanceof WrapLabel) { |
162 | ((WrapLabel) figure).setIcon(icon); |
163 | } else { |
164 | ((Label) figure).setIcon(icon); |
165 | } |
166 | } |
167 | |
168 | /** |
169 | * @generated |
170 | */ |
171 | public void setLabel(WrapLabel figure) { |
172 | unregisterVisuals(); |
173 | setFigure(figure); |
174 | defaultText = getLabelTextHelper(figure); |
175 | registerVisuals(); |
176 | refreshVisuals(); |
177 | } |
178 | |
179 | /** |
180 | * @generated |
181 | */ |
182 | protected List getModelChildren() { |
183 | return Collections.EMPTY_LIST; |
184 | } |
185 | |
186 | /** |
187 | * @generated |
188 | */ |
189 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
190 | return null; |
191 | } |
192 | |
193 | /** |
194 | * @generated |
195 | */ |
196 | protected EObject getParserElement() { |
197 | |
198 | EObject element = resolveSemanticElement(); |
199 | return element != null ? element : (View) getModel(); |
200 | } |
201 | |
202 | /** |
203 | * @generated not |
204 | */ |
205 | protected Image getLabelIcon() { |
206 | EObject parserElement = getParserElement(); |
207 | if (parserElement == null) { |
208 | return null; |
209 | } |
210 | AllocationContext ctx = (AllocationContext) resolveSemanticElement(); |
211 | if (ctx.getAssemblyContext_AllocationContext() != null) { |
212 | if (ctx.getAssemblyContext_AllocationContext() |
213 | .getEncapsulatedComponent__AssemblyContext() != null) { |
214 | return PalladioComponentModelElementTypes.getImage(ctx |
215 | .getAssemblyContext_AllocationContext() |
216 | .getEncapsulatedComponent__AssemblyContext() |
217 | .eClass()); |
218 | } |
219 | } |
220 | return PalladioComponentModelElementTypes.getImage(parserElement |
221 | .eClass()); |
222 | } |
223 | |
224 | /** |
225 | * @generated not |
226 | */ |
227 | protected String getLabelText() { |
228 | String text = ""; |
229 | AllocationContext ctx = (AllocationContext) resolveSemanticElement(); |
230 | if (ctx.getAssemblyContext_AllocationContext() != null) { |
231 | text += "<" |
232 | + ctx.getAssemblyContext_AllocationContext() |
233 | .getEntityName() + ">"; |
234 | if (ctx.getAssemblyContext_AllocationContext() |
235 | .getEncapsulatedComponent__AssemblyContext() != null) { |
236 | text = ctx.getAssemblyContext_AllocationContext() |
237 | .getEncapsulatedComponent__AssemblyContext() |
238 | .getEntityName() |
239 | + " " + text; |
240 | } |
241 | } |
242 | if (text == null || text.length() == 0) { |
243 | text = defaultText; |
244 | } |
245 | return text; |
246 | } |
247 | |
248 | /** |
249 | * @generated |
250 | */ |
251 | public void setLabelText(String text) { |
252 | setLabelTextHelper(getFigure(), text); |
253 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
254 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
255 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
256 | .refreshFeedback(); |
257 | } |
258 | } |
259 | |
260 | /** |
261 | * |
262 | * @generated |
263 | */ |
264 | public String getEditText() { |
265 | if (getParserElement() == null || getParser() == null) { |
266 | return ""; //$NON-NLS-1$ |
267 | } |
268 | return getParser().getEditString( |
269 | new EObjectAdapter(getParserElement()), |
270 | getParserOptions().intValue()); |
271 | } |
272 | |
273 | /** |
274 | * @generated |
275 | */ |
276 | protected boolean isEditable() { |
277 | return false; |
278 | } |
279 | |
280 | /** |
281 | * @generated |
282 | */ |
283 | public ICellEditorValidator getEditTextValidator() { |
284 | return new ICellEditorValidator() { |
285 | |
286 | public String isValid(final Object value) { |
287 | if (value instanceof String) { |
288 | final EObject element = getParserElement(); |
289 | final IParser parser = getParser(); |
290 | try { |
291 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
292 | .runExclusive(new RunnableWithResult.Impl() { |
293 | |
294 | public void run() { |
295 | setResult(parser.isValidEditString( |
296 | new EObjectAdapter(element), |
297 | (String) value)); |
298 | } |
299 | }); |
300 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
301 | : valid.getMessage(); |
302 | } catch (InterruptedException ie) { |
303 | ie.printStackTrace(); |
304 | } |
305 | } |
306 | |
307 | // shouldn't get here |
308 | return null; |
309 | } |
310 | }; |
311 | } |
312 | |
313 | /** |
314 | * @generated |
315 | */ |
316 | public IContentAssistProcessor getCompletionProcessor() { |
317 | if (getParserElement() == null || getParser() == null) { |
318 | return null; |
319 | } |
320 | return getParser().getCompletionProcessor( |
321 | new EObjectAdapter(getParserElement())); |
322 | } |
323 | |
324 | /** |
325 | * @generated |
326 | */ |
327 | public ParserOptions getParserOptions() { |
328 | return ParserOptions.NONE; |
329 | } |
330 | |
331 | /** |
332 | * @generated |
333 | */ |
334 | public IParser getParser() { |
335 | if (parser == null) { |
336 | String parserHint = ((View) getModel()).getType(); |
337 | IAdaptable hintAdapter = new PalladioComponentModelParserProvider.HintAdapter( |
338 | PalladioComponentModelElementTypes.AllocationContext_3001, |
339 | getParserElement(), parserHint); |
340 | parser = ParserService.getInstance().getParser(hintAdapter); |
341 | } |
342 | return parser; |
343 | } |
344 | |
345 | /** |
346 | * @generated |
347 | */ |
348 | protected DirectEditManager getManager() { |
349 | if (manager == null) { |
350 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
351 | .getTextCellEditorClass(this), |
352 | PalladioComponentModelEditPartFactory |
353 | .getTextCellEditorLocator(this))); |
354 | } |
355 | return manager; |
356 | } |
357 | |
358 | /** |
359 | * @generated |
360 | */ |
361 | protected void setManager(DirectEditManager manager) { |
362 | this.manager = manager; |
363 | } |
364 | |
365 | /** |
366 | * @generated |
367 | */ |
368 | protected void performDirectEdit() { |
369 | getManager().show(); |
370 | } |
371 | |
372 | /** |
373 | * @generated |
374 | */ |
375 | protected void performDirectEdit(Point eventLocation) { |
376 | if (getManager().getClass() == TextDirectEditManager.class) { |
377 | ((TextDirectEditManager) getManager()).show(eventLocation |
378 | .getSWTPoint()); |
379 | } |
380 | } |
381 | |
382 | /** |
383 | * @generated |
384 | */ |
385 | private void performDirectEdit(char initialCharacter) { |
386 | if (getManager() instanceof TextDirectEditManager) { |
387 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
388 | } else { |
389 | performDirectEdit(); |
390 | } |
391 | } |
392 | |
393 | /** |
394 | * @generated |
395 | */ |
396 | protected void performDirectEditRequest(Request request) { |
397 | final Request theRequest = request; |
398 | try { |
399 | getEditingDomain().runExclusive(new Runnable() { |
400 | |
401 | public void run() { |
402 | if (isActive() && isEditable()) { |
403 | if (theRequest |
404 | .getExtendedData() |
405 | .get( |
406 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
407 | Character initialChar = (Character) theRequest |
408 | .getExtendedData() |
409 | .get( |
410 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
411 | performDirectEdit(initialChar.charValue()); |
412 | } else if ((theRequest instanceof DirectEditRequest) |
413 | && (getEditText().equals(getLabelText()))) { |
414 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
415 | performDirectEdit(editRequest.getLocation()); |
416 | } else { |
417 | performDirectEdit(); |
418 | } |
419 | } |
420 | } |
421 | }); |
422 | } catch (InterruptedException e) { |
423 | e.printStackTrace(); |
424 | } |
425 | } |
426 | |
427 | /** |
428 | * @generated |
429 | */ |
430 | protected void refreshVisuals() { |
431 | super.refreshVisuals(); |
432 | refreshLabel(); |
433 | refreshFont(); |
434 | refreshFontColor(); |
435 | refreshUnderline(); |
436 | refreshStrikeThrough(); |
437 | } |
438 | |
439 | /** |
440 | * @generated |
441 | */ |
442 | protected void refreshLabel() { |
443 | setLabelTextHelper(getFigure(), getLabelText()); |
444 | setLabelIconHelper(getFigure(), getLabelIcon()); |
445 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
446 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
447 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
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 WrapLabel) { |
459 | ((WrapLabel) 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 WrapLabel) { |
470 | ((WrapLabel) 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 | * @generated |
498 | */ |
499 | protected void addSemanticListeners() { |
500 | if (getParser() instanceof ISemanticParser) { |
501 | EObject element = resolveSemanticElement(); |
502 | parserElements = ((ISemanticParser) getParser()) |
503 | .getSemanticElementsBeingParsed(element); |
504 | for (int i = 0; i < parserElements.size(); i++) { |
505 | addListenerFilter( |
506 | "SemanticModel" + i, this, (EObject) parserElements.get(i)); //$NON-NLS-1$ |
507 | } |
508 | } else { |
509 | super.addSemanticListeners(); |
510 | } |
511 | } |
512 | |
513 | /** |
514 | * @generated |
515 | */ |
516 | protected void removeSemanticListeners() { |
517 | if (parserElements != null) { |
518 | for (int i = 0; i < parserElements.size(); i++) { |
519 | removeListenerFilter("SemanticModel" + i); //$NON-NLS-1$ |
520 | } |
521 | } else { |
522 | super.removeSemanticListeners(); |
523 | } |
524 | } |
525 | |
526 | /** |
527 | * @generated |
528 | */ |
529 | protected AccessibleEditPart getAccessibleEditPart() { |
530 | if (accessibleEP == null) { |
531 | accessibleEP = new AccessibleGraphicalEditPart() { |
532 | |
533 | public void getName(AccessibleEvent e) { |
534 | e.result = getLabelTextHelper(getFigure()); |
535 | } |
536 | }; |
537 | } |
538 | return accessibleEP; |
539 | } |
540 | |
541 | /** |
542 | * @generated |
543 | */ |
544 | private View getFontStyleOwnerView() { |
545 | return getPrimaryView(); |
546 | } |
547 | |
548 | /** |
549 | * @generated |
550 | */ |
551 | protected void addNotationalListeners() { |
552 | super.addNotationalListeners(); |
553 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
554 | } |
555 | |
556 | /** |
557 | * @generated |
558 | */ |
559 | protected void removeNotationalListeners() { |
560 | super.removeNotationalListeners(); |
561 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
562 | } |
563 | |
564 | /** |
565 | * @generated not |
566 | */ |
567 | protected void handleNotificationEvent(Notification event) { |
568 | Object feature = event.getFeature(); |
569 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
570 | Integer c = (Integer) event.getNewValue(); |
571 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
572 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
573 | feature)) { |
574 | refreshUnderline(); |
575 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
576 | .equals(feature)) { |
577 | refreshStrikeThrough(); |
578 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
579 | feature) |
580 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
581 | feature) |
582 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
583 | .equals(feature) |
584 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
585 | feature)) { |
586 | refreshFont(); |
587 | } else { |
588 | refreshLabel(); |
589 | } |
590 | super.handleNotificationEvent(event); |
591 | } |
592 | |
593 | /** |
594 | * @generated |
595 | */ |
596 | protected IFigure createFigure() { |
597 | // Parent should assign one using setLabel() method |
598 | return null; |
599 | } |
600 | } |