<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * Used by GIS, Interpolated, Nearest, NRCS modes
 * (GISHandler.java)
 * (InterpolatedHandler.java)
 * (NearestHandler.java)
 * (NRCSHandler.java)
 */
package usda.weru.weps.location.chooser;

import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.measure.Quantity;
import javax.measure.quantity.Length;
import javax.measure.Unit;
import javax.swing.JPanel;

import usda.weru.gis.latlong.LatLong;
import usda.weru.gis.GISUtil;
import usda.weru.weps.location.InterpolatedStation;
import usda.weru.weps.location.Station;

/**
 *
 * @author Joseph Levin &lt;joelevin@weru.ksu.edu&gt;
 */
public class StationLabelView extends JPanel implements StationViewInterface, PropertyChangeListener {

    private static final long serialVersionUID = 1L;

    private StationChooser c_chooser;
    private final NumberFormat c_format = new DecimalFormat("0.0");

    /** Creates new form StationLabelView */
    public StationLabelView() {
        initComponents();
    }

    /** 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")
    // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;//GEN-BEGIN:initComponents
    private void initComponents() {

        label = new javax.swing.JLabel();
        distanceLabel = new javax.swing.JLabel();
        unitsLabel = new javax.swing.JLabel();

        label.setFont(label.getFont().deriveFont(label.getFont().getStyle() &amp; ~java.awt.Font.BOLD));
        label.setText("label goes here");

        distanceLabel.setFont(distanceLabel.getFont().deriveFont(distanceLabel.getFont().getStyle() &amp; ~java.awt.Font.BOLD));
        distanceLabel.setText("100");

        unitsLabel.setFont(unitsLabel.getFont().deriveFont(unitsLabel.getFont().getStyle() &amp; ~java.awt.Font.BOLD));
        unitsLabel.setText("km");

        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()
                .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(distanceLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(unitsLabel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(unitsLabel)
                .addComponent(distanceLabel))
        );
    }// &lt;/editor-fold&gt;//GEN-END:initComponents

    @Override
    public Component install(StationChooser chooser) {
        c_chooser = chooser;
        setStation(c_chooser.getSelectedStation());
        c_chooser.addPropertyChangeListener(this);
        return this;
    }

    @Override
    public void uninstall(StationChooser chooser) {
        c_chooser.removePropertyChangeListener(this);
        c_chooser = null;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        switch (evt.getPropertyName()) {
            case StationChooser.PROP_SELECTED_STATION:
                //the selected station changed'
                setStation(c_chooser.getSelectedStation());
                break;
            case StationChooser.PROP_DISTANCE_UNITS:
                Unit&lt;Length&gt; unit = c_chooser.getDistanceUnits();
                if (unit != null) {
                    unitsLabel.setText(unit.toString());
                } else {
                    unitsLabel.setText("--");
                }   //update again
                setStation(c_chooser.getSelectedStation());
                break;
            case StationChooser.PROP_LATLONG:
                setStation(c_chooser.getSelectedStation());
                break;
        }
    }

    public void setStation(Station station) {

        if (station != null) {
            label.setText(station.getDisplayName());
            
//            // MEH debugging for Larry:
//            String s = "MEH setStation: class:"+station.getClass().getSimpleName()+" station="+station.getDisplayName();
//            System.out.println(s);
//            //
        
            if (station instanceof InterpolatedStation) {
                distanceLabel.setVisible(false);
                unitsLabel.setVisible(false);
            } else {
                distanceLabel.setVisible(true);
                unitsLabel.setVisible(true);
                //TODO: perhaps cache the distances?  Is this a costly calculation?
                LatLong latlong1 = c_chooser.getLatLong();
                LatLong latlong2 = station.getLatLong();

                Quantity&lt;Length&gt; distance = GISUtil.distanceBetweenCoordinates(latlong1, latlong2);

                if (distance != null) {
                    double distanceInDisplayUnits = distance.to(c_chooser.getDistanceUnits()).getValue().doubleValue();
                    distanceLabel.setText(c_format.format(distanceInDisplayUnits));
                } else {
                    distanceLabel.setText("?");
                }
            }
        } else {
            label.setText("None Selected");
            distanceLabel.setText("");
            distanceLabel.setVisible(true);
            unitsLabel.setVisible(true);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        label.setEnabled(enabled);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel distanceLabel;
    private javax.swing.JLabel label;
    private javax.swing.JLabel unitsLabel;
    // End of variables declaration//GEN-END:variables

}
</pre></body></html>