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    package edu.wisc.ssec.mcidasv.ui;
029    
030    import static edu.wisc.ssec.mcidasv.util.CollectionHelpers.cast;
031    
032    import java.awt.EventQueue;
033    import java.awt.Font;
034    import java.awt.event.ActionEvent;
035    import java.awt.event.ActionListener;
036    import java.awt.event.ItemEvent;
037    import java.awt.event.ItemListener;
038    import java.util.List;
039    
040    import javax.swing.JFrame;
041    import javax.swing.JPanel;
042    import javax.swing.JButton;
043    import javax.swing.JToggleButton;
044    
045    import edu.wisc.ssec.mcidasv.util.McVGuiUtils;
046    import net.miginfocom.swing.MigLayout;
047    import javax.swing.JTextField;
048    import javax.swing.JLabel;
049    
050    import org.slf4j.Logger;
051    import org.slf4j.LoggerFactory;
052    
053    import ucar.unidata.idv.ViewManager;
054    import ucar.unidata.idv.ui.IdvComponentGroup;
055    import ucar.unidata.idv.ui.IdvWindow;
056    
057    public class LayerAnimationWindow extends JFrame {
058    
059        private static final Logger logger = LoggerFactory.getLogger(LayerAnimationWindow.class);
060        
061        private JPanel contentPane;
062        private JTextField fieldCurrentDwell;
063        private JToggleButton tglbtnEnableAnimation;
064        private JButton btnSlower;
065        private JButton btnFaster;
066        private JLabel lblDwell; 
067        private JLabel statusLabel;
068    
069        /**
070         * Create the frame.
071         */
072        public LayerAnimationWindow() {
073            setTitle("Animate Visibility");
074            setResizable(true);
075            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
076            setBounds(100, 100, 208, 162);
077            contentPane = new JPanel();
078            setContentPane(contentPane);
079            tglbtnEnableAnimation = new JToggleButton("Enable Animation");
080    
081            tglbtnEnableAnimation.addItemListener(new ItemListener() {
082                public void itemStateChanged(ItemEvent event) {
083                    tglbtnEnableAnimationChanged(event);
084                }
085            });
086            btnSlower = new JButton("Slower");
087            btnSlower.setEnabled(false);
088            btnSlower.addActionListener(new ActionListener() {
089                public void actionPerformed(ActionEvent event) {
090                    btnSlowerActionPerformed(event);
091                }
092            });
093    
094            btnFaster = new JButton("Faster");
095            btnFaster.setEnabled(false);
096            btnFaster.addActionListener(new ActionListener() {
097                public void actionPerformed(ActionEvent event) {
098                    btnFasterActionPerformed(event);
099                }
100            });
101    
102            lblDwell = new JLabel("Dwell (s):");
103            lblDwell.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
104            lblDwell.setEnabled(false);
105    
106            fieldCurrentDwell = new JTextField();
107            fieldCurrentDwell.setEnabled(false);
108            fieldCurrentDwell.setEditable(false);
109            fieldCurrentDwell.setText("0.0");
110            fieldCurrentDwell.setColumns(6);
111    
112            statusLabel = new JLabel("");
113            statusLabel.setEnabled(false);
114    
115            contentPane.setLayout(new MigLayout("", "[grow][grow][][]", "[][][][]"));
116            contentPane.add(tglbtnEnableAnimation, "flowy,cell 0 0 3 1,growx,aligny top");
117            contentPane.add(btnSlower, "cell 0 1,alignx right,growy");
118            contentPane.add(btnFaster, "cell 2 1,alignx left,growy");
119            contentPane.add(lblDwell, "cell 0 2,alignx right,aligny baseline");
120            contentPane.add(fieldCurrentDwell, "cell 2 2,alignx left,aligny baseline");
121            contentPane.add(statusLabel, "cell 0 3 3 1");
122        }
123    
124        // dear god! change thes
125        private void tglbtnEnableAnimationChanged(final ItemEvent event) {
126            logger.trace("toggle: {}", event);
127            boolean animationEnabled = (event.getStateChange() == ItemEvent.SELECTED);
128            btnSlower.setEnabled(animationEnabled);
129            btnFaster.setEnabled(animationEnabled);
130            ViewManager viewManager = getActiveViewManager();
131            viewManager.setAnimatedVisibilityCheckBox(animationEnabled);
132            double currentSpeed = viewManager.getVisibilityAnimationSpeed();
133            String dwell = Double.toString(currentSpeed / 1000.0);
134            fieldCurrentDwell.setText(dwell);
135        }
136    
137        private void btnFasterActionPerformed(final ActionEvent event) {
138            ViewManager viewManager = getActiveViewManager();
139            viewManager.fasterVisibilityAnimation();
140            double currentSpeed = viewManager.getVisibilityAnimationSpeed();
141            String dwell = Double.toString(currentSpeed / 1000.0);
142            fieldCurrentDwell.setText(dwell);
143            logger.trace("faster: animationSpeed={}", dwell);
144        }
145    
146        private void btnSlowerActionPerformed(final ActionEvent event) {
147            ViewManager viewManager = getActiveViewManager();
148            viewManager.slowerVisibilityAnimation();
149            double currentSpeed = viewManager.getVisibilityAnimationSpeed();
150            String dwell = Double.toString(currentSpeed / 1000.0);
151            fieldCurrentDwell.setText(dwell);
152            logger.trace("slower: animationSpeed={}", dwell);
153        }
154    
155        private ViewManager getActiveViewManager() {
156            List<ViewManager> viewManagers = McVGuiUtils.getActiveViewManagers();
157            if (viewManagers.size() != 1) {
158                statusLabel.setText("no multipanel support yet :(");
159                logger.trace("woe betide the person venturing into shared groups");
160            }
161            ViewManager viewManager = viewManagers.get(0);
162            logger.trace("found a ViewManager: name={} isActive={}", viewManager.getName(), viewManager.getIsActive());
163            return viewManager;
164        }
165    
166        /**
167         * Launch the application.
168         */
169        public static void main(String[] args) {
170            EventQueue.invokeLater(new Runnable() {
171                public void run() {
172                    try {
173                        LayerAnimationWindow frame = new LayerAnimationWindow();
174                        frame.setVisible(true);
175                    } catch (Exception e) {
176                        logger.error("init window", e);
177                    }
178                }
179            });
180        }
181    }