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