/*******************************************************************************
* Copyright (c) 2010-2015 Henshin developers. 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:
* TU Berlin, University of Luxembourg, SES S.A.
*******************************************************************************/
package de.tub.tfs.henshin.tggeditor.actions.imports;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.henshin.model.Attribute;
import org.eclipse.emf.henshin.model.HenshinFactory;
import org.eclipse.emf.henshin.model.NamedElement;
import org.eclipse.ui.IWorkbenchPart;
import de.tub.tfs.henshin.tgg.TggFactory;
public class ImportInstanceModelActionWithDefaultValues extends ImportInstanceModelAction {
/** The Constant ID. */
public static final String ID = "tggeditor.actions.ImportInstanceModelActionWithDefaultValue";
public ImportInstanceModelActionWithDefaultValues(IWorkbenchPart part) {
super(part);
setId(ID);
setText("Import instance (default values).");
setToolTipText("Import an instance model with default values.");
}
@Override
public boolean isEnabled() {
return false;
}
/**
* @param feat
*/
@Override
protected void createAttribute(EAttribute feat, boolean loadAttributesWithDefaultValues) {
Attribute attr;
if (feat.getEType().getName().equals("EFeatureMapEntry"))
// do nothing, because this map summarizes all features
return;
if (!eObj.eIsSet(feat) && (!loadAttributesWithDefaultValues || feat.getDefaultValue() == null))
// no value available, thus do not create an attribute
return;
// value is available
// process attribute
attr = TggFactory.eINSTANCE.createTAttribute();
attr.setNode(node);
attr.setType((EAttribute) feat);
if (attr.getType().getName().contains("name")
&& !(eObj instanceof NamedElement)) {
node.setName(attr.getValue());
}
if (feat.isMany()) {
if (eObj.eIsSet(feat)) {
// value of feature is set
String valueString = eObj.eGet(feat).toString();
// remove the square brackets of the array
// TODO: remove only if the list contains one element
if (valueString.length() > 1) {
valueString = valueString.substring(1,
valueString.length() - 1);
}
attr.setValue(valueString);
} else {
// feature is not set -> handle default values
if (feat.getDefaultValue() != null) {
attr.setValue(feat.getDefaultValue().toString());
}
}
} else {
// feature is single valued
if (eObj.eIsSet(feat)) {
attr.setValue(eObj.eGet(feat).toString());
} else {
// feature is not set -> handle default values
if (feat.getDefaultValue() != null) {
attr.setValue(feat.getDefaultValue().toString());
}
}
}
}
}