001/*
002 * $Id: WelcomeWindow.java,v 1.12 2011/03/24 16:06:35 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2011
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 */
030package edu.wisc.ssec.mcidasv.util;
031
032import static edu.wisc.ssec.mcidasv.util.McVGuiUtils.setButtonImage;
033
034import java.awt.Dimension;
035
036import javax.swing.JButton;
037import javax.swing.JLabel;
038import javax.swing.JPanel;
039import javax.swing.JScrollPane;
040import javax.swing.JTextPane;
041import javax.swing.event.HyperlinkEvent;
042
043/**
044 * {@code WelcomeWindow} is really just intended to <i>try</i> to detect known
045 * hardware problems and inform the user about any problems.
046 *
047 * <p>The current implementation does not perform <i>any</i> detection, but
048 * expect this to change.
049 */
050// NOTE TO MCV CODERS:
051// **DOCUMENT WHAT CHECKS AND/OR DETECTION ARE BEING PERFORMED**
052public class WelcomeWindow extends javax.swing.JFrame {
053
054    /** Path to {@literal "header"} image. */
055    private static final String LOGO_PATH = 
056        "/edu/wisc/ssec/mcidasv/images/mcidasv_logo.gif";
057
058    /** Path to the HTML to display within {@link #textPane}. */
059    private static final String WELCOME_HTML =
060        "/edu/wisc/ssec/mcidasv/resources/welcome.html";
061
062    /**
063     * Message to display if there was a problem loading
064     * {@link #WELCOME_HTML}.
065     */
066    private static final String ERROR_MESSAGE =
067        "McIDAS-V had a problem displaying its welcome message. Please"
068        + " contact the McIDAS Help Desk for assistance.";
069
070    /** Dimensions of the welcome window frame. */
071    private static final Dimension WINDOW_SIZE = new Dimension(495, 431);
072
073    /** Java-friendly location of the path to the welcome message. */
074    private final java.net.URL contents;
075
076    /** 
077     * Creates new form WelcomeWindow
078     */
079    public WelcomeWindow() {
080        this.contents = WelcomeWindow.class.getResource(WELCOME_HTML);
081        initComponents();
082    }
083
084    /** 
085     * This method is called from within the constructor to
086     * initialize the form.
087     * WARNING: Do NOT modify this code. The content of this method is
088     * always regenerated by the Form Editor.
089     */
090    // <editor-fold defaultstate="collapsed" desc="Generated Code">
091    private void initComponents() {
092
093        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
094        setTitle("Welcome to McIDAS-V");
095        setLocationByPlatform(true);
096        addWindowListener(new java.awt.event.WindowAdapter() {
097            public void windowClosing(java.awt.event.WindowEvent evt) {
098                formWindowClosing(evt);
099            }
100        });
101
102        logoPanel.setLayout(new java.awt.GridBagLayout());
103
104        logoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(LOGO_PATH))); // NOI18N
105        logoPanel.add(logoLabel, new java.awt.GridBagConstraints());
106
107        textPane.setEditable(false);
108        try {
109            textPane.setPage(contents);
110        } catch (java.io.IOException e) {
111            textPane.setText(ERROR_MESSAGE);
112            e.printStackTrace();
113        }
114        textPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
115            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
116                textPaneHyperlinkUpdate(evt);
117            }
118        });
119        scrollPane.setViewportView(textPane);
120
121        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
122        mainPanel.setLayout(mainPanelLayout);
123        mainPanelLayout.setHorizontalGroup(
124            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
125            .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
126        );
127        mainPanelLayout.setVerticalGroup(
128            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
129            .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
130        );
131
132        setButtonImage(startButton, McVGuiUtils.ICON_APPLY_SMALL);
133        startButton.addActionListener(new java.awt.event.ActionListener() {
134            public void actionPerformed(java.awt.event.ActionEvent evt) {
135                startButtonActionPerformed(evt);
136            }
137        });
138
139        setButtonImage(quitButton, McVGuiUtils.ICON_CANCEL_SMALL);
140        quitButton.addActionListener(new java.awt.event.ActionListener() {
141            public void actionPerformed(java.awt.event.ActionEvent evt) {
142                quitButtonActionPerformed(evt);
143            }
144        });
145
146        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
147        buttonPanel.setLayout(buttonPanelLayout);
148        buttonPanelLayout.setHorizontalGroup(
149            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
150            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
151                .addContainerGap(144, Short.MAX_VALUE)
152                .addComponent(quitButton)
153                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
154                .addComponent(startButton))
155        );
156        buttonPanelLayout.setVerticalGroup(
157            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
158            .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
159                .addComponent(startButton)
160                .addComponent(quitButton))
161        );
162
163        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
164        getContentPane().setLayout(layout);
165        layout.setHorizontalGroup(
166            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
167            .addGroup(layout.createSequentialGroup()
168                .addContainerGap()
169                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
170                    .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
171                    .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
172                    .addComponent(logoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
173                .addContainerGap())
174        );
175        layout.setVerticalGroup(
176            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
177            .addGroup(layout.createSequentialGroup()
178                .addContainerGap()
179                .addComponent(logoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
180                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
181                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
182                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
183                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
184                .addContainerGap())
185        );
186
187        pack();
188        setSize(WINDOW_SIZE);
189    }// </editor-fold>
190
191    /**
192     * Handles the user clicking on {@link #startButton}. 
193     * Executes {@code System.exit(0)} in an effort to signal to the startup
194     * scripts that the window terminated {@literal "normally"}.
195     * 
196     * @param evt Event to handle. Currently ignored.
197     */
198    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
199        System.exit(0);
200    }
201
202    /**
203     * Handles the user clicking on {@link #quitButton}. Doesn't do anything
204     * aside from handing off things to
205     * {@link #formWindowClosing(java.awt.event.WindowEvent)}
206     *
207     * @param evt Event to handle. Currently ignored.
208     *
209     * @see #formWindowClosing(java.awt.event.WindowEvent)
210     */
211    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {
212        formWindowClosing(null);
213    }
214
215    /**
216     * Handles the user opting to close the welcome window
217     * {@link javax.swing.JFrame}. Executes {@code System.exit(1)} in an 
218     * effort to signal to the startup scripts that window terminated 
219     * {@literal "abnormally"}.
220     * 
221     * <p>An abnormal termination will result in the startup script 
222     * terminating the launch of McIDAS-V.
223     * 
224     * @param evt Note that this parameter is currently ignored.
225     */
226    private void formWindowClosing(java.awt.event.WindowEvent evt) {
227        System.exit(1);
228    }
229
230    /**
231     * Listens to {@link #textPane} in order to handle the user clicking on
232     * HTML links.
233     *
234     * @param evt Event to handle. Anything other than
235     * {@link javax.swing.event.HyperlinkEvent.EventType#ACTIVATED} is ignored.
236     *
237     * @see WebBrowser#browse(String)
238     */
239    private void textPaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
240        if (evt.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
241            return;
242
243        String url = null;
244        if (evt.getURL() == null)
245            url = evt.getDescription();
246        else
247            url = evt.getURL().toString();
248
249        WebBrowser.browse(url);
250    }
251
252    /**
253     * @param args the command line arguments
254     */
255    public static void main(String args[]) {
256        java.awt.EventQueue.invokeLater(new Runnable() {
257            public void run() {
258                new WelcomeWindow().setVisible(true);
259            }
260        });
261    }
262
263    // boring gui components
264    private final JPanel buttonPanel = new JPanel();
265    private final JLabel logoLabel = new JLabel();
266    private final JPanel logoPanel = new JPanel();
267    private final JPanel mainPanel = new JPanel();
268    private final JButton quitButton = new JButton("Quit");
269    private final JScrollPane scrollPane = new JScrollPane();
270    private final JButton startButton = new JButton("Start McIDAS-V");
271    private final JTextPane textPane = new JTextPane();
272}