|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.ui.swing.viewer.plot; |
| 33 | + |
| 34 | +import org.jfree.chart.ChartPanel; |
| 35 | +import org.jfree.chart.JFreeChart; |
| 36 | +import org.scijava.convert.ConvertService; |
| 37 | +import org.scijava.plot.Plot; |
| 38 | +import org.scijava.ui.viewer.DisplayWindow; |
| 39 | +import org.scijava.ui.viewer.plot.PlotDisplay; |
| 40 | +import org.scijava.ui.viewer.plot.PlotDisplayPanel; |
| 41 | + |
| 42 | +import javax.swing.*; |
| 43 | +import java.awt.*; |
| 44 | +import java.util.Objects; |
| 45 | + |
| 46 | +/** |
| 47 | + * A JFreeChart-driven display panel for {@link Plot}s. |
| 48 | + * |
| 49 | + * @author Curtis Rueden |
| 50 | + */ |
| 51 | +public class SwingPlotDisplayPanel extends JPanel implements PlotDisplayPanel |
| 52 | +{ |
| 53 | + |
| 54 | + // -- instance variables -- |
| 55 | + |
| 56 | + private final DisplayWindow window; |
| 57 | + private final PlotDisplay display; |
| 58 | + private final ConvertService convertService; |
| 59 | + private Dimension prefferedSize; |
| 60 | + |
| 61 | + // -- constructor -- |
| 62 | + |
| 63 | + public SwingPlotDisplayPanel(final PlotDisplay display, |
| 64 | + final DisplayWindow window, final ConvertService convertService) |
| 65 | + { |
| 66 | + this.display = display; |
| 67 | + this.window = window; |
| 68 | + this.convertService = convertService; |
| 69 | + setLayout(new BorderLayout()); |
| 70 | + initPreferredSize(); |
| 71 | + setupChart(); |
| 72 | + window.setContent(this); |
| 73 | + } |
| 74 | + |
| 75 | + private void initPreferredSize() { |
| 76 | + Plot plot = display.get(0); |
| 77 | + prefferedSize = new Dimension(plot.getPreferredWidth(), plot.getPreferredHeight()); |
| 78 | + } |
| 79 | + |
| 80 | + private void setupChart() { |
| 81 | + final JFreeChart chart = convertToJFreeChart(display.get(0)); |
| 82 | + add(new ChartPanel(chart)); |
| 83 | + } |
| 84 | + |
| 85 | + private JFreeChart convertToJFreeChart(Plot plot) { |
| 86 | + return Objects.requireNonNull(convertService.convert(plot, JFreeChart.class)); |
| 87 | + } |
| 88 | + |
| 89 | + public static boolean supports(Plot abstractPlot, ConvertService convertService) { |
| 90 | + return convertService.supports(abstractPlot, JFreeChart.class); |
| 91 | + } |
| 92 | + |
| 93 | + // -- PlotDisplayPanel methods -- |
| 94 | + |
| 95 | + @Override |
| 96 | + public PlotDisplay getDisplay() { |
| 97 | + return display; |
| 98 | + } |
| 99 | + |
| 100 | + // -- DisplayPanel methods -- |
| 101 | + |
| 102 | + @Override |
| 103 | + public DisplayWindow getWindow() { |
| 104 | + return window; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void redoLayout() { } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void setLabel(final String s) { } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void redraw() { } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Dimension getPreferredSize() { |
| 118 | + return prefferedSize; |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments