package io.protostuff.jetbrains.plugin.settings;

import com.google.common.collect.Lists;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.ui.CollectionListModel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Plugin settings form.
 *
 * @author Kostiantyn Shchepanovskyi
 */
@SuppressWarnings("WeakerAccess")
public class SettingsForm {

    private final CollectionListModel<String> includePathModel;
    private final List<String> includePathListList;
    private final Project project;
    private JPanel panel;
    private com.intellij.ui.components.JBList includePathList;
    private JButton addButton;
    private JButton removeButton;
    private JLabel includePathsLabel;

    /**
     * Create new {@link SettingsForm} instance.
     */
    @SuppressWarnings("unchecked")
    public SettingsForm(Project project, ProtobufSettings settings) {
        this.project = project;
        List<String> internalIncludePathList = new ArrayList<>();
        internalIncludePathList.addAll(settings.getIncludePaths());
        includePathListList = Collections.unmodifiableList(internalIncludePathList);
        includePathModel = new CollectionListModel<>(internalIncludePathList, true);
        includePathList.setModel(includePathModel);
        addButton.addActionListener(e -> {
            FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
            FileChooser.chooseFile(descriptor, this.project, null, selectedFolder -> {
                String path = selectedFolder.getPath();
                includePathModel.add(path);
            });
        });
        removeButton.addActionListener(e -> {
            int selectedIndex = includePathList.getSelectedIndex();
            if (selectedIndex != -1) {
                includePathModel.removeRow(selectedIndex);
            }
        });
    }

    public JPanel getPanel() {
        return panel;
    }

    /**
     * Returns a copy of settings contained in the form.
     */
    public ProtobufSettings getSettings() {
        ProtobufSettings settings = new ProtobufSettings();
        settings.setIncludePaths(Lists.newArrayList(includePathListList));
        return settings;
    }

    public void reset(ProtobufSettings source) {
        includePathModel.removeAll();
        includePathModel.add(source.getIncludePaths());
    }

}

@@ -520,16 +520,59 @@
 .JPanel;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0A/**%0A *
@@ -1102,16 +1102,97 @@
 stance.%0A
+     *%0A     * @param settings is null if settings dialog runs without a project.%0A
      */%0A
@@ -1266,16 +1266,26 @@
 project,
+ @Nullable
  Protobu
@@ -1400,24 +1400,60 @@
 ayList%3C%3E();%0A
+        if (settings != null) %7B%0A    
         inte
@@ -1504,24 +1504,34 @@
 dePaths());%0A
+        %7D%0A
         incl
@@ -2340,24 +2340,151 @@
         %7D);%0A
+        if (settings == null) %7B%0A            addButton.setEnabled(false);%0A            removeButton.setEnabled(false);%0A        %7D%0A
     %7D%0A%0A    p

Fix NullPointerException in SettingsForm.<init>