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