Skip to main content
aboutsummaryrefslogblamecommitdiffstats
blob: 78202abc1e83fe137fcd2f00f148e9378ff6e35f (plain) (tree)











































































































































































































































































































































































































                                                                                                                                                                                                                                                   
/*
 * Copyright (c) 2007, 2008 Borland Software Corporation
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Dmitry Stadnik (Borland) - initial API and implementation
 *    Artem Tikhomirov (Borland) - [235113] alternative parser access
 *                                 [244419] custom parsers
 */

«IMPORT 'http://www.eclipse.org/gmf/2008/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/Ecore'»

«EXTENSION impl::parsers::i18n»

«DEFINE className FOR gmfgen::GenParsers»«defaultAbstractParserClassName()»«ENDDEFINE»
«DEFINE qualifiedClassName FOR gmfgen::GenParsers»«implPackageName».«EXPAND className»«ENDDEFINE»

«DEFINE Main FOR gmfgen::GenParsers-»
«EXPAND xpt::Common::copyright FOR editorGen-»
package «implPackageName»;

«EXPAND xpt::Common::generatedClassComment»
public abstract class «EXPAND className» implements org.eclipse.gmf.runtime.common.ui.services.parser.IParser {

	«EXPAND xpt::Common::generatedMemberComment»
	protected final org.eclipse.emf.ecore.EAttribute[] features;

	«EXPAND xpt::Common::generatedMemberComment»
	protected final org.eclipse.emf.ecore.EAttribute[] editableFeatures;

	«EXPAND xpt::Common::generatedMemberComment»
	private String viewPattern;

	«EXPAND xpt::Common::generatedMemberComment»
	private String editorPattern;

	«EXPAND xpt::Common::generatedMemberComment»
	private String editPattern;

