PersistentStateComponent data have changed when data have not execute set method
Answered
1.DataMappingConfigurable
public class DataMappingConfigurable extends AbstractConfigConfigurable {
protected FastRequestConfiguration config = FastRequestComponent.getInstance().getState();
private DataMappingConfigView view = new DataMappingConfigView(config);
@Override
public AbstractConfigurableView getView() {
return view;
}
@Override
public @Nullable
JComponent createComponent() {
return view.getComponent();
}
@Override
public String getDisplayName() {
return "Date Type Config";
}
@Override
public boolean isModified() {
return true;
}
@Override
public void apply() throws ConfigurationException {
System.out.println(config.getCustomDataMappingList().size());
System.out.println(view);
}
}
2.DataMappingConfigView
import com.google.common.collect.Lists;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import io.github.kings1990.plugin.fastrequest.model.DataMapping;
import io.github.kings1990.plugin.fastrequest.model.FastRequestConfiguration;
import io.github.kings1990.plugin.fastrequest.view.AbstractConfigurableView;
import io.github.kings1990.plugin.fastrequest.view.inner.DataMappingAddView;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class DataMappingConfigView extends AbstractConfigurableView{
private JPanel panel;
private JPanel defaultDataMappingPanel;
private JPanel customDataMappingPanel;
private List<DataMapping> currentCustomDataMappingList;
private JBTable customeTable;
public DataMappingConfigView(FastRequestConfiguration config){
super(config);
}
private void createUIComponents() {
renderingDefaultDataMappingPanel();
renderingCustomDataMappingPanel();
}
/**
* 渲染默认数据映射panel
*
* @author Kings
* @date 2021/05/24
*/
private void renderingDefaultDataMappingPanel() {
JBTable table = createTable(config.getDefaultDataMappingList());
ToolbarDecorator toolbarDecorator = ToolbarDecorator.createDecorator(table);
toolbarDecorator.setMoveDownAction(null);
toolbarDecorator.setMoveUpAction(null);
toolbarDecorator.setAddAction(null);
toolbarDecorator.setRemoveAction(null);
defaultDataMappingPanel = toolbarDecorator.createPanel();
}
/**
* 渲染自定义数据映射panel
*
* @author Kings
* @date 2021/05/24
*/
private void renderingCustomDataMappingPanel() {
currentCustomDataMappingList = config.getCustomDataMappingList();
if(currentCustomDataMappingList == null){
currentCustomDataMappingList = new ArrayList<>();
}
JBTable table = createCustomTable();
ToolbarDecorator toolbarDecorator = ToolbarDecorator.createDecorator(table);
toolbarDecorator.setMoveDownAction(null);
toolbarDecorator.setMoveUpAction(null);
toolbarDecorator.setAddAction(event -> {
DataMappingAddView dataMappingAddView = new DataMappingAddView();
if (dataMappingAddView.showAndGet()) {
DataMapping dataMapping = dataMappingAddView.getValue();
if(contains(currentCustomDataMappingList,dataMapping)){
Messages.showMessageDialog("Java Type already exist", "Error", Messages.getInformationIcon());
return;
}
currentCustomDataMappingList.add(dataMapping);
// table.setModel(new ListTableModel<>(getColumnInfo(),currentCustomDataMappingList));
System.out.println("---");
}
});
toolbarDecorator.setRemoveAction(event -> {
int selectedRow = table.getSelectedRow();
currentCustomDataMappingList.remove(selectedRow);
table.setModel(new ListTableModel<>(getColumnInfo(),currentCustomDataMappingList));
});
customDataMappingPanel = toolbarDecorator.createPanel();
setCustomeTable(table);
}
/**
* 是否包含
*
* @param list 列表
* @param target 目标
* @return boolean
* @author Kings
* @date 2021/05/24
*/
private boolean contains(List<DataMapping> list, DataMapping target) {
return list.stream().anyMatch(q->target.getType().equals(q.getType()));
}
private ColumnInfo<Object, Object>[] getColumnInfo() {
ColumnInfo<Object, Object>[] columnArray = new ColumnInfo[2];
List<String> envList = Lists.newArrayList("Java Type", "Default value");
for (int i = 0; i < envList.size(); i++) {
ColumnInfo<Object, Object> envColumn = new ColumnInfo<>(envList.get(i)) {
@Override
public @Nullable Object valueOf(Object o) {
return o;
}
};
columnArray[i] = envColumn;
}
return columnArray;
}
public JBTable createCustomTable() {
ColumnInfo<Object, Object>[] columns = getColumnInfo();
ListTableModel<DataMapping> model = new ListTableModel<>(columns, currentCustomDataMappingList);
JBTable table = new JBTable(model) {
@Override
public Object getValueAt(int row, int column) {
if (currentCustomDataMappingList.isEmpty()) {
return StringUtils.EMPTY;
}
DataMapping dataMapping = currentCustomDataMappingList.get(row);
if (dataMapping == null) {
return StringUtils.EMPTY;
}
if (column == 0) {
return dataMapping.getType();
} else {
return dataMapping.getValue();
}
}
};
table.setVisible(true);
return table;
}
public JBTable createTable(List<DataMapping> dataMappingList) {
ColumnInfo<Object, Object>[] columns = getColumnInfo();
ListTableModel<DataMapping> model = new ListTableModel<>(columns, dataMappingList);
JBTable table = new JBTable(model) {
@Override
public Object getValueAt(int row, int column) {
if (dataMappingList.isEmpty()) {
return StringUtils.EMPTY;
}
DataMapping dataMapping = dataMappingList.get(row);
if (dataMapping == null) {
return StringUtils.EMPTY;
}
if (column == 0) {
return dataMapping.getType();
} else {
return dataMapping.getValue();
}
}
};
table.setVisible(true);
return table;
}
@Override
public JComponent getComponent() {
return panel;
}
public List<DataMapping> getCurrentCustomDataMappingList() {
return currentCustomDataMappingList;
}
public void setCurrentCustomDataMappingList(List<DataMapping> currentCustomDataMappingList) {
this.currentCustomDataMappingList = currentCustomDataMappingList;
}
public JBTable getCustomeTable() {
return customeTable;
}
public void setCustomeTable(JBTable customeTable) {
this.customeTable = customeTable;
}
}
3.AbstractConfigurableView
public abstract class AbstractConfigurableView {
protected FastRequestConfiguration config;
public abstract JComponent getComponent();
public AbstractConfigurableView(FastRequestConfiguration config) {
this.config = config;
}
}
4
import com.intellij.openapi.options.Configurable;
import io.github.kings1990.plugin.fastrequest.config.FastRequestComponent;
import io.github.kings1990.plugin.fastrequest.model.FastRequestConfiguration;
import io.github.kings1990.plugin.fastrequest.view.AbstractConfigurableView;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public abstract class AbstractConfigConfigurable implements Configurable {
protected FastRequestConfiguration config = FastRequestComponent.getInstance().getState();
@Override
public @Nullable JComponent createComponent() {
return getView().getComponent();
}
public abstract AbstractConfigurableView getView();
}
5.
@State(name = "fastRequest", storages = {@Storage("fastRequest.xml")})
public class FastRequestComponent implements PersistentStateComponent<FastRequestConfiguration> {
private FastRequestConfiguration config;
@Nullable
@Override
public FastRequestConfiguration getState() {
System.out.println("------------------------------------------------------------");
if (config == null) {
config = new FastRequestConfiguration();
List<String> projectList = new ArrayList<>();
List<String> envList = new ArrayList<>();
List<NameGroup> dataList = new ArrayList<>();
config.setDataList(dataList);
config.setEnvList(envList);
config.setProjectList(projectList);
}
return config;
}
public static FastRequestComponent getInstance() {
return ServiceManager.getService(FastRequestComponent.class);
}
@Override
public void loadState(@NotNull FastRequestConfiguration state) {
XmlSerializerUtil.copyBean(state, Objects.requireNonNull(getState()));
}
}
question
please look at code 2
DataMappingConfigView.renderingCustomDataMappingPanel
customDataMappingList comes from
currentCustomDataMappingList = config.getCustomDataMappingList();
code
currentCustomDataMappingList.add(dataMapping);
when add executed, filed customDataMappingList in config has changed, but i have not execute setCustomDataMappingList method(config is PersistentStateComponent data)
how to make config's customDataMappingList is is old data before click view's apply button, but rendering the view the newest data when view add data
attachId
Upload id: 2021_05_25_RswuH1S4HE7d2jWk (file: debug.mp4)
Upload id: 2021_05_25_4fXABxp2wgGQJGqj (file: DataMappingConfigurable.java)
Please sign in to leave a comment.
Please provide full sources, not just snippets. It is impossible to diagnose without it.