1 | /* |
2 | * Copyright 2006 SDQ Research Group, University of Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.seff.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.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.commands.Command; |
21 | import org.eclipse.gef.editpolicies.NonResizableEditPolicy; |
22 | import org.eclipse.gef.handles.MoveHandle; |
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.diagram.ui.editparts.CompartmentEditPart; |
31 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
32 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
33 | import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles; |
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.dialogs.seff.OpenLoopIterationsDialog; |
53 | import de.uka.ipd.sdq.pcm.gmf.seff.edit.policies.PalladioComponentModelTextSelectionEditPolicy; |
54 | import de.uka.ipd.sdq.pcm.gmf.seff.part.PalladioComponentModelVisualIDRegistry; |
55 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelElementTypes; |
56 | import de.uka.ipd.sdq.pcm.gmf.seff.providers.PalladioComponentModelParserProvider; |
57 | import de.uka.ipd.sdq.pcm.seff.LoopAction; |
58 | |
59 | /** |
60 | * @generated |
61 | */ |
62 | public class Loop2IterationsLabelEditPart extends CompartmentEditPart implements |
63 | ITextAwareEditPart { |
64 | |
65 | /** |
66 | * @generated |
67 | */ |
68 | public static final int VISUAL_ID = 5004; |
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 Loop2IterationsLabelEditPart(View view) { |
94 | super(view); |
95 | } |
96 | |
97 | /** |
98 | * @generated not |
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 | installEditPolicy(EditPolicyRoles.OPEN_ROLE, |
123 | new OpenLoopIterationsDialog()); |
124 | } |
125 | |
126 | /** |
127 | * @generated |
128 | */ |
129 | protected String getLabelTextHelper(IFigure figure) { |
130 | if (figure instanceof WrappingLabel) { |
131 | return ((WrappingLabel) figure).getText(); |
132 | } else { |
133 | return ((Label) figure).getText(); |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * @generated |
139 | */ |
140 | protected void setLabelTextHelper(IFigure figure, String text) { |
141 | if (figure instanceof WrappingLabel) { |
142 | ((WrappingLabel) figure).setText(text); |
143 | } else { |
144 | ((Label) figure).setText(text); |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * @generated |
150 | */ |
151 | protected Image getLabelIconHelper(IFigure figure) { |
152 | if (figure instanceof WrappingLabel) { |
153 | return ((WrappingLabel) figure).getIcon(); |
154 | } else { |
155 | return ((Label) figure).getIcon(); |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * @generated |
161 | */ |
162 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
163 | if (figure instanceof WrappingLabel) { |
164 | ((WrappingLabel) figure).setIcon(icon); |
165 | } else { |
166 | ((Label) figure).setIcon(icon); |
167 | } |
168 | } |
169 | |
170 | /** |
171 | * @generated |
172 | */ |
173 | public void setLabel(WrappingLabel figure) { |
174 | unregisterVisuals(); |
175 | setFigure(figure); |
176 | defaultText = getLabelTextHelper(figure); |
177 | registerVisuals(); |
178 | refreshVisuals(); |
179 | } |
180 | |
181 | /** |
182 | * @generated |
183 | */ |
184 | protected List getModelChildren() { |
185 | return Collections.EMPTY_LIST; |
186 | } |
187 | |
188 | /** |
189 | * @generated |
190 | */ |
191 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
192 | return null; |
193 | } |
194 | |
195 | /** |
196 | * @generated |
197 | */ |
198 | protected EObject getParserElement() { |
199 | return resolveSemanticElement(); |
200 | } |
201 | |
202 | /** |
203 | * @generated |
204 | */ |
205 | protected Image getLabelIcon() { |
206 | EObject parserElement = getParserElement(); |
207 | if (parserElement == null) { |
208 | return null; |
209 | } |
210 | return PalladioComponentModelElementTypes.getImage(parserElement |
211 | .eClass()); |
212 | } |
213 | |
214 | /** |
215 | * @generated not |
216 | */ |
217 | protected String getLabelText() { |
218 | String text = null; |
219 | LoopAction loop = (LoopAction) resolveSemanticElement(); |
220 | if (loop.getIterationCount_LoopAction() != null) |
221 | text = loop.getIterationCount_LoopAction().getSpecification(); |
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 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
239 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
240 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
241 | .refreshFeedback(); |
242 | } |
243 | } |
244 | |
245 | /** |
246 | * @generated |
247 | */ |
248 | public String getEditText() { |
249 | if (getParserElement() == null || getParser() == null) { |
250 | return ""; //$NON-NLS-1$ |
251 | } |
252 | return getParser().getEditString( |
253 | new EObjectAdapter(getParserElement()), |
254 | getParserOptions().intValue()); |
255 | } |
256 | |
257 | /** |
258 | * @generated |
259 | */ |
260 | protected boolean isEditable() { |
261 | return false; |
262 | } |
263 | |
264 | /** |
265 | * @generated |
266 | */ |
267 | public ICellEditorValidator getEditTextValidator() { |
268 | return new ICellEditorValidator() { |
269 | |
270 | public String isValid(final Object value) { |
271 | if (value instanceof String) { |
272 | final EObject element = getParserElement(); |
273 | final IParser parser = getParser(); |
274 | try { |
275 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
276 | .runExclusive(new RunnableWithResult.Impl() { |
277 | |
278 | public void run() { |
279 | setResult(parser.isValidEditString( |
280 | new EObjectAdapter(element), |
281 | (String) value)); |
282 | } |
283 | }); |
284 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
285 | : valid.getMessage(); |
286 | } catch (InterruptedException ie) { |
287 | ie.printStackTrace(); |
288 | } |
289 | } |
290 | |
291 | // shouldn't get here |
292 | return null; |
293 | } |
294 | }; |
295 | } |
296 | |
297 | /** |
298 | * @generated |
299 | */ |
300 | public IContentAssistProcessor getCompletionProcessor() { |
301 | if (getParserElement() == null || getParser() == null) { |
302 | return null; |
303 | } |
304 | return getParser().getCompletionProcessor( |
305 | new EObjectAdapter(getParserElement())); |
306 | } |
307 | |
308 | /** |
309 | * @generated |
310 | */ |
311 | public ParserOptions getParserOptions() { |
312 | return ParserOptions.NONE; |
313 | } |
314 | |
315 | /** |
316 | * @generated |
317 | */ |
318 | public IParser getParser() { |
319 | if (parser == null) { |
320 | parser = PalladioComponentModelParserProvider |
321 | .getParser( |
322 | PalladioComponentModelElementTypes.LoopAction_3006, |
323 | getParserElement(), |
324 | PalladioComponentModelVisualIDRegistry |
325 | .getType(de.uka.ipd.sdq.pcm.gmf.seff.edit.parts.Loop2IterationsLabelEditPart.VISUAL_ID)); |
326 | } |
327 | return parser; |
328 | } |
329 | |
330 | /** |
331 | * @generated |
332 | */ |
333 | protected DirectEditManager getManager() { |
334 | if (manager == null) { |
335 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
336 | .getTextCellEditorClass(this), |
337 | PalladioComponentModelEditPartFactory |
338 | .getTextCellEditorLocator(this))); |
339 | } |
340 | return manager; |
341 | } |
342 | |
343 | /** |
344 | * @generated |
345 | */ |
346 | protected void setManager(DirectEditManager manager) { |
347 | this.manager = manager; |
348 | } |
349 | |
350 | /** |
351 | * @generated |
352 | */ |
353 | protected void performDirectEdit() { |
354 | getManager().show(); |
355 | } |
356 | |
357 | /** |
358 | * @generated |
359 | */ |
360 | protected void performDirectEdit(Point eventLocation) { |
361 | if (getManager().getClass() == TextDirectEditManager.class) { |
362 | ((TextDirectEditManager) getManager()).show(eventLocation |
363 | .getSWTPoint()); |
364 | } |
365 | } |
366 | |
367 | /** |
368 | * @generated |
369 | */ |
370 | private void performDirectEdit(char initialCharacter) { |
371 | if (getManager() instanceof TextDirectEditManager) { |
372 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
373 | } else { |
374 | performDirectEdit(); |
375 | } |
376 | } |
377 | |
378 | /** |
379 | * @generated |
380 | */ |
381 | protected void performDirectEditRequest(Request request) { |
382 | final Request theRequest = request; |
383 | try { |
384 | getEditingDomain().runExclusive(new Runnable() { |
385 | |
386 | public void run() { |
387 | if (isActive() && isEditable()) { |
388 | if (theRequest |
389 | .getExtendedData() |
390 | .get( |
391 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
392 | Character initialChar = (Character) theRequest |
393 | .getExtendedData() |
394 | .get( |
395 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
396 | performDirectEdit(initialChar.charValue()); |
397 | } else if ((theRequest instanceof DirectEditRequest) |
398 | && (getEditText().equals(getLabelText()))) { |
399 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
400 | performDirectEdit(editRequest.getLocation()); |
401 | } else { |
402 | performDirectEdit(); |
403 | } |
404 | } |
405 | } |
406 | }); |
407 | } catch (InterruptedException e) { |
408 | e.printStackTrace(); |
409 | } |
410 | } |
411 | |
412 | /** |
413 | * @generated |
414 | */ |
415 | protected void refreshVisuals() { |
416 | super.refreshVisuals(); |
417 | refreshLabel(); |
418 | refreshFont(); |
419 | refreshFontColor(); |
420 | refreshUnderline(); |
421 | refreshStrikeThrough(); |
422 | } |
423 | |
424 | /** |
425 | * @generated |
426 | */ |
427 | protected void refreshLabel() { |
428 | setLabelTextHelper(getFigure(), getLabelText()); |
429 | setLabelIconHelper(getFigure(), getLabelIcon()); |
430 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
431 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
432 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
433 | .refreshFeedback(); |
434 | } |
435 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
436 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
437 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
438 | .refreshFeedback(); |
439 | } |
440 | } |
441 | |
442 | /** |
443 | * @generated |
444 | */ |
445 | protected void refreshUnderline() { |
446 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
447 | NotationPackage.eINSTANCE.getFontStyle()); |
448 | if (style != null && getFigure() instanceof WrappingLabel) { |
449 | ((WrappingLabel) getFigure()).setTextUnderline(style.isUnderline()); |
450 | } |
451 | } |
452 | |
453 | /** |
454 | * @generated |
455 | */ |
456 | protected void refreshStrikeThrough() { |
457 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
458 | NotationPackage.eINSTANCE.getFontStyle()); |
459 | if (style != null && getFigure() instanceof WrappingLabel) { |
460 | ((WrappingLabel) getFigure()).setTextStrikeThrough(style |
461 | .isStrikeThrough()); |
462 | } |
463 | } |
464 | |
465 | /** |
466 | * @generated |
467 | */ |
468 | protected void refreshFont() { |
469 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
470 | NotationPackage.eINSTANCE.getFontStyle()); |
471 | if (style != null) { |
472 | FontData fontData = new FontData(style.getFontName(), style |
473 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
474 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
475 | setFont(fontData); |
476 | } |
477 | } |
478 | |
479 | /** |
480 | * @generated |
481 | */ |
482 | protected void setFontColor(Color color) { |
483 | getFigure().setForegroundColor(color); |
484 | } |
485 | |
486 | /** |
487 | * @generated not |
488 | */ |
489 | protected void addSemanticListeners() { |
490 | LoopAction loop = (LoopAction) resolveSemanticElement(); |
491 | addListenerFilter( |
492 | "SemanticModel", this, loop.getIterationCount_LoopAction()); //$NON-NLS-1$ |
493 | } |
494 | |
495 | /** |
496 | * @generated not |
497 | */ |
498 | protected void removeSemanticListeners() { |
499 | removeListenerFilter("SemanticModel"); //$NON-NLS-1$ |
500 | } |
501 | |
502 | /** |
503 | * @generated |
504 | */ |
505 | protected AccessibleEditPart getAccessibleEditPart() { |
506 | if (accessibleEP == null) { |
507 | accessibleEP = new AccessibleGraphicalEditPart() { |
508 | |
509 | public void getName(AccessibleEvent e) { |
510 | e.result = getLabelTextHelper(getFigure()); |
511 | } |
512 | }; |
513 | } |
514 | return accessibleEP; |
515 | } |
516 | |
517 | /** |
518 | * @generated |
519 | */ |
520 | private View getFontStyleOwnerView() { |
521 | return getPrimaryView(); |
522 | } |
523 | |
524 | /** |
525 | * @generated |
526 | */ |
527 | protected void addNotationalListeners() { |
528 | super.addNotationalListeners(); |
529 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
530 | } |
531 | |
532 | /** |
533 | * @generated |
534 | */ |
535 | protected void removeNotationalListeners() { |
536 | super.removeNotationalListeners(); |
537 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
538 | } |
539 | |
540 | /** |
541 | * @generated not |
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 | refreshLabel(); |
565 | } |
566 | super.handleNotificationEvent(event); |
567 | } |
568 | |
569 | /** |
570 | * @generated |
571 | */ |
572 | protected IFigure createFigure() { |
573 | // Parent should assign one using setLabel() method |
574 | return null; |
575 | } |
576 | } |