1 2 package $packageName$; 3 4 import org.eclipse.jface.dialogs.MessageDialog; 5 import org.eclipse.swt.SWT; 6 import org.eclipse.swt.events.SelectionAdapter; 7 import org.eclipse.swt.events.SelectionEvent; 8 import org.eclipse.swt.layout.FillLayout; 9 import org.eclipse.swt.layout.GridData; 10 import org.eclipse.swt.layout.GridLayout; 11 import org.eclipse.swt.widgets.Button; 12 import org.eclipse.swt.widgets.Composite; 13 import org.eclipse.swt.widgets.Label; 14 import org.eclipse.swt.widgets.Shell; 15 import org.eclipse.swt.widgets.Text; 16 import org.eclipse.ui.splash.AbstractSplashHandler; 17 18 22 public class InteractiveSplashHandler extends AbstractSplashHandler { 23 24 private final static int F_LABEL_HORIZONTAL_INDENT = 175; 25 26 private final static int F_BUTTON_WIDTH_HINT = 80; 27 28 private final static int F_TEXT_WIDTH_HINT = 175; 29 30 private final static int F_COLUMN_COUNT = 3; 31 32 private Composite fCompositeLogin; 33 34 private Text fTextUsername; 35 36 private Text fTextPassword; 37 38 private Button fButtonOK; 39 40 private Button fButtonCancel; 41 42 private boolean fAuthenticated; 43 44 47 public InteractiveSplashHandler() { 48 fCompositeLogin = null; 49 fTextUsername = null; 50 fTextPassword = null; 51 fButtonOK = null; 52 fButtonCancel = null; 53 fAuthenticated = false; 54 } 55 56 61 public void init(final Shell splash) { 62 super.init(splash); 64 configureUISplash(); 66 createUI(); 68 createUIListeners(); 70 splash.layout(true); 72 doEventLoop(); 75 } 76 77 80 private void doEventLoop() { 81 Shell splash = getSplash(); 82 while (fAuthenticated == false) { 83 if (splash.getDisplay().readAndDispatch() == false) { 84 splash.getDisplay().sleep(); 85 } 86 } 87 } 88 89 92 private void createUIListeners() { 93 createUIListenersButtonOK(); 95 createUIListenersButtonCancel(); 97 } 98 99 102 private void createUIListenersButtonCancel() { 103 fButtonCancel.addSelectionListener(new SelectionAdapter() { 104 public void widgetSelected(SelectionEvent e) { 105 handleButtonCancelWidgetSelected(); 106 } 107 }); 108 } 109 110 113 private void handleButtonCancelWidgetSelected() { 114 getSplash().getDisplay().close(); 116 System.exit(0); 117 } 118 119 122 private void createUIListenersButtonOK() { 123 fButtonOK.addSelectionListener(new SelectionAdapter() { 124 public void widgetSelected(SelectionEvent e) { 125 handleButtonOKWidgetSelected(); 126 } 127 }); 128 } 129 130 133 private void handleButtonOKWidgetSelected() { 134 String username = fTextUsername.getText(); 135 String password = fTextPassword.getText(); 136 if ((username.length() > 0) && 139 (password.length() > 0)) { 140 fAuthenticated = true; 141 } else { 142 MessageDialog.openError( 143 getSplash(), 144 "Authentication Failed", "A username and password must be specified to login."); } 147 } 148 149 152 private void createUI() { 153 createUICompositeLogin(); 155 createUICompositeBlank(); 157 createUILabelUserName(); 159 createUITextUserName(); 161 createUILabelPassword(); 163 createUITextPassword(); 165 createUILabelBlank(); 167 createUIButtonOK(); 169 createUIButtonCancel(); 171 } 172 173 176 private void createUIButtonCancel() { 177 fButtonCancel = new Button(fCompositeLogin, SWT.PUSH); 179 fButtonCancel.setText("Cancel"); GridData data = new GridData(SWT.NONE, SWT.NONE, false, false); 182 data.widthHint = F_BUTTON_WIDTH_HINT; 183 data.verticalIndent = 10; 184 fButtonCancel.setLayoutData(data); 185 } 186 187 190 private void createUIButtonOK() { 191 fButtonOK = new Button(fCompositeLogin, SWT.PUSH); 193 fButtonOK.setText("OK"); GridData data = new GridData(SWT.NONE, SWT.NONE, false, false); 196 data.widthHint = F_BUTTON_WIDTH_HINT; 197 data.verticalIndent = 10; 198 fButtonOK.setLayoutData(data); 199 } 200 201 204 private void createUILabelBlank() { 205 Label label = new Label(fCompositeLogin, SWT.NONE); 206 label.setVisible(false); 207 } 208 209 212 private void createUITextPassword() { 213 int style = SWT.PASSWORD | SWT.BORDER; 215 fTextPassword = new Text(fCompositeLogin, style); 216 GridData data = new GridData(SWT.NONE, SWT.NONE, false, false); 218 data.widthHint = F_TEXT_WIDTH_HINT; 219 data.horizontalSpan = 2; 220 fTextPassword.setLayoutData(data); 221 } 222 223 226 private void createUILabelPassword() { 227 Label label = new Label(fCompositeLogin, SWT.NONE); 229 label.setText("&Password:"); GridData data = new GridData(); 232 data.horizontalIndent = F_LABEL_HORIZONTAL_INDENT; 233 label.setLayoutData(data); 234 } 235 236 239 private void createUITextUserName() { 240 fTextUsername = new Text(fCompositeLogin, SWT.BORDER); 242 GridData data = new GridData(SWT.NONE, SWT.NONE, false, false); 244 data.widthHint = F_TEXT_WIDTH_HINT; 245 data.horizontalSpan = 2; 246 fTextUsername.setLayoutData(data); 247 } 248 249 252 private void createUILabelUserName() { 253 Label label = new Label(fCompositeLogin, SWT.NONE); 255 label.setText("&User Name:"); GridData data = new GridData(); 258 data.horizontalIndent = F_LABEL_HORIZONTAL_INDENT; 259 label.setLayoutData(data); 260 } 261 262 265 private void createUICompositeBlank() { 266 Composite spanner = new Composite(fCompositeLogin, SWT.NONE); 267 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 268 data.horizontalSpan = F_COLUMN_COUNT; 269 spanner.setLayoutData(data); 270 } 271 272 275 private void createUICompositeLogin() { 276 fCompositeLogin = new Composite(getSplash(), SWT.BORDER); 278 GridLayout layout = new GridLayout(F_COLUMN_COUNT, false); 279 fCompositeLogin.setLayout(layout); 280 } 281 282 285 private void configureUISplash() { 286 FillLayout layout = new FillLayout(); 288 getSplash().setLayout(layout); 289 getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT); 291 } 292 293 } 294 | Popular Tags |