1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui;
21
22 import java.awt.Color;
23 import java.awt.Dimension;
24 import java.awt.Font;
25 import java.awt.GridBagLayout;
26 import java.awt.Toolkit;
27 import java.awt.event.MouseAdapter;
28 import java.awt.event.MouseEvent;
29
30 import javax.swing.JLabel;
31 import javax.swing.JProgressBar;
32 import javax.swing.JWindow;
33 import javax.swing.SwingConstants;
34 import javax.swing.SwingUtilities;
35
36 import org.xnap.XNap;
37 import org.xnap.gui.util.GridBagHelper;
38 import org.xnap.gui.util.IconHelper;
39 import org.xnap.plugin.*;
40
41 /***
42 * Displays a splash window in the center of the screen when XNap's gui
43 * is launched.
44 */
45 public class SplashWindow extends JWindow
46 {
47
48
49
50
51 private static SplashWindow me = null;
52
53 private JProgressBar jpbStatus;
54 private JLabel statusLabel;
55
56 private static Object lock = new Object();
57
58
59
60 private SplashWindow(String filename)
61 {
62 setBackground(Color.white);
63 getContentPane().setBackground(Color.white);
64 getContentPane().setLayout(new GridBagLayout());
65
66 StringBuffer sb = new StringBuffer();
67 sb.append(XNap.tr("Version {0}, see About for License",
68 PluginManager.getCoreVersion()));
69 JLabel jlImage = new JLabel(sb.toString());
70 jlImage.setForeground(Color.black);
71 jlImage.setIcon(IconHelper.getImage(filename));
72 jlImage.setHorizontalAlignment(SwingConstants.CENTER);
73 jlImage.setVerticalAlignment(SwingConstants.CENTER);
74 jlImage.setHorizontalTextPosition(SwingConstants.CENTER);
75 jlImage.setVerticalTextPosition(SwingConstants.BOTTOM);
76 GridBagHelper.add(getContentPane(), jlImage);
77
78 jpbStatus = new JProgressBar(0, 100);
79 GridBagHelper.add(getContentPane(), jpbStatus);
80
81 statusLabel = new JLabel(XNap.tr("Loading XNap") + "...");
82 statusLabel.setForeground(Color.black);
83 statusLabel.setFont(new Font("Dialog", Font.PLAIN, 10));
84 GridBagHelper.add(getContentPane(), statusLabel);
85
86 pack();
87
88
89 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
90 Dimension labelSize = jlImage.getPreferredSize();
91 setLocation(screenSize.width / 2 - (labelSize.width / 2),
92 screenSize.height / 2 - (labelSize.height / 2));
93
94
95 addMouseListener(new MouseAdapter()
96 {
97 public void mousePressed(MouseEvent e)
98 {
99 synchronized (lock) {
100 lock.notify();
101 }
102 }
103 });
104 }
105
106
107
108 /***
109 * The splash window will be closed automatically after
110 * <code>timeout</code> milli seconds.
111 *
112 * @param timeout automatic closing will be disabled, if less or equal
113 * than 0
114 */
115 public static void showSplashWindow(final long timeout)
116 {
117 if (me == null) {
118 me = new SplashWindow("xnap_logo.png");
119
120 Thread t = new Thread(new CloseRunner(timeout),
121 "SplashWindowWaiter");
122 t.start();
123
124 me.setVisible(true);
125 }
126 }
127
128 /***
129 * Closes the window if shown on screen.
130 */
131 public static void closeSplashWindow()
132 {
133 if (me != null) {
134 synchronized (lock) {
135 lock.notify();
136 }
137 }
138 }
139
140 /***
141 *
142 */
143 public static int getProgress()
144 {
145 return (me != null) ? me.jpbStatus.getValue() : 0;
146 }
147
148 /***
149 * Increases the progress by <code>delta</code> percent and sets the
150 * status text to <code>newValue</code>.
151 */
152 public static void incProgress(int delta, String text)
153 {
154 if (me != null) {
155 incProgress(delta);
156 setText(text);
157 }
158 }
159
160 /***
161 * Increases the progress by <code>delta</code> percent.
162 */
163 public static void incProgress(int delta)
164 {
165 if (me != null) {
166 me.jpbStatus.setValue(me.jpbStatus.getValue() + delta);
167 }
168 }
169
170 /***
171 * Sets the progress to <code>progress</code> and the status text to
172 * <code>text</code>.
173 */
174 public static void setProgress(int progress, String text)
175 {
176 setProgress(progress);
177 setText(text);
178 }
179
180 /***
181 * Sets the progress to <code>newValue</code> percent.
182 */
183 public static void setProgress(int newValue)
184 {
185 if (me != null) {
186 me.jpbStatus.setValue(newValue);
187 }
188 }
189
190 /***
191 * Sets the status text to <code>newValue</code>.
192 */
193 public static void setText(String newValue)
194 {
195 if (me != null) {
196 me.statusLabel.setText(newValue);
197 }
198 }
199
200 public static void updateComponentTree()
201 {
202 if (me != null) {
203 SwingUtilities.updateComponentTreeUI(me);
204 }
205 }
206
207
208
209 public static class CloseRunner implements Runnable
210 {
211 long timeout;
212
213 public CloseRunner(long timeout)
214 {
215 this.timeout = timeout;
216 }
217
218 public void run()
219 {
220 try {
221 synchronized (lock) {
222 if (timeout > 0) {
223 lock.wait(timeout);
224 }
225 else {
226 lock.wait();
227 }
228 }
229 }
230 catch(InterruptedException e) {
231 }
232
233 Runnable runner = new Runnable()
234 {
235 public void run()
236 {
237 me.setVisible(false);
238 me.dispose();
239 me = null;
240 }
241 };
242 SwingUtilities.invokeLater(runner);
243 }
244 }
245
246 }
247
248
249
250