001: package newprocess.diagram.edit.parts;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.List;
006:
007: import newprocess.diagram.edit.policies.New_processTextSelectionEditPolicy;
008: import newprocess.diagram.providers.New_processElementTypes;
009:
010: import org.eclipse.draw2d.IFigure;
011: import org.eclipse.draw2d.Label;
012: import org.eclipse.draw2d.geometry.Point;
013: import org.eclipse.emf.common.notify.Notification;
014: import org.eclipse.emf.ecore.EObject;
015: import org.eclipse.emf.transaction.RunnableWithResult;
016: import org.eclipse.gef.AccessibleEditPart;
017: import org.eclipse.gef.EditPolicy;
018: import org.eclipse.gef.GraphicalEditPart;
019: import org.eclipse.gef.Request;
020: import org.eclipse.gef.commands.Command;
021: import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
022: import org.eclipse.gef.handles.NonResizableHandleKit;
023: import org.eclipse.gef.requests.DirectEditRequest;
024: import org.eclipse.gef.tools.DirectEditManager;
025: import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
026: import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus;
027: import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus;
028: import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
029: import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService;
030: import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
031: import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
032: import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
033: import org.eclipse.gmf.runtime.diagram.ui.editpolicies.LabelDirectEditPolicy;
034: import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramColorRegistry;
035: import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
036: import org.eclipse.gmf.runtime.diagram.ui.tools.TextDirectEditManager;
037: import org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel;
038: import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
039: import org.eclipse.gmf.runtime.emf.type.core.IElementType;
040: import org.eclipse.gmf.runtime.emf.ui.services.parser.ISemanticParser;
041: import org.eclipse.gmf.runtime.emf.ui.services.parser.ParserHintAdapter;
042: import org.eclipse.gmf.runtime.notation.FontStyle;
043: import org.eclipse.gmf.runtime.notation.NotationPackage;
044: import org.eclipse.gmf.runtime.notation.View;
045: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
046: import org.eclipse.jface.viewers.ICellEditorValidator;
047: import org.eclipse.swt.accessibility.AccessibleEvent;
048: import org.eclipse.swt.graphics.Color;
049: import org.eclipse.swt.graphics.FontData;
050: import org.eclipse.swt.graphics.Image;
051:
052: /**
053: * @generated
054: */
055: public class SyncActivityNameEditPart extends CompartmentEditPart
056: implements ITextAwareEditPart {
057:
058: /**
059: * @generated
060: */
061: public static final int VISUAL_ID = 5001;
062:
063: /**
064: * @generated
065: */
066: private DirectEditManager manager;
067:
068: /**
069: * @generated
070: */
071: private IParser parser;
072:
073: /**
074: * @generated
075: */
076: private List parserElements;
077:
078: /**
079: * @generated
080: */
081: private String defaultText;
082:
083: /**
084: * @generated
085: */
086: public SyncActivityNameEditPart(View view) {
087: super (view);
088: }
089:
090: /**
091: * @generated
092: */
093: protected void createDefaultEditPolicies() {
094: super .createDefaultEditPolicies();
095: installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
096: new LabelDirectEditPolicy());
097: installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE,
098: new NonResizableEditPolicy() {
099:
100: protected List createSelectionHandles() {
101: List handles = new ArrayList();
102: NonResizableHandleKit.addMoveHandle(
103: (GraphicalEditPart) getHost(), handles);
104: return handles;
105: }
106:
107: public Command getCommand(Request request) {
108: return null;
109: }
110:
111: public boolean understandsRequest(Request request) {
112: return false;
113: }
114: });
115: }
116:
117: /**
118: * @generated
119: */
120: protected String getLabelTextHelper(IFigure figure) {
121: if (figure instanceof WrapLabel) {
122: return ((WrapLabel) figure).getText();
123: } else {
124: return ((Label) figure).getText();
125: }
126: }
127:
128: /**
129: * @generated
130: */
131: protected void setLabelTextHelper(IFigure figure, String text) {
132: if (figure instanceof WrapLabel) {
133: ((WrapLabel) figure).setText(text);
134: } else {
135: ((Label) figure).setText(text);
136: }
137: }
138:
139: /**
140: * @generated
141: */
142: protected Image getLabelIconHelper(IFigure figure) {
143: if (figure instanceof WrapLabel) {
144: return ((WrapLabel) figure).getIcon();
145: } else {
146: return ((Label) figure).getIcon();
147: }
148: }
149:
150: /**
151: * @generated
152: */
153: protected void setLabelIconHelper(IFigure figure, Image icon) {
154: if (figure instanceof WrapLabel) {
155: ((WrapLabel) figure).setIcon(icon);
156: } else {
157: ((Label) figure).setIcon(icon);
158: }
159: }
160:
161: /**
162: * @generated
163: */
164: public void setLabel(WrapLabel figure) {
165: unregisterVisuals();
166: setFigure(figure);
167: defaultText = getLabelTextHelper(figure);
168: registerVisuals();
169: refreshVisuals();
170: }
171:
172: /**
173: * @generated
174: */
175: protected List getModelChildren() {
176: return Collections.EMPTY_LIST;
177: }
178:
179: /**
180: * @generated
181: */
182: public IGraphicalEditPart getChildBySemanticHint(String semanticHint) {
183: return null;
184: }
185:
186: /**
187: * @generated
188: */
189: protected EObject getParserElement() {
190: EObject element = resolveSemanticElement();
191: return element != null ? element : (View) getModel();
192: }
193:
194: /**
195: * @generated
196: */
197: protected Image getLabelIcon() {
198: return null;
199: }
200:
201: /**
202: * @generated
203: */
204: protected String getLabelText() {
205: String text = null;
206: if (getParser() != null) {
207: text = getParser().getPrintString(
208: new EObjectAdapter(getParserElement()),
209: getParserOptions().intValue());
210: }
211: if (text == null || text.length() == 0) {
212: text = defaultText;
213: }
214: return text;
215: }
216:
217: /**
218: * @generated
219: */
220: public void setLabelText(String text) {
221: setLabelTextHelper(getFigure(), text);
222: Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
223: if (pdEditPolicy instanceof New_processTextSelectionEditPolicy) {
224: ((New_processTextSelectionEditPolicy) pdEditPolicy)
225: .refreshFeedback();
226: }
227: }
228:
229: /**
230: * @generated
231: */
232: public String getEditText() {
233: if (getParser() == null) {
234: return ""; //$NON-NLS-1$
235: }
236: return getParser().getEditString(
237: new EObjectAdapter(getParserElement()),
238: getParserOptions().intValue());
239: }
240:
241: /**
242: * @generated
243: */
244: protected boolean isEditable() {
245: return getParser() != null;
246: }
247:
248: /**
249: * @generated
250: */
251: public ICellEditorValidator getEditTextValidator() {
252: return new ICellEditorValidator() {
253:
254: public String isValid(final Object value) {
255: if (value instanceof String) {
256: final EObject element = getParserElement();
257: final IParser parser = getParser();
258: try {
259: IParserEditStatus valid = (IParserEditStatus) getEditingDomain()
260: .runExclusive(
261: new RunnableWithResult.Impl() {
262:
263: public void run() {
264: setResult(parser
265: .isValidEditString(
266: new EObjectAdapter(
267: element),
268: (String) value));
269: }
270: });
271: return valid.getCode() == ParserEditStatus.EDITABLE ? null
272: : valid.getMessage();
273: } catch (InterruptedException ie) {
274: ie.printStackTrace();
275: }
276: }
277:
278: // shouldn't get here
279: return null;
280: }
281: };
282: }
283:
284: /**
285: * @generated
286: */
287: public IContentAssistProcessor getCompletionProcessor() {
288: if (getParser() == null) {
289: return null;
290: }
291: return getParser().getCompletionProcessor(
292: new EObjectAdapter(getParserElement()));
293: }
294:
295: /**
296: * @generated
297: */
298: public ParserOptions getParserOptions() {
299: return ParserOptions.NONE;
300: }
301:
302: /**
303: * @generated
304: */
305: public IParser getParser() {
306: if (parser == null) {
307: String parserHint = ((View) getModel()).getType();
308: ParserHintAdapter hintAdapter = new ParserHintAdapter(
309: getParserElement(), parserHint) {
310:
311: public Object getAdapter(Class adapter) {
312: if (IElementType.class.equals(adapter)) {
313: return New_processElementTypes.SyncActivity_2001;
314: }
315: return super .getAdapter(adapter);
316: }
317: };
318: parser = ParserService.getInstance().getParser(hintAdapter);
319: }
320: return parser;
321: }
322:
323: /**
324: * @generated
325: */
326: protected DirectEditManager getManager() {
327: if (manager == null) {
328: setManager(new TextDirectEditManager(this ,
329: TextDirectEditManager.getTextCellEditorClass(this ),
330: New_processEditPartFactory
331: .getTextCellEditorLocator(this )));
332: }
333: return manager;
334: }
335:
336: /**
337: * @generated
338: */
339: protected void setManager(DirectEditManager manager) {
340: this .manager = manager;
341: }
342:
343: /**
344: * @generated
345: */
346: protected void performDirectEdit() {
347: getManager().show();
348: }
349:
350: /**
351: * @generated
352: */
353: protected void performDirectEdit(Point eventLocation) {
354: if (getManager().getClass() == TextDirectEditManager.class) {
355: ((TextDirectEditManager) getManager()).show(eventLocation
356: .getSWTPoint());
357: }
358: }
359:
360: /**
361: * @generated
362: */
363: private void performDirectEdit(char initialCharacter) {
364: if (getManager() instanceof TextDirectEditManager) {
365: ((TextDirectEditManager) getManager())
366: .show(initialCharacter);
367: } else {
368: performDirectEdit();
369: }
370: }
371:
372: /**
373: * @generated
374: */
375: protected void performDirectEditRequest(Request request) {
376: final Request theRequest = request;
377: try {
378: getEditingDomain().runExclusive(new Runnable() {
379:
380: public void run() {
381: if (isActive() && isEditable()) {
382: if (theRequest
383: .getExtendedData()
384: .get(
385: RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR) instanceof Character) {
386: Character initialChar = (Character) theRequest
387: .getExtendedData()
388: .get(
389: RequestConstants.REQ_DIRECTEDIT_EXTENDEDDATA_INITIAL_CHAR);
390: performDirectEdit(initialChar.charValue());
391: } else if ((theRequest instanceof DirectEditRequest)
392: && (getEditText()
393: .equals(getLabelText()))) {
394: DirectEditRequest editRequest = (DirectEditRequest) theRequest;
395: performDirectEdit(editRequest.getLocation());
396: } else {
397: performDirectEdit();
398: }
399: }
400: }
401: });
402: } catch (InterruptedException e) {
403: e.printStackTrace();
404: }
405: }
406:
407: /**
408: * @generated
409: */
410: protected void refreshVisuals() {
411: super .refreshVisuals();
412: refreshLabel();
413: refreshFont();
414: refreshFontColor();
415: refreshUnderline();
416: refreshStrikeThrough();
417: }
418:
419: /**
420: * @generated
421: */
422: protected void refreshLabel() {
423: setLabelTextHelper(getFigure(), getLabelText());
424: setLabelIconHelper(getFigure(), getLabelIcon());
425: Object pdEditPolicy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
426: if (pdEditPolicy instanceof New_processTextSelectionEditPolicy) {
427: ((New_processTextSelectionEditPolicy) pdEditPolicy)
428: .refreshFeedback();
429: }
430: }
431:
432: /**
433: * @generated
434: */
435: protected void refreshUnderline() {
436: FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle(
437: NotationPackage.eINSTANCE.getFontStyle());
438: if (style != null && getFigure() instanceof WrapLabel) {
439: ((WrapLabel) getFigure()).setTextUnderline(style
440: .isUnderline());
441: }
442: }
443:
444: /**
445: * @generated
446: */
447: protected void refreshStrikeThrough() {
448: FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle(
449: NotationPackage.eINSTANCE.getFontStyle());
450: if (style != null && getFigure() instanceof WrapLabel) {
451: ((WrapLabel) getFigure()).setTextStrikeThrough(style
452: .isStrikeThrough());
453: }
454: }
455:
456: /**
457: * @generated
458: */
459: protected void refreshFont() {
460: FontStyle style = (FontStyle) getFontStyleOwnerView().getStyle(
461: NotationPackage.eINSTANCE.getFontStyle());
462: if (style != null) {
463: FontData fontData = new FontData(
464: style.getFontName(),
465: style.getFontHeight(),
466: (style.isBold() ? org.eclipse.swt.SWT.BOLD
467: : org.eclipse.swt.SWT.NORMAL)
468: | (style.isItalic() ? org.eclipse.swt.SWT.ITALIC
469: : org.eclipse.swt.SWT.NORMAL));
470: setFont(fontData);
471: }
472: }
473:
474: /**
475: * @generated
476: */
477: protected void setFontColor(Color color) {
478: getFigure().setForegroundColor(color);
479: }
480:
481: /**
482: * @generated
483: */
484: protected void addSemanticListeners() {
485: if (getParser() instanceof ISemanticParser) {
486: EObject element = resolveSemanticElement();
487: parserElements = ((ISemanticParser) getParser())
488: .getSemanticElementsBeingParsed(element);
489: for (int i = 0; i < parserElements.size(); i++) {
490: addListenerFilter(
491: "SemanticModel" + i, this , (EObject) parserElements.get(i)); //$NON-NLS-1$
492: }
493: } else {
494: super .addSemanticListeners();
495: }
496: }
497:
498: /**
499: * @generated
500: */
501: protected void removeSemanticListeners() {
502: if (parserElements != null) {
503: for (int i = 0; i < parserElements.size(); i++) {
504: removeListenerFilter("SemanticModel" + i); //$NON-NLS-1$
505: }
506: } else {
507: super .removeSemanticListeners();
508: }
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
551: */
552: protected void handleNotificationEvent(Notification event) {
553: Object feature = event.getFeature();
554: if (NotationPackage.eINSTANCE.getFontStyle_FontColor().equals(
555: feature)) {
556: Integer c = (Integer) event.getNewValue();
557: setFontColor(DiagramColorRegistry.getInstance().getColor(c));
558: } else if (NotationPackage.eINSTANCE.getFontStyle_Underline()
559: .equals(feature)) {
560: refreshUnderline();
561: } else if (NotationPackage.eINSTANCE
562: .getFontStyle_StrikeThrough().equals(feature)) {
563: refreshStrikeThrough();
564: } else if (NotationPackage.eINSTANCE.getFontStyle_FontHeight()
565: .equals(feature)
566: || NotationPackage.eINSTANCE.getFontStyle_FontName()
567: .equals(feature)
568: || NotationPackage.eINSTANCE.getFontStyle_Bold()
569: .equals(feature)
570: || NotationPackage.eINSTANCE.getFontStyle_Italic()
571: .equals(feature)) {
572: refreshFont();
573: } else {
574: if (getParser() != null
575: && getParser().isAffectingEvent(event,
576: getParserOptions().intValue())) {
577: refreshLabel();
578: }
579: if (getParser() instanceof ISemanticParser) {
580: ISemanticParser modelParser = (ISemanticParser) getParser();
581: if (modelParser
582: .areSemanticElementsAffected(null, event)) {
583: removeSemanticListeners();
584: if (resolveSemanticElement() != null) {
585: addSemanticListeners();
586: }
587: refreshLabel();
588: }
589: }
590: }
591: super .handleNotificationEvent(event);
592: }
593:
594: /**
595: * @generated
596: */
597: protected IFigure createFigure() {
598: // Parent should assign one using setLabel() method
599: return null;
600: }
601: }
|