/* * MessageFormatParser.java * * This file is part of the STS-Tool project. * Copyright (c) 2011-2012 "University of Trento - DISI" All rights reserved. * * Is strictly forbidden to remove this copyright notice from this source code. * * Disclaimer of Warranty: * STS-Tool (this software) is provided "as-is" and without warranty of any kind, * express, implied or otherwise, including without limitation, any warranty of * merchantability or fitness for a particular purpose. * In no event shall the copyright holder or contributors be liable for any direct, * indirect, incidental, special, exemplary, or consequential damages * including, but not limited to, procurement of substitute goods or services; * loss of use, data, or profits; or business interruption) however caused and on * any theory of liability, whether in contract, strict liability, or tort (including * negligence or otherwise) arising in any way out of the use of this software, even * if advised of the possibility of such damage. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License version 3 * as published by the Free Software Foundation with the addition of the * following permission added to Section 15 as permitted in Section 7(a): * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY * "University of Trento - DISI","University of Trento - DISI" DISCLAIMS THE * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. * * See the GNU Affero General Public License for more details. * You should have received a copy of the GNU Affero General Public License * along with this program; if not, see http://www.gnu.org/licenses or write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA, 02110-1301 USA, or download the license from the following URL: * http://www.sts-tool.eu/License.php * * For more information, please contact STS-Tool group at this * address: ststool@disi.unitn.it * */ package eu.aniketos.wp1.ststool.diagram.parsers; import java.text.FieldPosition; import java.text.MessageFormat; import java.text.ParsePosition; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.emf.ecore.EAttribute; import org.eclipse.emf.ecore.EObject; import org.eclipse.gmf.runtime.common.core.command.ICommand; import org.eclipse.gmf.runtime.common.ui.services.parser.IParserEditStatus; import org.eclipse.gmf.runtime.common.ui.services.parser.ParserEditStatus; import org.eclipse.osgi.util.NLS; import eu.aniketos.wp1.ststool.diagram.part.Messages; import eu.aniketos.wp1.ststool.diagram.part.StsToolDiagramEditorPlugin; /** * @generated */ public class MessageFormatParser extends AbstractParser { /** * @generated */ private String defaultPattern; /** * @generated */ private String defaultEditablePattern; /** * @generated */ private MessageFormat viewProcessor; /** * @generated */ private MessageFormat editorProcessor; /** * @generated */ private MessageFormat editProcessor; /** * @generated */ public MessageFormatParser(EAttribute[] features) { super(features); } /** * @generated */ public MessageFormatParser(EAttribute[] features, EAttribute[] editableFeatures) { super(features, editableFeatures); } /** * @generated */ protected String getDefaultPattern(){ if (defaultPattern == null) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < features.length; i++) { if (i > 0) { sb.append(' '); } sb.append('{'); sb.append(i); sb.append('}'); } defaultPattern = sb.toString(); } return defaultPattern; } /** * @generated */ @Override public void setViewPattern(String viewPattern){ super.setViewPattern(viewPattern); viewProcessor = null; } /** * @generated */ @Override public void setEditorPattern(String editorPattern){ super.setEditorPattern(editorPattern); editorProcessor = null; } /** * @generated */ protected MessageFormat getViewProcessor(){ if (viewProcessor == null) { viewProcessor = new MessageFormat(getViewPattern() == null ? getDefaultPattern() : getViewPattern()); } return viewProcessor; } /** * @generated */ protected MessageFormat getEditorProcessor(){ if (editorProcessor == null) { editorProcessor = new MessageFormat(getEditorPattern() == null ? getDefaultEditablePattern() : getEditorPattern()); } return editorProcessor; } /** * @generated */ protected String getDefaultEditablePattern(){ if (defaultEditablePattern == null) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < editableFeatures.length; i++) { if (i > 0) { sb.append(' '); } sb.append('{'); sb.append(i); sb.append('}'); } defaultEditablePattern = sb.toString(); } return defaultEditablePattern; } /** * @generated */ @Override public void setEditPattern(String editPattern){ super.setEditPattern(editPattern); editProcessor = null; } /** * @generated */ protected MessageFormat getEditProcessor(){ if (editProcessor == null) { editProcessor = new MessageFormat(getEditPattern() == null ? getDefaultEditablePattern() : getEditPattern()); } return editProcessor; } /** * @generated */ public String getEditString(IAdaptable adapter,int flags){ EObject element = (EObject) adapter.getAdapter(EObject.class); return getEditorProcessor().format(getEditableValues(element), new StringBuffer(), new FieldPosition(0)).toString(); } /** * @generated */ public IParserEditStatus isValidEditString(IAdaptable adapter,String editString){ ParsePosition pos = new ParsePosition(0); Object[] values = getEditProcessor().parse(editString, pos); if (values == null) { return new ParserEditStatus(StsToolDiagramEditorPlugin.ID, IParserEditStatus.UNEDITABLE, NLS.bind(Messages.MessageFormatParser_InvalidInputError, new Integer(pos.getErrorIndex()))); } return validateNewValues(values); } /** * @generated */ public ICommand getParseCommand(IAdaptable adapter,String newString,int flags){ Object[] values = getEditProcessor().parse(newString, new ParsePosition(0)); return getParseCommand(adapter, values, flags); } /** * @generated */ public String getPrintString(IAdaptable adapter,int flags){ EObject element = (EObject) adapter.getAdapter(EObject.class); return getViewProcessor().format(getValues(element), new StringBuffer(), new FieldPosition(0)).toString(); } }