001    /*
002     * This file is part of McIDAS-V
003     *
004     * Copyright 2007-2013
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    
029    package edu.wisc.ssec.mcidasv.control.cyclone;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    import javax.swing.table.AbstractTableModel;
035    
036    import ucar.unidata.data.storm.StormParam;
037    import ucar.unidata.data.storm.StormTrack;
038    import ucar.unidata.data.storm.StormTrackPoint;
039    import visad.Real;
040    import visad.Unit;
041    import visad.VisADException;
042    
043    /**
044     * 
045     * @author Unidata Development Team
046     * @version $Revision$
047     */
048    
049    public class StormTrackTableModel extends AbstractTableModel {
050    
051            /**
052             * default
053             */
054            private static final long serialVersionUID = 1L;
055    
056            /** _more_ */
057            private StormDisplayState stormDisplayState;
058    
059            /** _more_ */
060            private StormTrack track;
061    
062            /** _more_ */
063            private List<StormTrackPoint> points;
064    
065            /** _more_ */
066            private List<StormParam> params;
067    
068            /**
069             * _more_
070             * 
071             * @param stormDisplayState
072             *            _more_
073             * @param track
074             *            _more_
075             */
076            public StormTrackTableModel(StormDisplayState stormDisplayState,
077                            StormTrack track) {
078                    this.stormDisplayState = stormDisplayState;
079                    this.track = track;
080                    this.points = track.getTrackPoints();
081                    List<StormParam> tmp = track.getParams();
082                    this.params = new ArrayList<StormParam>();
083                    for (StormParam param : tmp) {
084                            if (!param.getDerived()) {
085                                    this.params.add(param);
086                            }
087                    }
088            }
089    
090            /**
091             * _more_
092             * 
093             * @param rowIndex
094             *            _more_
095             * @param columnIndex
096             *            _more_
097             * 
098             * @return _more_
099             */
100            public boolean isCellEditable(int rowIndex, int columnIndex) {
101                    // if (true) {
102                    // return false;
103                    // }
104                    if (columnIndex == 0) {
105                            return false;
106                    }
107                    // return stormDisplayState.getStormTrackControl().isEditable();
108                    return stormDisplayState.getStormTrackControl().getEditMode();
109            }
110    
111            /**
112             * _more_
113             * 
114             * @return _more_
115             */
116            public StormTrack getStormTrack() {
117                    return track;
118            }
119    
120            /**
121             * _more_
122             * 
123             * @return _more_
124             */
125            public int getRowCount() {
126                    return points.size();
127            }
128    
129            /**
130             * _more_
131             * 
132             * @return _more_
133             */
134            public int getColumnCount() {
135                    return 3 + params.size();
136            }
137    
138            /**
139             * _more_
140             * 
141             * @param aValue
142             *            _more_
143             * @param rowIndex
144             *            _more_
145             * @param column
146             *            _more_
147             */
148            public void setValueAt(Object aValue, int rowIndex, int column) {
149                    StormTrackPoint stp = points.get(rowIndex);
150                    if (column == 0) {
151                            return;
152                    } else if (column == 1) {
153                            // latitude
154                    } else if (column == 2) {
155                            // longitude
156                    } else {
157                            StormParam param = params.get(column - 3);
158                            Real r = stp.getAttribute(param);
159                            double newValue = new Double(aValue.toString()).doubleValue();
160                            // Set the value
161                            Real rr = null;
162                            try {
163                                    rr = r.cloneButValue(newValue);
164    
165                            } catch (VisADException ep) {
166                            }
167    
168                            stp.setAttribute(rr);
169                    }
170                    stormDisplayState.markHasBeenEdited();
171            }
172    
173            /**
174             * _more_
175             * 
176             * @param row
177             *            _more_
178             * @param column
179             *            _more_
180             * 
181             * @return _more_
182             */
183            public Object getValueAt(int row, int column) {
184                    if (row >= points.size()) {
185                            return "";
186                    }
187                    StormTrackPoint stp = points.get(row);
188                    if (column == 0) {
189                            if (track.getWay().isObservation()) {
190                                    return stp.getTime();
191                            }
192                            return "" + stp.getForecastHour();
193                    }
194                    if (column == 1) {
195                            return stp.getLocation().getLatitude();
196                    }
197                    if (column == 2) {
198                            return stp.getLocation().getLongitude();
199                    }
200                    StormParam param = params.get(column - 3);
201                    Real r = stp.getAttribute(param);
202                    if (r != null) {
203                            return r.toString();
204                    }
205                    return "";
206            }
207    
208            /**
209             * _more_
210             * 
211             * @param column
212             *            _more_
213             * 
214             * @return _more_
215             */
216            public String getColumnName(int column) {
217                    if (column == 0) {
218                            return track.getWay().isObservation() ? "Time" : "Hour";
219                    }
220                    if (column == 1) {
221                            return "Lat";
222                    }
223                    if (column == 2) {
224                            return "Lon";
225                    }
226                    StormParam param = params.get(column - 3);
227                    Unit unit = param.getUnit();
228                    return param.toString() + ((unit == null) ? "" : "[" + unit + "]");
229            }
230    
231    }