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