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.resource.OpenSchedulingPolicyDialog; |
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 ProcessingResourceSpecificationSchedulingPolicyEditPart extends |
62 | CompartmentEditPart implements ITextAwareEditPart { |
63 | |
64 | /** |
65 | * @generated |
66 | */ |
67 | public static final int VISUAL_ID = 5004; |
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 ProcessingResourceSpecificationSchedulingPolicyEditPart(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, |
125 | new OpenSchedulingPolicyDialog()); |
126 | } |
127 | |
128 | /** |
129 | * @generated |
130 | */ |
131 | protected String getLabelTextHelper(IFigure figure) { |
132 | if (figure instanceof WrappingLabel) { |
133 | return ((WrappingLabel) figure).getText(); |
134 | } else { |
135 | return ((Label) figure).getText(); |
136 | } |
137 | } |
138 | |
139 | /** |
140 | * @generated |
141 | */ |
142 | protected void setLabelTextHelper(IFigure figure, String text) { |
143 | if (figure instanceof WrappingLabel) { |
144 | ((WrappingLabel) figure).setText(text); |
145 | } else { |
146 | ((Label) figure).setText(text); |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * @generated |
152 | */ |
153 | protected Image getLabelIconHelper(IFigure figure) { |
154 | if (figure instanceof WrappingLabel) { |
155 | return ((WrappingLabel) figure).getIcon(); |
156 | } else { |
157 | return ((Label) figure).getIcon(); |
158 | } |
159 | } |
160 | |
161 | /** |
162 | * @generated |
163 | */ |
164 | protected void setLabelIconHelper(IFigure figure, Image icon) { |
165 | if (figure instanceof WrappingLabel) { |
166 | ((WrappingLabel) figure).setIcon(icon); |
167 | } else { |
168 | ((Label) figure).setIcon(icon); |
169 | } |
170 | } |
171 | |
172 | /** |
173 | * @generated |
174 | */ |
175 | public void setLabel(WrappingLabel figure) { |
176 | unregisterVisuals(); |
177 | setFigure(figure); |
178 | defaultText = getLabelTextHelper(figure); |
179 | registerVisuals(); |
180 | refreshVisuals(); |
181 | } |
182 | |
183 | /** |
184 | * @generated |
185 | */ |
186 | protected List getModelChildren() { |
187 | return Collections.EMPTY_LIST; |
188 | } |
189 | |
190 | /** |
191 | * @generated |
192 | */ |
193 | public IGraphicalEditPart getChildBySemanticHint(String semanticHint) { |
194 | return null; |
195 | } |
196 | |
197 | /** |
198 | * @generated |
199 | */ |
200 | protected EObject getParserElement() { |
201 | return resolveSemanticElement(); |
202 | } |
203 | |
204 | /** |
205 | * @generated |
206 | */ |
207 | protected Image getLabelIcon() { |
208 | return null; |
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 | 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 getParser() != null; |
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.ProcessingResourceSpecification_3001, |
323 | getParserElement(), |
324 | PalladioComponentModelVisualIDRegistry |
325 | .getType(de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.ProcessingResourceSpecificationSchedulingPolicyEditPart.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 |
488 | */ |
489 | protected void addSemanticListeners() { |
490 | if (getParser() instanceof ISemanticParser) { |
491 | EObject element = resolveSemanticElement(); |
492 | parserElements = ((ISemanticParser) getParser()) |
493 | .getSemanticElementsBeingParsed(element); |
494 | for (int i = 0; i < parserElements.size(); i++) { |
495 | addListenerFilter( |
496 | "SemanticModel" + i, this, (EObject) parserElements.get(i)); //$NON-NLS-1$ |
497 | } |
498 | } else { |
499 | super.addSemanticListeners(); |
500 | } |
501 | } |
502 | |
503 | /** |
504 | * @generated |
505 | */ |
506 | protected void removeSemanticListeners() { |
507 | if (parserElements != null) { |
508 | for (int i = 0; i < parserElements.size(); i++) { |
509 | removeListenerFilter("SemanticModel" + i); //$NON-NLS-1$ |
510 | } |
511 | } else { |
512 | super.removeSemanticListeners(); |
513 | } |
514 | } |
515 | |
516 | /** |
517 | * @generated |
518 | */ |
519 | protected AccessibleEditPart getAccessibleEditPart() { |
520 | if (accessibleEP == null) { |
521 | accessibleEP = new AccessibleGraphicalEditPart() { |
522 | |
523 | public void getName(AccessibleEvent e) { |
524 | e.result = getLabelTextHelper(getFigure()); |
525 | } |
526 | }; |
527 | } |
528 | return accessibleEP; |
529 | } |
530 | |
531 | /** |
532 | * @generated |
533 | */ |
534 | private View getFontStyleOwnerView() { |
535 | return getPrimaryView(); |
536 | } |
537 | |
538 | /** |
539 | * @generated |
540 | */ |
541 | protected void addNotationalListeners() { |
542 | super.addNotationalListeners(); |
543 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
544 | } |
545 | |
546 | /** |
547 | * @generated |
548 | */ |
549 | protected void removeNotationalListeners() { |
550 | super.removeNotationalListeners(); |
551 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
552 | } |
553 | |
554 | /** |
555 | * @generated |
556 | */ |
557 | protected void handleNotificationEvent(Notification event) { |
558 | Object feature = event.getFeature(); |
559 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
560 | Integer c = (Integer) event.getNewValue(); |
561 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
562 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
563 | feature)) { |
564 | refreshUnderline(); |
565 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
566 | .equals(feature)) { |
567 | refreshStrikeThrough(); |
568 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
569 | feature) |
570 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
571 | feature) |
572 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
573 | .equals(feature) |
574 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
575 | feature)) { |
576 | refreshFont(); |
577 | } else { |
578 | if (getParser() != null |
579 | && getParser().isAffectingEvent(event, |
580 | getParserOptions().intValue())) { |
581 | refreshLabel(); |
582 | } |
583 | if (getParser() instanceof ISemanticParser) { |
584 | ISemanticParser modelParser = (ISemanticParser) getParser(); |
585 | if (modelParser.areSemanticElementsAffected(null, event)) { |
586 | removeSemanticListeners(); |
587 | if (resolveSemanticElement() != null) { |
588 | addSemanticListeners(); |
589 | } |
590 | refreshLabel(); |
591 | } |
592 | } |
593 | } |
594 | super.handleNotificationEvent(event); |
595 | } |
596 | |
597 | /** |
598 | * @generated |
599 | */ |
600 | protected IFigure createFigure() { |
601 | // Parent should assign one using setLabel() method |
602 | return null; |
603 | } |
604 | |
605 | } |