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 java.text.MessageFormat;
027: import java.text.ParseException;
028:
029: import org.eclipse.draw2d.IFigure;
030: import org.eclipse.draw2d.Label;
031: import org.eclipse.draw2d.geometry.Rectangle;
032: import org.eclipse.emf.ecore.EAttribute;
033: import org.eclipse.emf.edit.command.SetCommand;
034: import org.eclipse.emf.transaction.TransactionalEditingDomain;
035: import org.eclipse.emf.transaction.util.TransactionUtil;
036: import org.eclipse.gef.EditPart;
037: import org.eclipse.gef.EditPolicy;
038: import org.eclipse.gef.Request;
039: import org.eclipse.gef.RequestConstants;
040: import org.eclipse.gef.commands.Command;
041: import org.eclipse.gef.commands.UnexecutableCommand;
042: import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
043: import org.eclipse.gef.editpolicies.DirectEditPolicy;
044: import org.eclipse.gef.requests.DirectEditRequest;
045: import org.eclipse.gef.tools.CellEditorLocator;
046: import org.eclipse.gef.tools.DirectEditManager;
047: import org.eclipse.gmf.runtime.notation.FontStyle;
048: import org.eclipse.gmf.runtime.notation.NotationPackage;
049: import org.eclipse.gmf.runtime.notation.View;
050: import org.eclipse.jface.resource.ImageDescriptor;
051:
052: import org.eclipse.jface.viewers.CellEditor;
053: import org.eclipse.jface.viewers.TextCellEditor;
054: import org.eclipse.swt.SWT;
055: import org.eclipse.swt.graphics.Color;
056: import org.eclipse.swt.graphics.Font;
057: import org.eclipse.swt.graphics.FontData;
058: import org.eclipse.swt.graphics.Image;
059:
060: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.Sequencer;
061: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.componentflowPackage;
062: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.part.ComponentFlowEditorDiagramEditorPlugin;
063:
064: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.providers.ComponentFlowEditorAbstractParser;
065:
066: /**
067: * @generated
068: */
069: public class SequencerNameEditPart extends AbstractGraphicalEditPart {
070:
071: /**
072: * @generated
073: */
074: public static final int VISUAL_ID = 5009;
075:
076: /**
077: * @generated
078: */
079: private DirectEditManager manager;
080:
081: /**
082: * @generated
083: */
084: private String defaultText;
085:
086: /**
087: * @generated
088: */
089: public SequencerNameEditPart(View view) {
090: setModel(view);
091: }
092:
093: /**
094: * @generated NOT
095: */
096: protected void createEditPolicies() {/*
097: installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new DirectEditPolicy() {
098: protected void showCurrentEditValue(DirectEditRequest request) {
099: String value = (String) request.getCellEditor().getValue();
100: getLabel().setText(value);
101: }
102:
103: protected Command getDirectEditCommand(DirectEditRequest request) {
104: String value = (String) request.getCellEditor().getValue();
105: if (value == null) {
106: //Invalid value is transformed into a null by the validator.
107: //XXX: implement validator
108: return UnexecutableCommand.INSTANCE;
109: }
110: final Object[] parseResult;
111: try {
112: parseResult = new MessageFormat(EDIT_PATTERN).parse(value);
113: } catch (IllegalArgumentException e) {
114: return UnexecutableCommand.INSTANCE;
115: } catch (ParseException e) {
116: return UnexecutableCommand.INSTANCE;
117: }
118: TransactionalEditingDomain editingDomain = TransactionUtil
119: .getEditingDomain(getUpdatableParent().getDiagramNode()
120: .getDiagram().getElement());
121: if (parseResult.length != 1) {
122: return UnexecutableCommand.INSTANCE;
123: }
124: org.eclipse.emf.common.command.Command domainModelCommand = createDomainModelCommand(
125: editingDomain, parseResult[0]);
126: return new WrappingCommand(editingDomain, domainModelCommand);
127: }
128:
129: org.eclipse.emf.common.command.Command createDomainModelCommand(
130: TransactionalEditingDomain editingDomain, Object value) {
131: Sequencer element = resolveSemanticElement();
132: EAttribute feature = (EAttribute) componentflowPackage.eINSTANCE
133: .getBaseElement_Name();
134: Object valueToSet;
135: try {
136: valueToSet = ComponentFlowEditorAbstractParser.parseValue(
137: feature, value);
138: } catch (IllegalArgumentException e) {
139: return org.eclipse.emf.common.command.UnexecutableCommand.INSTANCE;
140: }
141: return SetCommand.create(editingDomain, element, feature,
142: valueToSet);
143: }
144: });
145: */
146: }
147:
148: /**
149: * @generated
150: */
151: public void performRequest(Request req) {
152: if (RequestConstants.REQ_DIRECT_EDIT == req.getType()) {
153: performDirectEdit();
154: } else {
155: super .performRequest(req);
156: }
157: }
158:
159: /**
160: * @generated
161: */
162: protected DirectEditManager getManager() {
163: if (manager == null) {
164: manager = new DirectEditManager(this , TextCellEditor.class,
165: new CellEditorLocator() {
166: public void relocate(CellEditor celleditor) {
167: Rectangle rect = getLabel().getTextBounds();
168: getLabel().translateToAbsolute(rect);
169: celleditor.getControl().setBounds(rect.x,
170: rect.y, rect.width, rect.height);
171: }
172: }) {
173: protected void initCellEditor() {
174: getCellEditor().setValue(getLabelEditText());
175: }
176: };
177: }
178: return manager;
179: }
180:
181: /**
182: * @generated NOT
183: */
184: protected void performDirectEdit() {
185: //getManager().show();
186: }
187:
188: /**
189: * @generated
190: */
191: protected void refreshVisuals() {
192: super .refreshVisuals();
193: refreshLabel();
194: refreshFont();
195: refreshFontColor();
196: }
197:
198: /**
199: * @generated
200: */
201: protected void refreshLabel() {
202: getLabel().setText(getLabelText());
203: getLabel().setIcon(getLabelIcon());
204: }
205:
206: /**
207: * @generated
208: */
209: private static final String VIEW_PATTERN = "{0}";
210:
211: /**
212: * @generated
213: */
214: private static final String EDIT_PATTERN = "{0}";
215:
216: /**
217: * @generated
218: */
219: protected String getLabelText() {
220: Sequencer element = resolveSemanticElement();
221: if (element == null) {
222: return defaultText;
223: }
224: return buildTextByPattern(element, VIEW_PATTERN);
225: }
226:
227: /**
228: * @generated
229: */
230: protected String getLabelEditText() {
231: Sequencer element = resolveSemanticElement();
232: if (element == null) {
233: return defaultText;
234: }
235: return buildTextByPattern(element, EDIT_PATTERN);
236: }
237:
238: /**
239: * @generated
240: */
241: protected String buildTextByPattern(Sequencer element,
242: String pattern) {
243: if (element.getName() == null) {
244: return defaultText;
245: }
246: return MessageFormat.format(pattern, new Object[] { element
247: .getName() });
248: }
249:
250: /**
251: * @generated
252: */
253: protected void refreshFont() {
254: FontStyle style = (FontStyle) getUpdatableParent()
255: .getDiagramNode().getStyle(
256: NotationPackage.eINSTANCE.getFontStyle());
257: Font toDispose = createdFont;
258: if (style != null) {
259: String fontName = style.getFontName();
260: int fontHeight = style.getFontHeight();
261: int fontStyle = SWT.NORMAL;
262: if (style.isBold()) {
263: fontStyle |= SWT.BOLD;
264: }
265: if (style.isItalic()) {
266: fontStyle |= SWT.ITALIC;
267: }
268: Font currentFont = getFigure().getFont();
269: if (currentFont != null) {
270: FontData currentFontData = currentFont.getFontData()[0];
271: if (currentFontData.getName().equals(fontName)
272: && currentFontData.getHeight() == fontHeight
273: && currentFontData.getStyle() == fontStyle) {
274: return;
275: }
276: }
277: createdFont = new Font(null, fontName, fontHeight,
278: fontStyle);
279: getFigure().setFont(createdFont);
280: } else {
281: //revert to the default font
282: getFigure().setFont(getViewer().getControl().getFont());
283: createdFont = null;
284: }
285: if (toDispose != null) {
286: toDispose.dispose();
287: }
288: }
289:
290: /**
291: * The font (created by {@link #refreshFont()}) currently assigned to the label (unless the default font is assigned).
292: * Whenever another non-default font is assigned to it, it is safe to dispose the previous one.
293: * @generated
294: */
295: private Font createdFont;
296:
297: /**
298: * @generated
299: */
300: protected void refreshFontColor() {
301: FontStyle style = (FontStyle) getUpdatableParent()
302: .getDiagramNode().getStyle(
303: NotationPackage.eINSTANCE.getFontStyle());
304: Color toDispose = createdFontColor;
305: if (style != null) {
306: int fontColor = style.getFontColor();
307: int red = fontColor & 0x000000FF;
308: int green = (fontColor & 0x0000FF00) >> 8;
309: int blue = (fontColor & 0x00FF0000) >> 16;
310: Color currentColor = getLabel().getForegroundColor();
311: if (currentColor != null && currentColor.getRed() == red
312: && currentColor.getGreen() == green
313: && currentColor.getBlue() == blue) {
314: return;
315: }
316: createdFontColor = new Color(null, red, green, blue);
317: getFigure().setForegroundColor(createdFontColor);
318: } else {
319: getFigure().setForegroundColor(
320: getViewer().getControl().getForeground());
321: createdFontColor = null;
322: }
323: if (toDispose != null) {
324: toDispose.dispose();
325: }
326: }
327:
328: /**
329: * The color (created by {@link #refreshFontColor()}) currently assigned to the label.
330: * Whenever another color is assigned to it, it is safe to dispose the previous one.
331: * @generated
332: */
333: private Color createdFontColor;
334:
335: /**
336: * @generated NOT
337: */
338: protected Image getLabelIcon() {
339: /*Sequencer element = resolveSemanticElement();
340: ImageDescriptor imageDescriptor = ComponentFlowEditorDiagramEditorPlugin
341: .getInstance().getItemImageDescriptor(element);
342: if (imageDescriptor != null) {
343: return imageDescriptor.createImage();
344: }
345: return ImageDescriptor.getMissingImageDescriptor().createImage();*/
346: return null;
347: }
348:
349: /**
350: * @generated
351: */
352: private Sequencer resolveSemanticElement() {
353: for (EditPart editPart = this ; editPart != null; editPart = editPart
354: .getParent()) {
355: View view = (View) editPart.getModel();
356: if (view != null && view.getElement() != null) {
357: return (Sequencer) view.getElement();
358: }
359: }
360: return null;
361: }
362:
363: /**
364: * @generated
365: */
366: private SequencerEditPart getUpdatableParent() {
367: for (EditPart editPart = getParent(); editPart != null; editPart = editPart
368: .getParent()) {
369: if (editPart instanceof SequencerEditPart) {
370: return (SequencerEditPart) editPart;
371: }
372: }
373: return null;
374: }
375:
376: /**
377: * @generated
378: */
379: public void activate() {
380: super .activate();
381: SequencerEditPart updatableParent = getUpdatableParent();
382: if (updatableParent != null) {
383: updatableParent.addRefresher(NotationPackage.eINSTANCE
384: .getFontStyle_FontColor(), fontColorRefresher);
385: updatableParent.addRefresher(NotationPackage.eINSTANCE
386: .getFontStyle_FontHeight(), fontRefresher);
387: updatableParent.addRefresher(NotationPackage.eINSTANCE
388: .getFontStyle_FontName(), fontRefresher);
389: updatableParent.addRefresher(NotationPackage.eINSTANCE
390: .getFontStyle_Bold(), fontRefresher);
391: updatableParent.addRefresher(NotationPackage.eINSTANCE
392: .getFontStyle_Italic(), fontRefresher);
393: updatableParent.addRefresher(componentflowPackage.eINSTANCE
394: .getBaseElement_Name(), labelRefresher);
395: }
396: }
397:
398: /**
399: * @generated
400: */
401: public void deactivate() {
402: super .deactivate();
403: SequencerEditPart updatableParent = getUpdatableParent();
404: if (updatableParent != null) {
405: updatableParent.removeRefresher(NotationPackage.eINSTANCE
406: .getFontStyle_FontColor(), fontColorRefresher);
407: updatableParent.removeRefresher(NotationPackage.eINSTANCE
408: .getFontStyle_FontHeight(), fontRefresher);
409: updatableParent.removeRefresher(NotationPackage.eINSTANCE
410: .getFontStyle_FontName(), fontRefresher);
411: updatableParent.removeRefresher(NotationPackage.eINSTANCE
412: .getFontStyle_Bold(), fontRefresher);
413: updatableParent.removeRefresher(NotationPackage.eINSTANCE
414: .getFontStyle_Italic(), fontRefresher);
415: updatableParent.removeRefresher(
416: componentflowPackage.eINSTANCE
417: .getBaseElement_Name(), labelRefresher);
418: }
419: }
420:
421: /**
422: * @generated
423: */
424: private IUpdatableEditPart.Refresher labelRefresher = new IUpdatableEditPart.Refresher() {
425: public void refresh() {
426: refreshLabel();
427: }
428: };
429:
430: /**
431: * @generated
432: */
433: private IUpdatableEditPart.Refresher fontColorRefresher = new IUpdatableEditPart.Refresher() {
434: public void refresh() {
435: refreshFontColor();
436: }
437: };
438:
439: /**
440: * @generated
441: */
442: private IUpdatableEditPart.Refresher fontRefresher = new IUpdatableEditPart.Refresher() {
443: public void refresh() {
444: refreshFont();
445: }
446: };
447:
448: /**
449: * @generated
450: */
451: protected IFigure createFigure() {
452: // Parent should assign one using setLabel method
453: return null;
454: }
455:
456: /**
457: * @generated
458: */
459: public Label getLabel() {
460: return (Label) getFigure();
461: }
462:
463: /**
464: * @generated
465: */
466: public void setLabel(Label figure) {
467: unregisterVisuals();
468: setFigure(figure);
469: defaultText = figure.getText();
470: registerVisuals();
471: refreshVisuals();
472: }
473:
474: }
|