public class MyComponent implements ProjectComponent, JDOMExternalizable {
private Map someMap = new HashMap();
/**
* you need to write all your state to the element passed in. you "own"
* this element. this method writes your state out whenever IDEA
* decides it's time... make it run as fast as you can :)
*/
public void writeExternal(Element element) throws WriteExternalException {
// iterate over keys
Iterator keyIterator = someMap.keySet().iterator();
while (keyIterator.hasNext()) {
String key = (String)keyIterator.next();
//create a new element named "map_entry"
Element entryElement = new Element("map_entry");
// add an attribute named "name" and put your key in as the value
entryElement = entryElement.setAttribute("name", key);
// likewise with "value"
entryElement = entryElement.setAttribute("value", someMap.get(key).toString());
// add your new element back to what was passed in.
element.addContent(entryElement);
}
}
/**
* the Element passed in is the root of your configuration... you "own"
* this element. this method is run once when your component is first
* created.. so get all your goodies at once.
*/
public void readExternal(Element element) throws InvalidDataException {
// get all elements named "map_entries"
List entries = element.getChildren("map_entries");
for (int i = 0; i < entries.size(); i++) {
Element entry = (Element) entries.get(i);
// get the attributes from the entry
String name = entry.getAttribute("name").getValue();
String value = entry.getAttribute("value").getValue();
// put them in the map for the plugin to use later
someMap.put(name, value);
}
}