1 package com.sslexplorer.server; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Component ; 6 import java.awt.Dimension ; 7 import java.awt.GraphicsConfiguration ; 8 import java.awt.GraphicsDevice ; 9 import java.awt.Image ; 10 import java.awt.Rectangle ; 11 import java.awt.Toolkit ; 12 import java.awt.event.ActionEvent ; 13 import java.awt.event.ActionListener ; 14 import java.awt.event.MouseAdapter ; 15 import java.awt.event.MouseEvent ; 16 17 import javax.swing.ImageIcon ; 18 import javax.swing.JComponent ; 19 import javax.swing.JFrame ; 20 import javax.swing.JLabel ; 21 import javax.swing.JPanel ; 22 import javax.swing.JWindow ; 23 import javax.swing.SwingConstants ; 24 import javax.swing.SwingUtilities ; 25 import javax.swing.Timer ; 26 import javax.swing.border.Border ; 27 28 39 public class SplashWindow extends JWindow implements ActionListener , SwingConstants { 40 41 private JPanel mainPanel; 43 private Timer timer; 44 private boolean appDismissed; 45 private boolean timerExpired; 46 47 54 public SplashWindow(JFrame parent, Image img, int timeout) { 55 this(parent, img, timeout, null); 56 } 57 58 66 public SplashWindow(JFrame parent, Image img, int timeout, JComponent accessoryComponent) { 67 super(parent); 68 ImageIcon image = new ImageIcon (img); 70 int w = image.getIconWidth() + 5; 71 int h = image.getIconHeight() + 5; 72 mainPanel = new JPanel (new BorderLayout ()); 74 mainPanel.setOpaque(false); 75 if (accessoryComponent != null) { 77 mainPanel.add(accessoryComponent, BorderLayout.SOUTH); 78 } 80 JLabel p = new JLabel (image); 81 p.setBorder(null); 82 p.addMouseListener(new MouseAdapter () { 83 84 public void mouseClicked(MouseEvent e) { 85 hide(); 86 } 87 }); 88 mainPanel.add(p, BorderLayout.CENTER); 89 getContentPane().setLayout(new BorderLayout ()); 91 getContentPane().add(mainPanel); 92 timer = new Timer (0, null); 94 timer.setRepeats(false); 95 timer.setInitialDelay(timeout); 96 pack(); 98 positionComponent(SwingConstants.CENTER, this); 99 } 100 101 108 public static void positionComponent(int p, Component c) { 109 110 positionComponent(p, c, c); 111 112 } 113 114 public static void positionComponent(int p, Component c, Component o) { 115 Rectangle d = null; 116 120 try { 121 122 126 127 GraphicsConfiguration config = o.getGraphicsConfiguration(); 129 GraphicsDevice dev = config.getDevice(); 130 d = config.getBounds(); 131 132 } catch (Throwable t) { 134 } 135 positionComponent(p, c, d); 136 137 } 138 139 public static void positionComponent(int p, Component c, Rectangle d) { 140 if (d == null) { 141 Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); 142 d = new Rectangle (0, 0, s != null ? s.width : 800, s != null ? s.height : 600); 143 } 144 145 switch (p) { 146 case NORTH_WEST: 147 c.setLocation(d.x, d.y); 148 break; 149 case NORTH: 150 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y); 151 break; 152 case NORTH_EAST: 153 c.setLocation(d.x + (d.width - c.getSize().width), d.y); 154 break; 155 case WEST: 156 c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2); 157 break; 158 case SOUTH_WEST: 159 c.setLocation(d.x, d.y + (d.height - c.getSize().height)); 160 break; 161 case EAST: 162 c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2); 163 break; 164 case SOUTH_EAST: 165 c.setLocation(d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30); 166 break; 167 case CENTER: 168 c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2); 169 break; 170 } 171 } 172 173 178 public void setBackground(Color b) { 179 getContentPane().setBackground(b); 180 mainPanel.setBackground(b); 181 } 182 183 188 public void setBorder(Border b) { 189 mainPanel.setBorder(b); 190 positionComponent(SwingConstants.CENTER, this); 191 } 192 193 198 public Border getBorder() { 199 return mainPanel.getBorder(); 200 } 201 202 205 public void dismiss() { 206 if (!timerExpired) { 207 appDismissed = true; 208 } else { 209 dismissImpl(); 210 } 211 } 212 213 private void dismissImpl() { 214 SwingUtilities.invokeLater(new Runnable () { 215 public void run() { 216 dispose(); 217 } 218 }); 219 } 220 221 226 public void actionPerformed(ActionEvent evt) { 227 if (appDismissed) { 228 dismissImpl(); 229 } else { 230 timerExpired = true; 231 } 232 } 233 234 239 public void show() { 240 timer.addActionListener(this); 241 timer.start(); 242 super.show(); 243 } 244 245 250 public void hide() { 251 timer.stop(); 252 timer.removeActionListener(this); 253 super.hide(); 254 } 255 } | Popular Tags |