Remove duplication of configuration classes

This commit is contained in:
Alexey Andreev 2016-02-19 23:33:01 +03:00
parent 5706012d0e
commit 8f734ba3b5
4 changed files with 23 additions and 68 deletions

View File

@ -1,46 +0,0 @@
/*
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.idea;
public class TeaVMConfiguration {
private boolean enabled;
private String mainClass;
private String targetDirectory;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getMainClass() {
return mainClass;
}
public void setMainClass(String mainClass) {
this.mainClass = mainClass;
}
public String getTargetDirectory() {
return targetDirectory;
}
public void setTargetDirectory(String targetDirectory) {
this.targetDirectory = targetDirectory;
}
}

View File

@ -21,20 +21,21 @@ import com.intellij.openapi.components.Storage;
import com.intellij.openapi.module.ModuleComponent; import com.intellij.openapi.module.ModuleComponent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.teavm.idea.jps.model.TeaVMJpsConfiguration;
@State(name = "teavm", storages = @Storage(id = "other", file = "$MODULE_FILE$")) @State(name = "teavm", storages = @Storage(id = "other", file = "$MODULE_FILE$"))
public class TeaVMConfigurationStorage implements PersistentStateComponent<TeaVMConfiguration>, ModuleComponent { public class TeaVMConfigurationStorage implements PersistentStateComponent<TeaVMJpsConfiguration>, ModuleComponent {
private TeaVMConfiguration state; private TeaVMJpsConfiguration state = new TeaVMJpsConfiguration();
@Nullable @Nullable
@Override @Override
public TeaVMConfiguration getState() { public TeaVMJpsConfiguration getState() {
return state; return state.createCopy();
} }
@Override @Override
public void loadState(TeaVMConfiguration state) { public void loadState(TeaVMJpsConfiguration state) {
this.state = state; this.state.applyChanges(state);
} }
@Override @Override

View File

@ -22,8 +22,8 @@ import com.intellij.openapi.options.ConfigurationException;
import javax.swing.JComponent; import javax.swing.JComponent;
import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.teavm.idea.TeaVMConfiguration;
import org.teavm.idea.TeaVMConfigurationStorage; import org.teavm.idea.TeaVMConfigurationStorage;
import org.teavm.idea.jps.model.TeaVMJpsConfiguration;
public class TeaVMConfigurable implements Configurable { public class TeaVMConfigurable implements Configurable {
private final Module module; private final Module module;
@ -61,7 +61,7 @@ public class TeaVMConfigurable implements Configurable {
@Override @Override
public void apply() throws ConfigurationException { public void apply() throws ConfigurationException {
TeaVMConfiguration config = new TeaVMConfiguration(); TeaVMJpsConfiguration config = new TeaVMJpsConfiguration();
panel.save(config); panel.save(config);
TeaVMConfigurationStorage configStorage = ModuleServiceManager.getService(module, TeaVMConfigurationStorage configStorage = ModuleServiceManager.getService(module,
TeaVMConfigurationStorage.class); TeaVMConfigurationStorage.class);

View File

@ -29,20 +29,20 @@ import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JTextField; import javax.swing.JTextField;
import org.teavm.idea.TeaVMConfiguration; import org.teavm.idea.jps.model.TeaVMJpsConfiguration;
class TeaVMConfigurationPanel extends JPanel { class TeaVMConfigurationPanel extends JPanel {
private final JCheckBox enabledCheckBox = new JCheckBox("TeaVM enabled for this module"); private final JCheckBox enabledCheckBox = new JCheckBox("TeaVM enabled for this module");
private final JTextField mainClassField = new JTextField(); private final JTextField mainClassField = new JTextField();
private final JTextField targetDirectoryField = new JTextField(); private final JTextField targetDirectoryField = new JTextField();
private final TeaVMConfiguration initialConfiguration = new TeaVMConfiguration(); private final TeaVMJpsConfiguration initialConfiguration = new TeaVMJpsConfiguration();
private final List<JComponent> editComponents = Arrays.asList(mainClassField, targetDirectoryField); private final List<JComponent> editComponents = Arrays.asList(mainClassField, targetDirectoryField);
private final List<Field<?>> fields = Arrays.asList( private final List<Field<?>> fields = Arrays.asList(
new Field<>(TeaVMConfiguration::setEnabled, TeaVMConfiguration::isEnabled, new Field<>(TeaVMJpsConfiguration::setEnabled, TeaVMJpsConfiguration::isEnabled,
enabledCheckBox::setSelected, enabledCheckBox::isSelected), enabledCheckBox::setSelected, enabledCheckBox::isSelected),
new Field<>(TeaVMConfiguration::setMainClass, TeaVMConfiguration::getMainClass, new Field<>(TeaVMJpsConfiguration::setMainClass, TeaVMJpsConfiguration::getMainClass,
mainClassField::setText, mainClassField::getText), mainClassField::setText, mainClassField::getText),
new Field<>(TeaVMConfiguration::setTargetDirectory, TeaVMConfiguration::getTargetDirectory, new Field<>(TeaVMJpsConfiguration::setTargetDirectory, TeaVMJpsConfiguration::getTargetDirectory,
targetDirectoryField::setText, targetDirectoryField::getText) targetDirectoryField::setText, targetDirectoryField::getText)
); );
@ -75,9 +75,9 @@ class TeaVMConfigurationPanel extends JPanel {
add(targetDirectoryField, fieldConstrains); add(targetDirectoryField, fieldConstrains);
} }
public void load(TeaVMConfiguration config) { public void load(TeaVMJpsConfiguration config) {
if (config == null) { if (config == null) {
config = new TeaVMConfiguration(); config = new TeaVMJpsConfiguration();
} }
updateInitialConfiguration(config); updateInitialConfiguration(config);
for (Field<?> field : fields) { for (Field<?> field : fields) {
@ -86,18 +86,18 @@ class TeaVMConfigurationPanel extends JPanel {
updateEnabledState(); updateEnabledState();
} }
public void save(TeaVMConfiguration config) { public void save(TeaVMJpsConfiguration config) {
for (Field<?> field : fields) { for (Field<?> field : fields) {
saveField(field, config); saveField(field, config);
} }
updateInitialConfiguration(config); updateInitialConfiguration(config);
} }
private <T> void loadField(Field<T> field, TeaVMConfiguration config) { private <T> void loadField(Field<T> field, TeaVMJpsConfiguration config) {
field.editConsumer.accept(field.dataSupplier.apply(config)); field.editConsumer.accept(field.dataSupplier.apply(config));
} }
private <T> void saveField(Field<T> field, TeaVMConfiguration config) { private <T> void saveField(Field<T> field, TeaVMJpsConfiguration config) {
field.dataConsumer.accept(config, field.editSupplier.get()); field.dataConsumer.accept(config, field.editSupplier.get());
} }
@ -115,19 +115,19 @@ class TeaVMConfigurationPanel extends JPanel {
return !Objects.equals(field.dataSupplier.apply(initialConfiguration), field.editSupplier.get()); return !Objects.equals(field.dataSupplier.apply(initialConfiguration), field.editSupplier.get());
} }
private void updateInitialConfiguration(TeaVMConfiguration config) { private void updateInitialConfiguration(TeaVMJpsConfiguration config) {
initialConfiguration.setEnabled(config.isEnabled()); initialConfiguration.setEnabled(config.isEnabled());
initialConfiguration.setMainClass(config.getMainClass()); initialConfiguration.setMainClass(config.getMainClass());
initialConfiguration.setTargetDirectory(config.getTargetDirectory()); initialConfiguration.setTargetDirectory(config.getTargetDirectory());
} }
static class Field<T> { static class Field<T> {
final BiConsumer<TeaVMConfiguration, T> dataConsumer; final BiConsumer<TeaVMJpsConfiguration, T> dataConsumer;
final Function<TeaVMConfiguration, T> dataSupplier; final Function<TeaVMJpsConfiguration, T> dataSupplier;
final Consumer<T> editConsumer; final Consumer<T> editConsumer;
final Supplier<T> editSupplier; final Supplier<T> editSupplier;
public Field(BiConsumer<TeaVMConfiguration, T> dataConsumer, Function<TeaVMConfiguration, T> dataSupplier, public Field(BiConsumer<TeaVMJpsConfiguration, T> dataConsumer, Function<TeaVMJpsConfiguration, T> dataSupplier,
Consumer<T> editConsumer, Supplier<T> editSupplier) { Consumer<T> editConsumer, Supplier<T> editSupplier) {
this.dataConsumer = dataConsumer; this.dataConsumer = dataConsumer;
this.dataSupplier = dataSupplier; this.dataSupplier = dataSupplier;