KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > ui > ShellMain


1 package net.sourceforge.tracelog.ui;
2
3 import net.sourceforge.tracelog.utils.Util;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.graphics.Image;
7 import org.eclipse.swt.graphics.Rectangle;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Event;
11 import org.eclipse.swt.widgets.Label;
12 import org.eclipse.swt.widgets.Listener;
13 import org.eclipse.swt.widgets.MessageBox;
14 import org.eclipse.swt.widgets.ProgressBar;
15 import org.eclipse.swt.widgets.Shell;
16
17 public class ShellMain extends AbstractWidget {
18     private Shell mainShell;
19
20     // TODO remove this before deploying
21
// private int splashCountdown = 0; // for testing only
22

23     private int splashCountdown = 3; // 3 seconds splash
24

25     ShellMain() {
26         super();
27     }
28
29     /**
30      * Displays splash screen.
31      */

32     private void displaySplash() {
33         final Image image = new Image(display, getClass().getResourceAsStream("/images/splash.bmp"));
34
35         final Shell splash = new Shell(SWT.ON_TOP);
36         GridLayout gridLayout = new GridLayout();
37         gridLayout.marginHeight = 0;
38         gridLayout.marginWidth = 0;
39         gridLayout.horizontalSpacing = 0;
40         gridLayout.verticalSpacing = 0;
41         splash.setLayout(gridLayout);
42
43         Label label = new Label(splash, SWT.NONE);
44         label.setImage(image);
45
46         final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
47         bar.setMaximum(splashCountdown);
48         bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
49
50         splash.pack();
51         centerPositionShell(splash);
52         splash.open();
53
54         display.asyncExec(new Runnable JavaDoc() {
55             public void run() {
56
57                 int wait = splashCountdown;
58                 for (int i = 0; i < wait; ++i) {
59                     bar.setSelection(i + 1);
60                     try {
61                         Thread.sleep(1000);
62                         --splashCountdown;
63                     }
64                     catch (Exception JavaDoc ignored) {
65                     }
66                 }
67
68                 splash.close();
69                 image.dispose();
70                 splash.dispose();
71
72                 // display main shell
73
displayMainShell();
74             }
75         });
76
77         while (!display.isDisposed()) {
78             if (!display.readAndDispatch())
79                 display.sleep();
80         }
81
82         display.dispose();
83     }
84
85     /**
86      * Positions the shell in the middle of the primary monitor screen.
87      *
88      * @param shell
89      * Shell to be positioned.
90      */

91     private void centerPositionShell(Shell shell) {
92         Rectangle shellRect = shell.getBounds();
93         Rectangle displayRect = display.getPrimaryMonitor().getBounds();
94         int x = (displayRect.width - shellRect.width) / 2;
95         int y = (displayRect.height - shellRect.height) / 2;
96         shell.setLocation(x, y);
97     }
98
99     /**
100      * Creates the main shell if required and displays it.
101      */

102     private void displayMainShell() {
103         // only create the main shell if it is not created or has already
104
// disposed
105
if (mainShell == null || mainShell.isDisposed()) {
106             mainShell = new Shell(display);
107             mainShell.setLayout(new GridLayout());
108             mainShell.setText(appTitle + " " + appVersion);
109             mainShell.setImage(new Image(display, Util.getOwnResource(projectProperties.getShellIconPath())));
110             mainShell.setSize(800, 600);
111             mainShell.setMinimumSize(800, 600);
112             centerPositionShell(mainShell);
113
114             // prevents main shell from closing when user clicks on the X at the
115
// top right of the shell. Rather, make it missing so that the log
116
// viewer would run silently at back end. The beauty with this is
117
// that when user clicks on the system tray, user can view
118
// historical log. Another advantage of doing this is to enable the
119
// system tray icon to blink when log viewer is updated.
120
mainShell.addListener(SWT.Close, new Listener() {
121                 public void handleEvent(Event event) {
122                     event.doit = false;
123
124                     MessageBox messageBox = new MessageBox(mainShell, SWT.YES | SWT.NO);
125                     messageBox.setText("Exit Comfirmation");
126                     messageBox.setMessage("Would you like " + appTitle + " to run silently in the system tray?");
127
128                     int response = messageBox.open();
129
130                     if (response == SWT.YES) {
131                         mainShell.setVisible(false);
132                     }
133                     else {
134                         display.dispose();
135                     }
136                 }
137             });
138
139             IMediator actionMediator = new ActionMediator();
140             super.setMediator(actionMediator);
141
142             widgetFactory.createOptionShell(mainShell, actionMediator);
143             // widgetFactory.createLogConfigShell(mainShell, actionMediator);
144

145             widgetFactory.createAboutShell(mainShell, actionMediator);
146             widgetFactory.createHistoryShell(mainShell, actionMediator);
147
148             widgetFactory.createMainShellMenuBar(mainShell, actionMediator).run();
149             widgetFactory.createMainShellButtonBar(mainShell, actionMediator).run();
150             widgetFactory.createMainShellLogViewer(mainShell, actionMediator).run();
151
152             mainShell.open();
153             mainShell.forceFocus();
154
155             widgetFactory.createTrayShell(actionMediator).run();
156         }
157         // if main shell already initialized, display in on the screen.
158
else {
159             mainShell.setMinimized(false);
160             mainShell.setFocus();
161             mainShell.setVisible(true);
162         }
163     }
164
165     public void run() {
166         if (splashCountdown == 0) {
167             displayMainShell();
168         }
169         else {
170             displaySplash();
171         }
172     }
173 }
174
Popular Tags