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