001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.edit.parts;
025:
026: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.MessageExchange;
027: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.componentflowPackage;
028:
029: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.providers.ComponentFlowEditorAbstractParser;
030:
031: import java.text.MessageFormat;
032: import java.text.ParseException;
033:
034: import org.eclipse.draw2d.Connection;
035: import org.eclipse.draw2d.ConnectionLocator;
036: import org.eclipse.draw2d.IFigure;
037: import org.eclipse.draw2d.Label;
038:
039: import org.eclipse.draw2d.geometry.Point;
040: import org.eclipse.draw2d.geometry.Rectangle;
041:
042: import org.eclipse.emf.ecore.EAttribute;
043:
044: import org.eclipse.emf.edit.command.SetCommand;
045:
046: import org.eclipse.emf.transaction.TransactionalEditingDomain;
047:
048: import org.eclipse.emf.transaction.util.TransactionUtil;
049:
050: import org.eclipse.gef.EditPart;
051: import org.eclipse.gef.EditPolicy;
052: import org.eclipse.gef.GraphicalEditPart;
053: import org.eclipse.gef.Request;
054: import org.eclipse.gef.RequestConstants;
055:
056: import org.eclipse.gef.commands.Command;
057: import org.eclipse.gef.commands.UnexecutableCommand;
058:
059: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
060:
061: import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
062: import org.eclipse.gef.editpolicies.DirectEditPolicy;
063:
064: import org.eclipse.gef.requests.DirectEditRequest;
065:
066: import org.eclipse.gef.tools.CellEditorLocator;
067: import org.eclipse.gef.tools.DirectEditManager;
068:
069: import org.eclipse.gmf.runtime.notation.FontStyle;
070: import org.eclipse.gmf.runtime.notation.Location;
071: import org.eclipse.gmf.runtime.notation.Node;
072: import org.eclipse.gmf.runtime.notation.NotationPackage;
073: import org.eclipse.gmf.runtime.notation.View;
074:
075: import org.eclipse.jface.viewers.CellEditor;
076: import org.eclipse.jface.viewers.TextCellEditor;
077:
078: import org.eclipse.swt.SWT;
079:
080: import org.eclipse.swt.graphics.Color;
081: import org.eclipse.swt.graphics.Font;
082: import org.eclipse.swt.graphics.FontData;
083:
084: /**
085: * @generated
086: */
087: public class MessageExchangeLabelEditPart extends
088: AbstractGraphicalEditPart {
089:
090: /**
091: * @generated
092: */
093: public static final int VISUAL_ID = 6001;
094:
095: /**
096: * @generated
097: */
098: private DirectEditManager manager;
099:
100: /**
101: * @generated
102: */
103: private String defaultText;
104:
105: /**
106: * @generated
107: */
108: public MessageExchangeLabelEditPart(View view) {
109: assert view instanceof Node;
110: setModel(view);
111: }
112:
113: /**
114: * @generated
115: */
116: private Node getDiagramNode() {
117: return (Node) getModel();
118: }
119:
120: /**
121: * @generated
122: */
123: protected void createEditPolicies() {
124: installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
125: new ConnectionEndpointEditPolicy() {
126: public EditPart getHost() {
127: return getUpdatableParent();
128: }
129: });
130: installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
131: new DirectEditPolicy() {
132: protected void showCurrentEditValue(
133: DirectEditRequest request) {
134: String value = (String) request.getCellEditor()
135: .getValue();
136: getLabel().setText(value);
137: }
138:
139: protected Command getDirectEditCommand(
140: DirectEditRequest request) {
141: String value = (String) request.getCellEditor()
142: .getValue();
143: if (value == null) {
144: //Invalid value is transformed into a null by the validator.
145: //XXX: implement validator
146: return UnexecutableCommand.INSTANCE;
147: }
148: final Object[] parseResult;
149: try {
150: parseResult = new MessageFormat(
151: EDIT_PATTERN).parse(value);
152: } catch (IllegalArgumentException e) {
153: return UnexecutableCommand.INSTANCE;
154: } catch (ParseException e) {
155: return UnexecutableCommand.INSTANCE;
156: }
157: TransactionalEditingDomain editingDomain = TransactionUtil
158: .getEditingDomain(getUpdatableParent()
159: .getDiagramEdge().getDiagram()
160: .getElement());
161: if (parseResult.length != 1) {
162: return UnexecutableCommand.INSTANCE;
163: }
164: org.eclipse.emf.common.command.Command domainModelCommand = createDomainModelCommand(
165: editingDomain, parseResult[0]);
166: return new WrappingCommand(editingDomain,
167: domainModelCommand);
168: }
169:
170: org.eclipse.emf.common.command.Command createDomainModelCommand(
171: TransactionalEditingDomain editingDomain,
172: Object value) {
173: MessageExchange element = resolveSemanticElement();
174: EAttribute feature = (EAttribute) componentflowPackage.eINSTANCE
175: .getMessageExchange_Label();
176: Object valueToSet;
177: try {
178: valueToSet = ComponentFlowEditorAbstractParser
179: .parseValue(feature, value);
180: } catch (IllegalArgumentException e) {
181: return org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE;
182: }
183: return SetCommand.create(editingDomain,
184: element, feature, valueToSet);
185: }
186: });
187: }
188:
189: /**
190: * @generated
191: */
192: public void performRequest(Request req) {
193: if (RequestConstants.REQ_DIRECT_EDIT == req.getType()) {
194: performDirectEdit();
195: } else {
196: super .performRequest(req);
197: }
198: }
199:
200: /**
201: * @generated
202: */
203: protected DirectEditManager getManager() {
204: if (manager == null) {
205: manager = new DirectEditManager(this , TextCellEditor.class,
206: new CellEditorLocator() {
207: public void relocate(CellEditor celleditor) {
208: Rectangle rect = getLabel().getTextBounds();
209: getLabel().translateToAbsolute(rect);
210: celleditor.getControl().setBounds(rect.x,
211: rect.y, rect.width, rect.height);
212: }
213: }) {
214: protected void initCellEditor() {
215: getCellEditor().setValue(getLabelEditText());
216: }
217: };
218: }
219: return manager;
220: }
221:
222: /**
223: * @generated
224: */
225: protected void performDirectEdit() {
226: getManager().show();
227: }
228:
229: /**
230: * @generated
231: */
232: protected void refreshVisuals() {
233: super .refreshVisuals();
234: refreshLabel();
235: refreshFont();
236: refreshFontColor();
237: refreshBounds();
238: }
239:
240: /**
241: * @generated
242: */
243: protected void refreshBounds() {
244: Node node = getDiagramNode();
245: assert node.getLayoutConstraint() instanceof Location;
246: final Location location = (Location) node.getLayoutConstraint();
247: MessageExchangeEditPart parent = getUpdatableParent();
248: Connection connection = (Connection) parent.getFigure();
249: ((GraphicalEditPart) getParent()).setLayoutConstraint(this ,
250: getFigure(), new ConnectionLocator(connection,
251: ConnectionLocator.MIDDLE) {
252: protected Point getReferencePoint() {
253: return super .getReferencePoint().translate(
254: location.getX(), location.getY());
255: }
256: });
257: }
258:
259: /**
260: * @generated
261: */
262: protected void refreshLabel() {
263: getLabel().setText(getLabelText());
264: }
265:
266: /**
267: * @generated
268: */
269: private static final String VIEW_PATTERN = "{0}";
270:
271: /**
272: * @generated
273: */
274: private static final String EDIT_PATTERN = "{0}";
275:
276: /**
277: * @generated
278: */
279: protected String getLabelText() {
280: MessageExchange element = resolveSemanticElement();
281: if (element == null) {
282: return defaultText;
283: }
284: return buildTextByPattern(element, VIEW_PATTERN);
285: }
286:
287: /**
288: * @generated
289: */
290: protected String getLabelEditText() {
291: MessageExchange element = resolveSemanticElement();
292: if (element == null) {
293: return defaultText;
294: }
295: return buildTextByPattern(element, EDIT_PATTERN);
296: }
297:
298: /**
299: * @generated
300: */
301: protected String buildTextByPattern(MessageExchange element,
302: String pattern) {
303: if (element.getLabel() == null) {
304: return defaultText;
305: }
306: return MessageFormat.format(pattern, new Object[] { element
307: .getLabel() });
308: }
309:
310: /**
311: * @generated
312: */
313: protected void refreshFont() {
314: FontStyle style = (FontStyle) getUpdatableParent()
315: .getDiagramEdge().getStyle(
316: NotationPackage.eINSTANCE.getFontStyle());
317: Font toDispose = createdFont;
318: if (style != null) {
319: String fontName = style.getFontName();
320: int fontHeight = style.getFontHeight();
321: int fontStyle = SWT.NORMAL;
322: if (style.isBold()) {
323: fontStyle |= SWT.BOLD;
324: }
325: if (style.isItalic()) {
326: fontStyle |= SWT.ITALIC;
327: }
328: Font currentFont = getFigure().getFont();
329: if (currentFont != null) {
330: FontData currentFontData = currentFont.getFontData()[0];
331: if (currentFontData.getName().equals(fontName)
332: && currentFontData.getHeight() == fontHeight
333: && currentFontData.getStyle() == fontStyle) {
334: return;
335: }
336: }
337: createdFont = new Font(null, fontName, fontHeight,
338: fontStyle);
339: getFigure().setFont(createdFont);
340: } else {
341: //revert to the default font
342: getFigure().setFont(getViewer().getControl().getFont());
343: createdFont = null;
344: }
345: if (toDispose != null) {
346: toDispose.dispose();
347: }
348: }
349:
350: /**
351: * The font (created by {@link #refreshFont()}) currently assigned to the label (unless the default font is assigned).
352: * Whenever another non-default font is assigned to it, it is safe to dispose the previous one.
353: * @generated
354: */
355: private Font createdFont;
356:
357: /**
358: * @generated
359: */
360: protected void refreshFontColor() {
361: FontStyle style = (FontStyle) getUpdatableParent()
362: .getDiagramEdge().getStyle(
363: NotationPackage.eINSTANCE.getFontStyle());
364: Color toDispose = createdFontColor;
365: if (style != null) {
366: int fontColor = style.getFontColor();
367: int red = fontColor & 0x000000FF;
368: int green = (fontColor & 0x0000FF00) >> 8;
369: int blue = (fontColor & 0x00FF0000) >> 16;
370: Color currentColor = getLabel().getForegroundColor();
371: if (currentColor != null && currentColor.getRed() == red
372: && currentColor.getGreen() == green
373: && currentColor.getBlue() == blue) {
374: return;
375: }
376: createdFontColor = new Color(null, red, green, blue);
377: getFigure().setForegroundColor(createdFontColor);
378: } else {
379: getFigure().setForegroundColor(
380: getViewer().getControl().getForeground());
381: createdFontColor = null;
382: }
383: if (toDispose != null) {
384: toDispose.dispose();
385: }
386: }
387:
388: /**
389: * The color (created by {@link #refreshFontColor()}) currently assigned to the label.
390: * Whenever another color is assigned to it, it is safe to dispose the previous one.
391: * @generated
392: */
393: private Color createdFontColor;
394:
395: /**
396: * @generated
397: */
398: private MessageExchange resolveSemanticElement() {
399: for (EditPart editPart = this ; editPart != null; editPart = editPart
400: .getParent()) {
401: View view = (View) editPart.getModel();
402: if (view != null && view.getElement() != null) {
403: return (MessageExchange) view.getElement();
404: }
405: }
406: return null;
407: }
408:
409: /**
410: * @generated
411: */
412: private MessageExchangeEditPart getUpdatableParent() {
413: for (EditPart editPart = getParent(); editPart != null; editPart = editPart
414: .getParent()) {
415: if (editPart instanceof MessageExchangeEditPart) {
416: return (MessageExchangeEditPart) editPart;
417: }
418: }
419: return null;
420: }
421:
422: /**
423: * @generated
424: */
425: public void activate() {
426: super .activate();
427: MessageExchangeEditPart updatableParent = getUpdatableParent();
428: if (updatableParent != null) {
429: updatableParent.addRefresher(NotationPackage.eINSTANCE
430: .getFontStyle_FontColor(), fontColorRefresher);
431: updatableParent.addRefresher(NotationPackage.eINSTANCE
432: .getFontStyle_FontHeight(), fontRefresher);
433: updatableParent.addRefresher(NotationPackage.eINSTANCE
434: .getFontStyle_FontName(), fontRefresher);
435: updatableParent.addRefresher(NotationPackage.eINSTANCE
436: .getFontStyle_Bold(), fontRefresher);
437: updatableParent.addRefresher(NotationPackage.eINSTANCE
438: .getFontStyle_Italic(), fontRefresher);
439: updatableParent.addRefresher(componentflowPackage.eINSTANCE
440: .getMessageExchange_Label(), labelRefresher);
441: }
442: }
443:
444: /**
445: * @generated
446: */
447: public void deactivate() {
448: super .deactivate();
449: MessageExchangeEditPart updatableParent = getUpdatableParent();
450: if (updatableParent != null) {
451: updatableParent.removeRefresher(NotationPackage.eINSTANCE
452: .getFontStyle_FontColor(), fontColorRefresher);
453: updatableParent.removeRefresher(NotationPackage.eINSTANCE
454: .getFontStyle_FontHeight(), fontRefresher);
455: updatableParent.removeRefresher(NotationPackage.eINSTANCE
456: .getFontStyle_FontName(), fontRefresher);
457: updatableParent.removeRefresher(NotationPackage.eINSTANCE
458: .getFontStyle_Bold(), fontRefresher);
459: updatableParent.removeRefresher(NotationPackage.eINSTANCE
460: .getFontStyle_Italic(), fontRefresher);
461: updatableParent
462: .removeRefresher(componentflowPackage.eINSTANCE
463: .getMessageExchange_Label(), labelRefresher);
464: }
465: }
466:
467: /**
468: * @generated
469: */
470: private IUpdatableEditPart.Refresher labelRefresher = new IUpdatableEditPart.Refresher() {
471: public void refresh() {
472: refreshLabel();
473: }
474: };
475:
476: /**
477: * @generated
478: */
479: private IUpdatableEditPart.Refresher fontColorRefresher = new IUpdatableEditPart.Refresher() {
480: public void refresh() {
481: refreshFontColor();
482: }
483: };
484:
485: /**
486: * @generated
487: */
488: private IUpdatableEditPart.Refresher fontRefresher = new IUpdatableEditPart.Refresher() {
489: public void refresh() {
490: refreshFont();
491: }
492: };
493:
494: /**
495: * @generated
496: */
497: protected OrderFigure createLabel() {
498: return new OrderFigure();
499: }
500:
501: /**
502: * @generated
503: */
504: protected IFigure createFigure() {
505: OrderFigure label = createLabel();
506: defaultText = label.getText();
507: return label;
508: }
509:
510: /**
511: * @generated
512: */
513: public Label getLabel() {
514: return (Label) getFigure();
515: }
516:
517: /**
518: * @generated
519: */
520: public void setLabel(IFigure figure) {
521: unregisterVisuals();
522: setFigure(figure);
523: defaultText = ""; //$NON-NLS-1$
524: registerVisuals();
525: refreshVisuals();
526: }
527:
528: /**
529: * @generated
530: */
531: public class OrderFigure extends org.eclipse.draw2d.Label {
532:
533: /**
534: * @generated
535: */
536: public OrderFigure() {
537:
538: org.eclipse.draw2d.StackLayout myGenLayoutManager = new org.eclipse.draw2d.StackLayout();
539:
540: this .setLayoutManager(myGenLayoutManager);
541:
542: this .setText("");
543: createContents();
544: }
545:
546: /**
547: * @generated
548: */
549: private void createContents() {
550: }
551:
552: /**
553: * @generated
554: */
555: private boolean myUseLocalCoordinates = false;
556:
557: /**
558: * @generated
559: */
560: protected boolean useLocalCoordinates() {
561: return myUseLocalCoordinates;
562: }
563:
564: /**
565: * @generated
566: */
567: protected void setUseLocalCoordinates(
568: boolean useLocalCoordinates) {
569: myUseLocalCoordinates = useLocalCoordinates;
570: }
571:
572: }
573:
574: }
|