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