View Javadoc

1   package com.explosion.utilities.dialog;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Canvas;
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.Font;
28  import java.awt.Graphics;
29  import java.awt.Image;
30  import java.awt.Toolkit;
31  
32  import javax.swing.JWindow;
33  
34  import com.explosion.expf.ExpConstants;
35  import com.explosion.expf.preferences.SystemPreferences;
36  import com.explosion.utilities.FileSystemUtils;
37  import com.explosion.utilities.GeneralConstants;
38  import com.explosion.utilities.GeneralUtils;
39  
40  public class SplashScreen extends JWindow
41  {
42  
43      private SplashCanvas canvas;
44  
45      private String splashScreenImage = null;
46  
47      private Color textColor = Color.white;
48      
49      int parentHeight = -1;
50      int parentWidth = -1;
51      int parentXpos = -1;
52      int parentYpos = -1;
53  
54      public SplashScreen(String splashScreenImage, Color textColor)
55      {
56          try
57          {
58              this.splashScreenImage = splashScreenImage;
59              this.textColor = textColor;
60              init();
61          } catch (Exception e)
62          {
63              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
64          }
65      }
66      
67      public SplashScreen(String splashScreenImage, Color textColor, int parentHeight, int parentWidth, int parentXpos, int parentYpos)
68      {
69          try
70          {
71              this.splashScreenImage = splashScreenImage;
72              this.textColor = textColor;
73              init();
74          } catch (Exception e)
75          {
76              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
77          }
78      }
79  
80      private void init() throws Exception
81      {
82          canvas = new SplashCanvas(splashScreenImage, "Loading...", textColor);
83          this.getContentPane().add(canvas, BorderLayout.CENTER);
84          this.pack();
85          setLocation();
86          this.setVisible(true);
87      }
88  
89      /***
90       * Alligns the Splash Screen in the centre of where the app will be.
91       *
92       */
93      public void setLocation()
94      {
95      	boolean startCentered = ((Boolean) SystemPreferences.getPreference(ExpConstants.STARTCENTERED).getValue()).booleanValue();
96          if (startCentered || SystemPreferences.getPreference(ExpConstants.HEIGHT).isDefaulted())
97          {
98          	GeneralUtils.centreWindowOnScreen(this);	
99          }
100         else
101         {
102         	int appHeight = ((Integer) SystemPreferences.getPreference(ExpConstants.HEIGHT).getValue()).intValue() - 15;
103             int appWidth = ((Integer) SystemPreferences.getPreference(ExpConstants.WIDTH).getValue()).intValue() - 15;
104             int appX = ((Integer) SystemPreferences.getPreference(ExpConstants.XPOS).getValue()).intValue();
105 	        int appY = ((Integer) SystemPreferences.getPreference(ExpConstants.YPOS).getValue()).intValue();
106 	        
107 	        /* Work out an ideal position */
108 	        int xpos = appX + ((appWidth/2) - (this.getWidth()/2));
109 	        int ypos = appY + ((appHeight/2) - (this.getHeight()/2));
110 	        
111 	        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
112 	        
113 	        /* Check X bounds */
114 	        xpos = xpos < 0 ? 0 : xpos;
115 	        xpos = xpos+this.getWidth() >  screenSize.getWidth() ? (int)screenSize.getWidth() - this.getWidth() : xpos;
116 	        
117 	        /* Check Y bounds */
118 	        ypos = ypos < 0 ? 0 : ypos;
119 	        ypos = ypos+this.getHeight() >  screenSize.getHeight() ? (int)screenSize.getHeight() - this.getHeight() : ypos;
120 	        
121 	        this.setLocation(xpos,ypos);
122         }
123     }
124     
125     public void updateText(String text)
126     {
127         canvas.updateText(text);
128     }
129 
130 }
131 
132 class SplashCanvas extends Canvas
133 {
134 
135     private Image image;
136 
137     private String text;
138 
139     private Font font = GeneralConstants.DEFAULT_WINDOW_FONT;
140 
141     private final int xpos = 35;
142 
143     private final int ypos = 292;
144 
145     private Color textColor = Color.white;
146     
147     private boolean drawBorder = true;
148     
149     private Color borderColor = Color.black;
150 
151     public SplashCanvas(String imageName, String initialText, Color textColor)
152     {
153         try
154         {
155             image = FileSystemUtils.loadImage(imageName, this);
156             this.setSize(new Dimension(image.getWidth(null), image.getHeight(null)));
157             text = initialText;
158             this.textColor = textColor;
159             this.setFont(font);
160             this.setBackground(Color.WHITE);
161         } catch (Exception e)
162         {
163             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
164         }
165     }
166 
167     public void update(Graphics g)
168     {
169         paint(g);
170     }
171 
172     public void paint(Graphics g)
173     {
174         g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
175         g.setColor(textColor);
176         g.drawString(text, xpos, ypos);
177         if (drawBorder)
178         {
179           g.setColor(borderColor);
180           g.drawRect(0,0,image.getWidth(null)-1, image.getHeight(null)-2);
181         }
182     }
183 
184     public void updateText(String text)
185     {
186         this.text = text;
187         this.repaint();
188     }
189     
190     /***
191      * This should be set to true (default) if you want a 1 pixel black (default) border around your splashscreen
192      * @param draw
193      */
194     public void setDrawBorder(boolean draw)
195     {
196         this.drawBorder = draw;
197     }
198     
199     /***
200      * Sets the color the border will be, black is default
201      * @param borderColor
202      */
203     public void setBorderColor(Color borderColor)
204     {
205         this.borderColor = borderColor;
206     }
207 }