	«EXPAND xpt::Common::generatedMemberComment»
	public «EXPAND className»(org.eclipse.emf.ecore.EAttribute[] features) {
		if (features == null || java.util.Arrays.asList(features).contains(null)) {
			throw new IllegalArgumentException();
		}
		this.editableFeatures = this.features = features;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public «EXPAND className»(org.eclipse.emf.ecore.EAttribute[] features,
			org.eclipse.emf.ecore.EAttribute[] editableFeatures) {
		if (features == null || java.util.Arrays.asList(features).contains(null)) {
			throw new IllegalArgumentException();
		}
		this.features = features;
		if (editableFeatures == null || java.util.Arrays.asList(editableFeatures).contains(null)) {
			throw new IllegalArgumentException();
		}
		this.editableFeatures = editableFeatures;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public String getViewPattern() {
		return viewPattern;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public void setViewPattern(String viewPattern) {
		this.viewPattern = viewPattern;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public String getEditorPattern() {
		return editorPattern;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public void setEditorPattern(String editorPattern) {
		this.editorPattern = editorPattern;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public String getEditPattern() {
		return editPattern;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public void setEditPattern(String editPattern) {
		this.editPattern = editPattern;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public boolean isAffectingEvent(Object event, int flags) {
		if (event instanceof org.eclipse.emf.common.notify.Notification) {
			return isAffectingFeature(((org.eclipse.emf.common.notify.Notification) event).getFeature());
		}
		return false;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected boolean isAffectingFeature(Object feature) {
		for (int i = 0; i < features.length; i++) {
			if (features[i] == feature) {
				return true;
			}
		}
		return false;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public org.eclipse.jface.text.contentassist.IContentAssistProcessor getCompletionProcessor(
			org.eclipse.core.runtime.IAdaptable element) {
		return null;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected Object[] getValues(org.eclipse.emf.ecore.EObject element) {
		Object[] values = new Object[features.length];
		for (int i = 0; i < features.length; i++) {
			values[i] = getValue(element, features[i]);
		}
		return values;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected Object[] getEditableValues(org.eclipse.emf.ecore.EObject element) {
		Object[] values = new Object[editableFeatures.length];
		for (int i = 0; i < editableFeatures.length; i++) {
			values[i] = getValue(element, editableFeatures[i]);
		}
		return values;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected Object getValue(org.eclipse.emf.ecore.EObject element, org.eclipse.emf.ecore.EAttribute feature) {
		Object value = element.eGet(feature);
		Class iClass = feature.getEAttributeType().getInstanceClass();
		if (String.class.equals(iClass)) {
			if (value == null) {
				value = ""; «EXPAND xpt::Common::nonNLS»
			}
		}
		return value;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected org.eclipse.gmf.runtime.common.core.command.ICommand getParseCommand(
			org.eclipse.core.runtime.IAdaptable adapter, Object[] values, int flags) {
		if (values == null || validateNewValues(values).getCode() !=
				org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus.EDITABLE) {
			return org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand.INSTANCE;
		}
		org.eclipse.emf.ecore.EObject element =
				(org.eclipse.emf.ecore.EObject) adapter.getAdapter(org.eclipse.emf.ecore.EObject.class);
		org.eclipse.emf.transaction.TransactionalEditingDomain editingDomain =
				org.eclipse.emf.transaction.util.TransactionUtil.getEditingDomain(element);
		if (editingDomain == null) {
			return org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand.INSTANCE;
		}
		org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand command =
				new org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand(
						editingDomain, "Set Values"); «EXPAND xpt::Common::nonNLS»
		for (int i = 0; i < values.length; i++) {
			command.compose(getModificationCommand(element, editableFeatures[i], values[i]));
		}
		return command;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected org.eclipse.gmf.runtime.common.core.command.ICommand getModificationCommand(
			org.eclipse.emf.ecore.EObject element, org.eclipse.emf.ecore.EAttribute feature, Object value) {
		value = getValidNewValue(feature, value);
		if (value instanceof InvalidValue) {
			return org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand.INSTANCE;
		}
		org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest request =
				new org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest(element, feature, value);
		return new org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand(request);
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus validateNewValues(Object[] values) {
		if (values.length != editableFeatures.length) {
			return org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus.UNEDITABLE_STATUS;
		}
		for (int i = 0; i < values.length; i++) {
			Object value = getValidNewValue(editableFeatures[i], values[i]);
			if (value instanceof InvalidValue) {
				return new org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus(
						«editorGen.plugin.getActivatorQualifiedClassName()».ID,
						org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus.UNEDITABLE,
						value.toString());
			}
		}
		return org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus.EDITABLE_STATUS;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected Object getValidNewValue(org.eclipse.emf.ecore.EAttribute feature, Object value) {
		org.eclipse.emf.ecore.EClassifier type = feature.getEType();
		if (type instanceof org.eclipse.emf.ecore.EDataType) {
			Class iClass = type.getInstanceClass();
			if (Boolean.TYPE.equals(iClass)) {
				if (value instanceof Boolean) {
					// ok
				} else if (value instanceof String) {
					value = Boolean.valueOf((String) value);
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Character.TYPE.equals(iClass)) {
				if (value instanceof Character) {
					// ok
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						value = new Character(s.charAt(0));
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Byte.TYPE.equals(iClass)) {
				if (value instanceof Byte) {
					// ok
				} else if (value instanceof Number) {
					value = new Byte(((Number) value).byteValue());
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						try {
							value = Byte.valueOf(s);
						} catch (NumberFormatException nfe) {
							value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserWrongStringConversionMessage()) FOR editorGen», iClass.getName()));
						}
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Short.TYPE.equals(iClass)) {
				if (value instanceof Short) {
					// ok
				} else if (value instanceof Number) {
					value = new Short(((Number) value).shortValue());
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						try {
							value = Short.valueOf(s);
						} catch (NumberFormatException nfe) {
							value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserWrongStringConversionMessage()) FOR editorGen», iClass.getName()));
						}
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Integer.TYPE.equals(iClass)) {
				if (value instanceof Integer) {
					// ok
				} else if (value instanceof Number) {
					value = new Integer(((Number) value).intValue());
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						try {
							value = Integer.valueOf(s);
						} catch (NumberFormatException nfe) {
							value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserWrongStringConversionMessage()) FOR editorGen», iClass.getName()));
						}
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Long.TYPE.equals(iClass)) {
				if (value instanceof Long) {
					// ok
				} else if (value instanceof Number) {
					value = new Long(((Number) value).longValue());
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						try {
							value = Long.valueOf(s);
						} catch (NumberFormatException nfe) {
							value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserWrongStringConversionMessage()) FOR editorGen», iClass.getName()));
						}
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Float.TYPE.equals(iClass)) {
				if (value instanceof Float) {
					// ok
				} else if (value instanceof Number) {
					value = new Float(((Number) value).floatValue());
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						try {
							value = Float.valueOf(s);
						} catch (NumberFormatException nfe) {
							value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserWrongStringConversionMessage()) FOR editorGen», iClass.getName()));
						}
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (Double.TYPE.equals(iClass)) {
				if (value instanceof Double) {
					// ok
				} else if (value instanceof Number) {
					value = new Double(((Number) value).doubleValue());
				} else if (value instanceof String) {
					String s = (String) value;
					if (s.length() == 0) {
						value = null;
					} else {
						try {
							value = Double.valueOf(s);
						} catch (NumberFormatException nfe) {
							value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserWrongStringConversionMessage()) FOR editorGen», iClass.getName()));
						}
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», iClass.getName()));
				}
			} else if (type instanceof org.eclipse.emf.ecore.EEnum) {
				if (value instanceof String) {
					org.eclipse.emf.ecore.EEnumLiteral literal =
							((org.eclipse.emf.ecore.EEnum) type).getEEnumLiteralByLiteral((String) value);
					if (literal == null) {
						value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnknownLiteralMessage()) FOR editorGen», value));
					} else {
						value = literal.getInstance();
					}
				} else {
					value = new InvalidValue(org.eclipse.osgi.util.NLS.bind(«EXPAND xpt::Externalizer::accessorCall(i18nKeyForAbstractParserUnexpectedValueTypeMessage()) FOR editorGen», String.class.getName()));
				}
			}
		}
		return value;
	}

	«EXPAND xpt::Common::generatedMemberComment»
	protected class InvalidValue {

		«EXPAND xpt::Common::generatedMemberComment»
		private String description;

		«EXPAND xpt::Common::generatedMemberComment»
		public InvalidValue(String description) {
			this.description = description;
		}

		«EXPAND xpt::Common::generatedMemberComment»
		public String toString() {
			return description;
		}
	}
	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenParsers»«ENDDEFINE»

«DEFINE i18nValues FOR gmfgen::GenParsers-»
«IF (implementations)[gmfgen::PredefinedParser]->size() > 0-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForAbstractParserUnexpectedValueTypeMessage(), 'Value of type {0} is expected')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForAbstractParserWrongStringConversionMessage(), 'String value does not convert to {0} value')-»
«EXPAND xpt::Externalizer::messageEntry(i18nKeyForAbstractParserUnknownLiteralMessage(), 'Unknown literal: {0}')-»
«ENDIF-»
«ENDDEFINE»

«DEFINE i18nAccessors FOR gmfgen::GenParsers-»
«IF (implementations)[gmfgen::PredefinedParser]->size() > 0-»
«EXPAND xpt::Externalizer::accessorField(i18nKeyForAbstractParserUnexpectedValueTypeMessage())-»
«EXPAND xpt::Externalizer::accessorField(i18nKeyForAbstractParserWrongStringConversionMessage())-»
«EXPAND xpt::Externalizer::accessorField(i18nKeyForAbstractParserUnknownLiteralMessage())-»
«ENDIF-»
«ENDDEFINE»

Back to the top