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