/*
 * Copyright 2006-2020 The MZmine Development Team
 *
 * This file is part of MZmine.
 *
 * MZmine is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * MZmine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with MZmine; if not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

package io.github.mzmine.modules.visualization.msms;

import com.google.common.collect.Range;
import io.github.mzmine.datamodel.RawDataFile;
import io.github.mzmine.gui.chartbasics.simplechart.SimpleXYZScatterPlot;
import io.github.mzmine.gui.chartbasics.simplechart.datasets.ColoredXYZDataset;
import io.github.mzmine.gui.chartbasics.simplechart.renderers.ColoredXYZDotRenderer;
import io.github.mzmine.main.MZmineCore;
import io.github.mzmine.modules.visualization.spectra.simplespectra.SpectraVisualizerModule;
import io.github.mzmine.modules.visualization.spectra.simplespectra.SpectraVisualizerParameters;
import io.github.mzmine.parameters.ParameterSet;
import io.github.mzmine.parameters.parametertypes.selectors.RawDataFilesSelectionType;
import io.github.mzmine.taskcontrol.TaskStatus;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.MouseButton;
import org.jetbrains.annotations.Nullable;
import org.jfree.chart.util.SortOrder;

public class MsMsChart extends SimpleXYZScatterPlot<MsMsDataProvider> {

  private MsMsDataProvider dataProvider;
  private ColoredXYZDataset dataset;
  private ColoredXYZDotRenderer renderer;
  private SortOrder zOrder;

  public MsMsChart(ParameterSet parameters) {
    super("MS/MS visualizer");

    MsMsXYAxisType xAxisType = parameters.getParameter(MsMsParameters.xAxisType).getValue();
    MsMsXYAxisType yAxisType = parameters.getParameter(MsMsParameters.yAxisType).getValue();
    RawDataFile[] dataFiles = parameters.getParameter(MsMsParameters.dataFiles).getValue()
        .getMatchingRawDataFiles();

    setDomainAxisLabel(xAxisType.toString());
    setRangeAxisLabel(yAxisType.toString());
    setDomainAxisNumberFormatOverride(MZmineCore.getConfiguration().getRTFormat());
    setRangeAxisNumberFormatOverride(MZmineCore.getConfiguration().getMZFormat());

    renderer = new ColoredXYZDotRenderer();

    dataProvider = new MsMsDataProvider(parameters);
    dataset = new ColoredXYZDataset(dataProvider);
    addDataset(dataset, renderer);

    getXYPlot().getDomainAxis().setUpperMargin(0);
    getXYPlot().getDomainAxis().setLowerMargin(0);
    getXYPlot().getRangeAxis().setUpperMargin(0);
    getXYPlot().getRangeAxis().setLowerMargin(0);

    // Show spectrum of the clicked data point scan
    setOnMousePressed(event -> {

      if (!event.getButton().equals(MouseButton.PRIMARY) || event.getClickCount() != 2) {
        return;
      }

      MsMsDataPoint clickedDataPoint = dataProvider.getDataPoint(getCursorPosition().getValueIndex());

      // Run spectrum module
      ParameterSet spectrumParameters =
          MZmineCore.getConfiguration().getModuleParameters(SpectraVisualizerModule.class);
      spectrumParameters.getParameter(SpectraVisualizerParameters.dataFiles)
          .setValue(RawDataFilesSelectionType.SPECIFIC_FILES, dataFiles);
      spectrumParameters.getParameter(SpectraVisualizerParameters.scanNumber)
          .setValue(clickedDataPoint.getScanNumber());
      MZmineCore.runMZmineModule(SpectraVisualizerModule.class, spectrumParameters);
    });

    // Do not show legend
    setLegendCanvas(new Canvas());
  }

  public MsMsXYAxisType getXAxisType() {
    return dataProvider.getXAxisType();
  }

  public MsMsXYAxisType getYAxisType() {
    return dataProvider.getYAxisType();
  }

  public void setXAxisType(MsMsXYAxisType xAxisType) {
    dataProvider.setXAxisType(xAxisType);
    dataProvider.sortZValues(zOrder);
    dataset.fireDatasetChanged();
  }

  public void setYAxisType(MsMsXYAxisType yAxisType) {
    dataProvider.setYAxisType(yAxisType);
    dataProvider.sortZValues(zOrder);
    dataset.fireDatasetChanged();
  }

  public void setZAxisType(MsMsZAxisType zAxisType) {
    dataProvider.setZAxisType(zAxisType);
    dataProvider.sortZValues(zOrder);
    dataset.fireDatasetChanged();
  }

  public void highlightPoints(MsMsXYAxisType valuesType1, @Nullable Range<Double> range1,
      MsMsXYAxisType valuesType2, @Nullable Range<Double> range2) {
    dataProvider.highlightPoints(valuesType1, range1, valuesType2, range2);
    dataProvider.sortZValues(zOrder);
    dataset.fireDatasetChanged();
  }

  public void setDataFile(RawDataFile dataFile) {
    dataProvider.setDataFile(dataFile);
    dataset.run();
    dataProvider.sortZValues(zOrder);
    dataset.fireDatasetChanged();
  }

  public void sortZValues(SortOrder zOrder) {
    this.zOrder = zOrder;
    dataProvider.sortZValues(zOrder);
    renderer.setZOrder(zOrder);
    dataset.fireDatasetChanged();
  }

  public SimpleObjectProperty<TaskStatus> datasetStatusProperty() {
    return dataset.statusProperty();
  }

}

@@ -1572,16 +1572,39 @@
 Status;%0A
+import java.awt.Color;%0A
 import j
@@ -3060,24 +3060,74 @@
 Margin(0);%0A%0A
+    getXYPlot().setBackgroundPaint(Color.WHITE);%0A%0A
     // Show 

Fix MS/MS visualizer background color