com.sciaps.view.PlotConfigPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.sciaps.view.PlotConfigPanel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sciaps.view;

import com.devsmart.ThreadUtils;
import com.sciaps.common.Constants;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.SwingUtilities;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.Range;

/**
 *
 * @author jchen
 */
public class PlotConfigPanel extends javax.swing.JPanel {

    private final int LEFT = 0;
    private final int RIGHT = 1;
    private final int UP = 2;
    private final int DOWN = 3;

    private final XYPlot plot_;
    private final ValueAxis rangeAxis_;
    private final ValueAxis domainAxis_;
    boolean mousePressed_;

    /**
     * Creates new form PlotRangeSetterPanel
     *
     * @param plot
     */
    public PlotConfigPanel(XYPlot plot) {
        initComponents();

        plot_ = plot;
        rangeAxis_ = plot.getRangeAxis();
        domainAxis_ = plot.getDomainAxis();

        btnLeft_.setIcon(createImageIcon("/images/left.png"));
        btnRight_.setIcon(createImageIcon("/images/right.png"));
        btnAutoRange_.setIcon(createImageIcon("/images/center.png"));
        btnUp_.setIcon(createImageIcon("/images/up.png"));
        btnDown_.setIcon(createImageIcon("/images/down.png"));

        mousePressed_ = false;
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupZoom_ = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        chkDomainGridline_ = new javax.swing.JCheckBox();
        chkRangeGridline_ = new javax.swing.JCheckBox();
        chkMarkerAutoZoom_ = new javax.swing.JCheckBox();
        chkShowDataPoint_ = new javax.swing.JCheckBox();
        btnSetBgColor_ = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        radioZoomDomain_ = new javax.swing.JRadioButton();
        radioZoomRange_ = new javax.swing.JRadioButton();
        btnZoomIn_ = new javax.swing.JButton();
        btnZoomOut_ = new javax.swing.JButton();
        btnLeft_ = new javax.swing.JButton();
        btnRight_ = new javax.swing.JButton();
        btnAutoRange_ = new javax.swing.JButton();
        btnUp_ = new javax.swing.JButton();
        btnDown_ = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridBagLayout());

