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.OpenProcessingRateDialog; |
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 | import de.uka.ipd.sdq.pcm.resourceenvironment.ProcessingResourceSpecification; |
58 | |
59 | /** |
60 | * @generated |
61 | */ |
62 | public class WrappingLabelEditPart extends CompartmentEditPart implements |
63 | ITextAwareEditPart { |
64 | |
65 | /** |
66 | * @generated |
67 | */ |
68 | public static final int VISUAL_ID = 5009; |
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 WrappingLabelEditPart(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 OpenProcessingRateDialog()); |
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 | return null; |
207 | } |
208 | |
209 | /** |
210 | * @generated not |
211 | */ |
212 | protected String getLabelText() { |
213 | String text = null; |
214 | ProcessingResourceSpecification spec = (ProcessingResourceSpecification) resolveSemanticElement(); |
215 | if (spec.getProcessingRate_ProcessingResourceSpecification() != null) |
216 | text = spec.getProcessingRate_ProcessingResourceSpecification() |
217 | .getSpecification(); |
218 | if (text == null || text.length() == 0) { |
219 | text = defaultText; |
220 | } |
221 | return "Processing Rate: " + text; // custom code |
222 | } |
223 | |
224 | /** |
225 | * @generated |
226 | */ |
227 | public void setLabelText(String text) { |
228 | setLabelTextHelper(getFigure(), text); |
229 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
230 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
231 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
232 | .refreshFeedback(); |
233 | } |
234 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
235 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
236 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
237 | .refreshFeedback(); |
238 | } |
239 | } |
240 | |
241 | /** |
242 | * @generated |
243 | */ |
244 | public String getEditText() { |
245 | if (getParserElement() == null || getParser() == null) { |
246 | return ""; //$NON-NLS-1$ |
247 | } |
248 | return getParser().getEditString( |
249 | new EObjectAdapter(getParserElement()), |
250 | getParserOptions().intValue()); |
251 | } |
252 | |
253 | /** |
254 | * @generated |
255 | */ |
256 | protected boolean isEditable() { |
257 | return false; |
258 | } |
259 | |
260 | /** |
261 | * @generated |
262 | */ |
263 | public ICellEditorValidator getEditTextValidator() { |
264 | return new ICellEditorValidator() { |
265 | |
266 | public String isValid(final Object value) { |
267 | if (value instanceof String) { |
268 | final EObject element = getParserElement(); |
269 | final IParser parser = getParser(); |
270 | try { |
271 | IParserEditStatus valid = (IParserEditStatus) getEditingDomain() |
272 | .runExclusive(new RunnableWithResult.Impl() { |
273 | |
274 | public void run() { |
275 | setResult(parser.isValidEditString( |
276 | new EObjectAdapter(element), |
277 | (String) value)); |
278 | } |
279 | }); |
280 | return valid.getCode() == ParserEditStatus.EDITABLE ? null |
281 | : valid.getMessage(); |
282 | } catch (InterruptedException ie) { |
283 | ie.printStackTrace(); |
284 | } |
285 | } |
286 | |
287 | // shouldn't get here |
288 | return null; |
289 | } |
290 | }; |
291 | } |
292 | |
293 | /** |
294 | * @generated |
295 | */ |
296 | public IContentAssistProcessor getCompletionProcessor() { |
297 | if (getParserElement() == null || getParser() == null) { |
298 | return null; |
299 | } |
300 | return getParser().getCompletionProcessor( |
301 | new EObjectAdapter(getParserElement())); |
302 | } |
303 | |
304 | /** |
305 | * @generated |
306 | */ |
307 | public ParserOptions getParserOptions() { |
308 | return ParserOptions.NONE; |
309 | } |
310 | |
311 | /** |
312 | * @generated |
313 | */ |
314 | public IParser getParser() { |
315 | if (parser == null) { |
316 | parser = PalladioComponentModelParserProvider |
317 | .getParser( |
318 | PalladioComponentModelElementTypes.ProcessingResourceSpecification_3001, |
319 | getParserElement(), |
320 | PalladioComponentModelVisualIDRegistry |
321 | .getType(de.uka.ipd.sdq.pcm.gmf.resource.edit.parts.WrappingLabelEditPart.VISUAL_ID)); |
322 | } |
323 | return parser; |
324 | } |
325 | |
326 | /** |
327 | * @generated |
328 | */ |
329 | protected DirectEditManager getManager() { |
330 | if (manager == null) { |
331 | setManager(new TextDirectEditManager(this, TextDirectEditManager |
332 | .getTextCellEditorClass(this), |
333 | PalladioComponentModelEditPartFactory |
334 | .getTextCellEditorLocator(this))); |
335 | } |
336 | return manager; |
337 | } |
338 | |
339 | /** |
340 | * @generated |
341 | */ |
342 | protected void setManager(DirectEditManager manager) { |
343 | this.manager = manager; |
344 | } |
345 | |
346 | /** |
347 | * @generated |
348 | */ |
349 | protected void performDirectEdit() { |
350 | getManager().show(); |
351 | } |
352 | |
353 | /** |
354 | * @generated |
355 | */ |
356 | protected void performDirectEdit(Point eventLocation) { |
357 | if (getManager().getClass() == TextDirectEditManager.class) { |
358 | ((TextDirectEditManager) getManager()).show(eventLocation |
359 | .getSWTPoint()); |
360 | } |
361 | } |
362 | |
363 | /** |
364 | * @generated |
365 | */ |
366 | private void performDirectEdit(char initialCharacter) { |
367 | if (getManager() instanceof TextDirectEditManager) { |
368 | ((TextDirectEditManager) getManager()).show(initialCharacter); |
369 | } else { |
370 | performDirectEdit(); |
371 | } |
372 | } |
373 | |
374 | /** |
375 | * @generated |
376 | */ |
377 | protected void performDirectEditRequest(Request request) { |
378 | final Request theRequest = request; |
379 | try { |
380 | getEditingDomain().runExclusive(new Runnable() { |
381 | |
382 | public void run() { |
383 | if (isActive() && isEditable()) { |
384 | if (theRequest |
385 | .getExtendedData() |
386 | .get( |
387 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) { |
388 | Character initialChar = (Character) theRequest |
389 | .getExtendedData() |
390 | .get( |
391 | RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR); |
392 | performDirectEdit(initialChar.charValue()); |
393 | } else if ((theRequest instanceof DirectEditRequest) |
394 | && (getEditText().equals(getLabelText()))) { |
395 | DirectEditRequest editRequest = (DirectEditRequest) theRequest; |
396 | performDirectEdit(editRequest.getLocation()); |
397 | } else { |
398 | performDirectEdit(); |
399 | } |
400 | } |
401 | } |
402 | }); |
403 | } catch (InterruptedException e) { |
404 | e.printStackTrace(); |
405 | } |
406 | } |
407 | |
408 | /** |
409 | * @generated |
410 | */ |
411 | protected void refreshVisuals() { |
412 | super.refreshVisuals(); |
413 | refreshLabel(); |
414 | refreshFont(); |
415 | refreshFontColor(); |
416 | refreshUnderline(); |
417 | refreshStrikeThrough(); |
418 | } |
419 | |
420 | /** |
421 | * @generated |
422 | */ |
423 | protected void refreshLabel() { |
424 | setLabelTextHelper(getFigure(), getLabelText()); |
425 | setLabelIconHelper(getFigure(), getLabelIcon()); |
426 | Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE); |
427 | if (pdEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
428 | ((PalladioComponentModelTextSelectionEditPolicy) pdEditPolicy) |
429 | .refreshFeedback(); |
430 | } |
431 | Object sfEditPolicy = getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE); |
432 | if (sfEditPolicy instanceof PalladioComponentModelTextSelectionEditPolicy) { |
433 | ((PalladioComponentModelTextSelectionEditPolicy) sfEditPolicy) |
434 | .refreshFeedback(); |
435 | } |
436 | } |
437 | |
438 | /** |
439 | * @generated |
440 | */ |
441 | protected void refreshUnderline() { |
442 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
443 | NotationPackage.eINSTANCE.getFontStyle()); |
444 | if (style != null && getFigure() instanceof WrappingLabel) { |
445 | ((WrappingLabel) getFigure()).setTextUnderline(style.isUnderline()); |
446 | } |
447 | } |
448 | |
449 | /** |
450 | * @generated |
451 | */ |
452 | protected void refreshStrikeThrough() { |
453 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
454 | NotationPackage.eINSTANCE.getFontStyle()); |
455 | if (style != null && getFigure() instanceof WrappingLabel) { |
456 | ((WrappingLabel) getFigure()).setTextStrikeThrough(style |
457 | .isStrikeThrough()); |
458 | } |
459 | } |
460 | |
461 | /** |
462 | * @generated |
463 | */ |
464 | protected void refreshFont() { |
465 | FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle( |
466 | NotationPackage.eINSTANCE.getFontStyle()); |
467 | if (style != null) { |
468 | FontData fontData = new FontData(style.getFontName(), style |
469 | .getFontHeight(), (style.isBold() ? SWT.BOLD : SWT.NORMAL) |
470 | | (style.isItalic() ? SWT.ITALIC : SWT.NORMAL)); |
471 | setFont(fontData); |
472 | } |
473 | } |
474 | |
475 | /** |
476 | * @generated |
477 | */ |
478 | protected void setFontColor(Color color) { |
479 | getFigure().setForegroundColor(color); |
480 | } |
481 | |
482 | /** |
483 | * @generated not |
484 | */ |
485 | protected void addSemanticListeners() { |
486 | ProcessingResourceSpecification spec = (ProcessingResourceSpecification) resolveSemanticElement(); |
487 | addListenerFilter( |
488 | "SemanticModel", this, spec.getProcessingRate_ProcessingResourceSpecification()); //$NON-NLS-1$ |
489 | |
490 | } |
491 | |
492 | /** |
493 | * @generated not |
494 | */ |
495 | protected void removeSemanticListeners() { |
496 | removeListenerFilter("SemanticModel"); //$NON-NLS-1$ |
497 | } |
498 | |
499 | /** |
500 | * @generated |
501 | */ |
502 | protected AccessibleEditPart getAccessibleEditPart() { |
503 | if (accessibleEP == null) { |
504 | accessibleEP = new AccessibleGraphicalEditPart() { |
505 | |
506 | public void getName(AccessibleEvent e) { |
507 | e.result = getLabelTextHelper(getFigure()); |
508 | } |
509 | }; |
510 | } |
511 | return accessibleEP; |
512 | } |
513 | |
514 | /** |
515 | * @generated |
516 | */ |
517 | private View getFontStyleOwnerView() { |
518 | return getPrimaryView(); |
519 | } |
520 | |
521 | /** |
522 | * @generated |
523 | */ |
524 | protected void addNotationalListeners() { |
525 | super.addNotationalListeners(); |
526 | addListenerFilter("PrimaryView", this, getPrimaryView()); //$NON-NLS-1$ |
527 | } |
528 | |
529 | /** |
530 | * @generated |
531 | */ |
532 | protected void removeNotationalListeners() { |
533 | super.removeNotationalListeners(); |
534 | removeListenerFilter("PrimaryView"); //$NON-NLS-1$ |
535 | } |
536 | |
537 | /** |
538 | * @generated not |
539 | */ |
540 | protected void handleNotificationEvent(Notification event) { |
541 | Object feature = event.getFeature(); |
542 | if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(feature)) { |
543 | Integer c = (Integer) event.getNewValue(); |
544 | setFontColor(DiagramColorRegistry.getInstance().getColor(c)); |
545 | } else if (NotationPackage.eINSTANCE.getFontStyle_Underline().equals( |
546 | feature)) { |
547 | refreshUnderline(); |
548 | } else if (NotationPackage.eINSTANCE.getFontStyle_StrikeThrough() |
549 | .equals(feature)) { |
550 | refreshStrikeThrough(); |
551 | } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight().equals( |
552 | feature) |
553 | || NotationPackage.eINSTANCE.getFontStyle_FontName().equals( |
554 | feature) |
555 | || NotationPackage.eINSTANCE.getFontStyle_Bold() |
556 | .equals(feature) |
557 | || NotationPackage.eINSTANCE.getFontStyle_Italic().equals( |
558 | feature)) { |
559 | refreshFont(); |
560 | } else { |
561 | refreshLabel(); |
562 | } |
563 | super.handleNotificationEvent(event); |
564 | } |
565 | |
566 | /** |
567 | * @generated |
568 | */ |
569 | protected IFigure createFigure() { |
570 | // Parent should assign one using setLabel() method |
571 | return null; |
572 | } |
573 | |
574 | } |