001/*
002 * Copyright (c) 1997-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.awt.Color;
033import java.awt.Container;
034import java.awt.Dimension;
035import java.awt.FlowLayout;
036import java.awt.Font;
037import java.awt.GridLayout;
038import java.awt.Toolkit;
039import java.awt.event.ActionEvent;
040import java.awt.event.ActionListener;
041import java.awt.event.MouseEvent;
042import java.awt.event.MouseListener;
043import java.awt.font.TextAttribute;
044import java.lang.reflect.Method;
045import java.util.HashMap;
046import java.util.Map;
047
048import javax.swing.BorderFactory;
049import javax.swing.ImageIcon;
050import javax.swing.JButton;
051import javax.swing.JCheckBox;
052import javax.swing.JDialog;
053import javax.swing.JFrame;
054import javax.swing.JLabel;
055import javax.swing.JPanel;
056import javax.swing.border.Border;
057
058//////////////////////////////////////////////////////////////////////////
059//// WelcomeScreen
060/**
061 * A welcome screen which is displayed on Kepler Startup. The user can further
062 * select links (Scientists/Programmers) from the above screen, leading to
063 * tutorial getting started pages. The proposed design for the above screen can
064 * be found at: http://kepler-project.org/Wiki.jsp?page=SplashAndWelcomeScreens
065 * 
066 *@author nmangal
067 *@since November 9, 2006
068 *@version $Id: WelcomeScreen.java,v 1.0 2006/13/02 20:39:04
069 */
070
071public class WelcomeScreen extends JDialog {
072
073        private String keplerLogoURL;
074        private JCheckBox startupCheck;
075        private JButton close;
076        private JLabel scientistLink;
077        private JLabel programmerLink;
078
079        /**
080         * Construct the main panel.Further add subPanels to the main panel, namely
081         * infoPanel and buttonPanel. infoPanel is the bordered panel, consisting of
082         * Welcome message and "Getting Started" links for scientist and
083         * programmers. The button Panel simply consists of a Close button and a
084         * CheckBox to indicate the setting to show the welcome screen on future
085         * startups.
086         * 
087         *@param keplerLogoURL
088         *            Description of the Parameter
089         */
090        public WelcomeScreen(String keplerLogoURL) {
091
092                // super(new BorderLayout());
093                this.keplerLogoURL = keplerLogoURL;
094
095                // Create and set up the window.
096                int frameWidth = 508;
097                int frameHeight = 304;
098                setSize(frameWidth, frameHeight);
099                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
100                setLocation(screenSize.width / 2 - frameWidth / 2, screenSize.height
101                                / 2 - frameHeight / 2);
102                setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
103                setResizable(false);
104
105                // Add Icon Image to the welcome screen window
106                // Image img = Toolkit.getDefaultToolkit().getImage(keplerLogoURL);
107                // setIconImage( img );
108
109                // Set up the content pane.
110                Container contentPane = this.getContentPane();
111                contentPane.setLayout(new GridLayout(1, 1));
112
113                // mainPanel
114                JPanel mainPanel = new JPanel();
115                mainPanel.setBackground(new Color(190, 200, 211));
116                mainPanel.setLayout(new FlowLayout());
117                // A border that puts extra pixels at the sides & bottom
118                Border emptyBorder = BorderFactory.createEmptyBorder(9, 18, 52, 18);
119                mainPanel.setBorder(emptyBorder);
120
121                // Add the sub panels
122                addWelcomePanel(mainPanel);
123                addButtonPanel(mainPanel);
124
125                // Add the mainPanel to WelcomeScreen
126                contentPane.add(mainPanel);
127
128                // this is kind of a hack so as to make
129                // WelomeWindow on top of Kepler Main Page
130                // due to fact taht Kepler Main Page takes about
131                // 6 seconds to startup.
132                try {
133                        Thread.sleep(6500);
134                } catch (InterruptedException ex) {
135                        setVisible(false);
136                        dispose();
137                }
138
139                toFront();
140                requestFocus();
141
142                // Display the window.
143                setVisible(true);
144
145        }
146
147        /**
148         * This panel adds a startup setting checkbox and a close button to the
149         * right.
150         * 
151         *@param container
152         *            , to which the buttons are added
153         */
154        void addButtonPanel(Container container) {
155
156                // mainButtonPanel
157                JPanel mainButtonPanel = new JPanel();
158                // Setting layout to null, allows us to give
159                // specific alignments to components
160                mainButtonPanel.setLayout(null);
161                mainButtonPanel.setBackground(new Color(190, 200, 211));
162                mainButtonPanel.setPreferredSize(new Dimension(470, 201));
163
164                // add the components
165                addStartupCheck(mainButtonPanel);
166                addCloseButton(mainButtonPanel);
167
168                container.add(mainButtonPanel);
169        }
170
171        /**
172         * Add a checkBox to the button Panel
173         * 
174         *@param container
175         *            , to which the buttons are added
176         */
177        void addStartupCheck(Container container) {
178
179                startupCheck = new JCheckBox(
180                                "<html><table cellpadding=0><tr><td width=9/><td><font size=3>Show this dialog upon startup </font></td></tr></table></html>");
181                startupCheck.setLocation(0, -28);
182                startupCheck.setSize(170, 100);
183                startupCheck.setBorder(null);
184                startupCheck.setFont(new Font("Times New Roman", Font.PLAIN, 12));
185                startupCheck.setBackground(new Color(190, 200, 211));
186                startupCheck.setSelected(true);
187
188                container.add(startupCheck);
189        }
190
191        /**
192         * Add a close button to the button panel
193         * 
194         *@param container
195         *            , to which the buttons are added
196         */
197        void addCloseButton(Container container) {
198
199                close = new JButton("Close");
200                close.setSize(75, 22);
201                close.setLocation(395, 10);
202                close.setFont(new Font("Times New Roman", Font.PLAIN, 12));
203
204                close.addActionListener(new ActionListener() {
205                        public void actionPerformed(ActionEvent evt) {
206                                setVisible(false);
207                                dispose();
208                        }
209                });
210
211                container.add(close);
212        }
213
214        /**
215         * Add All the Message Labels & Links to the main WelcomePanel
216         * 
217         *@param container
218         *            , to which the buttons are added
219         */
220        void addWelcomePanel(Container container) {
221
222                JPanel mainWelcomePanel = new JPanel();
223                // to be able to add components at specific locations.
224                mainWelcomePanel.setLayout(null);
225                mainWelcomePanel.setBorder(BorderFactory.createLineBorder(Color.black));
226                mainWelcomePanel.setBackground(new Color(227, 231, 236));
227                mainWelcomePanel.setPreferredSize(new Dimension(470, 208));
228                mainWelcomePanel.setLocation(100, 100);
229
230                // Add the Image in the Left Area in the bordered Panel
231                JLabel logo = new JLabel(new ImageIcon(keplerLogoURL));
232                logo.setLocation(-28, -30);
233                logo.setSize(200, 200);
234                mainWelcomePanel.add(logo);
235
236                // Add the Main Welcome Message
237                JLabel welcomeMsg = new JLabel("Welcome to Kepler 1.0");
238                welcomeMsg.setLocation(130, -30);
239                welcomeMsg.setSize(400, 100);
240                // set Font
241                Font font = new Font("Arial", Font.BOLD, 28);
242                Map fontAttr = new HashMap();
243                fontAttr.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
244                font = font.deriveFont(fontAttr);
245                welcomeMsg.setFont(font);
246                mainWelcomePanel.add(welcomeMsg);
247
248                // Engrave Welcome Msg if Required/Make Bolder
249                // TextAttribute.WEIGHT_EXTRABOLD bug in jdk1.5,1.4.2, hence
250                // making msg bold this way.
251                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4920831
252                JLabel bolderMsg = new JLabel("Welcome to Kepler 1.0");
253                bolderMsg.setLocation(129, -30);
254                bolderMsg.setSize(400, 100);
255                bolderMsg.setFont(font);
256                // All the above label does is make
257                // the welcome message look bolder.
258                mainWelcomePanel.add(bolderMsg);
259
260                // Add the Kepler Intro message
261                JLabel introMsg = new JLabel();
262                introMsg.setLocation(162, -127);
263                introMsg.setSize(235, 400);
264                introMsg
265                                .setText("<html>A collaborative environment for creating and executing scientific workflows</html>");
266                introMsg.setFont(new Font("Arial", Font.BOLD, 16));
267                mainWelcomePanel.add(introMsg);
268
269                // Add the Getting Started message
270                JLabel getStartedMsg = new JLabel("Getting Started");
271                getStartedMsg.setLocation(137, 85);
272                getStartedMsg.setSize(400, 100);
273                getStartedMsg.setFont(new Font("Arial", Font.BOLD, 20));
274                mainWelcomePanel.add(getStartedMsg);
275
276                // Engrave Getting Started Msg if Required/Make Bolder
277                // TextAttribute.WEIGHT_EXTRABOLD bug in jdk1.5,1.4.2
278                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4920831
279                JLabel bolderMsg2 = new JLabel("Getting Started");
280                bolderMsg2.setLocation(136, 85);
281                bolderMsg2.setSize(400, 100);
282                bolderMsg2.setFont(new Font("Arial", Font.BOLD, 20));
283                mainWelcomePanel.add(bolderMsg2);
284
285                // Add the Scientists Link
286                scientistLink = new JLabel();
287                scientistLink.setLocation(162, 150);
288                scientistLink.setSize(77, 40);
289                scientistLink.setFont(new Font("Arial", Font.PLAIN, 18));
290                scientistLink.setText("<html><a href=" + "" + ">" + "Scientists"
291                                + "</a>");
292                scientistLink.addMouseListener(new MouseListener() {
293
294                        String url = "http://kepler-project.org/Wiki.jsp?page=Documentation";
295
296                        public void mouseClicked(MouseEvent evt) {
297                                new BrowserLauncher().openFrameLink(url);
298                        }
299
300                        public void mouseExited(MouseEvent evt) {
301                        }
302
303                        public void mouseEntered(MouseEvent evt) {
304
305                                scientistLink.setToolTipText(url);
306                        }
307
308                        public void mousePressed(MouseEvent evt) {
309                        }
310
311                        public void mouseReleased(MouseEvent evt) {
312                        }
313                });
314                mainWelcomePanel.add(scientistLink);
315
316                // Add the Programmers Link
317                programmerLink = new JLabel();
318                programmerLink.setLocation(162, 177);
319                programmerLink.setSize(108, 20);
320                programmerLink.setFont(new Font("Arial", Font.PLAIN, 18));
321                programmerLink.setText("<html><a href=" + "" + ">" + "Programmers"
322                                + "</a>");
323                programmerLink.addMouseListener(new MouseListener() {
324
325                        String url = "http://kepler-project.org/Wiki.jsp?page=DevelopmentForKepler";
326
327                        public void mouseClicked(MouseEvent evt) {
328                                new BrowserLauncher().openFrameLink(url);
329                        }
330
331                        public void mouseExited(MouseEvent evt) {
332                        }
333
334                        public void mouseEntered(MouseEvent evt) {
335
336                                programmerLink.setToolTipText(url);
337                        }
338
339                        public void mousePressed(MouseEvent evt) {
340                        }
341
342                        public void mouseReleased(MouseEvent evt) {
343                        }
344
345                });
346                mainWelcomePanel.add(programmerLink);
347
348                container.add(mainWelcomePanel);
349
350        }
351
352        /**
353         *@return Selected State of startup setting
354         */
355        public boolean getStartupCheck() {
356
357                return startupCheck.isSelected();
358        }
359
360        /**
361         * The main program for the WelcomeScreen class
362         * 
363         *@param args
364         *            The command line arguments
365         */
366        public static void main(String[] args) {
367                javax.swing.SwingUtilities.invokeLater(new Runnable() {
368                        public void run() {
369
370                                String keplerLogo = System.getProperty("KEPLER")
371                                                + "common/resources/images/KeplerLogoNew.png";
372                                WelcomeScreen w = new WelcomeScreen(keplerLogo);
373                        }
374                });
375        }
376
377        /**
378         * BrowserLauncher to launch URL on the WelcomeScreen
379         * 
380         *@author berkley
381         *@since November 9, 2006
382         */
383
384        public static class BrowserLauncher {
385
386                /**
387                 * open browser depending on operating system.
388                 * 
389                 *@param url
390                 *            Description of the Parameter
391                 */
392                public static void openFrameLink(String url) {
393
394                        try {
395                                String os = System.getProperty("os.name");
396                                if (os.startsWith("Windows")) {
397                                        // os is windows
398                                        Runtime.getRuntime().exec(
399                                                        "rundll32 url.dll,FileProtocolHandler " + url);
400                                } else if (os.startsWith("Mac OS")) {
401                                        // os is mac
402                                        Method openURLMethod = (Class
403                                                        .forName("com.apple.eio.FileManager"))
404                                                        .getDeclaredMethod("openURL",
405                                                                        new Class[] { String.class });
406                                        openURLMethod.invoke(null, new Object[] { url });
407                                } else {
408                                        // os is either unix or linux
409                                        String[] browsers = { "firefox", "opera", "konqueror",
410                                                        "epiphany", "mozilla", "netscape" };
411                                        String browser = "";
412                                        for (int i = 0; i < browsers.length && browser == null; i++) {
413                                                if (Runtime.getRuntime().exec(
414                                                                new String[] { "which", browsers[i] })
415                                                                .waitFor() == 0) {
416                                                        browser = browsers[i];
417                                                }
418                                        }
419                                        if (browser != null) {
420                                                Runtime.getRuntime()
421                                                                .exec(new String[] { browser, url });
422                                        }
423                                }
424
425                        } catch (Exception e) {
426                                System.err.println(e.toString());
427                        }
428
429                }
430
431        }
432
433}