Mercurial > hg > thermostat-tools-eclipse
view com.redhat.thermostat.tools.eclipse.plugin/src/com/redhat/thermostat/tools/eclipse/plugin/model/Plugin.java @ 105:28252d3475bd
Don't generate empty `commands` and `extensions` elements
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Wed, 19 Feb 2014 16:39:36 -0500 |
parents | 8f2dd0dbdb87 |
children | cba7b8de5f76 |
line wrap: on
line source
package com.redhat.thermostat.tools.eclipse.plugin.model; import java.util.ArrayList; import java.util.List; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="plugin") @XmlAccessorType(XmlAccessType.NONE) public class Plugin extends ModelObject { private List<Command> commands; private List<Extension> extensions; public Plugin() { commands = new ArrayList<>(); extensions = new ArrayList<>(); } @XmlElementWrapper(name="commands") @XmlElement(name="command") public List<Command> getCommands() { return this.commands; } public void addCommand(Command command) { this.commands.add(command); firePropertyChange("commands", null, this.commands); //$NON-NLS-1$ } public Command getCommand(String name) { for (Command command : commands) { if (command.getName().equals(name)) { return command; } } return null; } public List<String> getCommandNames() { List<String> result = new ArrayList<>(); for (Command command : commands) { result.add(command.getName()); } return result; } public void removeCommand(String name) { for (int i = 0; i < commands.size(); i++) { Command command = commands.get(i); if (command.getName().equals(name)) { commands.remove(i); firePropertyChange("commands", null, this.commands); //$NON-NLS-1$ return; } } } @XmlElementWrapper(name="extensions") @XmlElement(name="extension") public List<Extension> getExtensions() { return this.extensions; } public void addExtension(Extension extension) { this.extensions.add(extension); firePropertyChange("extensions", null, this.extensions); //$NON-NLS-1$ } public Extension getExtension(String name) { for (Extension extension : extensions) { if (extension.getName().equals(name)) { return extension; } } return null; } public List<String> getExtensionNames() { List<String> result = new ArrayList<>(); for (Extension extension : extensions) { result.add(extension.getName()); } return result; } public void removeExtension(String name) { for (int i = 0; i < extensions.size(); i++) { Extension extension = extensions.get(i); if (extension.getName().equals(name)) { extensions.remove(i); firePropertyChange("extensions", null, this.extensions); //$NON-NLS-1$ return; } } } // JAXB-specific hooks // try and pretty up the resulting xml file to create no empty lists /** Turn empty list into no-element when marshalling to xml */ void beforeMarshal(Marshaller marshaller) { if (this.commands == null || this.commands.isEmpty()) { this.commands = null; } if (this.extensions == null || this.extensions.isEmpty()) { this.extensions = null; } } /** If null elements were read, re-initialize to empty list */ void afterUnmarshal(Unmarshaller unmarshaller, Object parent) { if (this.commands == null) { this.commands = new ArrayList<>(); } if (this.extensions == null) { this.extensions = new ArrayList<>(); } } }