001    /*
002     * $Id: YearDisplayState.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.awt.Color;
034    import java.awt.Dimension;
035    import java.awt.event.ActionEvent;
036    import java.awt.event.ActionListener;
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    import javax.swing.JButton;
041    import javax.swing.JComponent;
042    import javax.swing.JLabel;
043    
044    import ucar.unidata.data.storm.StormTrack;
045    import ucar.unidata.util.GuiUtils;
046    import ucar.unidata.util.LogUtil;
047    import ucar.unidata.util.Misc;
048    import ucar.visad.Util;
049    import ucar.visad.display.StationModelDisplayable;
050    import ucar.visad.display.TrackDisplayable;
051    import visad.Data;
052    import visad.DateTime;
053    import visad.FieldImpl;
054    
055    /**
056     * 
057     * @author Unidata Development Team
058     * @version $Revision: 1.1 $
059     */
060    
061    public class YearDisplayState {
062    
063            /** _more_ */
064            public static final int STATE_INACTIVE = 0;
065    
066            /** _more_ */
067            public static final int STATE_LOADING = 1;
068    
069            /** _more_ */
070            public static final int STATE_ACTIVE = 2;
071    
072            /** _more_ */
073            private static int[] nextColor = { 0 };
074    
075            /** _more_ */
076            private StormTrackControl stormTrackControl;
077    
078            /** _more_ */
079            private int year;
080    
081            /** _more_ */
082            private Color color;
083    
084            /** _more_ */
085            private int state = STATE_INACTIVE;
086    
087            /** _more_ */
088            private TrackDisplayable trackDisplay;
089    
090            /** _more_ */
091            private StationModelDisplayable labelDisplay;
092    
093            /** _more_ */
094            private List<StormTrack> stormTracks = new ArrayList<StormTrack>();
095    
096            /** _more_ */
097            private JLabel label;
098    
099            /** _more_ */
100            private JButton button;
101    
102            /** _more_ */
103            private GuiUtils.ColorSwatch colorSwatch;
104    
105            /**
106             * _more_
107             */
108            public YearDisplayState() {
109            }
110    
111            /**
112             * _more_
113             * 
114             * 
115             * @param stormTrackControl
116             *            _more_
117             * @param year
118             *            _more_
119             * 
120             */
121            public YearDisplayState(StormTrackControl stormTrackControl, int year) {
122                    this.stormTrackControl = stormTrackControl;
123                    this.year = year;
124                    color = StormDisplayState.getNextColor(nextColor);
125            }
126    
127            /**
128             * _more_
129             * 
130             * @return _more_
131             */
132            protected JComponent getColorSwatch() {
133                    if (colorSwatch == null) {
134                            colorSwatch = new GuiUtils.ColorSwatch(getColor(),
135                                            "Set track color") {
136                                    public void setBackground(Color newColor) {
137                                            super.setBackground(newColor);
138                                            YearDisplayState.this.color = newColor;
139                                            if (trackDisplay != null) {
140                                                    try {
141                                                            trackDisplay.setColor(newColor);
142                                                    } catch (Exception exc) {
143                                                            LogUtil.logException("Setting color", exc);
144                                                    }
145                                            }
146                                    }
147                            };
148                            colorSwatch.setMinimumSize(new Dimension(15, 15));
149                            colorSwatch.setPreferredSize(new Dimension(15, 15));
150                    }
151                    return colorSwatch;
152            }
153    
154            /**
155             * _more_
156             * 
157             * @return _more_
158             */
159            public String toString() {
160                    return "" + year;
161            }
162    
163            /** _more_ */
164            private List pointObs;
165    
166            /**
167             * _more_
168             * 
169             * @return _more_
170             */
171            protected List getPointObs() {
172                    return pointObs;
173            }
174    
175            /**
176             * _more_
177             * 
178             * @param doYearTime
179             *            _more_
180             * @param tracks
181             *            _more_
182             * @param times
183             *            _more_
184             * @param fields
185             *            _more_
186             * @param pointObs
187             *            _more_
188             * 
189             * @throws Exception
190             *             _more_
191             */
192            public void setData(boolean doYearTime, List<StormTrack> tracks,
193                            List times, List fields, List pointObs) throws Exception {
194                    this.pointObs = pointObs;
195                    stormTracks.clear();
196                    stormTracks.addAll(tracks);
197                    if (trackDisplay == null) {
198                            trackDisplay = new TrackDisplayable("year track ");
199                            trackDisplay.setLineWidth(2);
200                            stormTrackControl.addDisplayable(trackDisplay);
201                            trackDisplay.setColor(color);
202                            /*
203                             * labelDisplay = new StationModelDisplayable("storm year labels");
204                             * labelDisplay.setScale( stormTrackControl.getDisplayScale());
205                             * StationModelManager smm =
206                             * stormTrackControl.getControlContext().getStationModelManager();
207                             * StationModel model = smm.getStationModel("Location");
208                             * labelDisplay.setStationModel(model);
209                             * stormTrackControl.addDisplayable(labelDisplay);
210                             */
211                    }
212    
213                    if (doYearTime) {
214                            DateTime dttm = (DateTime) times.get(0);
215                            trackDisplay.setOverrideAnimationSet(Misc.newList(dttm));
216                            Data[] datas = (Data[]) fields.toArray(new Data[fields.size()]);
217                            times = Misc.newList(new DateTime(dttm.cloneButValue(dttm
218                                            .getValue() - 1000)), dttm, new DateTime(dttm
219                                            .cloneButValue(dttm.getValue() + 1000)));
220                            FieldImpl indexField = Util.indexedField(datas, false);
221                            fields = Misc.newList(indexField, indexField, indexField);
222                            trackDisplay.setTrack(Util.makeTimeField(fields, times));
223                            // System.err.println ("field:" + Util.makeTimeField(fields,
224                            // times));
225                    } else {
226                            trackDisplay.setOverrideAnimationSet((List) null);
227                            trackDisplay.setTrack(Util.makeTimeField(fields, times));
228                            // System.err.println ("no year");
229                            // labelDisplay.setStationData(
230                            // PointObFactory.makeTimeSequenceOfPointObs(pointObs, -1, -1));
231                    }
232    
233            }
234    
235            /**
236             * _more_
237             * 
238             * @return _more_
239             */
240            public JButton getButton() {
241                    if (button == null) {
242                            button = new JButton("");
243                            GuiUtils.setFixedWidthFont(button);
244                            setState(state);
245                            button.addActionListener(new ActionListener() {
246                                    public void actionPerformed(ActionEvent ae) {
247                                            if (state == STATE_ACTIVE) {
248                                                    state = STATE_INACTIVE;
249                                                    unload();
250                                            } else if (state == STATE_LOADING) {
251                                                    state = STATE_INACTIVE;
252                                            } else if (state == STATE_INACTIVE) {
253                                                    state = STATE_LOADING;
254                                                    stormTrackControl.loadYear(YearDisplayState.this);
255                                            }
256                                            setState(state);
257                                    }
258                            });
259                    }
260                    return button;
261            }
262    
263            /**
264             * _more_
265             * 
266             * @param msg
267             *            _more_
268             */
269            public void setStatus(String msg) {
270                    getLabel().setText(msg);
271            }
272    
273            /**
274             * _more_
275             * 
276             * @return _more_
277             */
278            public JLabel getLabel() {
279                    if (label == null) {
280                            label = new JLabel("");
281                    }
282                    return label;
283            }
284    
285            /**
286             * _more_
287             */
288            public void unload() {
289                    if (trackDisplay != null) {
290                            try {
291                                    stormTrackControl.removeDisplayable(trackDisplay);
292                                    if (labelDisplay != null) {
293                                            stormTrackControl.removeDisplayable(labelDisplay);
294                                    }
295                                    stormTrackControl.unloadYear(this);
296                            } catch (Exception exc) {
297                                    LogUtil.logException("Unloading tracks", exc);
298                            }
299                            trackDisplay = null;
300                            labelDisplay = null;
301                    }
302            }
303    
304            /**
305             * _more_
306             * 
307             * @return _more_
308             */
309            public List<StormTrack> getStormTracks() {
310                    return stormTracks;
311            }
312    
313            /**
314             * _more_
315             * 
316             * @return _more_
317             */
318            public StormTrackControl getStormTrackControl() {
319                    return stormTrackControl;
320            }
321    
322            /**
323             * _more_
324             * 
325             * @param stormTrackControl
326             *            _more_
327             */
328            public void setStormTrackControl(StormTrackControl stormTrackControl) {
329                    this.stormTrackControl = stormTrackControl;
330            }
331    
332            /**
333             * Set the Year property.
334             * 
335             * @param value
336             *            The new value for Year
337             */
338            public void setYear(int value) {
339                    year = value;
340            }
341    
342            /**
343             * Get the Year property.
344             * 
345             * @return The Year
346             */
347            public int getYear() {
348                    return year;
349            }
350    
351            /**
352             * Set the Color property.
353             * 
354             * @param value
355             *            The new value for Color
356             */
357            public void setColor(Color value) {
358                    color = value;
359            }
360    
361            /**
362             * Get the Color property.
363             * 
364             * @return The Color
365             */
366            public Color getColor() {
367                    return color;
368            }
369    
370            /**
371             * Get the Active property.
372             * 
373             * @return The Active
374             */
375            public boolean getActive() {
376                    return state == STATE_ACTIVE;
377            }
378    
379            /**
380             * Set the State property.
381             * 
382             * @param value
383             *            The new value for State
384             */
385            public void setState(int value) {
386                    state = value;
387                    if (button != null) {
388                            if (state == STATE_ACTIVE) {
389                                    button.setText("Unload");
390                            } else if (state == STATE_LOADING) {
391                                    button.setText("Cancel");
392                            } else if (state == STATE_INACTIVE) {
393                                    button.setText("Load  ");
394                            }
395                    }
396            }
397    
398            /**
399             * Get the State property.
400             * 
401             * @return The State
402             */
403            public int getState() {
404                    return state;
405            }
406    
407    }