001    /*
002     * $Id: StormTrackTableModel.java,v 1.1 2012/01/04 20:39:38 tommyj Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    
031    package edu.wisc.ssec.mcidasv.control.cyclone;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    import javax.swing.table.AbstractTableModel;
037    
038    import ucar.unidata.data.storm.StormParam;
039    import ucar.unidata.data.storm.StormTrack;
040    import ucar.unidata.data.storm.StormTrackPoint;
041    import visad.Real;
042    import visad.Unit;
043    import visad.VisADException;
044    
045    /**
046     * 
047     * @author Unidata Development Team
048     * @version $Revision: 1.1 $
049     */
050    
051    public class StormTrackTableModel extends AbstractTableModel {
052    
053            /**
054             * default
055             */
056            private static final long serialVersionUID = 1L;
057    
058            /** _more_ */
059            private StormDisplayState stormDisplayState;
060    
061            /** _more_ */
062            private StormTrack track;
063    
064            /** _more_ */
065            private List<StormTrackPoint> points;
066    
067            /** _more_ */
068            private List<StormParam> params;
069    
070            /**
071             * _more_
072             * 
073             * @param stormDisplayState
074             *            _more_
075             * @param track
076             *            _more_
077             */
078            public StormTrackTableModel(StormDisplayState stormDisplayState,
079                            StormTrack track) {
080                    this.stormDisplayState = stormDisplayState;
081                    this.track = track;
082                    this.points = track.getTrackPoints();
083                    List<StormParam> tmp = track.getParams();
084                    this.params = new ArrayList<StormParam>();
085                    for (StormParam param : tmp) {
086                            if (!param.getDerived()) {
087                                    this.params.add(param);
088                            }
089                    }
090            }
091    
092            /**
093             * _more_
094             * 
095             * @param rowIndex
096             *            _more_
097             * @param columnIndex
098             *            _more_
099             * 
100             * @return _more_
101             */
102            public boolean isCellEditable(int rowIndex, int columnIndex) {
103                    // if (true) {
104                    // return false;
105                    // }
106                    if (columnIndex == 0) {
107                            return false;
108                    }
109                    // return stormDisplayState.getStormTrackControl().isEditable();
110                    return stormDisplayState.getStormTrackControl().getEditMode();
111            }
112    
113            /**
114             * _more_
115             * 
116             * @return _more_
117             */
118            public StormTrack getStormTrack() {
119                    return track;
120            }
121    
122            /**
123             * _more_
124             * 
125             * @return _more_
126             */
127            public int getRowCount() {
128                    return points.size();
129            }
130    
131            /**
132             * _more_
133             * 
134             * @return _more_
135             */
136            public int getColumnCount() {
137                    return 3 + params.size();
138            }
139    
140            /**
141             * _more_
142             * 
143             * @param aValue
144             *            _more_
145             * @param rowIndex
146             *            _more_
147             * @param column
148             *            _more_
149             */
150            public void setValueAt(Object aValue, int rowIndex, int column) {
151                    StormTrackPoint stp = points.get(rowIndex);
152                    if (column == 0) {
153                            return;
154                    } else if (column == 1) {
155                            // latitude
156                    } else if (column == 2) {
157                            // longitude
158                    } else {
159                            StormParam param = params.get(column - 3);
160                            Real r = stp.getAttribute(param);
161                            double newValue = new Double(aValue.toString()).doubleValue();
162                            // Set the value
163                            Real rr = null;
164                            try {
165                                    rr = r.cloneButValue(newValue);
166    
167                            } catch (VisADException ep) {
168                            }
169    
170                            stp.setAttribute(rr);
171                    }
172                    stormDisplayState.markHasBeenEdited();
173            }
174    
175            /**
176             * _more_
177             * 
178             * @param row
179             *            _more_
180             * @param column
181             *            _more_
182             * 
183             * @return _more_
184             */
185            public Object getValueAt(int row, int column) {
186                    if (row >= points.size()) {
187                            return "";
188                    }
189                    StormTrackPoint stp = points.get(row);
190                    if (column == 0) {
191                            if (track.getWay().isObservation()) {
192                                    return stp.getTime();
193                            }
194                            return "" + stp.getForecastHour();
195                    }
196                    if (column == 1) {
197                            return stp.getLocation().getLatitude();
198                    }
199                    if (column == 2) {
200                            return stp.getLocation().getLongitude();
201                    }
202                    StormParam param = params.get(column - 3);
203                    Real r = stp.getAttribute(param);
204                    if (r != null) {
205                            return r.toString();
206                    }
207                    return "";
208            }
209    
210            /**
211             * _more_
212             * 
213             * @param column
214             *            _more_
215             * 
216             * @return _more_
217             */
218            public String getColumnName(int column) {
219                    if (column == 0) {
220                            return track.getWay().isObservation() ? "Time" : "Hour";
221                    }
222                    if (column == 1) {
223                            return "Lat";
224                    }
225                    if (column == 2) {
226                            return "Lon";
227                    }
228                    StormParam param = params.get(column - 3);
229                    Unit unit = param.getUnit();
230                    return param.toString() + ((unit == null) ? "" : "[" + unit + "]");
231            }
232    
233    }