KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > $packageName$ > BrowserSplashHandler


1
2 package $packageName$;
3
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.browser.Browser;
6 import org.eclipse.swt.browser.ProgressEvent;
7 import org.eclipse.swt.browser.ProgressListener;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.events.SelectionListener;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Shell;
14 import org.eclipse.ui.splash.AbstractSplashHandler;
15
16 /**
17  * @since 3.3
18  *
19  */

20 public class BrowserSplashHandler extends AbstractSplashHandler {
21
22     private final static String JavaDoc F_BROWSER_URL = "http://www.google.com"; //$NON-NLS-1$
23

24     private Browser fBrowser;
25     
26     private Button fButton;
27     
28     private boolean fClose;
29
30     /**
31      *
32      */

33     public BrowserSplashHandler() {
34         fBrowser = null;
35         fButton = null;
36         fClose = false;
37     }
38     
39     /*
40      * (non-Javadoc)
41      *
42      * @see org.eclipse.ui.internal.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets.Shell,
43      * org.eclipse.ui.IWorkbench)
44      */

45     public void init(final Shell splash) {
46         // Store the shell
47
super.init(splash);
48         // Configure the shell layout
49
configureUISplash();
50         // Create UI
51
createUI();
52         // Create UI listeners
53
createUIListeners();
54         // Force the UI to layout
55
splash.layout(true);
56         // Keep the splash screen visible and prevent the RCP application from
57
// loading until the close button is clicked.
58
doEventLoop();
59     }
60     
61     /**
62      *
63      */

64     private void doEventLoop() {
65         Shell splash = getSplash();
66         while (fClose == false) {
67             if (splash.getDisplay().readAndDispatch() == false) {
68                 splash.getDisplay().sleep();
69             }
70         }
71     }
72     
73     /**
74      *
75      */

76     private void createUIListeners() {
77         // Create the browser listeners
78
createUIListenersBrowser();
79         // Create the button listeners
80
createUIListenersButton();
81     }
82
83     /**
84      *
85      */

86     private void createUIListenersButton() {
87         fButton.addSelectionListener(new SelectionListener() {
88             public void widgetDefaultSelected(SelectionEvent e) {
89                 // NO-OP
90
}
91             public void widgetSelected(SelectionEvent e) {
92                 fClose = true;
93             }
94         });
95     }
96
97     /**
98      *
99      */

100     private void createUIListenersBrowser() {
101         fBrowser.addProgressListener(new ProgressListener() {
102             public void changed(ProgressEvent event) {
103                 // NO-OP
104
}
105             public void completed(ProgressEvent event) {
106                 // Only show the UI when the URL is fully loaded into the
107
// browser
108
fBrowser.setVisible(true);
109                 fButton.setVisible(true);
110             }
111         });
112     }
113     
114     /**
115      *
116      */

117     private void createUI() {
118         // Create the web browser
119
createUIBrowser();
120         // Create the close button
121
createUIButton();
122     }
123
124     /**
125      *
126      */

127     private void createUIButton() {
128         Shell splash = getSplash();
129         fButton = new Button(splash, SWT.PUSH);
130         fButton.setText("Close"); //$NON-NLS-1$
131
fButton.setVisible(false);
132         // Configure the button bounds
133
configureUIButtonBounds();
134         // Configure layout data
135
GridData data = new GridData(SWT.CENTER, SWT.FILL, false, false);
136         data.widthHint = 80;
137         fButton.setLayoutData(data);
138     }
139
140     /**
141      *
142      */

143     private void configureUIButtonBounds() {
144         Shell splash = getSplash();
145         
146         int button_x_coord = (splash.getSize().x / 2)
147                 - (fButton.computeSize(SWT.DEFAULT, SWT.DEFAULT).x / 2);
148         int button_y_coord = splash.getSize().y
149                 - fButton.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
150         int button_x_width = splash.getSize().x;
151         int button_y_width = fButton.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
152         
153         fButton.setBounds(button_x_coord, button_y_coord, button_x_width,
154                 button_y_width);
155     }
156     
157     /**
158      *
159      */

160     private void createUIBrowser() {
161         fBrowser = new Browser(getSplash(), SWT.NONE);
162         fBrowser.setUrl(F_BROWSER_URL);
163         fBrowser.setVisible(false);
164         // Configure layout data
165
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
166         fBrowser.setLayoutData(data);
167     }
168
169     /**
170      *
171      */

172     private void configureUISplash() {
173         GridLayout layout = new GridLayout(1, true);
174         getSplash().setLayout(layout);
175     }
176
177 }
178
Popular Tags