<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * StationChooser.java
 *
 * Created on Jul 31, 2009, 3:06:27 PM
 */
package usda.weru.weps.location.chooser;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.measure.Quantity;
import javax.measure.quantity.Length;
import javax.measure.Unit;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import usda.weru.util.wepsFileChooser2.WepsFileTypes2;
import usda.weru.weps.RunFileBean;
import usda.weru.weps.location.Station;
import usda.weru.weps.location.StationMode;
import usda.weru.gis.latlong.LatLong;

/**
 *
 * @author Joseph Levin &lt;joelevin@weru.ksu.edu&gt;
 */
public class StationChooser extends JPanel {

    private static final long serialVersionUID = 1L;
    public static final String PROP_DISTANCE_UNITS = "distanceUnits";
    public static final String PROP_DISTANCE = "distance";
    public static final String PROP_VIEW = "view";
    public static final String PROP_FILE_TYPE = "fileType";
    public static final String PROP_STATION_CHOICES = "stationChoices";
    public static final String PROP_SELECTED_STATION = "selectedStation";
    public static final String PROP_LATLONG = "latLong";
    public static final String PROP_TEXT = "text";
    public static final String PROP_STATION_MODE = "stationMode";
    public static final String PROP_RUN_FILE_BEAN = "runFileBean";
    public static final String PROP_STATION_TYPE = "stationType";
    public static final String PROP_LABEL = "label";
    public static final String PROP_SUB_LABEL = "subLabel";

    private StationViewList.ViewList c_view;
    private Quantity&lt;Length&gt; c_distance;
    private Unit&lt;Length&gt; c_distanceUnits;
    private WepsFileTypes2 c_fileType = WepsFileTypes2.File;
    private Station[] c_stationChoices;
    private String c_text;
    private StationMode c_stationMode;
    private String c_stationModeDefaultName;
    private StationMode[] c_StationModes;
    private Station c_selectedStation;
    private LatLong c_latlong;
    private RunFileBean c_rfb;
    private RunFileBean.StationType c_stationType;
    private JLabel c_label;
    private JLabel c_subLabel;
    private boolean initial = true;
    
    public StationChooser() {
        c_stationMode = null;
        c_stationModeDefaultName = null;
        c_StationModes = null;
        
        initComponents();
        setCircleActive(false);
        setMoreButtonVisible(false);

        //default view is label
        setView(StationViewList.ViewList.Label);
    }

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

        moreButton = new javax.swing.JButton();
        circle = new com.blogspot.gyfus.LoadingCircle();
        viewPanel = new javax.swing.JPanel();

