svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / splash / MultiSplashWindow.java @ 47816
History | View | Annotate | Download (8.92 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | |
25 | package org.gvsig.andami.ui.splash; |
||
26 | |||
27 | import java.awt.BorderLayout; |
||
28 | import java.awt.Color; |
||
29 | import java.awt.Dimension; |
||
30 | import java.awt.Font; |
||
31 | import java.awt.Frame; |
||
32 | import java.awt.Graphics; |
||
33 | import java.awt.Graphics2D; |
||
34 | import java.awt.Point; |
||
35 | import java.awt.RenderingHints; |
||
36 | import java.awt.Toolkit; |
||
37 | 42874 | jjdelcerro | import java.awt.event.ActionEvent; |
38 | import java.awt.event.ActionListener; |
||
39 | 40435 | jjdelcerro | import java.awt.event.MouseEvent; |
40 | import java.awt.event.MouseListener; |
||
41 | 47816 | jjdelcerro | import java.awt.font.FontRenderContext; |
42 | import java.awt.geom.Rectangle2D; |
||
43 | 40435 | jjdelcerro | |
44 | import javax.swing.BorderFactory; |
||
45 | import javax.swing.ImageIcon; |
||
46 | import javax.swing.JLabel; |
||
47 | import javax.swing.JProgressBar; |
||
48 | import javax.swing.JWindow; |
||
49 | 44075 | jjdelcerro | import javax.swing.SwingUtilities; |
50 | 42874 | jjdelcerro | import javax.swing.Timer; |
51 | 45098 | jjdelcerro | import org.gvsig.andami.Launcher; |
52 | 40435 | jjdelcerro | |
53 | import org.gvsig.andami.PluginServices; |
||
54 | import org.gvsig.andami.messages.Messages; |
||
55 | import org.gvsig.andami.ui.theme.Theme; |
||
56 | 40821 | jjdelcerro | import org.slf4j.Logger; |
57 | import org.slf4j.LoggerFactory; |
||
58 | 40435 | jjdelcerro | |
59 | |||
60 | |||
61 | 42874 | jjdelcerro | public class MultiSplashWindow extends JWindow implements |
62 | 40435 | jjdelcerro | MouseListener {
|
63 | 40821 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(MultiSplashWindow.class); |
64 | |||
65 | 40435 | jjdelcerro | private static final long serialVersionUID = 572592158521767258L; |
66 | private JProgressBar pb; |
||
67 | 40821 | jjdelcerro | private int progress = 0; |
68 | 40435 | jjdelcerro | private JLabel lblStatus; |
69 | private Timer timer; |
||
70 | private int numLogos = 0; |
||
71 | private ImageIcon[] img = null; |
||
72 | private Dimension splashDimension; |
||
73 | private int index = 0; |
||
74 | private int current; |
||
75 | private int lastIndex = -1; |
||
76 | private Theme theme;
|
||
77 | 42874 | jjdelcerro | private int[] timers; |
78 | 40435 | jjdelcerro | |
79 | private String version=""; |
||
80 | private String[] versions=null; |
||
81 | private Point point=new Point(270,240); |
||
82 | private Point[] points=null; |
||
83 | private int fontsize=18; |
||
84 | private int[] fontSizes=null; |
||
85 | private Color fontcolor=new Color(80,170,240); |
||
86 | private Color[] fontColors=null; |
||
87 | 45098 | jjdelcerro | private final boolean showsplash; |
88 | 40435 | jjdelcerro | |
89 | public MultiSplashWindow(Frame f, Theme theme, int progressBarStepCount) { |
||
90 | super(f);
|
||
91 | 45098 | jjdelcerro | this.showsplash = (boolean) Launcher.getArguments().get("splash",true); |
92 | 40435 | jjdelcerro | this.theme = theme;
|
93 | this.timers = theme.getTimers();
|
||
94 | lblStatus = new JLabel(Messages.getString("SplashWindow.Iniciando")); |
||
95 | lblStatus.setBorder(BorderFactory.createEtchedBorder());
|
||
96 | lblStatus.setBackground(Color.WHITE);
|
||
97 | |||
98 | pack(); |
||
99 | pb = new JProgressBar(0, progressBarStepCount); |
||
100 | getContentPane().setLayout(new BorderLayout()); |
||
101 | getContentPane().add(lblStatus, BorderLayout.NORTH);
|
||
102 | getContentPane().add(pb, BorderLayout.SOUTH);
|
||
103 | |||
104 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
||
105 | |||
106 | init(); |
||
107 | setLocation((screenSize.width / 2) - (splashDimension.width / 2), |
||
108 | (screenSize.height / 2) - (splashDimension.height / 2)); |
||
109 | index = 0;
|
||
110 | 45098 | jjdelcerro | setVisible(this.showsplash);
|
111 | 40435 | jjdelcerro | this.addMouseListener(this); |
112 | } |
||
113 | |||
114 | public void init() { |
||
115 | img = theme.getSplashImages(); |
||
116 | numLogos = img.length; |
||
117 | |||
118 | if (numLogos == 0) { |
||
119 | numLogos = 1;
|
||
120 | img = new ImageIcon[1]; |
||
121 | img[0] = PluginServices.getIconTheme().get("splash-default"); |
||
122 | 42874 | jjdelcerro | timers = new int[1]; |
123 | timers[0] = 1000; |
||
124 | 40435 | jjdelcerro | } |
125 | versions=theme.getVersions(); |
||
126 | points=theme.getPositions(); |
||
127 | fontSizes=theme.getFontSizes(); |
||
128 | fontColors=theme.getFontColors(); |
||
129 | int splashWidth = img[0].getIconWidth(); |
||
130 | int splashHeight = img[0].getIconHeight(); |
||
131 | |||
132 | for (int i = 1; i < numLogos; i++) { |
||
133 | splashWidth = Math.max(splashWidth, img[i].getIconWidth());
|
||
134 | splashHeight = Math.max(splashHeight, img[i].getIconHeight());
|
||
135 | } |
||
136 | |||
137 | splashDimension = new Dimension(splashWidth, splashHeight); |
||
138 | setSize(splashDimension.width, splashDimension.height + 45);
|
||
139 | |||
140 | start(); |
||
141 | } |
||
142 | |||
143 | public void paint(Graphics g) { |
||
144 | if (lastIndex == current) {
|
||
145 | return;
|
||
146 | } |
||
147 | |||
148 | super.paint(g);
|
||
149 | |||
150 | if ((img == null) || (img[current] == null)) { |
||
151 | return;
|
||
152 | } |
||
153 | |||
154 | ImageIcon image = img[current];
|
||
155 | g.drawImage(image.getImage(), |
||
156 | (getWidth() / 2) - (image.getIconWidth() / 2), |
||
157 | ((getHeight() / 2) - (image.getIconHeight() / 2)), this); |
||
158 | |||
159 | // Activate antialias for text
|
||
160 | if (g instanceof Graphics2D) { |
||
161 | ((Graphics2D) g).setRenderingHint(
|
||
162 | RenderingHints.KEY_TEXT_ANTIALIASING,
|
||
163 | RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||
164 | } |
||
165 | |||
166 | Font font = new Font("SansSerif", Font.BOLD, fontsize); |
||
167 | if (fontSizes.length>0 && fontSizes[current]!=0) { |
||
168 | font = new Font("SansSerif", Font.BOLD, fontSizes[current]); |
||
169 | } |
||
170 | g.setFont(font); |
||
171 | |||
172 | Color color=fontcolor;
|
||
173 | if (fontColors.length>0 && fontColors[current]!=null) { |
||
174 | color=fontColors[current]; |
||
175 | } |
||
176 | g.setColor(color); |
||
177 | |||
178 | String ver=version;
|
||
179 | if (versions.length>0 && versions[current]!=null) { |
||
180 | ver=versions[current]; |
||
181 | } |
||
182 | |||
183 | Point p=point;
|
||
184 | if (points.length>0 && points[current]!=null){ |
||
185 | p=points[current]; |
||
186 | } |
||
187 | |||
188 | 47816 | jjdelcerro | int xx = (int) p.getX(); |
189 | String s = PluginServices.getText(this,ver); |
||
190 | if( xx<0 ) { |
||
191 | FontRenderContext context = g.getFontMetrics().getFontRenderContext();
|
||
192 | Rectangle2D bounds = font.getStringBounds(s, context);
|
||
193 | xx = splashDimension.width - (int)bounds.getWidth() + xx;
|
||
194 | if( xx < 1 ) { |
||
195 | xx = 1;
|
||
196 | } |
||
197 | } |
||
198 | g.drawString(s,xx,(int)p.getY());
|
||
199 | 40435 | jjdelcerro | |
200 | lastIndex = current; |
||
201 | } |
||
202 | |||
203 | public void tick() { |
||
204 | current = index; |
||
205 | 42874 | jjdelcerro | timer.setDelay(timers[current]); |
206 | 40435 | jjdelcerro | repaint(); |
207 | index++; |
||
208 | |||
209 | if (index >= numLogos) {
|
||
210 | index = 0;
|
||
211 | } |
||
212 | } |
||
213 | |||
214 | public void start() { |
||
215 | 42874 | jjdelcerro | timer = new Timer(1000,new ActionListener() { |
216 | |||
217 | @Override
|
||
218 | public void actionPerformed(ActionEvent e) { |
||
219 | tick(); |
||
220 | } |
||
221 | }); |
||
222 | 40435 | jjdelcerro | timer.start(); |
223 | } |
||
224 | |||
225 | /**
|
||
226 | * Cierra la ventana
|
||
227 | */
|
||
228 | public void close() { |
||
229 | 40821 | jjdelcerro | if( this.progress != this.pb.getMaximum() ) { |
230 | logger.warn("Max value of launch progress can be set to "+this.progress); |
||
231 | } |
||
232 | 40435 | jjdelcerro | setVisible(false);
|
233 | stop(); |
||
234 | dispose(); |
||
235 | } |
||
236 | |||
237 | public void stop() { |
||
238 | timer.stop(); |
||
239 | 42874 | jjdelcerro | timer = null;
|
240 | 40435 | jjdelcerro | } |
241 | |||
242 | /**
|
||
243 | * DOCUMENT ME!
|
||
244 | *
|
||
245 | * @param args DOCUMENT ME!
|
||
246 | */
|
||
247 | public static void main(String[] args) { |
||
248 | Frame frame = new Frame(); |
||
249 | Theme theme = new Theme();
|
||
250 | MultiSplashWindow ba = new MultiSplashWindow(frame, theme, 5); |
||
251 | ba.setVisible(true);
|
||
252 | ba.setSize(500, 500); |
||
253 | ba.init(); |
||
254 | ba.start(); |
||
255 | } |
||
256 | |||
257 | /**
|
||
258 | * @see com.iver.mdiApp.ui.AppLoadingListener#process(int)
|
||
259 | */
|
||
260 | public void process(int p, String str) { |
||
261 | lblStatus.setText(str); |
||
262 | 40821 | jjdelcerro | if( this.progress < p ) { |
263 | this.progress = p;
|
||
264 | } |
||
265 | if (pb.getValue() != this.progress && this.progress <= pb.getMaximum() ) { |
||
266 | pb.setValue(this.progress);
|
||
267 | } |
||
268 | doLayout(); |
||
269 | repaint(); |
||
270 | } |
||
271 | 40435 | jjdelcerro | |
272 | 44075 | jjdelcerro | public void process(final String str) { |
273 | if( !SwingUtilities.isEventDispatchThread() ) { |
||
274 | try {
|
||
275 | SwingUtilities.invokeAndWait(new Runnable() { |
||
276 | @Override
|
||
277 | public void run() { |
||
278 | process(str); |
||
279 | } |
||
280 | }); |
||
281 | } catch (Exception ex) { |
||
282 | } |
||
283 | return;
|
||
284 | } |
||
285 | 40821 | jjdelcerro | lblStatus.setText(str); |
286 | 44075 | jjdelcerro | lblStatus.paintImmediately(lblStatus.getVisibleRect()); |
287 | |||
288 | 40821 | jjdelcerro | this.progress++;
|
289 | if( this.progress <= pb.getMaximum() ) { |
||
290 | pb.setValue(this.progress);
|
||
291 | 44075 | jjdelcerro | pb.paintImmediately(pb.getVisibleRect()); |
292 | 40435 | jjdelcerro | } |
293 | } |
||
294 | |||
295 | public void mouseClicked(MouseEvent e) { |
||
296 | this.setVisible(false); |
||
297 | } |
||
298 | |||
299 | public void mouseEntered(MouseEvent e) { } |
||
300 | public void mouseExited(MouseEvent e) { } |
||
301 | public void mousePressed(MouseEvent e) { } |
||
302 | public void mouseReleased(MouseEvent e) { } |
||
303 | } |