Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / splash / MultiSplashWindow.java @ 43167

History | View | Annotate | Download (7.85 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
42
import javax.swing.BorderFactory;
43
import javax.swing.ImageIcon;
44
import javax.swing.JLabel;
45
import javax.swing.JProgressBar;
46
import javax.swing.JWindow;
47 42874 jjdelcerro
import javax.swing.Timer;
48 40435 jjdelcerro
49
import org.gvsig.andami.PluginServices;
50
import org.gvsig.andami.messages.Messages;
51
import org.gvsig.andami.ui.theme.Theme;
52 40821 jjdelcerro
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54 40435 jjdelcerro
55
56
57 42874 jjdelcerro
public class MultiSplashWindow extends JWindow implements
58 40435 jjdelcerro
    MouseListener {
59 40821 jjdelcerro
        private static final Logger logger = LoggerFactory.getLogger(MultiSplashWindow.class);
60
61 40435 jjdelcerro
    private static final long serialVersionUID = 572592158521767258L;
62
    private JProgressBar pb;
63 40821 jjdelcerro
    private int progress = 0;
64 40435 jjdelcerro
    private JLabel lblStatus;
65
    private Timer timer;
66
    private int numLogos = 0;
67
    private ImageIcon[] img = null;
68
    private Dimension splashDimension;
69
    private int index = 0;
70
    private int current;
71
    private int lastIndex = -1;
72
    private Theme theme;
73 42874 jjdelcerro
    private int[] timers;
74 40435 jjdelcerro
75
    private String version="";
76
    private String[] versions=null;
77
    private Point point=new Point(270,240);
78
    private Point[] points=null;
79
    private int fontsize=18;
80
    private int[] fontSizes=null;
81
    private Color fontcolor=new Color(80,170,240);
82
    private Color[] fontColors=null;
83
84
    public MultiSplashWindow(Frame f, Theme theme, int progressBarStepCount) {
85
        super(f);
86
        this.theme = theme;
87
        this.timers = theme.getTimers();
88
        lblStatus = new JLabel(Messages.getString("SplashWindow.Iniciando"));
89
        lblStatus.setBorder(BorderFactory.createEtchedBorder());
90
        lblStatus.setBackground(Color.WHITE);
91
92
        pack();
93
        pb = new JProgressBar(0, progressBarStepCount);
94
        getContentPane().setLayout(new BorderLayout());
95
        getContentPane().add(lblStatus, BorderLayout.NORTH);
96
        getContentPane().add(pb, BorderLayout.SOUTH);
97
98
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
99
100
        init();
101
        setLocation((screenSize.width / 2) - (splashDimension.width / 2),
102
            (screenSize.height / 2) - (splashDimension.height / 2));
103
        index = 0;
104
        setVisible(true);
105
        this.addMouseListener(this);
106
    }
107
108
    public void init() {
109
        img = theme.getSplashImages();
110
        numLogos = img.length;
111
112
        if (numLogos == 0) {
113
            numLogos = 1;
114
            img = new ImageIcon[1];
115
            img[0] = PluginServices.getIconTheme().get("splash-default");
116 42874 jjdelcerro
            timers = new int[1];
117
            timers[0] = 1000;
118 40435 jjdelcerro
        }
119
        versions=theme.getVersions();
120
        points=theme.getPositions();
121
        fontSizes=theme.getFontSizes();
122
        fontColors=theme.getFontColors();
123
        int splashWidth = img[0].getIconWidth();
124
        int splashHeight = img[0].getIconHeight();
125
126
        for (int i = 1; i < numLogos; i++) {
127
            splashWidth = Math.max(splashWidth, img[i].getIconWidth());
128
            splashHeight = Math.max(splashHeight, img[i].getIconHeight());
129
        }
130
131
        splashDimension = new Dimension(splashWidth, splashHeight);
132
        setSize(splashDimension.width, splashDimension.height + 45);
133
134
        start();
135
    }
136
137
    public void paint(Graphics g) {
138
        if (lastIndex == current) {
139
            return;
140
        }
141
142
        super.paint(g);
143
144
        if ((img == null) || (img[current] == null)) {
145
            return;
146
        }
147
148
        ImageIcon image = img[current];
149
        g.drawImage(image.getImage(),
150
            (getWidth() / 2) - (image.getIconWidth() / 2),
151
            ((getHeight() / 2) - (image.getIconHeight() / 2)), this);
152
153
        // Activate antialias for text
154
        if (g instanceof Graphics2D) {
155
            ((Graphics2D) g).setRenderingHint(
156
                RenderingHints.KEY_TEXT_ANTIALIASING,
157
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
158
        }
159
160
        Font font = new Font("SansSerif", Font.BOLD, fontsize);
161
        if (fontSizes.length>0 && fontSizes[current]!=0) {
162
            font = new Font("SansSerif", Font.BOLD, fontSizes[current]);
163
        }
164
            g.setFont(font);
165
166
            Color color=fontcolor;
167
        if (fontColors.length>0 && fontColors[current]!=null) {
168
                color=fontColors[current];
169
        }
170
        g.setColor(color);
171
172
        String ver=version;
173
        if (versions.length>0 && versions[current]!=null) {
174
                ver=versions[current];
175
        }
176
177
        Point p=point;
178
        if (points.length>0 && points[current]!=null){
179
                p=points[current];
180
        }
181
182
        g.drawString(PluginServices.getText(this,ver),(int)p.getX(),(int)p.getY());
183
184
        lastIndex = current;
185
    }
186
187
    public void tick() {
188
        current = index;
189 42874 jjdelcerro
        timer.setDelay(timers[current]);
190 40435 jjdelcerro
        repaint();
191
        index++;
192
193
        if (index >= numLogos) {
194
            index = 0;
195
        }
196
    }
197
198
    public void start() {
199 42874 jjdelcerro
        timer = new Timer(1000,new ActionListener() {
200
201
            @Override
202
            public void actionPerformed(ActionEvent e) {
203
                tick();
204
            }
205
        });
206 40435 jjdelcerro
        timer.start();
207
    }
208
209
    /**
210
     * Cierra la ventana
211
     */
212
    public void close() {
213 40821 jjdelcerro
            if( this.progress != this.pb.getMaximum() ) {
214
                    logger.warn("Max value of launch progress can be set to "+this.progress);
215
            }
216 40435 jjdelcerro
        setVisible(false);
217
        stop();
218
        dispose();
219
    }
220
221
    public void stop() {
222
        timer.stop();
223 42874 jjdelcerro
        timer = null;
224 40435 jjdelcerro
    }
225
226
    /**
227
     * DOCUMENT ME!
228
     *
229
     * @param args DOCUMENT ME!
230
     */
231
    public static void main(String[] args) {
232
        Frame frame = new Frame();
233
        Theme theme = new Theme();
234
        MultiSplashWindow ba = new MultiSplashWindow(frame, theme, 5);
235
        ba.setVisible(true);
236
        ba.setSize(500, 500);
237
        ba.init();
238
        ba.start();
239
    }
240
241
    /**
242
     * @see com.iver.mdiApp.ui.AppLoadingListener#process(int)
243
     */
244
    public void process(int p, String str) {
245
        lblStatus.setText(str);
246 40821 jjdelcerro
        if( this.progress < p ) {
247
            this.progress = p;
248
        }
249
        if (pb.getValue() != this.progress && this.progress <= pb.getMaximum() ) {
250
            pb.setValue(this.progress);
251
        }
252
        doLayout();
253
        repaint();
254
    }
255 40435 jjdelcerro
256 40821 jjdelcerro
    public void process(String str) {
257
        lblStatus.setText(str);
258
        this.progress++;
259
        if( this.progress <= pb.getMaximum() ) {
260
                pb.setValue(this.progress);
261 40435 jjdelcerro
        }
262
        doLayout();
263
        repaint();
264
    }
265
266
    public void mouseClicked(MouseEvent e) {
267
        this.setVisible(false);
268
    }
269
270
        public void mouseEntered(MouseEvent e) { }
271
        public void mouseExited(MouseEvent e) { }
272
        public void mousePressed(MouseEvent e) { }
273
        public void mouseReleased(MouseEvent e) { }
274
    }