All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.diirt.datasource.sample.graphene.LineGraphSample Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-18 diirt developers. See COPYRIGHT.TXT
 * All rights reserved. Use is subject to license terms. See LICENSE.TXT
 */
package org.diirt.datasource.sample.graphene;

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.diirt.graphene.InterpolationScheme;
import org.diirt.graphene.LineGraph2DRendererUpdate;
import org.diirt.datasource.PVReader;
import org.diirt.datasource.PVManager;
import org.diirt.datasource.PVReaderListener;
import org.diirt.vtype.ValueUtil;
import org.diirt.datasource.PVReaderEvent;
import org.diirt.datasource.graphene.Graph2DResult;
import org.diirt.datasource.graphene.LineGraph2DExpression;
import static org.diirt.datasource.graphene.ExpressionLanguage.*;
import static org.diirt.datasource.formula.ExpressionLanguage.*;
import org.diirt.datasource.sample.SetupUtil;
import static org.diirt.util.concurrent.Executors.swingEDT;
import static org.diirt.util.time.TimeDuration.*;

/**
 *
 * @author carcassi
 */
public class LineGraphSample extends javax.swing.JFrame {

    /**
     * Creates new form MockWaterfallPlot
     */
    public LineGraphSample() {
        initComponents();
        plotView.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent e) {

                if (plot != null) {
                    plot.update(new LineGraph2DRendererUpdate().imageHeight(plotView.getHeight()).imageWidth(plotView.getWidth()));
                }
            }
        });
    }
    private PVReader pv;
    private LineGraph2DExpression plot;

    /**
     * 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")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        dataFormulaField = new javax.swing.JTextField();
        lastError = new javax.swing.JTextField();
        plotView = new org.diirt.datasource.sample.ImagePanel();
        jLabel2 = new javax.swing.JLabel();
        xColumnField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        yColumnField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tooltipColumnField = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Data Formula:");

        dataFormulaField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dataFormulaFieldActionPerformed(evt);
            }
        });

        lastError.setEditable(false);

        javax.swing.GroupLayout plotViewLayout = new javax.swing.GroupLayout(plotView);
        plotView.setLayout(plotViewLayout);
        plotViewLayout.setHorizontalGroup(
            plotViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 497, Short.MAX_VALUE)
        );
        plotViewLayout.setVerticalGroup(
            plotViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 197, Short.MAX_VALUE)
        );

        jLabel2.setText("X Column:");

        xColumnField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xColumnFieldActionPerformed(evt);
            }
        });

        jLabel3.setText("Y Column:");

        yColumnField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yColumnFieldActionPerformed(evt);
            }
        });

        jLabel4.setText("Tooltip Column:");

        tooltipColumnField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tooltipColumnFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dataFormulaField)
                        .addGap(12, 12, 12))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(plotView, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lastError))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(xColumnField)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(yColumnField)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tooltipColumnField)
                        .addContainerGap())))
            .addComponent(jSeparator1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dataFormulaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(xColumnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(yColumnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tooltipColumnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(plotView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lastError, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// //GEN-END:initComponents

    private void dataFormulaFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dataFormulaFieldActionPerformed
        if (pv != null) {
            pv.close();
            plotView.setImage(null);
            plot = null;
        }

        if (dataFormulaField.getText() == null || dataFormulaField.getText().trim().isEmpty()) {
            return;
        }

        if (plot == null) {
            plot = lineGraphOf(formula(dataFormulaField.getText()),
                    formulaArg(xColumnField.getText()),
                    formulaArg(yColumnField.getText()),
                    formulaArg(tooltipColumnField.getText()));
        }

        plot.update(new LineGraph2DRendererUpdate().imageHeight(plotView.getHeight()).imageWidth(plotView.getWidth()).interpolation(InterpolationScheme.LINEAR));
        pv = PVManager.read(plot)
                .notifyOn(swingEDT())
                .readListener(new PVReaderListener() {

                    @Override
                    public void pvChanged(PVReaderEvent event) {
                        setLastError(pv.lastException());
                        if (pv.getValue() != null) {
                            BufferedImage image = ValueUtil.toImage(pv.getValue().getImage());
                            plotView.setImage(image);
                        }
                    }
                })
                .maxRate(ofHertz(50));
    }//GEN-LAST:event_dataFormulaFieldActionPerformed

    private void yColumnFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yColumnFieldActionPerformed
        dataFormulaFieldActionPerformed(evt);
    }//GEN-LAST:event_yColumnFieldActionPerformed

    private void tooltipColumnFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tooltipColumnFieldActionPerformed
        dataFormulaFieldActionPerformed(evt);
    }//GEN-LAST:event_tooltipColumnFieldActionPerformed

    private void xColumnFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xColumnFieldActionPerformed
        dataFormulaFieldActionPerformed(evt);
    }//GEN-LAST:event_xColumnFieldActionPerformed

    private void setLastError(Exception ex) {
        if (ex != null) {
            lastError.setText(ex.getMessage());
            ex.printStackTrace();
            Logger.getLogger(LineGraphSample.class.getName()).log(Level.WARNING, "Error", ex);
        } else {
            lastError.setText("");
        }
    }
    final BufferedImage finalBuffer = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        SetupUtil.defaultCASetupForSwing();
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LineGraphSample().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField dataFormulaField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField lastError;
    private org.diirt.datasource.sample.ImagePanel plotView;
    private javax.swing.JTextField tooltipColumnField;
    private javax.swing.JTextField xColumnField;
    private javax.swing.JTextField yColumnField;
    // End of variables declaration//GEN-END:variables
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy