package de.upb.swtpra06.nde17.model.util;
import java.util.HashMap;
import java.util.Iterator;
import de.upb.swtpra06.nde17.DeploymentPlugin;
import de.upb.swtpra06.visual.AbstractVisualModelElement;
import de.upb.swtpra06.visual.KeyToParameterValue;
import de.upb.swtpra06.visual.KeyToPluginSpecificMap;
import de.upb.swtpra06.visual.ParameterValue;
import de.upb.swtpra06.visual.PluginSpecificMap;
import de.upb.swtpra06.visual.VisualFactory;
/**
* This class should help to simplify the interaction with the PluginSpecificMap
* of an AbstractVisualModelElement. The PluginSpecificMap contains a bunch of
* multiple nested list, with isn't easy to handle.
* The PluginSpecificMapHelper provides simple set and get methodes to interact
* with the PluginSpecificMap.
* The {@link DeploymentPlugin#PLUGIN_ID toplevel-key} is hardcoded.
*/
public class PluginSpecificMapHelper {
/** Key used for the ComponentInstance description */
public final static String COMPONENT_INSTANCE__DESCRIPTION = "component_instance__description";
/** Key used for the Node description */
public final static String NODE__DESCRIPTION = "node__description";
/** Key used for the Bus description */
public final static String BUS__DESCRIPTION = "bus__description";
/** Key used for the PortInstance description */
public final static String PORT_INSTANCE__DESCRIPTION = "port_instance__description";
private static HashMap defaults = null;
static {
defaults = new HashMap();
defaults.put(COMPONENT_INSTANCE__DESCRIPTION, "Description of a Component Instance.");
defaults.put(NODE__DESCRIPTION, "Description of a Node.");
defaults.put(BUS__DESCRIPTION, "Description of a Bus.");
defaults.put(PORT_INSTANCE__DESCRIPTION, "Description of a Port Instance.");
}
/**
* Gets the value from the PluginSpecificMap of the given AbstractVisualModelElement.
* If no value is currently mapped to the given key the default value is taken from a
* defaults-map. This defaultvalue will be mapped to given key and than returned.
* @param element the lookup will be done in the PluginSpecificMap of this element
* @param key the key to be looked up
* @return value for this key of the PluginSpecificMap from element
*/
public static String get(AbstractVisualModelElement element, String key) {
Iterator itPluginKeyToMap = element.getPluginSpecificParameter().iterator();
while (itPluginKeyToMap.hasNext()) {
KeyToPluginSpecificMap pluginKeyToMap = (KeyToPluginSpecificMap)itPluginKeyToMap.next();
if (pluginKeyToMap.getKey().equals(DeploymentPlugin.PLUGIN_ID)) {
PluginSpecificMap pluginMap = pluginKeyToMap.getValue();
Iterator itKeyToParam = pluginMap.getParameter().iterator();
while (itKeyToParam.hasNext()) {
KeyToParameterValue keyToParam = (KeyToParameterValue)itKeyToParam.next();
if (keyToParam.getKey().equals(key))
return keyToParam.getValue().getValue();
}
}
}
// Requested value was not found, so set default and return it.
if (!defaults.containsKey(key))
throw new IllegalArgumentException("No defaultvalue is mapped to given Key '" + key + "'");
String value = (String)defaults.get(key);
set(element, key, value);
return value;
}
/**
* Sets the value from the PluginSpecificMap of the given AbstractVisualModelElement.
* @param element the set will be done in the PluginSpecificMap of this element
* @param key the key to set to
* @param value the value to set
*/
public static void set(AbstractVisualModelElement element, String key, String value) {
VisualFactory vf = VisualFactory.eINSTANCE;
Iterator itKeyToPluginMap = element.getPluginSpecificParameter().iterator();
while (itKeyToPluginMap.hasNext()) {
KeyToPluginSpecificMap keyToPluginMap = (KeyToPluginSpecificMap)itKeyToPluginMap.next();
if (keyToPluginMap.getKey().equals(DeploymentPlugin.PLUGIN_ID)) {
PluginSpecificMap pluginMap = keyToPluginMap.getValue();
Iterator itKeyToParam = pluginMap.getParameter().iterator();
while (itKeyToParam.hasNext()) {
KeyToParameterValue keyToParam = (KeyToParameterValue)itKeyToParam.next();
if (keyToParam.getKey().equals(key)) {
keyToParam.getValue().setValue(value);
return;
}
}
ParameterValue param = vf.createParameterValue();
KeyToParameterValue keyToParam = vf.createKeyToParameterValue();
param.setValue(value);
keyToParam.setKey(key);
keyToParam.setValue(param);
pluginMap.getParameter().add(keyToParam);
return;
}
ParameterValue param = vf.createParameterValue();
KeyToParameterValue keyToParam = vf.createKeyToParameterValue();
PluginSpecificMap pluginMap = vf.createPluginSpecificMap();
param.setValue(value);
keyToParam.setKey(key);
keyToParam.setValue(param);
pluginMap.getParameter().add(keyToParam);
keyToPluginMap.setKey(DeploymentPlugin.PLUGIN_ID);
keyToPluginMap.setValue(pluginMap);
return;
}
ParameterValue param = vf.createParameterValue();
KeyToParameterValue keyToParam = vf.createKeyToParameterValue();
PluginSpecificMap pluginMap = vf.createPluginSpecificMap();
KeyToPluginSpecificMap keyToPluginMap = vf.createKeyToPluginSpecificMap();
param.setValue(value);
keyToParam.setKey(key);
keyToParam.setValue(param);
pluginMap.getParameter().add(keyToParam);
keyToPluginMap.setKey(DeploymentPlugin.PLUGIN_ID);
keyToPluginMap.setValue(pluginMap);
element.getPluginSpecificParameter().add(keyToPluginMap);
}
}