GlobalAntConfiguration must not save defaults, as app service

This commit is contained in:
Vladimir Krivosheev
2014-09-25 16:03:44 +02:00
parent dd143af833
commit b61d2b5918
6 changed files with 72 additions and 34 deletions
@@ -23,10 +23,8 @@ import org.jetbrains.annotations.NonNls;
import java.util.*;
public class ExternalizablePropertyContainer
extends AbstractProperty.AbstractPropertyContainer
implements JDOMExternalizable {
private static final Logger LOG = Logger.getInstance("#com.intellij.util.config.ExternalizablePropertyContainer");
public class ExternalizablePropertyContainer extends AbstractProperty.AbstractPropertyContainer implements JDOMExternalizable {
private static final Logger LOG = Logger.getInstance(ExternalizablePropertyContainer.class);
private final Map<AbstractProperty, Object> myValues = new HashMap<AbstractProperty, Object>();
private final Map<AbstractProperty, Externalizer> myExternalizers = new HashMap<AbstractProperty, Externalizer>();
@@ -69,9 +67,10 @@ public class ExternalizablePropertyContainer
}
private <T> Externalizer<List<T>> createListExternalizer(final Externalizer<T> itemExternalizer, final String itemTagName) {
return new ListExternalizer<T>(itemExternalizer, itemTagName);
return new ListExternalizer(itemExternalizer, itemTagName);
}
@Override
public void readExternal(Element element) throws InvalidDataException {
HashMap<String, AbstractProperty> propertyByName = new HashMap<String, AbstractProperty>();
for (AbstractProperty abstractProperty : myExternalizers.keySet()) {
@@ -96,37 +95,48 @@ public class ExternalizablePropertyContainer
}
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
if (myExternalizers.isEmpty()) {
return;
}
List<AbstractProperty> properties = new ArrayList<AbstractProperty>(myExternalizers.keySet());
Collections.sort(properties, AbstractProperty.NAME_COMPARATOR);
for (AbstractProperty property : properties) {
final Externalizer externalizer = myExternalizers.get(property);
Externalizer externalizer = myExternalizers.get(property);
if (externalizer == null) {
continue;
}
final Object propValue = property.get(this);
Object propValue = property.get(this);
if (!Comparing.equal(propValue, property.getDefault(this))) {
final Element child = new Element(property.getName());
Element child = new Element(property.getName());
externalizer.writeValue(child, propValue);
element.addContent(child);
if (!JDOMUtil.isEmpty(child)) {
element.addContent(child);
}
}
}
}
@Override
protected Object getValueOf(AbstractProperty property) {
Object value = myValues.get(property);
return value != null ? value : property.getDefault(this);
}
@Override
protected void setValueOf(AbstractProperty externalizableProperty, Object value) {
myValues.put(externalizableProperty, value);
}
@Override
public boolean hasProperty(AbstractProperty property) {
return myExternalizers.containsKey(property);
}
private class ListExternalizer<T> implements Externalizer<List<T>> {
private static class ListExternalizer<T> implements Externalizer<List<T>> {
@NonNls private static final String NULL_ELEMENT = "NULL_VALUE_ELEMENT";
private final Externalizer<T> myItemExternalizer;
private final String myItemTagName;
@@ -136,6 +146,7 @@ public class ExternalizablePropertyContainer
myItemTagName = itemTagName;
}
@Override
public List<T> readValue(Element dataElement) throws InvalidDataException {
ArrayList<T> list = new ArrayList<T>();
List<Element> children = dataElement.getChildren();
@@ -154,6 +165,7 @@ public class ExternalizablePropertyContainer
return list;
}
@Override
public void writeValue(Element dataElement, List<T> value) throws WriteExternalException {
for (Iterator<T> iterator = value.iterator(); iterator.hasNext();) {
T item = iterator.next();
@@ -29,11 +29,13 @@ import java.util.List;
public interface Externalizer<T> {
@NonNls String VALUE_ATTRIBUTE = "value";
Externalizer<String> STRING = new BaseExternalizer<String>(){
@Override
public String readValue(Element dataElement) {
return dataElement.getAttributeValue(VALUE_ATTRIBUTE);
}
};
Externalizer<Integer> INTEGER = new BaseExternalizer<Integer>() {
@Override
public Integer readValue(Element dataElement) {
try {
return new Integer(dataElement.getAttributeValue(VALUE_ATTRIBUTE));
@@ -46,11 +48,13 @@ public interface Externalizer<T> {
abstract class BaseExternalizer<T> implements Externalizer<T> {
@Override
public void writeValue(Element dataElement, T value) {
dataElement.setAttribute(VALUE_ATTRIBUTE, value.toString());
}
}
Externalizer<Boolean> BOOLEAN = new BaseExternalizer<Boolean>() {
@Override
public Boolean readValue(Element dataElement) {
return Boolean.valueOf(dataElement.getAttributeValue(VALUE_ATTRIBUTE));
}
@@ -67,12 +71,14 @@ public interface Externalizer<T> {
myFactory = factory;
}
@Override
public T readValue(Element dataElement) throws InvalidDataException {
T data = myFactory.create();
data.readExternal(dataElement);
return data;
}
@Override
public void writeValue(Element dataElement, T value) throws WriteExternalException {
value.writeExternal(dataElement);
}
@@ -87,6 +93,7 @@ public interface Externalizer<T> {
@NonNls private static final String KEY_ATTR = "key";
@NonNls private static final String VALUE_ATTR = "value";
@Override
public Storage readValue(Element dataElement) throws InvalidDataException {
Storage.MapStorage storage = new Storage.MapStorage();
List<Element> children = dataElement.getChildren(ITEM_TAG);
@@ -97,6 +104,7 @@ public interface Externalizer<T> {
return storage;
}
@Override
public void writeValue(Element dataElement, Storage storage) throws WriteExternalException {
Iterator<String> keys = ((Storage.MapStorage)storage).getKeys();
while (keys.hasNext()) {
@@ -27,12 +27,14 @@ public class StorageProperty extends AbstractProperty<Storage> {
myName = name;
}
@Override
public Storage getDefault(AbstractProperty.AbstractPropertyContainer container) {
Storage.MapStorage storage = new Storage.MapStorage();
set(container, storage);
return storage;
}
@Override
public Storage copy(Storage storage) {
if (!(storage instanceof Storage.MapStorage))
throw new UnsupportedOperationException(storage.getClass().getName());
@@ -45,6 +47,7 @@ public class StorageProperty extends AbstractProperty<Storage> {
return copy;
}
@Override
public String getName() {
return myName;
}
+1 -4
View File
@@ -73,6 +73,7 @@
<configurationType implementation="com.intellij.lang.ant.config.execution.AntRunConfigurationType"/>
<programRunner implementation="com.intellij.lang.ant.config.execution.AntRunner"/>
<applicationService serviceImplementation="com.intellij.lang.ant.config.impl.GlobalAntConfiguration"/>
</extensions>
<application-components>
@@ -80,10 +81,6 @@
<interface-class>com.intellij.lang.ant.AntSupport</interface-class>
<implementation-class>com.intellij.lang.ant.AntSupport</implementation-class>
</component>
<component>
<interface-class>com.intellij.lang.ant.config.impl.GlobalAntConfiguration</interface-class>
<implementation-class>com.intellij.lang.ant.config.impl.GlobalAntConfiguration</implementation-class>
</component>
</application-components>
<project-components>
@@ -34,6 +34,7 @@ import java.util.List;
public interface AntClasspathEntry {
Externalizer<AntClasspathEntry> EXTERNALIZER = new Externalizer<AntClasspathEntry>() {
@Override
public AntClasspathEntry readValue(Element dataElement) throws InvalidDataException {
String pathUrl = dataElement.getAttributeValue(SinglePathEntry.PATH);
if (pathUrl != null)
@@ -44,6 +45,7 @@ public interface AntClasspathEntry {
throw new InvalidDataException();
}
@Override
public void writeValue(Element dataElement, AntClasspathEntry entry) throws WriteExternalException {
entry.writeExternal(dataElement);
}
@@ -68,6 +70,7 @@ public interface AntClasspathEntry {
myMapper = mapper;
}
@Override
public List<AntClasspathEntry> create() {
final VirtualFile[] files = FileChooser.chooseFiles(myDescriptor, myParentComponent, null, null);
return files.length == 0 ? null : ContainerUtil.map(files, myMapper);
@@ -21,16 +21,17 @@ import com.intellij.lang.ant.config.AntBuildFile;
import com.intellij.lang.ant.config.AntBuildTarget;
import com.intellij.lang.ant.config.AntConfiguration;
import com.intellij.lang.ant.config.AntConfigurationBase;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
@@ -38,14 +39,19 @@ import com.intellij.util.config.*;
import com.intellij.util.containers.ContainerUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Map;
public class GlobalAntConfiguration implements ApplicationComponent, JDOMExternalizable {
private static final Logger LOG = Logger.getInstance("#com.intellij.lang.ant.config.impl.AntGlobalConfiguration");
@State(
name = "GlobalAntConfiguration",
storages = {@com.intellij.openapi.components.Storage(file = StoragePathMacros.APP_CONFIG + "/other.xml")}
)
public class GlobalAntConfiguration implements PersistentStateComponent<Element> {
private static final Logger LOG = Logger.getInstance(GlobalAntConfiguration.class);
public static final StorageProperty FILTERS_TABLE_LAYOUT = new StorageProperty("filtersTableLayout");
public static final StorageProperty PROPERTIES_TABLE_LAYOUT = new StorageProperty("propertiesTableLayout");
static final ListProperty<AntInstallation> ANTS = ListProperty.create("registeredAnts");
@@ -53,6 +59,7 @@ public class GlobalAntConfiguration implements ApplicationComponent, JDOMExterna
private final AntInstallation myBundledAnt;
public static final String BUNDLED_ANT_NAME = AntBundle.message("ant.reference.bundled.ant.name");
public final Condition<AntInstallation> IS_USER_ANT = new Condition<AntInstallation>() {
@Override
public boolean value(AntInstallation antInstallation) {
return antInstallation != myBundledAnt;
}
@@ -74,15 +81,9 @@ public class GlobalAntConfiguration implements ApplicationComponent, JDOMExterna
myBundledAnt = createBundledAnt();
}
@NotNull
public String getComponentName() {
return "GlobalAntConfiguration";
}
public void initComponent() { }
public static AntInstallation createBundledAnt() {
AntInstallation bundledAnt = new AntInstallation() {
@Override
public AntReference getReference() {
return AntReference.BUNDLED_ANT;
}
@@ -97,18 +98,32 @@ public class GlobalAntConfiguration implements ApplicationComponent, JDOMExterna
return bundledAnt;
}
public void disposeComponent() {}
public void readExternal(Element element) throws InvalidDataException {
myProperties.readExternal(element);
@Nullable
@Override
public Element getState() {
Element element = new Element("state");
try {
myProperties.writeExternal(element);
}
catch (WriteExternalException e) {
LOG.error(e);
return null;
}
return element;
}
public void writeExternal(Element element) throws WriteExternalException {
myProperties.writeExternal(element);
@Override
public void loadState(Element state) {
try {
myProperties.readExternal(state);
}
catch (InvalidDataException e) {
LOG.error(e);
}
}
public static GlobalAntConfiguration getInstance() {
return ApplicationManager.getApplication().getComponent(GlobalAntConfiguration.class);
return ServiceManager.getService(GlobalAntConfiguration.class);
}
public Map<AntReference, AntInstallation> getConfiguredAnts() {