001: package newprocess.diagram.providers;
002:
003: import java.text.MessageFormat;
004: import java.text.ParsePosition;
005:
006: import org.eclipse.core.runtime.IAdaptable;
007: import org.eclipse.emf.ecore.EClassifier;
008: import org.eclipse.emf.ecore.EDataType;
009: import org.eclipse.emf.ecore.EEnum;
010: import org.eclipse.emf.ecore.EEnumLiteral;
011: import org.eclipse.emf.ecore.EObject;
012: import org.eclipse.emf.ecore.EStructuralFeature;
013: import org.eclipse.gmf.runtime.common.core.command.ICommand;
014: import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand;
015: import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
016: import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus;
017: import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus;
018: import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;
019: import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
020: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
021: import newprocess.diagram.part.New_processDiagramEditorPlugin;
022:
023: /**
024: * @generated
025: */
026: public abstract class New_processAbstractParser implements IParser {
027:
028: /**
029: * @generated
030: */
031: private String viewPattern;
032:
033: /**
034: * @generated
035: */
036: private MessageFormat viewProcessor;
037:
038: /**
039: * @generated
040: */
041: private String editPattern;
042:
043: /**
044: * @generated
045: */
046: private MessageFormat editProcessor;
047:
048: /**
049: * @generated
050: */
051: public String getViewPattern() {
052: return viewPattern;
053: }
054:
055: /**
056: * @generated
057: */
058: protected MessageFormat getViewProcessor() {
059: return viewProcessor;
060: }
061:
062: /**
063: * @generated
064: */
065: public void setViewPattern(String viewPattern) {
066: this .viewPattern = viewPattern;
067: viewProcessor = createViewProcessor(viewPattern);
068: }
069:
070: /**
071: * @generated
072: */
073: protected MessageFormat createViewProcessor(String viewPattern) {
074: return new MessageFormat(viewPattern);
075: }
076:
077: /**
078: * @generated
079: */
080: public String getEditPattern() {
081: return editPattern;
082: }
083:
084: /**
085: * @generated
086: */
087: protected MessageFormat getEditProcessor() {
088: return editProcessor;
089: }
090:
091: /**
092: * @generated
093: */
094: public void setEditPattern(String editPattern) {
095: this .editPattern = editPattern;
096: editProcessor = createEditProcessor(editPattern);
097: }
098:
099: /**
100: * @generated
101: */
102: protected MessageFormat createEditProcessor(String editPattern) {
103: return new MessageFormat(editPattern);
104: }
105:
106: /**
107: * @generated
108: */
109: public String getPrintString(IAdaptable adapter, int flags) {
110: return getStringByPattern(adapter, flags, getViewPattern(),
111: getViewProcessor());
112: }
113:
114: /**
115: * @generated
116: */
117: public String getEditString(IAdaptable adapter, int flags) {
118: return getStringByPattern(adapter, flags, getEditPattern(),
119: getEditProcessor());
120: }
121:
122: /**
123: * @generated
124: */
125: protected abstract String getStringByPattern(IAdaptable adapter,
126: int flags, String pattern, MessageFormat processor);
127:
128: /**
129: * @generated
130: */
131: public IParserEditStatus isValidEditString(IAdaptable element,
132: String editString) {
133: ParsePosition pos = new ParsePosition(0);
134: Object[] values = getEditProcessor().parse(editString, pos);
135: if (values == null) {
136: return new ParserEditStatus(
137: New_processDiagramEditorPlugin.ID,
138: IParserEditStatus.UNEDITABLE, "Invalid input at "
139: + pos.getErrorIndex());
140: }
141: return validateNewValues(values);
142: }
143:
144: /**
145: * @generated
146: */
147: protected IParserEditStatus validateNewValues(Object[] values) {
148: return ParserEditStatus.EDITABLE_STATUS;
149: }
150:
151: /**
152: * @generated
153: */
154: public ICommand getParseCommand(IAdaptable adapter,
155: String newString, int flags) {
156: Object[] values = getEditProcessor().parse(newString,
157: new ParsePosition(0));
158: if (values == null
159: || validateNewValues(values).getCode() != IParserEditStatus.EDITABLE) {
160: return UnexecutableCommand.INSTANCE;
161: }
162: return getParseCommand(adapter, values);
163: }
164:
165: /**
166: * @generated
167: */
168: protected abstract ICommand getParseCommand(IAdaptable adapter,
169: Object[] values);
170:
171: /**
172: * @generated
173: */
174: public IContentAssistProcessor getCompletionProcessor(
175: IAdaptable element) {
176: return null;
177: }
178:
179: /**
180: * @generated
181: */
182: protected ICommand getModificationCommand(EObject element,
183: EStructuralFeature feature, Object value) {
184: value = getValidNewValue(feature, value);
185: if (value instanceof InvalidValue) {
186: return UnexecutableCommand.INSTANCE;
187: }
188: SetRequest request = new SetRequest(element, feature, value);
189: return new SetValueCommand(request);
190: }
191:
192: /**
193: * @generated
194: */
195: protected Object getValidValue(EStructuralFeature feature,
196: Object value) {
197: EClassifier type = feature.getEType();
198: if (type instanceof EDataType) {
199: Class iClass = type.getInstanceClass();
200: if (String.class.equals(iClass)) {
201: if (value == null) {
202: value = ""; //$NON-NLS-1$
203: }
204: }
205: }
206: return value;
207: }
208:
209: /**
210: * @generated
211: */
212: protected Object getValidNewValue(EStructuralFeature feature,
213: Object value) {
214: EClassifier type = feature.getEType();
215: if (type instanceof EDataType) {
216: Class iClass = type.getInstanceClass();
217: if (Boolean.TYPE.equals(iClass)) {
218: if (value instanceof Boolean) {
219: // ok
220: } else if (value instanceof String) {
221: value = Boolean.valueOf((String) value);
222: } else {
223: value = new InvalidValue(
224: "Value of type Boolean is expected");
225: }
226: } else if (Character.TYPE.equals(iClass)) {
227: if (value instanceof Character) {
228: // ok
229: } else if (value instanceof String) {
230: String s = (String) value;
231: if (s.length() == 0) {
232: value = null;
233: } else {
234: value = new Character(s.charAt(0));
235: }
236: } else {
237: value = new InvalidValue(
238: "Value of type Character is expected");
239: }
240: } else if (Byte.TYPE.equals(iClass)) {
241: if (value instanceof Byte) {
242: // ok
243: } else if (value instanceof Number) {
244: value = new Byte(((Number) value).byteValue());
245: } else if (value instanceof String) {
246: String s = (String) value;
247: if (s.length() == 0) {
248: value = null;
249: } else {
250: try {
251: value = Byte.valueOf(s);
252: } catch (NumberFormatException nfe) {
253: value = new InvalidValue(
254: "String value does not convert to Byte value");
255: }
256: }
257: } else {
258: value = new InvalidValue(
259: "Value of type Byte is expected");
260: }
261: } else if (Short.TYPE.equals(iClass)) {
262: if (value instanceof Short) {
263: // ok
264: } else if (value instanceof Number) {
265: value = new Short(((Number) value).shortValue());
266: } else if (value instanceof String) {
267: String s = (String) value;
268: if (s.length() == 0) {
269: value = null;
270: } else {
271: try {
272: value = Short.valueOf(s);
273: } catch (NumberFormatException nfe) {
274: value = new InvalidValue(
275: "String value does not convert to Short value");
276: }
277: }
278: } else {
279: value = new InvalidValue(
280: "Value of type Short is expected");
281: }
282: } else if (Integer.TYPE.equals(iClass)) {
283: if (value instanceof Integer) {
284: // ok
285: } else if (value instanceof Number) {
286: value = new Integer(((Number) value).intValue());
287: } else if (value instanceof String) {
288: String s = (String) value;
289: if (s.length() == 0) {
290: value = null;
291: } else {
292: try {
293: value = Integer.valueOf(s);
294: } catch (NumberFormatException nfe) {
295: value = new InvalidValue(
296: "String value does not convert to Integer value");
297: }
298: }
299: } else {
300: value = new InvalidValue(
301: "Value of type Integer is expected");
302: }
303: } else if (Long.TYPE.equals(iClass)) {
304: if (value instanceof Long) {
305: // ok
306: } else if (value instanceof Number) {
307: value = new Long(((Number) value).longValue());
308: } else if (value instanceof String) {
309: String s = (String) value;
310: if (s.length() == 0) {
311: value = null;
312: } else {
313: try {
314: value = Long.valueOf(s);
315: } catch (NumberFormatException nfe) {
316: value = new InvalidValue(
317: "String value does not convert to Long value");
318: }
319: }
320: } else {
321: value = new InvalidValue(
322: "Value of type Long is expected");
323: }
324: } else if (Float.TYPE.equals(iClass)) {
325: if (value instanceof Float) {
326: // ok
327: } else if (value instanceof Number) {
328: value = new Float(((Number) value).floatValue());
329: } else if (value instanceof String) {
330: String s = (String) value;
331: if (s.length() == 0) {
332: value = null;
333: } else {
334: try {
335: value = Float.valueOf(s);
336: } catch (NumberFormatException nfe) {
337: value = new InvalidValue(
338: "String value does not convert to Float value");
339: }
340: }
341: } else {
342: value = new InvalidValue(
343: "Value of type Float is expected");
344: }
345: } else if (Double.TYPE.equals(iClass)) {
346: if (value instanceof Double) {
347: // ok
348: } else if (value instanceof Number) {
349: value = new Double(((Number) value).doubleValue());
350: } else if (value instanceof String) {
351: String s = (String) value;
352: if (s.length() == 0) {
353: value = null;
354: } else {
355: try {
356: value = Double.valueOf(s);
357: } catch (NumberFormatException nfe) {
358: value = new InvalidValue(
359: "String value does not convert to Double value");
360: }
361: }
362: } else {
363: value = new InvalidValue(
364: "Value of type Double is expected");
365: }
366: } else if (type instanceof EEnum) {
367: if (value instanceof String) {
368: EEnumLiteral literal = ((EEnum) type)
369: .getEEnumLiteralByLiteral((String) value);
370: if (literal == null) {
371: value = new InvalidValue("Unknown literal: "
372: + value);
373: } else {
374: value = literal.getInstance();
375: }
376: } else {
377: value = new InvalidValue(
378: "Value of type String is expected");
379: }
380: }
381: }
382: return value;
383: }
384:
385: /**
386: * @generated
387: */
388: protected class InvalidValue {
389:
390: /**
391: * @generated
392: */
393: private String description;
394:
395: /**
396: * @generated
397: */
398: public InvalidValue(String description) {
399: this .description = description;
400: }
401:
402: /**
403: * @generated
404: */
405: public String toString() {
406: return description;
407: }
408: }
409: }
|