Java Source Code: __.in.InteractiveSplashHandler


   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: /**
  19:  * @since 3.3
  20:  * 
  21:  */
  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:    /**
  45:     * 
  46:     */
  47:	      public InteractiveSplashHandler() {
  48:        fCompositeLogin = null;
  49:        fTextUsername = null;
  50:        fTextPassword = null;
  51:        fButtonOK = null;
  52:        fButtonCancel = null;
  53:        fAuthenticated = false;
  54:    }
  55:    
  56:    /*
  57:     * (non-Javadoc)
  58:     * 
  59:     * @see org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets.Shell)
  60:     */
  61:	      public void init(final Shell splash) {
  62:        // Store the shell
  63:        super.init(splash);
  64:        // Configure the shell layout
  65:        configureUISplash();
  66:        // Create UI
  67:        createUI();        
  68:        // Create UI listeners
  69:        createUIListeners();
  70:        // Force the splash screen to layout
  71:        splash.layout(true);
  72:        // Keep the splash screen visible and prevent the RCP application from 
  73:        // loading until the close button is clicked.
  74:        doEventLoop();
  75:    }
  76:    
  77:    /**
  78:     * 
  79:     */
  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:    /**
  90:     * 
  91:     */
  92:	      private void createUIListeners() {
  93:        // Create the OK button listeners
  94:        createUIListenersButtonOK();
  95:        // Create the cancel button listeners
  96:        createUIListenersButtonCancel();
  97:    }
  98:
  99:    /**
 100:     * 
 101:     */
 102:	      private void createUIListenersButtonCancel() {
 103:	          fButtonCancel.addSelectionListener(new SelectionAdapter() {
 104:	              public void widgetSelected(SelectionEvent e) {
 105:                handleButtonCancelWidgetSelected();
 106:            }
 107:        });        
 108:    }
 109:
 110:    /**
 111:     * 
 112:     */
 113:	      private void handleButtonCancelWidgetSelected() {
 114:        // Abort the loading of the RCP application
 115:        getSplash().getDisplay().close();
 116:        System.exit(0);        
 117:    }
 118:    
 119:    /**
 120:     * 
 121:     */
 122:	      private void createUIListenersButtonOK() {
 123:	          fButtonOK.addSelectionListener(new SelectionAdapter() {
 124:	              public void widgetSelected(SelectionEvent e) {
 125:                handleButtonOKWidgetSelected();
 126:            }
 127:        });                
 128:    }
 129:
 130:    /**
 131:     * 
 132:     */
 133:	      private void handleButtonOKWidgetSelected() {
 134:        String username = fTextUsername.getText();
 135:        String password = fTextPassword.getText();
 136:        // Aunthentication is successful if a user provides any username and
 137:        // any password
 138:        if ((username.length() > 0) &&
 139:	                  (password.length() > 0)) {
 140:            fAuthenticated = true;
 141:        } else {
 142:            MessageDialog.openError(
 143:                    getSplash(),
 144:                    "Authentication Failed",  //$NON-NLS-1$
 145:                    "A username and password must be specified to login.");  //$NON-NLS-1$
 146:        }
 147:    }
 148:    
 149:    /**
 150:     * 
 151:     */
 152:	      private void createUI() {
 153:        // Create the login panel
 154:        createUICompositeLogin();
 155:        // Create the blank spanner
 156:        createUICompositeBlank();
 157:        // Create the user name label
 158:        createUILabelUserName();
 159:        // Create the user name text widget
 160:        createUITextUserName();
 161:        // Create the password label
 162:        createUILabelPassword();
 163:        // Create the password text widget
 164:        createUITextPassword();
 165:        // Create the blank label
 166:        createUILabelBlank();
 167:        // Create the OK button
 168:        createUIButtonOK();
 169:        // Create the cancel button
 170:        createUIButtonCancel();
 171:    }        
 172:    
 173:    /**
 174:     * 
 175:     */
 176:	      private void createUIButtonCancel() {
 177:        // Create the button
 178:        fButtonCancel = new Button(fCompositeLogin, SWT.PUSH);
 179:        fButtonCancel.setText("Cancel"); //$NON-NLS-1$
 180:        // Configure layout data
 181:        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:    /**
 188:     * 
 189:     */
 190:	      private void createUIButtonOK() {
 191:        // Create the button
 192:        fButtonOK = new Button(fCompositeLogin, SWT.PUSH);
 193:        fButtonOK.setText("OK"); //$NON-NLS-1$
 194:        // Configure layout data
 195:        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:    /**
 202:     * 
 203:     */
 204:	      private void createUILabelBlank() {
 205:        Label label = new Label(fCompositeLogin, SWT.NONE);
 206:        label.setVisible(false);
 207:    }
 208:
 209:    /**
 210:     * 
 211:     */
 212:	      private void createUITextPassword() {
 213:        // Create the text widget
 214:        int style = SWT.PASSWORD | SWT.BORDER;
 215:        fTextPassword = new Text(fCompositeLogin, style);
 216:        // Configure layout data
 217:        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:    /**
 224:     * 
 225:     */
 226:	      private void createUILabelPassword() {
 227:        // Create the label
 228:        Label label = new Label(fCompositeLogin, SWT.NONE);
 229:        label.setText("&Password:"); //$NON-NLS-1$
 230:        // Configure layout data
 231:        GridData data = new GridData();
 232:        data.horizontalIndent = F_LABEL_HORIZONTAL_INDENT;
 233:        label.setLayoutData(data);                    
 234:    }
 235:
 236:    /**
 237:     * 
 238:     */
 239:	      private void createUITextUserName() {
 240:        // Create the text widget
 241:        fTextUsername = new Text(fCompositeLogin, SWT.BORDER);
 242:        // Configure layout data
 243:        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:    /**
 250:     * 
 251:     */
 252:	      private void createUILabelUserName() {
 253:        // Create the label
 254:        Label label = new Label(fCompositeLogin, SWT.NONE);
 255:        label.setText("&User Name:"); //$NON-NLS-1$
 256:        // Configure layout data
 257:        GridData data = new GridData();
 258:        data.horizontalIndent = F_LABEL_HORIZONTAL_INDENT;
 259:        label.setLayoutData(data);        
 260:    }
 261:
 262:    /**
 263:     * 
 264:     */
 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:    /**
 273:     * 
 274:     */
 275:	      private void createUICompositeLogin() {
 276:        // Create the composite
 277:        fCompositeLogin = new Composite(getSplash(), SWT.BORDER);
 278:        GridLayout layout = new GridLayout(F_COLUMN_COUNT, false);
 279:        fCompositeLogin.setLayout(layout);        
 280:    }
 281:
 282:    /**
 283:     * 
 284:     */
 285:	      private void configureUISplash() {
 286:        // Configure layout
 287:        FillLayout layout = new FillLayout(); 
 288:        getSplash().setLayout(layout);
 289:        // Force shell to inherit the splash background
 290:        getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT);
 291:    }
 292:    
 293:}