SplashScreen.java
/*
* Copyright © 2011 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.moviebooking.ui;
import java.io.InputStream;
import java.io.IOException;
import org.eclipse.ercp.swt.mobile.Command;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
/**
* Simple splash screen it display the progress while loading the
* MovieDB
*/
class SplashScreen implements SelectionListener {
private Composite splashComposite;
private Label progressLabel;
private ProgressBar progressBar;
private Command exitCommand;
private MovieBooking main;
private Image appImage;
SplashScreen(MovieBooking main, Shell mainShell) {
this.main = main;
// Parent Composite of the splash screen widgets
splashComposite = new Composite(mainShell, SWT.NONE);
// splashComposite's size and location are controlled
// by the GridLayout of the mainShell
splashComposite.setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, true));
// Load the application's image
InputStream imageStream = this.getClass().getResourceAsStream("/res/splash.png");
if (imageStream != null) {
appImage = new Image(mainShell.getDisplay(), imageStream);
try {
imageStream.close();
} catch (IOException e) {
// ignore, in this case it is not relevant
}
}
// Create the widgets
Label titleLabel = new Label(splashComposite, SWT.CENTER);
titleLabel.setText("MovieBooking");
Label imageLabel = new Label(splashComposite, SWT.CENTER);
if (appImage != null) {
imageLabel.setImage(appImage);
}
progressLabel = new Label(splashComposite, SWT.CENTER);
progressLabel.setText("Loading...");
progressBar = new ProgressBar(splashComposite, SWT.CENTER);
progressBar.setMaximum(10);
progressBar.setMinimum(0);
// Distribute the widgets using a FormLayout
FormLayout layout = new FormLayout();
splashComposite.setLayout(layout);
// Set the image at the center
FormData imageLabelData = new FormData();
Point imageSize = imageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
imageLabelData.top = new FormAttachment(50, -imageSize.y / 2);
imageLabelData.left = new FormAttachment(50, -imageSize.x / 2);
imageLabel.setLayoutData(imageLabelData);
// Set title label above the image
FormData titleLabelData = new FormData();
titleLabelData.left = new FormAttachment(imageLabel, 0, SWT.CENTER);
titleLabelData.bottom = new FormAttachment(imageLabel, 0, SWT.TOP);
titleLabel.setLayoutData(titleLabelData);
// Set the progress bar at the center, below the title label
FormData progressBarData = new FormData();
progressBarData.left = new FormAttachment(imageLabel, 0, SWT.CENTER);
progressBarData.top = new FormAttachment(imageLabel);
progressBar.setLayoutData(progressBarData);
// Set the progress label below the progress bar
FormData progressLabelData = new FormData();
progressLabelData.left = new FormAttachment(imageLabel, 0, SWT.CENTER);
progressLabelData.top = new FormAttachment(progressBar, 0, SWT.BOTTOM);
progressLabel.setLayoutData(progressLabelData);
// Add the exit command
exitCommand = new Command(mainShell, Command.EXIT, 0);
exitCommand.setText("Exit");
exitCommand.addSelectionListener(this);
exitCommand.setDefaultCommand();
}
public void widgetSelected(SelectionEvent e) {
if (e.widget == exitCommand) {
destroy();
main.exit();
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
// Set the text of the progress label
void setLabel(String label) {
if (!progressLabel.isDisposed()) {
progressLabel.setText(label);
splashComposite.layout();
}
}
// Set how many movies will be displayed. In practice it changes the
// range of the progress bar
void setMoviesCount(int count) {
if (!progressBar.isDisposed()) {
progressBar.setMaximum(count);
}
}
// Advance the progress bar
void advance() {
if (!progressBar.isDisposed()) {
progressBar.setSelection(progressBar.getSelection() + 1);
}
}
void destroy() {
exitCommand.dispose();
splashComposite.dispose();
if (appImage != null) {
appImage.dispose();
}
}
}