/*
 * LocationPanel.java
 *
 * Created on Jul 30, 2009, 4:22:12 PM
 */
package usda.weru.weps.location;

import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.help.CSH;
import javax.measure.Measurable;
import javax.measure.Measure;
import javax.measure.quantity.Angle;
import javax.measure.quantity.Length;
import javax.measure.unit.NonSI;
import javax.measure.unit.SI;
import javax.measure.unit.Unit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jscience.geography.coordinates.LatLong;
import usda.weru.gis.GISUtil;
import usda.weru.gis.gui.MapController;
import usda.weru.gis.gui.MapViewer;
import usda.weru.util.ConfigData;
import usda.weru.util.MeasurableField;
import usda.weru.util.MeasurableField.ValueListener;
import usda.weru.util.Util;
import usda.weru.util.WepsFileChooser;
import usda.weru.weps.RunFileBean;
import usda.weru.weps.RunFileData;
import usda.weru.weps.location.chooser.SiteChooser;
import usda.weru.weps.location.chooser.StationChooser;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class LocationPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = Logger.getLogger(LocationPanel.class);
    private final RunFileBean c_rfb;
    private LatLong c_latlong;

    /**
     * Creates new form LocationPanel
     * @param rfb 
     */
    public LocationPanel(RunFileBean rfb) {
        c_rfb = rfb;
        //let's get this show going.
        initComponents();
        addHelp();
        //link the stationchoosers to their bean and station type
        cligenStationChooser.setRunFileBean(c_rfb);
        cligenStationChooser.setStationType(RunFileBean.StationType.Cligen);

        windgenStationChooser.setRunFileBean(c_rfb);
        windgenStationChooser.setStationType(RunFileBean.StationType.Windgen);

        //init the settings that come from the CD
        updateUnits();
        updateSiteChooserRoot();

        initListeners();
        initBindings();
    }

    private void initListeners() {
        //user editor latlong
        ValueListener<Angle> latlongValueListener = new ValueListener<Angle>() {

            @Override
            public void valueChanged(Measurable<Angle> oldValue, Measurable<Angle> newValue) {
                double lat = latitudeField.getValue().doubleValue(NonSI.DEGREE_ANGLE);
                double lon = longitudeField.getValue().doubleValue(NonSI.DEGREE_ANGLE);
                c_rfb.setLatLong(LatLong.valueOf(lat, lon, NonSI.DEGREE_ANGLE));
            }
        };

        latitudeField.addValueListener(latlongValueListener);
        longitudeField.addValueListener(latlongValueListener);

        //latlong change from bean
        c_rfb.addPropertyChangeListener(RunFileBean.PROP_LATLONG, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateLatLong();
            }
        });

        //configdata units
        ConfigData.getDefault().addPropertyChangeListener(ConfigData.Units, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateUnits();
            }
        });

        ConfigData.getDefault().addPropertyChangeListener(ConfigData.SiteChooserShowCountry, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateSiteChooserRoot();
            }
        });

        //allowed cligen modes
        ConfigData.getDefault().addPropertyChangeListener(ConfigData.CligenAllowedModes, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                cligenStationChooser.setAllowedStationModes(ConfigData.getDefault().getAllowedCligenStationModes());
            }
        });

        //allowed windgen modes
        ConfigData.getDefault().addPropertyChangeListener(ConfigData.WindgenAllowedModes, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                windgenStationChooser.setAllowedStationModes(ConfigData.getDefault().getAllowedWindgenStationModes());
            }
        });
    }

    private void initBindings() {
        //add the bean bindings
        BindingGroup bindings = new BindingGroup();
        //elevation
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, c_rfb,
                BeanProperty.create(RunFileBean.PROP_ELEVATION), elevationField,
                BeanProperty.create(MeasurableField.PROP_VALUE)));

        //allowed cligen modes
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ, ConfigData.getDefault(),
                BeanProperty.create(ConfigData.PROP_ALLOWED_CLIGEN_STATION_MODES), cligenStationChooser,
                BeanProperty.create(StationChooser.PROP_ALLOWED_STATION_MODES)));
        //cligen mode
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, c_rfb,
                BeanProperty.create(RunFileBean.PROP_CLIGEN_STATION_MODE), cligenStationChooser,
                BeanProperty.create(StationChooser.PROP_STATION_MODE)));
        //cligen station
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, c_rfb,
                BeanProperty.create(RunFileBean.PROP_CLIGEN_STATION), cligenStationChooser,
                BeanProperty.create(StationChooser.PROP_SELECTED_STATION)));
        //latlong
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ, c_rfb,
                BeanProperty.create(RunFileBean.PROP_LATLONG), cligenStationChooser,
                BeanProperty.create(StationChooser.PROP_LATLONG)));

        //allowed windgen modes
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ, ConfigData.getDefault(),
                BeanProperty.create(ConfigData.PROP_ALLOWED_WINDGEN_STATION_MODES), windgenStationChooser,
                BeanProperty.create(StationChooser.PROP_ALLOWED_STATION_MODES)));
        //windgen mode
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, c_rfb,
                BeanProperty.create(RunFileBean.PROP_WINDGEN_STATION_MODE), windgenStationChooser,
                BeanProperty.create(StationChooser.PROP_STATION_MODE)));
        //windgen station
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, c_rfb,
                BeanProperty.create(RunFileBean.PROP_WINDGEN_STATION), windgenStationChooser,
                BeanProperty.create(StationChooser.PROP_SELECTED_STATION)));
        //latlong
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ, c_rfb,
                BeanProperty.create(RunFileBean.PROP_LATLONG), windgenStationChooser,
                BeanProperty.create(StationChooser.PROP_LATLONG)));

        //site
        bindings.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, c_rfb,
                BeanProperty.create(RunFileBean.PROP_SITE), siteChooser,
                BeanProperty.create(SiteChooser.PROP_SELECTED_SITE)));

        bindings.bind();

    }

    /**
     *
     * @return
     */
    public RunFileBean getRunFileBean() {
        return c_rfb;
    }

    private void updateLatLong() {
        c_latlong = c_rfb.getLatLong();
        if (c_latlong == null) {
            LOGGER.warn("LatLong is null.  Unable to update the lat long fields.");
            latitudeField.setValue(Measure.valueOf(0, NonSI.DEGREE_ANGLE));
            longitudeField.setValue(Measure.valueOf(0, NonSI.DEGREE_ANGLE));
            JOptionPane.showMessageDialog(this, "Unable to determine latitude and longitude.",
                    "Invalid Data", JOptionPane.WARNING_MESSAGE);
            latitudeField.requestFocusInWindow();
            return;
        }
        double lat = c_latlong.latitudeValue(NonSI.DEGREE_ANGLE);
        latitudeField.setValue(Measure.valueOf(lat, NonSI.DEGREE_ANGLE));

        double lon = c_latlong.longitudeValue(NonSI.DEGREE_ANGLE);
        longitudeField.setValue(Measure.valueOf(lon, NonSI.DEGREE_ANGLE));

    }

    private void updateSiteChooserRoot() {
        Site<?> currentValue = siteChooser.getSelectedSite();
        if (ConfigData.getDefault().isSiteChooserShowCountries()) {
            siteChooser.setRootSite(null);
        } else {
            siteChooser.setRootSite(Site.UNITED_STATES);
        }
        siteChooser.setSelectedSite(currentValue);
    }

    private void updateUnits() {
        Unit<Length> distanceUnit = SI.KILOMETER;
        Unit<Length> elevationUnit = SI.METER;
        if (Util.USUnits.equals(ConfigData.getDefault().getData(ConfigData.Units))) {
            distanceUnit = NonSI.MILE;
            elevationUnit = NonSI.FOOT;
        }
        cligenStationChooser.setDistanceUnits(distanceUnit);
        windgenStationChooser.setDistanceUnits(distanceUnit);
        elevationField.setUnits(elevationUnit);

        //lat long always uses degree angle
        latitudeField.setUnits(NonSI.DEGREE_ANGLE);
        longitudeField.setUnits(NonSI.DEGREE_ANGLE);
    }

    /**
     * This panel holds all the location information for the wind and climate generation station like
     * the latitudes and longitudes passing through it, the county and state in which they fall, etc.
     * @param sTitle The string that carries the title for the panel.
     */
    private void addHelp() {
        CSH.setHelpIDString(siteLabel, "scSelection_html");
        CSH.setHelpIDString(siteChooser, "scSelection_html");
        CSH.setHelpIDString(this, "locationPanel_html");
        CSH.setHelpIDString(logitudeLabel, "llSelection_html");
        CSH.setHelpIDString(longitudeField, "llSelection_html");
        CSH.setHelpIDString(latitudeLabel, "llSelection_html");
        CSH.setHelpIDString(latitudeField, "llSelection_html");
        CSH.setHelpIDString(elevationLabel, "locationPanel_html");
        CSH.setHelpIDString(elevationField, "locationPanel_html");
        CSH.setHelpIDString(cligenLabelPanel, "ssMethods_html");
        CSH.setHelpIDString(cligenStationChooser, "ssMethods_html");
        CSH.setHelpIDString(windgenLabelPanel, "ssMethods_html");
        CSH.setHelpIDString(windgenStationChooser, "ssMethods_html");
        CSH.setHelpIDString(mapButton, "mvSelection_html");
    }

    /** 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() {

        cligenLabelPanel = new javax.swing.JPanel();
        cligenLabel = new javax.swing.JLabel();
        cligenSubLabel = new javax.swing.JLabel();
        cligenStationChooser = new usda.weru.weps.location.chooser.StationChooser();
        windgenLabelPanel = new javax.swing.JPanel();
        wingenLabel = new javax.swing.JLabel();
        windgenSubLabel = new javax.swing.JLabel();
        windgenStationChooser = new usda.weru.weps.location.chooser.StationChooser();
        latitudeLabel = new javax.swing.JLabel();
        logitudeLabel = new javax.swing.JLabel();
        elevationLabel = new javax.swing.JLabel();
        elevationUnits = new javax.swing.JLabel();
        mapButtonPanel = new javax.swing.JPanel();
        mapButton = new javax.swing.JButton();
        logitudeUnits = new javax.swing.JLabel();
        latitudeUnits = new javax.swing.JLabel();
        elevationField = new usda.weru.util.MeasurableField<Length>();
        latitudeField = new usda.weru.util.MeasurableField<Angle>();
        longitudeField = new usda.weru.util.MeasurableField<Angle>();
        siteChooser = new usda.weru.weps.location.chooser.SiteChooser();
        siteLabel = siteChooser.getLabelPanel();

        cligenLabel.setText("Cligen:");

        cligenSubLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        cligenSubLabel.setText(" ");

        javax.swing.GroupLayout cligenLabelPanelLayout = new javax.swing.GroupLayout(cligenLabelPanel);
        cligenLabelPanel.setLayout(cligenLabelPanelLayout);
        cligenLabelPanelLayout.setHorizontalGroup(
            cligenLabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cligenLabelPanelLayout.createSequentialGroup()
                .addComponent(cligenLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cligenSubLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
        );
        cligenLabelPanelLayout.setVerticalGroup(
            cligenLabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cligenLabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cligenLabel)
                .addComponent(cligenSubLabel))
        );

        cligenStationChooser.setFileType(WepsFileChooser.CLIMATE);
        cligenStationChooser.setLabel(cligenLabel);
        cligenStationChooser.setSubLabel(cligenSubLabel);

        wingenLabel.setText("Windgen:");

        windgenSubLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        windgenSubLabel.setText(" ");

        javax.swing.GroupLayout windgenLabelPanelLayout = new javax.swing.GroupLayout(windgenLabelPanel);
        windgenLabelPanel.setLayout(windgenLabelPanelLayout);
        windgenLabelPanelLayout.setHorizontalGroup(
            windgenLabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(windgenLabelPanelLayout.createSequentialGroup()
                .addComponent(wingenLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(windgenSubLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE))
        );
        windgenLabelPanelLayout.setVerticalGroup(
            windgenLabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(windgenLabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(wingenLabel)
                .addComponent(windgenSubLabel))
        );

        windgenStationChooser.setFileType(WepsFileChooser.WIND);
        windgenStationChooser.setLabel(wingenLabel);
        windgenStationChooser.setSubLabel(windgenSubLabel);

        latitudeLabel.setText("Latitude:");

        logitudeLabel.setText("Longitude:");

        elevationLabel.setText("Elevation:");

        mapButton.setText("View Map");
        mapButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mapButtonActionPerformed(evt);
            }
        });
        mapButtonPanel.add(mapButton);

        elevationField.setDefaultPattern("#0");
        elevationField.setDisplayPattern("#0");
        elevationField.setEditPattern("#0.0");
        elevationField.setUnitsLabel(elevationUnits);

        latitudeField.setDisplayPattern("0.00 N;0.00 S");
        latitudeField.setEditPattern("0.00000;-0.00000");
        latitudeField.setUnitsLabel(latitudeUnits);

        longitudeField.setDisplayPattern("0.00 E;0.00 W");
        longitudeField.setEditPattern("0.00000;-0.00000");
        longitudeField.setUnitsLabel(logitudeUnits);

        siteLabel.setLayout(new java.awt.GridLayout(0, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mapButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(windgenStationChooser, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                    .addComponent(cligenStationChooser, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(siteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(logitudeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(elevationLabel)
                            .addComponent(latitudeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(siteChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                            .addComponent(elevationField, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                            .addComponent(latitudeField, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                            .addComponent(longitudeField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(elevationUnits, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(logitudeUnits, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(latitudeUnits, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(windgenLabelPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cligenLabelPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(siteLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(siteChooser, 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(latitudeLabel)
                    .addComponent(latitudeUnits)
                    .addComponent(latitudeField, 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(logitudeLabel)
                    .addComponent(logitudeUnits)
                    .addComponent(longitudeField, 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(elevationLabel)
                    .addComponent(elevationUnits)
                    .addComponent(elevationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cligenLabelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cligenStationChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(windgenLabelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(windgenStationChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mapButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {siteChooser, siteLabel});

    }// </editor-fold>//GEN-END:initComponents
    private MapViewer c_mapViewer;
    private void mapButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mapButtonActionPerformed

        synchronized (MapViewer.class) {
            if (c_mapViewer == null) {
                c_mapViewer = new MapViewer();

                final PropertyChangeListener latLongListener = new PropertyChangeListener() {

                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        c_mapViewer.setCurrentLocationMarker(c_rfb.getLatLong());
                    }
                };

                c_rfb.addPropertyChangeListener(RunFileBean.PROP_LATLONG, latLongListener);

                c_mapViewer.setCurrentLocationMarker(c_rfb.getLatLong());
                c_mapViewer.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowClosed(WindowEvent e) {
                        synchronized (MapViewer.class) {
                            c_rfb.removePropertyChangeListener(RunFileBean.PROP_LATLONG, latLongListener);
                            c_mapViewer = null;
                        }
                    }
                });
                MapController control = c_mapViewer.getMapController();

                control.setCenter(GISUtil.toCoordinate(c_rfb.getLatLong()));
                control.setZoomFactor(500.0d);
                c_mapViewer.demoMetaData(c_rfb.getLatLong());
                c_mapViewer.addPropertyChangeListener(MapViewer.PROP_SELECTEDLATLONG, new PropertyChangeListener() {

                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        c_mapViewer.setVisible(false);
                        c_mapViewer.dispose();
                        c_rfb.setLatLong(c_mapViewer.getSelectedLatLong());
                    }
                });
                c_mapViewer.setVisible(true);

            } else {
                //show the viewer

                int state = c_mapViewer.getExtendedState();
                //remove any iconified bits
                state &= ~JFrame.ICONIFIED;
                c_mapViewer.setExtendedState(state);
                //bring it to the front
                c_mapViewer.toFront();
            }
        }


    }//GEN-LAST:event_mapButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cligenLabel;
    private javax.swing.JPanel cligenLabelPanel;
    private usda.weru.weps.location.chooser.StationChooser cligenStationChooser;
    private javax.swing.JLabel cligenSubLabel;
    private usda.weru.util.MeasurableField<Length> elevationField;
    private javax.swing.JLabel elevationLabel;
    private javax.swing.JLabel elevationUnits;
    private usda.weru.util.MeasurableField<Angle> latitudeField;
    private javax.swing.JLabel latitudeLabel;
    private javax.swing.JLabel latitudeUnits;
    private javax.swing.JLabel logitudeLabel;
    private javax.swing.JLabel logitudeUnits;
    private usda.weru.util.MeasurableField<Angle> longitudeField;
    private javax.swing.JButton mapButton;
    private javax.swing.JPanel mapButtonPanel;
    private usda.weru.weps.location.chooser.SiteChooser siteChooser;
    private javax.swing.JPanel siteLabel;
    private javax.swing.JPanel windgenLabelPanel;
    private usda.weru.weps.location.chooser.StationChooser windgenStationChooser;
    private javax.swing.JLabel windgenSubLabel;
    private javax.swing.JLabel wingenLabel;
    // End of variables declaration//GEN-END:variables

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame("Location Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new LocationPanel(new RunFileData().getBean()));

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                frame.setVisible(true);
                frame.pack();
            }
        });
    }
}
