package br.com.visualmidia.ui.splash;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class SplashScreen {
private Display display;
private ProgressBar bar;
private Label message;
private int amount;
private int count;
private Shell splash;
public SplashScreen(Shell parent) {
configureDisplay(parent);
}
private void configureDisplay(Shell parent) {
display = parent.getDisplay();
}
static int[] circle(int r, int offsetX, int offsetY) {
int[] polygon = new int[8 * r + 4];
// x^2 + y^2 = r^2
for (int i = 0; i < 2 * r + 1; i++) {
int x = i - r;
int y = (int) Math.sqrt(r * r - x * x);
polygon[2 * i] = offsetX + x;
polygon[2 * i + 1] = offsetY + y;
polygon[8 * r - 2 * i - 2] = offsetX + x;
polygon[8 * r - 2 * i - 1] = offsetY - y;
}
return polygon;
}
public void open() {
splash = new Shell(SWT.ON_TOP | SWT.NO_BACKGROUND | SWT.NO_TRIM);
FormLayout layout = new FormLayout();
splash.setLayout(layout);
splash.setSize(600, 400);
splash.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event arg0) {
GC gc = arg0.gc;
gc.drawImage(new Image(display, "img/splash.png"), 0, 0);
}
});
bar = new ProgressBar(splash, SWT.SMOOTH);
bar.setMaximum(count);
bar.setVisible(false);
FormData progressData = new FormData();
progressData.left = new FormAttachment(0, 25);
progressData.top = new FormAttachment(0, 178);
bar.setLayoutData(progressData);
message = new Label(splash, SWT.LEFT);
message.setForeground(new Color(null, 106, 106, 106));
message.setFont(new Font(null, "Tahoma", 8, SWT.BOLD));
message.setBackgroundImage(new Image(display, "img/back_bar.png"));
FormData messageData = new FormData();
messageData.top = new FormAttachment(bar, 0);
messageData.left = new FormAttachment(0, 36);
messageData.right = new FormAttachment(100, -340);
message.setLayoutData(messageData);
Rectangle splashRect = splash.getBounds();
Rectangle displayRect = display.getBounds();
int x = (displayRect.width - splashRect.width) / 2;
int y = (displayRect.height - splashRect.height) / 2;
splash.setLocation(x, y);
splash.open();
display.asyncExec(new Runnable() {
public void run() {
try {
while (bar.getSelection() != count) {
if (!display.readAndDispatch())
display.sleep();
}
splash.close();
Thread.sleep(10);
} catch (Throwable e) {
}
}
});
}
public void raiseBar() {
int selection = bar.getSelection();
for (int i = selection; i <= selection + amount; i++)
if (i > bar.getMaximum())
bar.setSelection(bar.getMaximum());
else
bar.setSelection(i);
}
public void setMessage(String message) {
this.message.setText(" " + message);
}
public void setRaiseAmount(int amount) {
this.amount = amount;
}
public void setWaitTime(int count) {
this.count = count;
}
public void close() {
splash.close();
}
}