        chkDomainGridline_.setSelected(true);
        chkDomainGridline_.setText("Domain Gridline");
        chkDomainGridline_.setToolTipText("Hide/Show domain gridline");
        chkDomainGridline_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkDomainGridline_ActionPerformed(evt);
            }
        });
        jPanel2.add(chkDomainGridline_, new java.awt.GridBagConstraints());
        chkDomainGridline_.getAccessibleContext().setAccessibleDescription("Show/Hide domain gridline");

        chkRangeGridline_.setSelected(true);
        chkRangeGridline_.setText("Range Gridline");
        chkRangeGridline_.setToolTipText("Hide/Show range gridline");
        chkRangeGridline_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkRangeGridline_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(chkRangeGridline_, gridBagConstraints);
        chkRangeGridline_.getAccessibleContext().setAccessibleDescription("Show/Hide range gridline");

        chkMarkerAutoZoom_.setSelected(true);
        chkMarkerAutoZoom_.setText("Marker Auto Zoom");
        chkMarkerAutoZoom_.setToolTipText("Enable/Disable auto zoom on marker");
        jPanel2.add(chkMarkerAutoZoom_, new java.awt.GridBagConstraints());

        chkShowDataPoint_.setText("Data Point");
        chkShowDataPoint_.setToolTipText("Show/Hide series point shape");
        chkShowDataPoint_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkShowDataPoint_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel2.add(chkShowDataPoint_, gridBagConstraints);
        chkShowDataPoint_.getAccessibleContext().setAccessibleDescription("Show/Hide series data point");

        btnSetBgColor_.setText("BG Color");
        btnSetBgColor_.setToolTipText("Set background color");
        btnSetBgColor_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnSetBgColor_.setMaximumSize(new java.awt.Dimension(70, 20));
        btnSetBgColor_.setMinimumSize(new java.awt.Dimension(70, 20));
        btnSetBgColor_.setPreferredSize(new java.awt.Dimension(70, 20));
        btnSetBgColor_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetBgColor_ActionPerformed(evt);
            }
        });
        jPanel2.add(btnSetBgColor_, new java.awt.GridBagConstraints());

        jPanel3.add(jPanel2);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnGroupZoom_.add(radioZoomDomain_);
        radioZoomDomain_.setSelected(true);
        radioZoomDomain_.setText("Domain");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(radioZoomDomain_, gridBagConstraints);

        btnGroupZoom_.add(radioZoomRange_);
        radioZoomRange_.setText("Range");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        jPanel1.add(radioZoomRange_, gridBagConstraints);

        btnZoomIn_.setText("Zoom In");
        btnZoomIn_.setToolTipText("Set background color");
        btnZoomIn_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnZoomIn_.setMaximumSize(new java.awt.Dimension(85, 20));
        btnZoomIn_.setMinimumSize(new java.awt.Dimension(85, 20));
        btnZoomIn_.setPreferredSize(new java.awt.Dimension(85, 20));
        btnZoomIn_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomIn_ActionPerformed(evt);
            }
        });
        jPanel1.add(btnZoomIn_, new java.awt.GridBagConstraints());

        btnZoomOut_.setText("Zoom Out");
        btnZoomOut_.setToolTipText("Set background color");
        btnZoomOut_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnZoomOut_.setMaximumSize(new java.awt.Dimension(85, 20));
        btnZoomOut_.setMinimumSize(new java.awt.Dimension(85, 20));
        btnZoomOut_.setPreferredSize(new java.awt.Dimension(85, 20));
        btnZoomOut_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZoomOut_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel1.add(btnZoomOut_, gridBagConstraints);

        jPanel3.add(jPanel1);

        btnLeft_.setToolTipText("Shift left");
        btnLeft_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnLeft_.setMaximumSize(new java.awt.Dimension(35, 35));
        btnLeft_.setMinimumSize(new java.awt.Dimension(35, 35));
        btnLeft_.setPreferredSize(new java.awt.Dimension(35, 35));
        btnLeft_.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnLeft_MousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnLeft_MouseReleased(evt);
            }
        });
        btnLeft_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLeft_ActionPerformed(evt);
            }
        });
        jPanel3.add(btnLeft_);

        btnRight_.setToolTipText("Shift right");
        btnRight_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnRight_.setMaximumSize(new java.awt.Dimension(35, 35));
        btnRight_.setMinimumSize(new java.awt.Dimension(35, 35));
        btnRight_.setPreferredSize(new java.awt.Dimension(35, 35));
        btnRight_.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnRight_MousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnRight_MouseReleased(evt);
            }
        });
        btnRight_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRight_ActionPerformed(evt);
            }
        });
        jPanel3.add(btnRight_);

        btnAutoRange_.setToolTipText("Center");
        btnAutoRange_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnAutoRange_.setMaximumSize(new java.awt.Dimension(35, 35));
        btnAutoRange_.setMinimumSize(new java.awt.Dimension(35, 35));
        btnAutoRange_.setPreferredSize(new java.awt.Dimension(35, 35));
        btnAutoRange_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAutoRange_ActionPerformed(evt);
            }
        });
        jPanel3.add(btnAutoRange_);

        btnUp_.setToolTipText("Shift up");
        btnUp_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnUp_.setMaximumSize(new java.awt.Dimension(35, 35));
        btnUp_.setMinimumSize(new java.awt.Dimension(35, 35));
        btnUp_.setPreferredSize(new java.awt.Dimension(35, 35));
        btnUp_.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnUp_MousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnUp_MouseReleased(evt);
            }
        });
        btnUp_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUp_ActionPerformed(evt);
            }
        });
        jPanel3.add(btnUp_);

        btnDown_.setToolTipText("Shift down");
        btnDown_.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnDown_.setMaximumSize(new java.awt.Dimension(35, 35));
        btnDown_.setMinimumSize(new java.awt.Dimension(35, 35));
        btnDown_.setPreferredSize(new java.awt.Dimension(35, 35));
        btnDown_.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnDown_MousePressed(evt);
            }

            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnDown_MouseReleased(evt);
            }
        });
        btnDown_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDown_ActionPerformed(evt);
            }
        });
        jPanel3.add(btnDown_);

        jScrollPane1.setViewportView(jPanel3);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnLeft_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLeft_ActionPerformed
        doShiftLeft();
    }//GEN-LAST:event_btnLeft_ActionPerformed

    private void btnRight_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRight_ActionPerformed
        doShiftRight();
    }//GEN-LAST:event_btnRight_ActionPerformed

    private void btnUp_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUp_ActionPerformed
        doShiftUp();
    }//GEN-LAST:event_btnUp_ActionPerformed

    private void btnDown_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDown_ActionPerformed
        doShiftDown();
    }//GEN-LAST:event_btnDown_ActionPerformed

    private void btnLeft_MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLeft_MousePressed
        mousePressed_ = true;
        doMousePressedAction(LEFT);

    }//GEN-LAST:event_btnLeft_MousePressed

    private void btnLeft_MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLeft_MouseReleased
        mousePressed_ = false;
    }//GEN-LAST:event_btnLeft_MouseReleased

    private void btnRight_MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRight_MousePressed
        mousePressed_ = true;
        doMousePressedAction(RIGHT);
    }//GEN-LAST:event_btnRight_MousePressed

    private void btnRight_MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRight_MouseReleased
        mousePressed_ = false;
    }//GEN-LAST:event_btnRight_MouseReleased

    private void btnUp_MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnUp_MousePressed
        mousePressed_ = true;
        doMousePressedAction(UP);
    }//GEN-LAST:event_btnUp_MousePressed

    private void btnUp_MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnUp_MouseReleased
        mousePressed_ = false;
    }//GEN-LAST:event_btnUp_MouseReleased

    private void btnDown_MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDown_MousePressed
        mousePressed_ = true;
        doMousePressedAction(DOWN);
    }//GEN-LAST:event_btnDown_MousePressed

    private void btnDown_MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDown_MouseReleased
        mousePressed_ = false;
    }//GEN-LAST:event_btnDown_MouseReleased

    private void btnAutoRange_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAutoRange_ActionPerformed
        domainAxis_.setAutoRange(true);
        rangeAxis_.setAutoRange(true);
    }//GEN-LAST:event_btnAutoRange_ActionPerformed

    private void chkDomainGridline_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkDomainGridline_ActionPerformed
        plot_.setDomainGridlinesVisible(chkDomainGridline_.isSelected());
    }//GEN-LAST:event_chkDomainGridline_ActionPerformed

    private void chkRangeGridline_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkRangeGridline_ActionPerformed
        plot_.setRangeGridlinesVisible(chkRangeGridline_.isSelected());
    }//GEN-LAST:event_chkRangeGridline_ActionPerformed

    private void btnSetBgColor_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetBgColor_ActionPerformed
        Color backgroundColor = JColorChooser.showDialog(Constants.MAIN_FRAME, "Choose chart background color",
                Color.white);
        plot_.setBackgroundPaint(backgroundColor);
    }//GEN-LAST:event_btnSetBgColor_ActionPerformed

    private void chkShowDataPoint_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkShowDataPoint_ActionPerformed
        XYSplineRenderer render = (XYSplineRenderer) plot_.getRenderer();
        boolean value = chkShowDataPoint_.isSelected();
        for (int i = 0; i < plot_.getSeriesCount(); i++) {
            render.setSeriesShapesVisible(i, value);
        }
    }//GEN-LAST:event_chkShowDataPoint_ActionPerformed

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

        if (radioZoomDomain_.isSelected()) {
            double lowerVal = plot_.getDomainAxis().getLowerBound();
            double upperVal = plot_.getDomainAxis().getUpperBound();
            double offset = (upperVal - lowerVal) * .05;
            plot_.getDomainAxis().setLowerBound(lowerVal + offset);
            plot_.getDomainAxis().setUpperBound(upperVal - offset);
        } else {
            double lowerVal = plot_.getRangeAxis().getLowerBound();
            double upperVal = plot_.getRangeAxis().getUpperBound();
            double offset = (upperVal - lowerVal) * .05;
            plot_.getRangeAxis().setLowerBound(lowerVal + offset);
            plot_.getRangeAxis().setUpperBound(upperVal - offset);
        }
    }//GEN-LAST:event_btnZoomIn_ActionPerformed

    private void btnZoomOut_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZoomOut_ActionPerformed
        if (radioZoomDomain_.isSelected()) {
            double lowerVal = plot_.getDomainAxis().getLowerBound();
            double upperVal = plot_.getDomainAxis().getUpperBound();
            double offset = (upperVal - lowerVal) * .05;
            plot_.getDomainAxis().setLowerBound(lowerVal - offset);
            plot_.getDomainAxis().setUpperBound(upperVal + offset);
        } else {
            double lowerVal = plot_.getRangeAxis().getLowerBound();
            double upperVal = plot_.getRangeAxis().getUpperBound();
            double offset = (upperVal - lowerVal) * .05;
            plot_.getRangeAxis().setLowerBound(lowerVal - offset);
            plot_.getRangeAxis().setUpperBound(upperVal + offset);
        }
    }//GEN-LAST:event_btnZoomOut_ActionPerformed

    private void doMousePressedAction(final int action) {

        ThreadUtils.IOThreads.execute(new Runnable() {

            @Override
            public void run() {
                while (mousePressed_) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {

                            switch (action) {
                            case LEFT:
                                doShiftLeft();
                                break;
                            case RIGHT:
                                doShiftRight();
                                break;
                            case UP:
                                doShiftUp();
                                break;
                            case DOWN:
                                doShiftDown();
                                break;
                            default:
                                mousePressed_ = false;
                            }
                        }
                    });

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        mousePressed_ = false;
                    }

                }
            }
        });
    }

    private Range createDomainRange(final int action) {
        double lowerBound = domainAxis_.getRange().getLowerBound();
        double upperBound = domainAxis_.getRange().getUpperBound();
        double shiftValue = Math.ceil((upperBound - lowerBound) * .05);

        if (action == LEFT) {
            return (new Range(lowerBound - shiftValue, upperBound - shiftValue));
        } else if (action == RIGHT) {
            return (new Range(lowerBound + shiftValue, upperBound + shiftValue));
        }

        return null;
    }

    private Range createRangeRange(final int action) {
        double lowerBound = rangeAxis_.getRange().getLowerBound();
        double upperBound = rangeAxis_.getRange().getUpperBound();
        double shiftValue = (upperBound - lowerBound) * .05;

        if (action == DOWN) {
            return (new Range(lowerBound - shiftValue, upperBound - shiftValue));
        } else if (action == UP) {
            return (new Range(lowerBound + shiftValue, upperBound + shiftValue));
        }

        return null;
    }

    private void doShiftLeft() {
        Range range = createDomainRange(LEFT);
        if (range != null) {
            domainAxis_.setRange(range);
        }
    }

    private void doShiftRight() {
        Range range = createDomainRange(RIGHT);
        if (range != null) {
            domainAxis_.setRange(range);
        }
    }

    private void doShiftUp() {
        Range range = createRangeRange(UP);
        if (range != null) {
            rangeAxis_.setRange(range);
        }
    }

    private void doShiftDown() {
        Range range = createRangeRange(DOWN);
        if (range != null) {
            rangeAxis_.setRange(range);
        }
    }

    private ImageIcon createImageIcon(String path) {

        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        }

        return null;
    }

    public boolean getLineShapeVisibility() {
        return chkShowDataPoint_.isSelected();
    }

    public boolean getMarkerAutoZoom() {
        return chkMarkerAutoZoom_.isSelected();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAutoRange_;
    private javax.swing.JButton btnDown_;
    private javax.swing.ButtonGroup btnGroupZoom_;
    private javax.swing.JButton btnLeft_;
    private javax.swing.JButton btnRight_;
    private javax.swing.JButton btnSetBgColor_;
    private javax.swing.JButton btnUp_;
    private javax.swing.JButton btnZoomIn_;
    private javax.swing.JButton btnZoomOut_;
    private javax.swing.JCheckBox chkDomainGridline_;
    private javax.swing.JCheckBox chkMarkerAutoZoom_;
    private javax.swing.JCheckBox chkRangeGridline_;
    private javax.swing.JCheckBox chkShowDataPoint_;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton radioZoomDomain_;
    private javax.swing.JRadioButton radioZoomRange_;
    // End of variables declaration//GEN-END:variables
}