001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2023
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.util;
030
031import java.awt.Component;
032import java.awt.Container;
033import java.awt.EventQueue;
034
035import javax.swing.JButton;
036import javax.swing.JDialog;
037import javax.swing.JOptionPane;
038
039import java.util.List;
040import java.util.ArrayList;
041
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045public class OptionPaneClicker {
046
047    private static final Logger logger =
048        LoggerFactory.getLogger(OptionPaneClicker.class);
049
050    private final Thread clickDelayer;
051
052    public OptionPaneClicker(JOptionPane optionPane,
053                             String dialogTitle,
054                             long clickDelay,
055                             String buttonText)
056    {
057        JDialog frame = optionPane.createDialog(null, dialogTitle);
058        frame.setModal(true);
059        optionPane.selectInitialValue();
060        optionPane.setVisible(true);
061
062        clickDelayer = new Thread() {
063            public void run() {
064                try {
065                    sleep(clickDelay);
066                } catch (InterruptedException e) {
067                    logger.error("Click delayer interrupted!", e);
068                } finally {
069                    final JButton btn = getButton(frame, buttonText);
070                    if (btn != null) {
071                        EventQueue.invokeLater(btn::doClick);
072                    }
073                }
074            }
075        };
076
077        simulateClick();
078    }
079
080    private void simulateClick() {
081        clickDelayer.start();
082    }
083
084    public static JButton getButton(Container container, String text) {
085        JButton btn = null;
086        List<Container> children = new ArrayList<>(25);
087        for (Component child : container.getComponents()) {
088            if (child instanceof JButton) {
089                JButton button = (JButton)child;
090                if (text.equals(button.getText())) {
091                    btn = button;
092                    break;
093                }
094            } else if (child instanceof Container) {
095                children.add((Container)child);
096            }
097        }
098        if (btn == null) {
099            for (Container cont : children) {
100                JButton button = getButton(cont, text);
101                if (button != null) {
102                    btn = button;
103                    break;
104                }
105            }
106        }
107        return btn;
108    }
109}