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