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.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.RectangleFigure; |
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.MoveHandle; |
24 | import org.eclipse.gef.handles.NonResizableHandleKit; |
25 | import org.eclipse.gef.requests.DirectEditRequest; |
26 | import org.eclipse.gef.tools.DirectEditManager; |
27 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; |
28 | import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; |
29 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; |
30 | import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions; |
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.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.gmf.repository.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
53 | import de.uka.ipd.sdq.pcm.gmf.repository.part.PalladioComponentModelVisualIDRegistry; |
54 | import de.uka.ipd.sdq.pcm.gmf.repository.providers.PalladioComponentModelElementTypes; |
55 | import de.uka.ipd.sdq.pcm.gmf.repository.providers.PalladioComponentModelParserProvider; |
56 | |
57 | /** |
58 | * @generated |
59 | */ |
60 | public class InfrastructureInterfaceEntityNameEditPart extends |
61 | CompartmentEditPart implements ITextAwareEditPart { |
62 | |
63 | /** |
64 | * @generated |
65 | */ |
66 | public static final int VISUAL_ID = 5110; |
67 | |
68 | /** |
69 | * @generated |
70 | */ |
71 | private DirectEditManager manager; |
72 | |
73 | /** |
74 | * @generated |
75 | */ |
76 | private IParser parser; |
77 | |
78 | /** |
79 | * @generated |
80 | */ |
81 | private List parserElements; |
82 | |
83 | /** |
84 | * @generated |
85 | */ |
86 | private String defaultText; |
87 | |
88 | /** |
89 | * @generated |
90 | */ |
91 | public InfrastructureInterfaceEntityNameEditPart(View view) { |
92 | super(view); |
93 | } |
94 | |
95 | /** |
96 | * @generated |
97 | */ |
98 | protected void createDefaultEditPolicies() { |
99 | super.createDefaultEditPolicies(); |
100 | installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, |
101 | new PalladioComponentModelTextSelectionEditPolicy()); |
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 | ((MoveHandle) handles.get(0)).setBorder(null); |
112 | return handles; |
113 | } |
114 | |
115 | public Command getCommand(Request request) { |
116 | return null; |
117 | } |
118 | |
119 | public boolean understandsRequest(Request request) { |
120 | return false; |
121 | } |
122 | }); |
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 |
203 | */ |
204 | protected Image getLabelIcon() { |
205 | EObject parserElement = getParserElement(); |
206 | if (parserElement == null) { |
207 | return null; |
208 | } |
209 | return PalladioComponentModelElementTypes.getImage(parserElement |
210 | .eClass()); |
211 | } |
212 | |
213 | /** |
214 | * @generated |
215 | */ |
216 | protected String getLabelText() { |
217 | String text = null; |
218 | EObject parserElement = getParserElement(); |
219 | if (parserElement != null && getParser() != null) { |
220 | text = getParser().getPrintString( |
221 | new EObjectAdapter(parserElement), |
222 | getParserOptions().intValue()); |
223 | } |
224 | if (text == null || text.length() == 0) { |
225 | text = defaultText; |
226 | } |
227 | return text; |
228 | } |
229 | |
230 | /** |
231 | * @generated |
232 | */ |
233 | public void setLabelText(String text) { |
234 | setLabelTextHelper(getFigure(), text); |
235 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
236 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
237 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
238 | .refreshFeedback(); |
239 | } |
240 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
241 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
242 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
243 | .refreshFeedback(); |
244 | } |
245 | } |
246 | |
247 | /** |
248 | * @generated |
249 | */ |
250 | public String getEditText() { |
251 | if (getParserElement() == null || getParser() == null) { |
252 | return ""; //$NON-NLS-1$ |
253 | } |
254 | return getParser().getEditString( |
255 | new EObjectAdapter(getParserElement()), |
256 | getParserOptions().intValue()); |
257 | } |
258 | |
259 | /** |
260 | * @generated |
261 | */ |
262 | protected boolean isEditable() { |
263 | return getParser() != null; |
264 | } |
265 | |
266 | /** |
267 | * @generated |
268 | */ |
269 | public ICellEditorValidator getEditTextValidator() { |
270 | return new ICellEditorValidator() { |
271 | |
272 | public String isValid(final Object value) { |
273 | if (value instanceof String) { |
274 | final EObject element = getParserElement(); |
275 | final IParser parser = getParser(); |
276 | try { |
277 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
278 | .runExclusive(new RunnableWithResult.Impl() { |
279 | |
280 | public void run() { |
281 | setResult(parser.isValidEditString( |
282 | new EObjectAdapter(element), |
283 | (String) value)); |
284 | } |
285 | }); |
286 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
287 | : valid.getMessage(); |
288 | } catch (InterruptedException ie) { |
289 | ie.printStackTrace(); |
290 | } |
291 | } |
292 | |
293 | // shouldn't get here |
294 | return null; |
295 | } |
296 | }; |
297 | } |
298 | |
299 | /** |
300 | * @generated |
301 | */ |
302 | public IContentAssistProcessor getCompletionProcessor() { |
303 | if (getParserElement() == null || getParser() == null) { |
304 | return null; |
305 | } |
306 | return getParser().getCompletionProcessor( |
307 | new EObjectAdapter(getParserElement())); |
308 | } |
309 | |
310 | /** |
311 | * @generated |
312 | */ |
313 | public ParserOptions getParserOptions() { |
314 | return ParserOptions.NONE; |
315 | } |
316 | |
317 | /** |
318 | * @generated |
319 | */ |
320 | public IParser getParser() { |
321 | if (parser == null) { |
322 | parser = PalladioComponentModelParserProvider |
323 | .getParser( |
324 | PalladioComponentModelElementTypes.InfrastructureInterface_2109, |
325 | getParserElement(), |
326 | PalladioComponentModelVisualIDRegistry |
327 | .getType(de.uka.ipd.sdq.pcm.gmf.repository.edit.parts.InfrastructureInterfaceEntityNameEditPart.VISUAL_ID)); |
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 addNotationalListeners() { |
544 | super.addNotationalListeners(); |
545 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
546 | } |
547 | |
548 | /** |
549 | * @generated |
550 | */ |
551 | protected void removeNotationalListeners() { |
552 | super.removeNotationalListeners(); |
553 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
554 | } |
555 | |
556 | /** |
557 | * @generated |
558 | */ |
559 | protected void handleNotificationEvent(Notification event) { |
560 | Object feature = event.getFeature(); |
561 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
562 | Integer c = (Integer) event.getNewValue(); |
563 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
564 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
565 | feature)) { |
566 | refreshUnderline(); |
567 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
568 | .equals(feature)) { |
569 | refreshStrikeThrough(); |
570 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
571 | feature) |
572 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
573 | feature) |
574 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
575 | .equals(feature) |
576 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
577 | feature)) { |
578 | refreshFont(); |
579 | } else { |
580 | if (getParser() != null |
581 | && getParser().isAffectingEvent(event, |
582 | getParserOptions().intValue())) { |
583 | refreshLabel(); |
584 | } |
585 | if (getParser() instanceof ISemanticParser) { |
586 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
587 | if (modelParser.areSemanticElementsAffected(null, event)) { |
588 | removeSemanticListeners(); |
589 | if (resolveSemanticElement() != null) { |
590 | addSemanticListeners(); |
591 | } |
592 | refreshLabel(); |
593 | } |
594 | } |
595 | } |
596 | super.handleNotificationEvent(event); |
597 | } |
598 | |
599 | /** |
600 | * @generated |
601 | */ |
602 | protected IFigure createFigure() { |
603 | // Parent should assign one using setLabel() method |
604 | return null; |
605 | } |
606 | |
607 | } |