        moreButton.setText("...");
        moreButton.setToolTipText("Change station selection mode.");
        moreButton.setFocusable(false);
        moreButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moreButtonActionPerformed(evt);
            }
        });

        circle.setRotationSpeed(75);
        circle.setStylePreset(com.blogspot.gyfus.LoadingCircle.StylePresets.FireFox);

        javax.swing.GroupLayout circleLayout = new javax.swing.GroupLayout(circle);
        circle.setLayout(circleLayout);
        circleLayout.setHorizontalGroup(
            circleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
        );
        circleLayout.setVerticalGroup(
            circleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

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

        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(circle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(viewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addGap(0, 0, 0)
                .addComponent(moreButton, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(viewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
            .addComponent(moreButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(circle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// &lt;/editor-fold&gt;//GEN-END:initComponents

    /**
     * Opens the StationModeChooser dialog to allow the user to select the mode.
     * @param evt
     */
    private void moreButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moreButtonActionPerformed
        StationModeChooser dialog = new StationModeChooser(getStationModes(), getStationMode());

        if (dialog.showDialog(this) == StationModeChooser.APPROVE_OPTION) {
            StationMode mode = dialog.getSelectedStationMode();
            setStationMode(mode);
        }

    }//GEN-LAST:event_moreButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.blogspot.gyfus.LoadingCircle circle;
    private javax.swing.JButton moreButton;
    private javax.swing.JPanel viewPanel;
    // End of variables declaration//GEN-END:variables

    public void setRunFileBean(RunFileBean rfb) {
        RunFileBean old = c_rfb;
        c_rfb = rfb;
        firePropertyChange(PROP_RUN_FILE_BEAN, old, c_rfb);
    }

    public RunFileBean getRunFileBean() {
        return c_rfb;
    }

    public void setStationType(RunFileBean.StationType type) {
        RunFileBean.StationType old = c_stationType;
        c_stationType = type;
        firePropertyChange(PROP_STATION_TYPE, old, c_stationType);
    }

    public RunFileBean.StationType getStationType() {
        return c_stationType;
    }

    public void setLatLong(LatLong latlong) {
        LatLong old = c_latlong;
        c_latlong = latlong;
        firePropertyChange(PROP_LATLONG, old, c_latlong);
    }

    public LatLong getLatLong() {
        return c_latlong;
    }

    public void setStationModes(StationMode... modes) {
        c_StationModes = modes;
        boolean found = false;
        
        if (c_stationModeDefaultName != null) {
            for(StationMode mode : c_StationModes) {
                if (mode.getDisplayName().contentEquals(c_stationModeDefaultName)) {
                    setStationMode (mode);
                    found = true;
                    break;
                }
            }
        }
        for(StationMode mode : modes) {
            if(mode == c_stationMode) {
                found = true;
                break;
            }
        }
        if(!found &amp;&amp; modes != null &amp;&amp; modes.length &gt; 0) {
            setStationMode(modes[0]);
        }
        //update the more button's visibility
        setMoreButtonVisible(modes != null &amp;&amp; modes.length &gt; 1);
    }

    public StationMode[] getStationModes() {
        return c_StationModes != null ? c_StationModes : new StationMode[0];
    }
            
    public void setDefaultStationMode (String val) {
        c_stationModeDefaultName = val;
        if (val != null &amp;&amp; c_StationModes != null) {
            for(StationMode mode : c_StationModes) {
                if (mode.getDisplayName().contentEquals(val)) {
                    setStationMode (mode);
                }
            }
        }
    }

    public void setStationMode(StationMode mode) {

        StationMode old = c_stationMode;
        if (old != null) {
            //turn off the old mode, most won't do anything but perhaps clean up.
            old.uninstallView(this);
        }

        if (mode != null) {
            //turn on the new mode for this chooser
            setEnabled(true);
            mode.installView(this);
        } else {
            //mode is null, we need to disable any station selections
            setEnabled(false);
            setView(StationViewList.ViewList.Text);
            setText(mode != null ? "" : "Configuration Error");
        }

        c_stationMode = mode;
        firePropertyChange(PROP_STATION_MODE, old, c_stationMode);
    }

    public StationMode getStationMode() {
        return c_stationMode;
    }

    public void setMoreButtonVisible(boolean visible) {
            moreButton.setVisible(visible);
    }

    public boolean isMoreButtonVisible() {
        return moreButton.isVisible();
    }

    public void setDistanceUnits(Unit&lt;Length&gt; units) {
        Unit&lt;Length&gt; old = c_distanceUnits;
        c_distanceUnits = units;

        firePropertyChange(PROP_DISTANCE_UNITS, old, c_distanceUnits);
    }

    /**
     * Some views display the distance of the station.  Returns the units used 
     * to display the distance.
     * @return
     */
    public Unit&lt;Length&gt; getDistanceUnits() {
        return c_distanceUnits;
    }

    public void setDistance(Quantity&lt;Length&gt; distance) {
        Quantity&lt;Length&gt; old = c_distance;
        c_distance = distance;
        firePropertyChange(PROP_DISTANCE, old, c_distance);
    }

    public Quantity&lt;Length&gt; getDistance() {
        return c_distance;
    }

    public void setFileType(WepsFileTypes2 type) {
        WepsFileTypes2 old = c_fileType;
        c_fileType = type;
        firePropertyChange(PROP_FILE_TYPE, old, c_fileType);
    }

    public WepsFileTypes2 getFileType() {
        return c_fileType;
    }

    public Station[] setStationChoices(Station[] choices) {
        Station[] old = c_stationChoices;
        if((choices == null || choices.length &lt; 1)) {
            if(old != null &amp;&amp; old.length &gt;= 1) {
                if(this.getName() != null) {
                    if(this.getName().equals("cligenChooser")) {
                        JOptionPane.showMessageDialog(null, "You have set a cligen radius that does not encompass any stations. \nPopulating your choice list with the closest one.");
                    } else if(this.getName().equals("windgenChooser")) {
                        JOptionPane.showMessageDialog(null, "You have set a windgen radius that does not encompass any stations. \nPopulating your choice list with the closest one.");
                    } else {
                        JOptionPane.showMessageDialog(null, "You have set a radius that does not encompass any stations. \nPopulating your choice list with the closest one.");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "You have set a radius that does not encompass any stations. \nPopulating your choice list with the closest one.");
                }
                Station[] defaultStation = new Station[1];
                defaultStation[0] = old[0];
                c_stationChoices = defaultStation;
                setSelectedStation(c_stationChoices[0]);
                revalidate();
                repaint(); 
            } else if(!initial) {
            }
        } else {
            c_stationChoices = choices;
        }
        firePropertyChange(PROP_STATION_CHOICES, old, c_stationChoices);
        initial = false;
        return c_stationChoices == null ? new Station [0] : c_stationChoices;
    }

    public Station[] getStationChoices() {
        return c_stationChoices != null ? c_stationChoices : new Station[0];
    }

    public void setText(String text) {
        String old = c_text;
        c_text = text;
        firePropertyChange(PROP_TEXT, old, c_text);
    }

    public String getText() {
        return c_text;
    }

    public void setSelectedStation(Station station) {
        Station old = c_selectedStation;
        c_selectedStation = station;
        firePropertyChange(PROP_SELECTED_STATION, old, c_selectedStation);
    }

    public Station getSelectedStation() {
        return c_selectedStation;
    }

    public void setLabel(JLabel label) {
        JLabel old = c_label;
        c_label = label;
        firePropertyChange(PROP_LABEL, old, c_label);
    }

    public JLabel getLabel() {
        return c_label;
    }

    public void setSubLabel(JLabel label) {
        JLabel old = c_subLabel;
        c_subLabel = label;
        firePropertyChange(PROP_LABEL, old, c_subLabel);
    }

    public JLabel getSubLabel() {
        return c_subLabel;
    }

    public void setView(StationViewList.ViewList view) {
        StationViewList.ViewList old = c_view;
        c_view = view;

        if (old == c_view) {
            return; //prevent duplicated executions
        }

        //uninstall the old view
        if (old != null) {
            old.uninstall(this);
        }

        //remove any old views
        viewPanel.removeAll();

        //install the new view
        if (c_view != null) {
            Component viewComponent = c_view.install(this);
            viewComponent.setEnabled(isEnabled());
            viewPanel.add(viewComponent, BorderLayout.CENTER);
            viewPanel.validate();
        }

        firePropertyChange(PROP_VIEW, old, c_view);
    }

    public StationViewList.ViewList getView() {
        return c_view;
    }

    public void setCircleActive(boolean active) {
        circle.setVisible(active);
        circle.setActive(active);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        moreButton.setEnabled(enabled);
        for (Component child : viewPanel.getComponents()) {
            child.setEnabled(enabled);
        }
    }

//    public static enum View implements StationViewInterface {
//
//        Choice(StationChoiceView.class),
//        File(StationFileView.class),
//        Label(StationLabelView.class),
//        Text(StationTextView.class);
//
//        private final Class&lt;? extends StationViewInterface&gt; c_handlerClass;
//        private final Map&lt;StationChooser, StationViewInterface&gt; c_handlers;
//
//        View(Class&lt;? extends StationViewInterface&gt; handler) {
//            c_handlerClass = handler;
//            c_handlers = new HashMap&lt;StationChooser, StationViewInterface&gt;();
//        }
//
//        private synchronized StationViewInterface handler(StationChooser chooser) {
//            StationViewInterface handler = c_handlers.get(chooser);
//            if (handler == null) {
//                try {
//                    handler = c_handlerClass.getDeclaredConstructor().newInstance();
//                    c_handlers.put(chooser, handler);
//                } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            return handler;
//        }
//
//        @Override
//        public Component install(StationChooser chooser) {
//            return handler(chooser).install(chooser);
//        }
//
//        @Override
//        public void uninstall(StationChooser chooser) {
//            handler(chooser).uninstall(chooser);
//        }
//
//    }

//    public interface StationViewInterface {
//
//        /**
//         * Install the view on the for a StationChooser.  Now is the time to add
//         * a PropertyChangeListener to the chooser.
//         * @param chooser
//         * @return Component used as the view.
//         */
//        public Component install(StationChooser chooser);
//
//        /**
//         * Clean up.  Now is the time to remove a PropertyChangeListener from the
//         * chooser.
//         * @param chooser
//         */
//        public void uninstall(StationChooser chooser);
//
//    }
}
</pre></body></html>