001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2016
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see http://www.gnu.org/licenses.
027 */
028
029package edu.wisc.ssec.mcidasv.chooser;
030
031import java.beans.PropertyChangeEvent;
032import java.beans.PropertyChangeListener;
033import java.io.File;
034
035import javax.swing.JFileChooser;
036import javax.swing.JList;
037import javax.swing.filechooser.FileFilter;
038import javax.swing.filechooser.FileNameExtensionFilter;
039
040import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044import ucar.unidata.idv.chooser.IdvChooser;
045
046/**
047 * An extension of JFileChooser to handle Two-Line Element (TLE)
048 * files, for plotting satellite orbit tracks.
049 * 
050 * @author Gail Dengel and Tommy Jasmin
051 *
052 */
053public class TLEFileChooser extends JFileChooser implements PropertyChangeListener {
054
055    /**
056     * auto-generated default value
057     */
058    private static final long serialVersionUID = 1L;
059    private static final Logger logger = LoggerFactory.getLogger(TLEFileChooser.class);
060
061    /* the enclosing orbit track chooser */
062    private PolarOrbitTrackChooser potc = null;
063
064    /**
065     * Create the file chooser
066     *
067     * @param chooser {@code PolarOrbitTrackChooser} to which this {@code TLEFileChooser} belongs.
068     * @param directory Initial directory.
069     * @param filename Initial filename within {@code directory}.
070     */
071    public TLEFileChooser(PolarOrbitTrackChooser chooser, String directory, String filename) {
072        super(directory);
073        potc = chooser;
074
075        logger.debug("TLEFileChooser constructor...");
076        setControlButtonsAreShown(false);
077        setMultiSelectionEnabled(false);
078        FileFilter filter = new FileNameExtensionFilter("TLE files", "txt");
079        addChoosableFileFilter(filter);
080        setAcceptAllFileFilterUsed(false);
081        setFileFilter(filter);
082        addPropertyChangeListener(this);
083
084        File tmpFile = new File(directory + File.separatorChar + filename);
085//        logger.trace("tmpFile='{}' exists='{}'", tmpFile, tmpFile.exists());
086        setSelectedFile(null);
087        setSelectedFile(tmpFile);
088//        final JList list = McVGuiUtils.getDescendantOfType(JList.class, this, "Enabled", true);
089//        list.requestFocus();
090    }
091
092    @Override public void setSelectedFile(File file) {
093        // i REALLY don't know how to explain this one...but don't remove the
094        // following if-else stuff. at least on OSX, it has *something* to do with
095        // whether or not the UI actually shows the file selection.
096        // what is somewhat weird is that commenting out the current if-else
097        // and doing something like:
098        // if (file != null) {
099        //     boolean weird = file.exists();
100        // }
101        // does *NOT* work--but maybe HotSpot is optimizing away the unused code, right?
102        // wrong! the following also does not work:
103        // if (file != null && file.exists()) {
104        //    logger.trace("exists!");
105        // }
106        // i will note that calls to this method appear to be happening on threads
107        // other than the EDT...but using SwingUtilities.invokeLater and
108        // SwingUtilities.invokeAndWait have not worked so far (and I've tried
109        // the obvious places in the code, including POTC.doMakeContents()).
110        if (file != null) {
111            logger.trace("setting file='{}' exists={}", file, file.exists());
112        } else {
113            logger.trace("setting file='{}' exists=NULL", file);
114        }
115        super.setSelectedFile(file);
116    }
117
118    /**
119     * Approve the selection
120     */
121    @Override public void approveSelection() {
122        logger.trace("firing");
123        super.approveSelection();
124        potc.doLoad();
125    }
126
127    public void setPotc(PolarOrbitTrackChooser potc) {
128        this.potc = potc;
129    }
130
131    public PolarOrbitTrackChooser getPotc() {
132        return potc;
133    }
134
135    @Override public void propertyChange(PropertyChangeEvent pce) {
136        String propName = pce.getPropertyName();
137        if (propName.equals(SELECTED_FILE_CHANGED_PROPERTY)) {
138            // tell the chooser we have a file to load
139            handleFileChanged();
140        }
141    }
142
143    protected void handleFileChanged() {
144        if (potc != null) {
145            File f = getSelectedFile();
146            if ((f != null) && accept(f) && potc.localMode()) {
147                if (!f.isDirectory()) {
148                    // update last visited directory here
149                    String potcId = IdvChooser.PREF_DEFAULTDIR + potc.getId();
150                    String potcFileId = IdvChooser.PREF_DEFAULTDIR + potc.getId() + ".file";
151                    String dir = getSelectedFile().getParent();
152                    String file = getSelectedFile().getName();
153                    potc.getIdv().getStateManager().writePreference(
154                        potcId, dir
155                    );
156                    potc.getIdv().getStateManager().writePreference(
157                        potcFileId, file
158                    );
159
160                    logger.trace("potcId='{}' value='{}'", potcId, dir);
161                    logger.trace("potcFileId='{}' value='{}'", potcFileId, file);
162                    potc.enableLoadFromFile(true);
163                }
164            } else {
165                potc.enableLoadFromFile(false);
166            }
167        } else {
168            logger.warn("null potc, must be set by caller before use.");
169        }
170    }
171}