aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/branding/WelcomeWindow.java
blob: 7cc78b893c38bc043703c5c5b513da74d9f0a3b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.plugin.branding;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

import net.java.sip.communicator.plugin.desktoputil.*;

import org.jitsi.service.resources.*;

/**
 * The <tt>WelcomeWindow</tt> is actually the splash screen shown while the
 * application is loading. It displays the status of the loading process and
 * some general information about the version, licenses and contact details.
 *
 * @author Yana Stamcheva
 */
public class WelcomeWindow extends JDialog
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    private static final String APPLICATION_NAME
        = BrandingActivator.getResources()
            .getSettingsString("service.gui.APPLICATION_NAME");

    private static final int DEFAULT_TEXT_INDENT
        = BrandingActivator.getResources()
            .getSettingsInt("plugin.branding.SPLASH_SCREEN_TEXT_INDENT");

    private static final int PREFERRED_HEIGHT = 330;

    private static final int PREFERRED_WIDTH = 570;

    private final JLabel bundleLabel = new JLabel();

    /**
     * Constructor.
     */
    public WelcomeWindow()
    {
        JLabel titleLabel = new JLabel(APPLICATION_NAME);

        JLabel versionLabel = new JLabel(" "
                + System.getProperty("sip-communicator.version"));

        JTextArea logoArea = new JTextArea(
            BrandingActivator.getResources()
                .getI18NString("plugin.branding.LOGO_MESSAGE"));

        StyledHTMLEditorPane rightsArea = new StyledHTMLEditorPane();

        StyledHTMLEditorPane licenseArea = new StyledHTMLEditorPane();

        JPanel textPanel = new JPanel();

        Container mainPanel = new WindowBackground();

        this.setTitle(APPLICATION_NAME);

        this.setModal(false);
        this.setUndecorated(true);

        mainPanel.setLayout(new BorderLayout());

        textPanel.setPreferredSize(new Dimension(470, 280));
        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
        textPanel
                .setBorder(BorderFactory.createEmptyBorder(15, 15, 0, 15));
        textPanel.setOpaque(false);

        this.initTitleLabel(titleLabel);

        this.initVersionLabel(versionLabel);

        this.initLogoArea(logoArea);

        this.initRightsArea(rightsArea);

        this.initLicenseArea(licenseArea);

        Component loadingPanel = initLoadingPanel();

        textPanel.add(titleLabel);
        textPanel.add(versionLabel);
        textPanel.add(logoArea);
        textPanel.add(rightsArea);
        textPanel.add(licenseArea);

        mainPanel.add(textPanel, BorderLayout.CENTER);
        mainPanel.add(loadingPanel, BorderLayout.SOUTH);

        this.getContentPane().add(mainPanel);

        this.setResizable(false);

        mainPanel.setPreferredSize(
            new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(screenSize.width / 2 - 527 / 2,
            screenSize.height / 2 - 305 / 2);

        this.initCloseActions();
    }

    /**
     * Initializes the title label.
     *
     * @param titleLabel the title label
     */
    private void initTitleLabel(JLabel titleLabel)
    {
        titleLabel.setFont(
            titleLabel.getFont().deriveFont(Font.BOLD, 28));
        titleLabel.setForeground(Constants.TITLE_COLOR);
        titleLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
    }

    /**
     * Initializes the version label.
     *
     * @param versionLabel the version label
     */
    private void initVersionLabel(JLabel versionLabel)
    {
        versionLabel.setFont(
            versionLabel.getFont().deriveFont(Font.BOLD, 18));
        versionLabel.setForeground(Constants.TITLE_COLOR);
        versionLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
    }

    /**
     * Initializes the logo area.
     *
     * @param logoArea the logo area
     */
    private void initLogoArea(JTextArea logoArea)
    {
        int logoAreaFontSize = BrandingActivator.getResources().
            getSettingsInt("plugin.branding.ABOUT_LOGO_FONT_SIZE");

        logoArea.setFont(
            logoArea.getFont().deriveFont(Font.BOLD, logoAreaFontSize));
        logoArea.setForeground(Constants.TITLE_COLOR);
        logoArea.setOpaque(false);
        logoArea.setLineWrap(true);
        logoArea.setWrapStyleWord(true);
        logoArea.setEditable(false);
        logoArea.setPreferredSize(new Dimension(100, 20));
        logoArea.setAlignmentX(Component.RIGHT_ALIGNMENT);
        logoArea.setBorder(BorderFactory
            .createEmptyBorder(20, DEFAULT_TEXT_INDENT, 0, 0));
    }

    /**
     * Initializes the copyright area.
     *
     * @param rightsArea the copyright area.
     */
    private void initRightsArea(StyledHTMLEditorPane rightsArea)
    {
        rightsArea.setContentType("text/html");
        rightsArea.appendToEnd(
            BrandingActivator.getResources().getI18NString(
            "plugin.branding.WELCOME_MESSAGE",
            new String[]{
                Constants.TEXT_COLOR,
                APPLICATION_NAME,
                BrandingActivator.getResources()
                    .getSettingsString("service.gui.APPLICATION_WEB_SITE")
                }));

        rightsArea.setPreferredSize(new Dimension(50, 50));
        rightsArea
                .setBorder(BorderFactory
                    .createEmptyBorder(0, DEFAULT_TEXT_INDENT, 0, 0));
        rightsArea.setOpaque(false);
        rightsArea.setEditable(false);
        rightsArea.setAlignmentX(Component.RIGHT_ALIGNMENT);
    }

    /**
     * Initializes the license area.
     *
     * @param licenseArea the license area.
     */
    private void initLicenseArea(StyledHTMLEditorPane licenseArea)
    {
        licenseArea.setContentType("text/html");
        licenseArea.appendToEnd(
            BrandingActivator.getResources().getI18NString(
            "plugin.branding.LICENSE",
            new String[]
                       {
                            Constants.TEXT_COLOR
                       }));

        licenseArea.setPreferredSize(new Dimension(50, 20));
        licenseArea.setBorder(BorderFactory
                .createEmptyBorder(0, DEFAULT_TEXT_INDENT, 0, 0));
        licenseArea.setOpaque(false);
        licenseArea.setEditable(false);
        licenseArea.setAlignmentX(Component.RIGHT_ALIGNMENT);
    }

    private JPanel initLoadingPanel()
    {
        ResourceManagementService resources = BrandingActivator.getResources();
        JLabel loadingLabel
            = new JLabel(
                    resources.getI18NString("plugin.branding.LOADING") + ": ");
        JPanel loadingPanel = new JPanel(new BorderLayout());

        this.bundleLabel.setFont(loadingLabel.getFont().deriveFont(Font.PLAIN));

        loadingPanel.setOpaque(false);
        loadingPanel.add(loadingLabel, BorderLayout.WEST);
        loadingPanel.add(bundleLabel, BorderLayout.CENTER);

        int loadingPanelBorder
            = resources
                .getSettingsInt("plugin.branding.LOADING_BUNDLE_PANEL_BORDER");

        loadingPanel.setBorder(
            BorderFactory.createEmptyBorder(loadingPanelBorder,
                                            loadingPanelBorder,
                                            loadingPanelBorder,
                                            loadingPanelBorder));

        int loadingPanelHeight
            = resources
                .getSettingsInt("plugin.branding.LOADING_BUNDLE_PANEL_HEIGHT");

        loadingPanel.setPreferredSize(
            new Dimension(PREFERRED_WIDTH, loadingPanelHeight));

        return loadingPanel;
    }

    /**
     * Initializes close actions on mouse click and esc key.
     */
    private void initCloseActions()
    {
        // Close the splash screen on simple click or Esc.
        this.getGlassPane().addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                WelcomeWindow.this.close();
            }
        });

        this.getGlassPane().setVisible(true);

        ActionMap amap = this.getRootPane().getActionMap();

        amap.put("close", new CloseAction());

        InputMap imap = this.getRootPane().getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
    }

    /**
     * Disposes this window.
     */
    protected void close()
    {
        this.dispose();
    }

    /**
     * Sets the name of the currently loading bundle.
     *
     * @param bundleName the name of the bundle to display
     */
    public void setBundle(String bundleName)
    {
        bundleLabel.setText(bundleName);

        bundleLabel.revalidate();
        bundleLabel.getParent().repaint();
    }

    /**
     * The action invoked when user presses Escape key.
     */
    private class CloseAction extends UIAction
    {
        /**
         * Serial version UID.
         */
        private static final long serialVersionUID = 0L;

        public void actionPerformed(ActionEvent e)
        {
            WelcomeWindow.this.close();
        }
    }

    /**
     * Constructs the window background in order to have a background image.
     */
    private static class WindowBackground
        extends JPanel
    {
        /**
         * Serial version UID.
         */
        private static final long serialVersionUID = 0L;

        private BufferedImage cache;

        private int cacheHeight;

        private int cacheWidth;

        private final Image image;

        public WindowBackground()
        {
            setOpaque(true);

            Image image = null;
            try
            {
                image =
                    ImageIO.read(BrandingActivator.getResources().getImageURL(
                        "plugin.branding.SPLASH_SCREEN_BACKGROUND"));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            this.image = image;

            if (image != null)
            {
                setPreferredSize(new Dimension(image.getWidth(this), image
                    .getHeight(this)));
            }
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            g = g.create();
            try
            {
                internalPaintComponent(g);
            }
            finally
            {
                g.dispose();
            }
        }

        private void internalPaintComponent(Graphics g)
        {
            AntialiasingManager.activateAntialiasing(g);

            Graphics2D g2 = (Graphics2D) g;

            /*
             * Drawing an Image with a data layout and color model compatible
             * with this JPanel is magnitudes faster so create and use such an
             * Image from the original drawn by this instance.
             */
            int width = getWidth();
            int height = getHeight();
            boolean imageIsChanging = false;
            if ((cache == null) || (cacheWidth != width)
                || (cacheHeight != height))
            {
                cache =
                    g2.getDeviceConfiguration().createCompatibleImage(width,
                        height);
                cacheWidth = width;
                cacheHeight = height;

                Graphics2D cacheGraphics = cache.createGraphics();
                try
                {
                    super.paintComponent(cacheGraphics);

                    AntialiasingManager.activateAntialiasing(cacheGraphics);

                    imageIsChanging =
                        !cacheGraphics.drawImage(image, 0, 0, null);

                    cacheGraphics.setColor(new Color(150, 150, 150));
                    cacheGraphics.drawRoundRect(0, 0, width - 1, height - 1, 5,
                        5);
                }
                finally
                {
                    cacheGraphics.dispose();
                }
            }

            g2.drawImage(cache, 0, 0, null);

            /*
             * Once the original Image drawn by this instance has been fully
             * loaded, we're free to use its "compatible" caching representation
             * for the purposes of optimized execution speed.
             */
            if (imageIsChanging)
            {
                cache = null;
            }
        }
    }
}