1   /**
2    * Distribution License:
3    * BibleDesktop is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU General Public License, version 2 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/gpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: Splash.java 2091 2011-03-07 04:15:31Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.desktop;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.Font;
28  import java.awt.GridBagConstraints;
29  import java.awt.GridBagLayout;
30  import java.awt.Insets;
31  import java.awt.Toolkit;
32  import java.awt.event.MouseAdapter;
33  import java.awt.event.MouseEvent;
34  import java.io.IOException;
35  import java.io.ObjectInputStream;
36  
37  import javax.swing.BorderFactory;
38  import javax.swing.Icon;
39  import javax.swing.JLabel;
40  import javax.swing.JPanel;
41  import javax.swing.JWindow;
42  import javax.swing.SwingConstants;
43  
44  import org.crosswire.bibledesktop.BDMsg;
45  import org.crosswire.common.progress.JobManager;
46  import org.crosswire.common.progress.WorkEvent;
47  import org.crosswire.common.progress.WorkListener;
48  import org.crosswire.common.progress.swing.JobsProgressBar;
49  import org.crosswire.common.swing.GuiUtil;
50  
51  /**
52   * A Simple splash screen.
53   * <p>
54   * so start one of these call:
55   * 
56   * <pre>
57   * Splash s = new Splash();
58   * ... // init code
59   * s.close();
60   * </pre>
61   * 
62   * @see gnu.gpl.License for license details.<br>
63   *      The copyright to this program is held by it's authors.
64   * @author Joe Walker [joe at eireneh dot com]
65   */
66  public class Splash extends JWindow {
67      /**
68       * Create a splash window
69       */
70      public Splash() {
71          super(GuiUtil.getFrame(null));
72  
73          init();
74      }
75  
76      /**
77       * Init the graphics
78       */
79      private void init() {
80          // TRANSLATOR: This image is of an English Bible. It can be replaced with a localized one.
81          // It should be named splash_ll.png where ll is the 2 letter language code and put in the
82          // images directory. Then point this to it.
83          Icon icon = GuiUtil.getIcon(BDMsg.gettext("/images/splash.png"));
84  
85          JLabel lblPicture = new JLabel();
86          lblPicture.setBackground(Color.WHITE);
87          lblPicture.setOpaque(true);
88          lblPicture.setIcon(icon);
89          // lblPicture.setBorder(null);
90          lblPicture.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
91  
92          /*
93          JLabel lblText = new JLabel();
94          lblText.setFont(new Font(SPLASH_FONT, Font.BOLD, 48));
95          lblText.setForeground(new Color(0x99, 0x66, 0xAA));
96          lblText.setOpaque(false);
97          lblText.setVerticalAlignment(SwingConstants.BOTTOM);
98          lblText.setHorizontalAlignment(SwingConstants.TRAILING);
99          lblText.setText(Msg.SPLASH_TITLE.toString());
100         */
101 
102         JPanel pnlDisplay = new JPanel();
103         pnlDisplay.setLayout(new GridBagLayout());
104         // pnlDisplay.add(lblText, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
105         // GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
106         // 0, 10), 0, 0));
107         pnlDisplay.add(lblPicture, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0,
108                 0));
109 
110         JLabel lblInfo = new JLabel();
111         lblInfo.setBorder(null);
112         lblInfo.setFont(new Font(SPLASH_FONT, Font.PLAIN, 9));
113         lblInfo.setForeground(Color.WHITE);
114         lblInfo.setBackground(Color.BLACK);
115         // lblInfo.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
116         lblInfo.setHorizontalAlignment(SwingConstants.TRAILING);
117         lblInfo.setText(BDMsg.getVersionInfo() + ' ');
118         lblInfo.setOpaque(true);
119 
120         JobsProgressBar pnlJobs = new JobsProgressBar(false);
121         pnlJobs.setBackground(Color.WHITE);
122         pnlJobs.setForeground(Color.BLACK);
123         pnlJobs.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
124 
125         JPanel pnlInfo = new JPanel();
126         pnlInfo.setLayout(new BorderLayout(5, 0));
127         pnlInfo.setOpaque(true);
128         // pnlInfo.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
129         pnlInfo.add(lblInfo, BorderLayout.CENTER);
130         pnlInfo.add(pnlJobs, BorderLayout.SOUTH);
131 
132         this.getContentPane().setLayout(new BorderLayout());
133         this.getContentPane().add(pnlInfo, BorderLayout.SOUTH);
134         this.getContentPane().add(pnlDisplay, BorderLayout.CENTER);
135 
136         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
137         Dimension window = lblPicture.getPreferredSize();
138         this.setLocation(screen.width / 2 - (window.width / 2), screen.height / 2 - (window.height / 2));
139 
140         JobManager.addWorkListener(listener);
141 
142         this.addMouseListener(new MouseAdapter() {
143             /* (non-Javadoc)
144              * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
145              */
146             @Override
147             public void mousePressed(MouseEvent ev) {
148                 close();
149             }
150         });
151 
152         GuiUtil.applyDefaultOrientation(this);
153         this.pack();
154         this.setVisible(true);
155     }
156 
157     /**
158      * Shut up shop
159      */
160     public final void close() {
161         JobManager.removeWorkListener(listener);
162 
163         setVisible(false);
164         dispose();
165     }
166 
167     /**
168      * Serialization support.
169      * 
170      * @param is
171      * @throws IOException
172      * @throws ClassNotFoundException
173      */
174     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
175         listener = new CustomWorkListener();
176         is.defaultReadObject();
177     }
178 
179     private transient CustomWorkListener listener = new CustomWorkListener();
180 
181     private static final String SPLASH_FONT = "SanSerif";
182 
183     /**
184      * Serialization ID
185      */
186     private static final long serialVersionUID = 3258133565731256119L;
187 
188     /**
189      * Pack the frame if we get new jobs that could shunt things around
190      */
191     final class CustomWorkListener implements WorkListener {
192         /* (non-Javadoc)
193          * @see org.crosswire.common.progress.WorkListener#workProgressed(org.crosswire.common.progress.WorkEvent)
194          */
195         public void workProgressed(WorkEvent ev) {
196 //            Progress job = ev.getJob();
197 //            if (job.getWork() == 0 || job.isFinished()) {
198                 Splash.this.pack();
199 //            }
200         }
201 
202         /* (non-Javadoc)
203          * @see org.crosswire.common.progress.WorkListener#workStateChanged(org.crosswire.common.progress.WorkEvent)
204          */
205         public void workStateChanged(WorkEvent ev) {
206             // we don't care about these events.
207         }
208     }
209 }
210