umich.ms.batmass.gui.viewers.map2d.options.Map2DOptionsPanel.java Source code

Java tutorial

Introduction

Here is the source code for umich.ms.batmass.gui.viewers.map2d.options.Map2DOptionsPanel.java

Source

/* 
 * Copyright 2016 Dmitry Avtonomov.
 *
 * 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 umich.ms.batmass.gui.viewers.map2d.options;

import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.FileConfiguration;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.util.Exceptions;
import org.openide.windows.WindowManager;
import umich.ms.batmass.colorpicker.com.bric.swing.ColorPicker;
import umich.ms.batmass.gui.core.api.util.color.ColorHelper;
import umich.ms.batmass.gui.core.components.util.textinput.DocumentFilters;
import umich.ms.batmass.gui.viewers.map2d.components.BaseMap2D;
import umich.ms.batmass.gui.viewers.map2d.norm.RangeNormalizers;
import umich.ms.batmass.nbputils.config.BmConfig;

final class Map2DOptionsPanel extends javax.swing.JPanel {

    private final Map2DOptionsOptionsPanelController controller;
    private CompositeConfiguration config;
    private DefaultListModel<Color> colorListModel;

    Map2DOptionsPanel(Map2DOptionsOptionsPanelController controller) {
        this.controller = controller;
        this.config = new CompositeConfiguration();
        initComponents();
        colorList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (colorList.getSelectedValuesList().size() > 0) {
                    btnChangeColor.setEnabled(true);
                    btnMoveColorUp.setEnabled(true);
                    btnMoveDown.setEnabled(true);
                    btnRemoveColor.setEnabled(true);
                } else {
                    btnChangeColor.setEnabled(false);
                    btnMoveColorUp.setEnabled(false);
                    btnMoveDown.setEnabled(false);
                    btnRemoveColor.setEnabled(false);
                }
            }
        });
    }

    private ComboBoxModel<String> createIntensityNormComboModel() {
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
        model.addElement(RangeNormalizers.LOG.name());
        model.addElement(RangeNormalizers.SQUARE_ROOT.name());
        model.addElement(RangeNormalizers.NONE.name());
        return model;
    }

    private static class ColorListModel extends DefaultListModel<Color> {

        List<Color> colors = new LinkedList<>();

        public ColorListModel(Color[] colors) {
            this.colors.addAll(Arrays.asList(colors));
        }
    }

    private static class ColorCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            Color color = (Color) value;
            c.setBackground(color);
            setText(String.format("R=%d, G=%d, B=%d", color.getRed(), color.getGreen(), color.getBlue()));
            return c;
        }
    }

    private void swapColorsInModel(int idx1, int idx2) {
        Color c1 = colorListModel.getElementAt(idx1);
        Color c2 = colorListModel.getElementAt(idx2);
        colorListModel.setElementAt(c2, idx1);
        colorListModel.setElementAt(c1, idx2);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelColors = new javax.swing.JPanel();
        colorScrollPane = new javax.swing.JScrollPane();
        colorList = new javax.swing.JList<>();
        btnMoveColorUp = new javax.swing.JButton();
        btnMoveDown = new javax.swing.JButton();
        btnAddColor = new javax.swing.JButton();
        btnRemoveColor = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        textFieldNumColors = new javax.swing.JTextField();
        btnChangeColor = new javax.swing.JButton();
        panel2DMapOptions = new javax.swing.JPanel();
        checkBoxInterpRt = new javax.swing.JCheckBox();
        checkBoxBasePeak = new javax.swing.JCheckBox();
        checkBoxProfileModeGapFilling = new javax.swing.JCheckBox();
        checkBoxMzCloseZoomInterp = new javax.swing.JCheckBox();
        comboIntensityNorm = new javax.swing.JComboBox<>();
        jLabel1 = new javax.swing.JLabel();
        panelMisc = new javax.swing.JPanel();
        btnReset = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        panelColors.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.panelColors.border.title"))); // NOI18N

        colorList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        colorList.setDragEnabled(true);
        colorList.setVisibleRowCount(10);
        colorScrollPane.setViewportView(colorList);

        org.openide.awt.Mnemonics.setLocalizedText(btnMoveColorUp, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.btnMoveColorUp.text")); // NOI18N
        btnMoveColorUp.setEnabled(false);
        btnMoveColorUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveColorUpActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnMoveDown, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.btnMoveDown.text")); // NOI18N
        btnMoveDown.setEnabled(false);
        btnMoveDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMoveDownActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnAddColor, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.btnAddColor.text")); // NOI18N
        btnAddColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddColorActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnRemoveColor, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.btnRemoveColor.text")); // NOI18N
        btnRemoveColor.setEnabled(false);
        btnRemoveColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveColorActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
                org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.jLabel3.text")); // NOI18N

        textFieldNumColors.setDocument(DocumentFilters.getDigitsOnlyFilter());
        textFieldNumColors.setText(org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class,
                "Map2DOptionsPanel.textFieldNumColors.text")); // NOI18N
        textFieldNumColors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldNumColorsActionPerformed(evt);
            }
        });
        textFieldNumColors.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                textFieldNumColorsFocusGained(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnChangeColor, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.btnChangeColor.text")); // NOI18N
        btnChangeColor.setEnabled(false);
        btnChangeColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeColorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelColorsLayout = new javax.swing.GroupLayout(panelColors);
        panelColors.setLayout(panelColorsLayout);
        panelColorsLayout.setHorizontalGroup(panelColorsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelColorsLayout.createSequentialGroup().addContainerGap()
                        .addGroup(panelColorsLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(panelColorsLayout.createSequentialGroup().addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(textFieldNumColors))
                                .addComponent(colorScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 238,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelColorsLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(btnMoveColorUp, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnMoveDown, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnAddColor, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnRemoveColor, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnChangeColor, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(22, Short.MAX_VALUE)));
        panelColorsLayout.setVerticalGroup(panelColorsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelColorsLayout.createSequentialGroup().addContainerGap().addGroup(panelColorsLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(panelColorsLayout.createSequentialGroup().addComponent(btnMoveColorUp)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnMoveDown)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnChangeColor).addGap(18, 18, 18).addComponent(btnAddColor)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnRemoveColor))
                        .addComponent(colorScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panelColorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(textFieldNumColors,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        panel2DMapOptions.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.panel2DMapOptions.border.title"))); // NOI18N

        checkBoxInterpRt.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(checkBoxInterpRt, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.checkBoxInterpRt.text")); // NOI18N
        checkBoxInterpRt.setToolTipText(org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class,
                "Map2DOptionsPanel.checkBoxInterpRt.toolTipText")); // NOI18N
        checkBoxInterpRt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxInterpRtActionPerformed(evt);
            }
        });

        checkBoxBasePeak.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(checkBoxBasePeak, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.checkBoxBasePeak.text")); // NOI18N
        checkBoxBasePeak.setToolTipText(org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class,
                "Map2DOptionsPanel.checkBoxBasePeak.toolTipText")); // NOI18N
        checkBoxBasePeak.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxBasePeakActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(checkBoxProfileModeGapFilling, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.checkBoxProfileModeGapFilling.text")); // NOI18N
        checkBoxProfileModeGapFilling.setToolTipText(org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class,
                "Map2DOptionsPanel.checkBoxProfileModeGapFilling.toolTipText")); // NOI18N
        checkBoxProfileModeGapFilling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxProfileModeGapFillingActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(checkBoxMzCloseZoomInterp, org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.checkBoxMzCloseZoomInterp.text")); // NOI18N
        checkBoxMzCloseZoomInterp.setToolTipText(org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class,
                "Map2DOptionsPanel.checkBoxMzCloseZoomInterp.toolTipText")); // NOI18N
        checkBoxMzCloseZoomInterp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxMzCloseZoomInterpActionPerformed(evt);
            }
        });

        comboIntensityNorm.setModel(createIntensityNormComboModel());

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
                org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout panel2DMapOptionsLayout = new javax.swing.GroupLayout(panel2DMapOptions);
        panel2DMapOptions.setLayout(panel2DMapOptionsLayout);
        panel2DMapOptionsLayout.setHorizontalGroup(panel2DMapOptionsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panel2DMapOptionsLayout.createSequentialGroup().addContainerGap()
                        .addGroup(panel2DMapOptionsLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel2DMapOptionsLayout
                                        .createSequentialGroup().addComponent(checkBoxInterpRt).addGap(18, 18, 18)
                                        .addComponent(checkBoxBasePeak).addGap(18, 18, 18)
                                        .addComponent(checkBoxProfileModeGapFilling).addGap(0, 0, Short.MAX_VALUE))
                                .addGroup(panel2DMapOptionsLayout.createSequentialGroup()
                                        .addGroup(panel2DMapOptionsLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(panel2DMapOptionsLayout.createSequentialGroup()
                                                        .addComponent(jLabel1)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(comboIntensityNorm,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 125,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addComponent(checkBoxMzCloseZoomInterp))
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));
        panel2DMapOptionsLayout.setVerticalGroup(panel2DMapOptionsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panel2DMapOptionsLayout.createSequentialGroup().addContainerGap(10, Short.MAX_VALUE)
                        .addGroup(panel2DMapOptionsLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(checkBoxInterpRt).addComponent(checkBoxBasePeak)
                                .addComponent(checkBoxProfileModeGapFilling))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(checkBoxMzCloseZoomInterp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel2DMapOptionsLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(comboIntensityNorm, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1))
                        .addGap(21, 21, 21)));

        panelMisc.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.panelMisc.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(btnReset,
                org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.btnReset.text")); // NOI18N
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton1,
                org.openide.util.NbBundle.getMessage(Map2DOptionsPanel.class, "Map2DOptionsPanel.jButton1.text")); // NOI18N
        jButton1.setEnabled(false);

        javax.swing.GroupLayout panelMiscLayout = new javax.swing.GroupLayout(panelMisc);
        panelMisc.setLayout(panelMiscLayout);
        panelMiscLayout.setHorizontalGroup(panelMiscLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelMiscLayout.createSequentialGroup().addContainerGap()
                        .addGroup(panelMiscLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton1).addComponent(btnReset,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 119,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        panelMiscLayout.setVerticalGroup(panelMiscLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelMiscLayout.createSequentialGroup().addContainerGap().addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(btnReset)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(panelMisc, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(panel2DMapOptions, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(panelColors, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(panel2DMapOptions, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(panelColors, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelMisc, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void btnMoveColorUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveColorUpActionPerformed
        controller.changed();
        Color selectedColor = colorList.getSelectedValue();
        int selectedColorIdx = colorList.getSelectedIndex();
        if (selectedColor == null || selectedColorIdx == 0) {
            return;
        }
        if (selectedColorIdx > 0) {
            int prevColorIdx = selectedColorIdx - 1;
            swapColorsInModel(selectedColorIdx, prevColorIdx);
            colorList.setSelectedIndex(prevColorIdx);
        }
    }//GEN-LAST:event_btnMoveColorUpActionPerformed

    private void btnMoveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMoveDownActionPerformed
        controller.changed();
        Color selectedColor = colorList.getSelectedValue();
        int selectedColorIdx = colorList.getSelectedIndex();
        if (selectedColor == null || selectedColorIdx == colorListModel.size() - 1) {
            return;
        }
        if (selectedColorIdx < colorListModel.size() - 1) {
            int nextColorIdx = selectedColorIdx + 1;
            swapColorsInModel(selectedColorIdx, nextColorIdx);
            colorList.setSelectedIndex(nextColorIdx);
        }
    }//GEN-LAST:event_btnMoveDownActionPerformed

    private void btnAddColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddColorActionPerformed
        controller.changed();
        Color selectedColor = colorList.getSelectedValue();
        int selectedColorIdx = colorList.getSelectedIndex();
        if (selectedColor == null && colorListModel.size() > 0) {
            selectedColorIdx = colorListModel.size() - 1;
            selectedColor = colorListModel.get(colorListModel.size() - 1);
        } else if (colorListModel.size() == 0) {
            selectedColorIdx = -1;
            selectedColor = Color.RED;
        }
        int newColorIdx = selectedColorIdx + 1;
        Frame wnd = WindowManager.getDefault().getMainWindow();
        String colorPickerTitle = "Add new color";
        Color newColor = ColorPicker.showDialog(wnd, colorPickerTitle, selectedColor, false);
        if (newColor != null) {
            colorListModel.add(newColorIdx, newColor);
            colorList.setSelectedIndex(newColorIdx);
        }

    }//GEN-LAST:event_btnAddColorActionPerformed

    private void btnRemoveColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveColorActionPerformed
        controller.changed();
        Color selectedColor = colorList.getSelectedValue();
        int selectedColorIdx = colorList.getSelectedIndex();
        if (selectedColor != null) {
            colorListModel.remove(selectedColorIdx);
            if (selectedColorIdx < colorListModel.size()) {
                colorList.setSelectedIndex(selectedColorIdx);
            } else if (colorListModel.size() > 0) {
                colorList.setSelectedIndex(colorListModel.size() - 1);
            }
        }
    }//GEN-LAST:event_btnRemoveColorActionPerformed

    private void textFieldNumColorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldNumColorsActionPerformed
        controller.changed();
    }//GEN-LAST:event_textFieldNumColorsActionPerformed

    private void textFieldNumColorsFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textFieldNumColorsFocusGained
        textFieldNumColors.selectAll();
    }//GEN-LAST:event_textFieldNumColorsFocusGained

    private void btnChangeColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeColorActionPerformed

        Color selectedColor = colorList.getSelectedValue();
        int selectedColorIdx = colorList.getSelectedIndex();

        if (selectedColor != null) {
            Frame wnd = WindowManager.getDefault().getMainWindow();
            String colorPickerTitle = "Choose Color";
            Color newColor = ColorPicker.showDialog(wnd, colorPickerTitle, selectedColor, false);

            // This is the stock JDK Color Chooser.
            // To use it just comment the line above and uncomment this one.
            //Color newColor = JColorChooser.showDialog(wnd, "Choose Color", selectedColor);

            if (newColor != null) {
                colorListModel.setElementAt(newColor, selectedColorIdx);
            }
        }

    }//GEN-LAST:event_btnChangeColorActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        Map2DOptions opts = Map2DOptions.getInstance();

        String msg = "Are you sure you want to reset?\n" + "All changes will be lost.";
        NotifyDescriptor d = new NotifyDescriptor.Confirmation(msg, "Reset 2D map Options");
        d.setMessageType(NotifyDescriptor.QUESTION_MESSAGE);
        d.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
        Object choice = DialogDisplayer.getDefault().notify(d);
        if (!NotifyDescriptor.OK_OPTION.equals(choice)) {
            return;
        }

        try {
            opts.flushUserConfigToDefault();
            load();
        } catch (ConfigurationException | IOException e) {
            Exceptions.printStackTrace(e);
        }
    }//GEN-LAST:event_btnResetActionPerformed

    private void checkBoxMzCloseZoomInterpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxMzCloseZoomInterpActionPerformed
        controller.changed();
    }//GEN-LAST:event_checkBoxMzCloseZoomInterpActionPerformed

    private void checkBoxProfileModeGapFillingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxProfileModeGapFillingActionPerformed
        controller.changed();
    }//GEN-LAST:event_checkBoxProfileModeGapFillingActionPerformed

    private void checkBoxBasePeakActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxBasePeakActionPerformed
        controller.changed();
    }//GEN-LAST:event_checkBoxBasePeakActionPerformed

    private void checkBoxInterpRtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxInterpRtActionPerformed
        controller.changed();
    }//GEN-LAST:event_checkBoxInterpRtActionPerformed

    void load() throws ConfigurationException, IOException {
        config.clear();
        config = Map2DOptions.getInstance().getConfig();
        // the order of adding configurations to combined configuration is important
        // whoever is added first "shadows" the same key in all subsequently added
        // configuraions. Thus, add the DEFAULTs at the very end
        //        FileConfiguration userConfig = BmConfig.forClass(BaseMap2D.class);
        //        config.addConfiguration(userConfig);
        //        Configuration defaultConfig = Map2DOptions.getDefaultConfig();
        //        config.addConfiguration(defaultConfig);

        checkBoxBasePeak.setSelected(config.getBoolean("doBasePeakMode"));
        checkBoxInterpRt.setSelected(config.getBoolean("doUpscaling"));
        checkBoxMzCloseZoomInterp.setSelected(config.getBoolean("doMzCloseZoomGapFilling"));
        checkBoxProfileModeGapFilling.setSelected(config.getBoolean("doProfileModeGapFilling"));
        textFieldNumColors.setText(Integer.toString(config.getInt("colorLevels")));

        // populating color list
        colorList.setCellRenderer(new ColorCellRenderer());
        colorListModel = new DefaultListModel<>();
        Color[] colorsFromConfig = Map2DOptions.getColorsFromConfig(config);
        for (Color c : colorsFromConfig) {
            colorListModel.addElement(c);
        }
        colorList.setModel(colorListModel);
        //        colorList.setModel(new ColorListModel(getColorsFromConfig(config)));

        comboIntensityNorm.setSelectedItem(config.getString("intensityNormalizer"));
    }

    private void updateConfigToUIState() {
        config.setProperty("doBasePeakMode", checkBoxBasePeak.isSelected());
        config.setProperty("doUpscaling", checkBoxInterpRt.isSelected());
        config.setProperty("doMzCloseZoomGapFilling", checkBoxMzCloseZoomInterp.isSelected());
        config.setProperty("doProfileModeGapFilling", checkBoxProfileModeGapFilling.isSelected());
        config.setProperty("colorLevels", Integer.parseInt(textFieldNumColors.getText()));
        config.setProperty("colorPivots", getColorsFromUI());
        config.setProperty("intensityNormalizer", comboIntensityNorm.getModel().getSelectedItem());
    }

    void store() throws ConfigurationException, IOException {
        updateConfigToUIState();

        FileConfiguration userConfig = BmConfig.forClass(BaseMap2D.class);
        for (Iterator<String> i = config.getKeys(); i.hasNext();) {
            String key = i.next();
            Object value = config.getProperty(key);
            userConfig.setProperty(key, value);
        }
        userConfig.save();
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }

    private String[] getColorsFromUI() {
        int numColors = colorList.getModel().getSize();
        String[] colors = new String[numColors];

        for (int i = 0; i < numColors; i++) {
            Color color = colorList.getModel().getElementAt(i);
            colors[i] = ColorHelper.toHexString(color);
        }
        return colors;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddColor;
    private javax.swing.JButton btnChangeColor;
    private javax.swing.JButton btnMoveColorUp;
    private javax.swing.JButton btnMoveDown;
    private javax.swing.JButton btnRemoveColor;
    private javax.swing.JButton btnReset;
    private javax.swing.JCheckBox checkBoxBasePeak;
    private javax.swing.JCheckBox checkBoxInterpRt;
    private javax.swing.JCheckBox checkBoxMzCloseZoomInterp;
    private javax.swing.JCheckBox checkBoxProfileModeGapFilling;
    private javax.swing.JList<Color> colorList;
    private javax.swing.JScrollPane colorScrollPane;
    private javax.swing.JComboBox<String> comboIntensityNorm;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel panel2DMapOptions;
    private javax.swing.JPanel panelColors;
    private javax.swing.JPanel panelMisc;
    private javax.swing.JTextField textFieldNumColors;
    // End of variables declaration//GEN-END:variables
}