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