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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.teavm.idea.jps.model.TeaVMJpsConfiguration;
@State(name = "teavm", storages = @Storage(id = "other", file = "$MODULE_FILE$"))
public class TeaVMConfigurationStorage implements PersistentStateComponent<TeaVMConfiguration>, ModuleComponent {
private TeaVMConfiguration state;
public class TeaVMConfigurationStorage implements PersistentStateComponent<TeaVMJpsConfiguration>, ModuleComponent {
private TeaVMJpsConfiguration state = new TeaVMJpsConfiguration();
@Nullable
@Override
public TeaVMConfiguration getState() {
return state;
public TeaVMJpsConfiguration getState() {
return state.createCopy();
}
@Override
public void loadState(TeaVMConfiguration state) {
this.state = state;
public void loadState(TeaVMJpsConfiguration state) {
this.state.applyChanges(state);
}
@Override

View File

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

View File

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