Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 198389 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/rap/demo/controls/DialogsTab.java (-26 / +233 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2002, 2007 Innoopract Informationssysteme GmbH.
2
 * Copyright (c) 2002, 2008 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 16-23 Link Here
16
import org.eclipse.jface.dialogs.*;
16
import org.eclipse.jface.dialogs.*;
17
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.custom.CTabFolder;
18
import org.eclipse.swt.custom.CTabFolder;
19
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.*;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.layout.GridData;
20
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
21
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.widgets.*;
22
import org.eclipse.swt.widgets.*;
Lines 28-39 Link Here
28
  private Label loginDlgResLabel;
27
  private Label loginDlgResLabel;
29
  private Label messageDlgResLabel;
28
  private Label messageDlgResLabel;
30
  private Label errorDlgResLabel;
29
  private Label errorDlgResLabel;
30
  private Label messageBoxDlgResLabel;
31
32
  private Button okButton, cancelButton;
33
  private Button yesButton, noButton;
34
  private Button retryButton;
35
  private Button abortButton, ignoreButton;
36
  private Button iconErrorButton, iconInformationButton, iconQuestionButton;
37
  private Button iconWarningButton, iconWorkingButton, noIconButton;
38
39
  private Button showMessageBoxDlgButton;
31
40
32
  public DialogsTab( final CTabFolder topFolder ) {
41
  public DialogsTab( final CTabFolder topFolder ) {
33
    super( topFolder, "Dialogs" );
42
    super( topFolder, "Dialogs" );
34
  }
43
  }
35
44
36
  protected void createStyleControls( final Composite parent ) {
45
  protected void createStyleControls( final Composite parent ) {
46
    parent.setLayout( new GridLayout( 1, true ) );
47
48
    createMessageBoxStyleControls( parent );
37
  }
49
  }
38
50
39
  protected void createExampleControls( final Composite parent ) {
51
  protected void createExampleControls( final Composite parent ) {
Lines 42-78 Link Here
42
    group1.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
54
    group1.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
43
    group1.setText( "JFace Dialogs" );
55
    group1.setText( "JFace Dialogs" );
44
    group1.setLayout( new GridLayout( 3, true ) );
56
    group1.setLayout( new GridLayout( 3, true ) );
45
    
57
46
    // JFace input dialog
58
    // JFace input dialog
47
    Button showInputDlgButton = new Button( group1, SWT.PUSH );
59
    Button showInputDlgButton = new Button( group1, SWT.PUSH );
48
    showInputDlgButton.setText( "Input Dialog" );
60
    showInputDlgButton.setText( "Input Dialog" );
49
    showInputDlgButton.addSelectionListener( new SelectionAdapter() {
61
    showInputDlgButton.addSelectionListener( new SelectionAdapter() {
62
50
      public void widgetSelected( final SelectionEvent event ) {
63
      public void widgetSelected( final SelectionEvent event ) {
51
        showInputDialog();
64
        showInputDialog();
52
      }
65
      }
53
    } );
66
    } );
54
    showInputDlgButton.setLayoutData( createGridDataFillBoth() );
67
    showInputDlgButton.setLayoutData( createGridDataFillBoth() );
55
    insertSpaceLabels( group1, 2 );
68
    insertSpaceLabels( group1, 2 );
56
    
69
57
    inputDlgResLabel = new Label( group1, SWT.WRAP );
70
    inputDlgResLabel = new Label( group1, SWT.WRAP );
58
    inputDlgResLabel.setText( "Result:" );
71
    inputDlgResLabel.setText( "Result:" );
59
    GridData gdInputDlgResLabel = new GridData();
72
    GridData gdInputDlgResLabel = new GridData();
60
    gdInputDlgResLabel.horizontalSpan = 3;
73
    gdInputDlgResLabel.horizontalSpan = 3;
61
    inputDlgResLabel.setLayoutData( gdInputDlgResLabel );
74
    inputDlgResLabel.setLayoutData( gdInputDlgResLabel );
62
    
75
63
    Button showMessageInfoDlgButton = new Button( group1, SWT.PUSH );
76
    Button showMessageInfoDlgButton = new Button( group1, SWT.PUSH );
64
    showMessageInfoDlgButton.setLayoutData( createGridDataFillBoth() );
77
    showMessageInfoDlgButton.setLayoutData( createGridDataFillBoth() );
65
    showMessageInfoDlgButton.setText( "Info Message" );
78
    showMessageInfoDlgButton.setText( "Info Message" );
66
    showMessageInfoDlgButton.addSelectionListener( new SelectionAdapter() {
79
    showMessageInfoDlgButton.addSelectionListener( new SelectionAdapter() {
80
67
      public void widgetSelected( final SelectionEvent event ) {
81
      public void widgetSelected( final SelectionEvent event ) {
68
        showMessageDialogInfo();
82
        showMessageDialogInfo();
69
      }
83
      }
70
    } );
84
    } );
71
    
85
72
    Button showMessageWarningDlgButton = new Button( group1, SWT.PUSH );
86
    Button showMessageWarningDlgButton = new Button( group1, SWT.PUSH );
73
    showMessageWarningDlgButton.setLayoutData( createGridDataFillBoth() );
87
    showMessageWarningDlgButton.setLayoutData( createGridDataFillBoth() );
74
    showMessageWarningDlgButton.setText( "Warning Dialog" );
88
    showMessageWarningDlgButton.setText( "Warning Dialog" );
75
    showMessageWarningDlgButton.addSelectionListener( new SelectionAdapter() {
89
    showMessageWarningDlgButton.addSelectionListener( new SelectionAdapter() {
90
76
      public void widgetSelected( final SelectionEvent event ) {
91
      public void widgetSelected( final SelectionEvent event ) {
77
        showMessageDialogWarning();
92
        showMessageDialogWarning();
78
      }
93
      }
Lines 81-118 Link Here
81
    showMessageErrorDlgButton.setLayoutData( createGridDataFillBoth() );
96
    showMessageErrorDlgButton.setLayoutData( createGridDataFillBoth() );
82
    showMessageErrorDlgButton.setText( "Error Message" );
97
    showMessageErrorDlgButton.setText( "Error Message" );
83
    showMessageErrorDlgButton.addSelectionListener( new SelectionAdapter() {
98
    showMessageErrorDlgButton.addSelectionListener( new SelectionAdapter() {
99
84
      public void widgetSelected( final SelectionEvent event ) {
100
      public void widgetSelected( final SelectionEvent event ) {
85
        showMessageDialogError();
101
        showMessageDialogError();
86
      }
102
      }
87
    } );
103
    } );
88
    
104
89
    Button showMessageQuestionDlgButton = new Button( group1, SWT.PUSH );
105
    Button showMessageQuestionDlgButton = new Button( group1, SWT.PUSH );
90
    showMessageQuestionDlgButton.setLayoutData( createGridDataFillBoth() );
106
    showMessageQuestionDlgButton.setLayoutData( createGridDataFillBoth() );
91
    showMessageQuestionDlgButton.setText( "Question Dialog" );
107
    showMessageQuestionDlgButton.setText( "Question Dialog" );
92
    showMessageQuestionDlgButton.addSelectionListener( new SelectionAdapter() {
108
    showMessageQuestionDlgButton.addSelectionListener( new SelectionAdapter() {
109
93
      public void widgetSelected( final SelectionEvent event ) {
110
      public void widgetSelected( final SelectionEvent event ) {
94
        showMessageDialogQuestion();
111
        showMessageDialogQuestion();
95
      }
112
      }
96
    } );
113
    } );
97
    
114
98
    Button showMessageConfirmDlgButton = new Button( group1, SWT.PUSH );
115
    Button showMessageConfirmDlgButton = new Button( group1, SWT.PUSH );
99
    showMessageConfirmDlgButton.setLayoutData( createGridDataFillBoth() );
116
    showMessageConfirmDlgButton.setLayoutData( createGridDataFillBoth() );
100
    showMessageConfirmDlgButton.setText( "Confirm Message" );
117
    showMessageConfirmDlgButton.setText( "Confirm Message" );
101
    showMessageConfirmDlgButton.addSelectionListener( new SelectionAdapter() {
118
    showMessageConfirmDlgButton.addSelectionListener( new SelectionAdapter() {
119
102
      public void widgetSelected( final SelectionEvent event ) {
120
      public void widgetSelected( final SelectionEvent event ) {
103
        showMessageDialogConfirm();
121
        showMessageDialogConfirm();
104
      }
122
      }
105
    } );
123
    } );
106
    insertSpaceLabels( group1, 1 );
124
    insertSpaceLabels( group1, 1 );
107
    
125
108
    messageDlgResLabel = new Label( group1, SWT.WRAP );
126
    messageDlgResLabel = new Label( group1, SWT.WRAP );
109
    messageDlgResLabel.setText( "Result:" );
127
    messageDlgResLabel.setText( "Result:" );
110
    insertSpaceLabels( group1, 2 );
128
    insertSpaceLabels( group1, 2 );
111
    
129
112
    Button showErrorDlgButton = new Button( group1, SWT.PUSH );
130
    Button showErrorDlgButton = new Button( group1, SWT.PUSH );
113
    showErrorDlgButton.setLayoutData( createGridDataFillBoth() );
131
    showErrorDlgButton.setLayoutData( createGridDataFillBoth() );
114
    showErrorDlgButton.setText( "Error Dialog" );
132
    showErrorDlgButton.setText( "Error Dialog" );
115
    showErrorDlgButton.addSelectionListener( new SelectionAdapter() {
133
    showErrorDlgButton.addSelectionListener( new SelectionAdapter() {
134
116
      public void widgetSelected( final SelectionEvent event ) {
135
      public void widgetSelected( final SelectionEvent event ) {
117
        showErrorDialog();
136
        showErrorDialog();
118
      }
137
      }
Lines 122-129 Link Here
122
    errorDlgResLabel = new Label( group1, SWT.WRAP );
141
    errorDlgResLabel = new Label( group1, SWT.WRAP );
123
    errorDlgResLabel.setText( "Result:" );
142
    errorDlgResLabel.setText( "Result:" );
124
    insertSpaceLabels( group1, 2 );
143
    insertSpaceLabels( group1, 2 );
125
    
144
126
    
127
    Group group2 = new Group( parent, SWT.NONE );
145
    Group group2 = new Group( parent, SWT.NONE );
128
    group2.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
146
    group2.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
129
    group2.setText( "Custom Dialogs" );
147
    group2.setText( "Custom Dialogs" );
Lines 132-146 Link Here
132
    Button showLoginDlgButton = new Button( group2, SWT.PUSH );
150
    Button showLoginDlgButton = new Button( group2, SWT.PUSH );
133
    showLoginDlgButton.setText( "Login Dialog" );
151
    showLoginDlgButton.setText( "Login Dialog" );
134
    showLoginDlgButton.addSelectionListener( new SelectionAdapter() {
152
    showLoginDlgButton.addSelectionListener( new SelectionAdapter() {
153
135
      public void widgetSelected( final SelectionEvent event ) {
154
      public void widgetSelected( final SelectionEvent event ) {
136
        showLoginDialog();
155
        showLoginDialog();
137
      }
156
      }
138
    } );
157
    } );
139
    showLoginDlgButton.setLayoutData( createGridDataFillBoth() );
158
    showLoginDlgButton.setLayoutData( createGridDataFillBoth() );
140
    insertSpaceLabels( group2, 2 );
159
    insertSpaceLabels( group2, 2 );
141
    
160
142
    loginDlgResLabel = new Label( group2, SWT.WRAP );
161
    loginDlgResLabel = new Label( group2, SWT.WRAP );
143
    loginDlgResLabel.setText( "Result:" );
162
    loginDlgResLabel.setText( "Result:" );
163
164
    Group group3 = new Group( parent, SWT.NONE );
165
    group3.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
166
    group3.setText( "SWT Dialogs" );
167
    group3.setLayout( new GridLayout( 3, true ) );
168
169
    showMessageBoxDlgButton = new Button( group3, SWT.PUSH );
170
    showMessageBoxDlgButton.setText( "MessageBox Dialog" );
171
    showMessageBoxDlgButton.addSelectionListener( new SelectionAdapter() {
172
173
      public void widgetSelected( final SelectionEvent event ) {
174
        showMessageBoxDialog();
175
      }
176
    } );
177
    showMessageBoxDlgButton.setLayoutData( createGridDataFillBoth() );
178
    insertSpaceLabels( group3, 2 );
179
180
    messageBoxDlgResLabel = new Label( group3, SWT.WRAP );
181
    messageBoxDlgResLabel.setText( "Result:" );
144
  }
182
  }
145
183
146
  private GridData createGridDataFillBoth() {
184
  private GridData createGridDataFillBoth() {
Lines 155-160 Link Here
155
193
156
  private void showInputDialog() {
194
  private void showInputDialog() {
157
    final IInputValidator val = new IInputValidator() {
195
    final IInputValidator val = new IInputValidator() {
196
158
      public String isValid( String newText ) {
197
      public String isValid( String newText ) {
159
        String result = null;
198
        String result = null;
160
        if( newText.length() < 5 ) {
199
        if( newText.length() < 5 ) {
Lines 199-206 Link Here
199
                  + "Your answer will not be recorded or evaluated "
238
                  + "Your answer will not be recorded or evaluated "
200
                  + "nor does this question have any purpose apart from "
239
                  + "nor does this question have any purpose apart from "
201
                  + "filling the empty space in this dialog window.";
240
                  + "filling the empty space in this dialog window.";
202
    boolean result
241
    boolean result = MessageDialog.openQuestion( getShell(), title, mesg );
203
      = MessageDialog.openQuestion( getShell(), title, mesg );
204
    messageDlgResLabel.setText( "Question closed (" + result + ")" );
242
    messageDlgResLabel.setText( "Question closed (" + result + ")" );
205
    messageDlgResLabel.pack();
243
    messageDlgResLabel.pack();
206
  }
244
  }
Lines 208-215 Link Here
208
  private void showMessageDialogConfirm() {
246
  private void showMessageDialogConfirm() {
209
    String title = "Confirmation";
247
    String title = "Confirmation";
210
    String mesg = "Nothing will be done. Ok?";
248
    String mesg = "Nothing will be done. Ok?";
211
    boolean result
249
    boolean result = MessageDialog.openConfirm( getShell(), title, mesg );
212
      = MessageDialog.openConfirm( getShell(), title, mesg );
213
    messageDlgResLabel.setText( "Confirm closed (" + result + ")" );
250
    messageDlgResLabel.setText( "Confirm closed (" + result + ")" );
214
    messageDlgResLabel.pack();
251
    messageDlgResLabel.pack();
215
  }
252
  }
Lines 248-261 Link Here
248
    int returnCode = loginDialog.open();
285
    int returnCode = loginDialog.open();
249
    String username = loginDialog.getUsername();
286
    String username = loginDialog.getUsername();
250
    String password = loginDialog.getPassword();
287
    String password = loginDialog.getPassword();
251
    String pwd = password == null ? "n/a" : password.length() + " chars.";
288
    String pwd = password == null
252
    loginDlgResLabel.setText(   "Login Dialog User: "
289
                                 ? "n/a"
253
                               + username
290
                                 : password.length() + " chars.";
254
                               + ", Password: "
291
    loginDlgResLabel.setText( "Login Dialog User: "
255
                               + pwd
292
                              + username
256
                               + " ("
293
                              + ", Password: "
257
                               + returnCode
294
                              + pwd
258
                               + ")" );
295
                              + " ("
296
                              + returnCode
297
                              + ")" );
259
    loginDlgResLabel.pack();
298
    loginDlgResLabel.pack();
260
  }
299
  }
300
301
  private void showMessageBoxDialog() {
302
    int style = getStyle();
303
    if( okButton.getEnabled() && okButton.getSelection() ) {
304
      style |= SWT.OK;
305
    }  
306
    if( cancelButton.getEnabled() && cancelButton.getSelection() ) {
307
      style |= SWT.CANCEL;
308
    }  
309
    if( yesButton.getEnabled() && yesButton.getSelection() ) {
310
      style |= SWT.YES;
311
    }  
312
    if( noButton.getEnabled() && noButton.getSelection() ) {
313
      style |= SWT.NO;
314
    }  
315
    if( retryButton.getEnabled() && retryButton.getSelection() ) {
316
      style |= SWT.RETRY;
317
    }  
318
    if( abortButton.getEnabled() && abortButton.getSelection() ) {
319
      style |= SWT.ABORT;
320
    }  
321
    if( ignoreButton.getEnabled() && ignoreButton.getSelection() ) {
322
      style |= SWT.IGNORE;
323
    }  
324
    if( iconErrorButton.getEnabled() && iconErrorButton.getSelection() ) {
325
      style |= SWT.ICON_ERROR;
326
    }  
327
    if( iconInformationButton.getEnabled()
328
        && iconInformationButton.getSelection() ) {
329
      style |= SWT.ICON_INFORMATION;
330
    }  
331
    if( iconQuestionButton.getEnabled() && iconQuestionButton.getSelection() ) {
332
      style |= SWT.ICON_QUESTION;
333
    }  
334
    if( iconWarningButton.getEnabled() && iconWarningButton.getSelection() ) {
335
      style |= SWT.ICON_WARNING;
336
    }  
337
    if( iconWorkingButton.getEnabled() && iconWorkingButton.getSelection() ) {
338
      style |= SWT.ICON_WORKING;
339
    }  
340
    
341
    String title = "MessageBox Title";
342
    String mesg = "Lorem ipsum dolor sit amet consectetuer adipiscing elit.";
343
    MessageBox mb = new MessageBox( getShell(), style );
344
    mb.setText( title );
345
    mb.setMessage( mesg );
346
    int result = mb.open();
347
    String strResult = "";
348
    switch( result ) {
349
      case SWT.OK:
350
        strResult = "SWT.OK";
351
      break;
352
      case SWT.YES:
353
        strResult = "SWT.YES";
354
      break;
355
      case SWT.NO:
356
        strResult = "SWT.NO";
357
      break;
358
      case SWT.CANCEL:
359
        strResult = "SWT.CANCEL";
360
      break;
361
      case SWT.ABORT:
362
        strResult = "SWT.ABORT";
363
      break;
364
      case SWT.RETRY:
365
        strResult = "SWT.RETRY";
366
      break;
367
      case SWT.IGNORE:
368
        strResult = "SWT.IGNORE";
369
      break;
370
      default:
371
        strResult = "" + result;
372
      break;
373
    }
374
    messageBoxDlgResLabel.setText( "Result: " + strResult );
375
    messageBoxDlgResLabel.pack();
376
  }
377
378
  private void createMessageBoxStyleControls( final Composite parent ) {
379
    Group buttonStyleGroup = new Group( parent, SWT.NONE );
380
    buttonStyleGroup.setLayout( new GridLayout() );
381
    buttonStyleGroup.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_FILL
382
                                                  | GridData.VERTICAL_ALIGN_FILL ) );
383
    buttonStyleGroup.setText( "SWT MessageBox Styles" );
384
385
    okButton = new Button( buttonStyleGroup, SWT.CHECK );
386
    okButton.setText( "SWT.OK" );
387
    cancelButton = new Button( buttonStyleGroup, SWT.CHECK );
388
    cancelButton.setText( "SWT.CANCEL" );
389
    yesButton = new Button( buttonStyleGroup, SWT.CHECK );
390
    yesButton.setText( "SWT.YES" );
391
    noButton = new Button( buttonStyleGroup, SWT.CHECK );
392
    noButton.setText( "SWT.NO" );
393
    retryButton = new Button( buttonStyleGroup, SWT.CHECK );
394
    retryButton.setText( "SWT.RETRY" );
395
    abortButton = new Button( buttonStyleGroup, SWT.CHECK );
396
    abortButton.setText( "SWT.ABORT" );
397
    ignoreButton = new Button( buttonStyleGroup, SWT.CHECK );
398
    ignoreButton.setText( "SWT.IGNORE" );
399
400
    Group iconStyleGroup = new Group( parent, SWT.NONE );
401
    iconStyleGroup.setLayout( new GridLayout() );
402
    iconStyleGroup.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_FILL
403
                                                | GridData.VERTICAL_ALIGN_FILL ) );
404
    iconStyleGroup.setText( "SWT MessageBox Icon Styles" );
405
406
    iconErrorButton = new Button( iconStyleGroup, SWT.RADIO );
407
    iconErrorButton.setText( "SWT.ICON_ERROR" );
408
    iconInformationButton = new Button( iconStyleGroup, SWT.RADIO );
409
    iconInformationButton.setText( "SWT.ICON_INFORMATION" );
410
    iconQuestionButton = new Button( iconStyleGroup, SWT.RADIO );
411
    iconQuestionButton.setText( "SWT.ICON_QUESTION" );
412
    iconWarningButton = new Button( iconStyleGroup, SWT.RADIO );
413
    iconWarningButton.setText( "SWT.ICON_WARNING" );
414
    iconWorkingButton = new Button( iconStyleGroup, SWT.RADIO );
415
    iconWorkingButton.setText( "SWT.ICON_WORKING" );
416
    noIconButton = new Button( iconStyleGroup, SWT.RADIO );
417
    noIconButton.setText( "No Icon" );
418
419
    SelectionListener buttonStyleListener = new SelectionAdapter() {
420
421
      public void widgetSelected( SelectionEvent event ) {
422
        buttonStyleSelected( event );
423
      }
424
    };
425
426
    okButton.addSelectionListener( buttonStyleListener );
427
    cancelButton.addSelectionListener( buttonStyleListener );
428
    yesButton.addSelectionListener( buttonStyleListener );
429
    noButton.addSelectionListener( buttonStyleListener );
430
    retryButton.addSelectionListener( buttonStyleListener );
431
    abortButton.addSelectionListener( buttonStyleListener );
432
    ignoreButton.addSelectionListener( buttonStyleListener );
433
434
    noIconButton.setSelection( true );
435
  }
436
437
  private void buttonStyleSelected( SelectionEvent event ) {
438
    boolean ok = okButton.getSelection();
439
    boolean cancel = cancelButton.getSelection();
440
    boolean yes = yesButton.getSelection();
441
    boolean no = noButton.getSelection();
442
    boolean abort = abortButton.getSelection();
443
    boolean retry = retryButton.getSelection();
444
    boolean ignore = ignoreButton.getSelection();
445
446
    okButton.setEnabled( !( yes || no || retry || abort || ignore ) );
447
    cancelButton.setEnabled( !( abort || ignore || ( yes != no ) ) );
448
    yesButton.setEnabled( !( ok || retry || abort || ignore || ( cancel && !yes && !no ) ) );
449
    noButton.setEnabled( !( ok || retry || abort || ignore || ( cancel && !yes && !no ) ) );
450
    retryButton.setEnabled( !( ok || yes || no ) );
451
    abortButton.setEnabled( !( ok || cancel || yes || no ) );
452
    ignoreButton.setEnabled( !( ok || cancel || yes || no ) );
453
454
    showMessageBoxDlgButton.setEnabled( !( ok
455
                                           || cancel
456
                                           || yes
457
                                           || no
458
                                           || retry
459
                                           || abort || ignore )
460
                                        || ok
461
                                        || ( ok && cancel )
462
                                        || ( yes && no )
463
                                        || ( yes && no && cancel )
464
                                        || ( retry && cancel )
465
                                        || ( abort && retry && ignore ) );
466
467
  }
261
}
468
}
(-)src/org/eclipse/swt/SWT.java (+55 lines)
Lines 1002-1007 Link Here
1002
   * behavior (value is 1&lt;&lt;4).
1002
   * behavior (value is 1&lt;&lt;4).
1003
   */
1003
   */
1004
  public static final int ICON_WORKING = 1 << 4;
1004
  public static final int ICON_WORKING = 1 << 4;
1005
  
1006
  /**
1007
   * The <code>MessageBox</code> style constant for an OK button;
1008
   * valid combinations are OK, OK|CANCEL
1009
   * (value is 1&lt;&lt;5).
1010
   */
1011
  public static final int OK = 1 << 5;
1012
1013
  /**
1014
   * The <code>MessageBox</code> style constant for YES button;
1015
   * valid combinations are YES|NO, YES|NO|CANCEL
1016
   * (value is 1&lt;&lt;6).
1017
   */
1018
  public static final int YES = 1 << 6;
1019
1020
  /**
1021
   * The <code>MessageBox</code> style constant for NO button;
1022
   * valid combinations are YES|NO, YES|NO|CANCEL
1023
   * (value is 1&lt;&lt;7).
1024
   */
1025
  public static final int NO = 1 << 7;
1026
1027
  /**
1028
   * The <code>MessageBox</code> style constant for a CANCEL button;
1029
   * valid combinations are OK|CANCEL, YES|NO|CANCEL, RETRY|CANCEL
1030
   * (value is 1&lt;&lt;8).  This style constant is also used with 
1031
   * <code>Text</code> in combination with SEARCH.
1032
   * 
1033
   * <p><b>Used By:</b><ul>
1034
   * <li><code>MessageBox</code></li>
1035
   * <li><code>Text</code></li>
1036
   * </ul></p>
1037
   */
1038
  public static final int CANCEL = 1 << 8;
1039
1040
  /**
1041
   * The <code>MessageBox</code> style constant for an ABORT button;
1042
   * the only valid combination is ABORT|RETRY|IGNORE
1043
   * (value is 1&lt;&lt;9).
1044
   */
1045
  public static final int ABORT = 1 << 9;
1046
1047
  /**
1048
   * The <code>MessageBox</code> style constant for a RETRY button;
1049
   *  valid combinations are ABORT|RETRY|IGNORE, RETRY|CANCEL
1050
   * (value is 1&lt;&lt;10).
1051
   */
1052
  public static final int RETRY = 1 << 10;
1053
1054
  /**
1055
   * The <code>MessageBox</code> style constant for an IGNORE button;
1056
   * the only valid combination is ABORT|RETRY|IGNORE
1057
   * (value is 1&lt;&lt;11).
1058
   */
1059
  public static final int IGNORE = 1 << 11;
1005
1060
1006
  /**
1061
  /**
1007
   * The <code>Composite</code> constant to indicate that
1062
   * The <code>Composite</code> constant to indicate that
(-)src/org/eclipse/swt/widgets/MessageBox.java (+318 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2002, 2008 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.swt.widgets;
12
13
import java.util.StringTokenizer;
14
15
import org.eclipse.rwt.graphics.Graphics;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.SWTException;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.graphics.*;
21
import org.eclipse.swt.internal.graphics.TextSizeDetermination;
22
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.layout.GridLayout;
24
25
/**
26
 * Instances of this class are used to inform or warn the user.
27
 * <dl>
28
 * <dt><b>Styles:</b></dt>
29
 * <dd>ICON_ERROR, ICON_INFORMATION, ICON_QUESTION, ICON_WARNING, ICON_WORKING</dd>
30
 * <dd>OK, OK | CANCEL</dd>
31
 * <dd>YES | NO, YES | NO | CANCEL</dd>
32
 * <dd>RETRY | CANCEL</dd>
33
 * <dd>ABORT | RETRY | IGNORE</dd>
34
 * <dt><b>Events:</b></dt>
35
 * <dd>(none)</dd>
36
 * </dl>
37
 * <p>
38
 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION, ICON_QUESTION,
39
 * ICON_WARNING and ICON_WORKING may be specified.
40
 * </p><p>
41
 * IMPORTANT: This class is intended to be subclassed <em>only</em>
42
 * within the SWT implementation.
43
 * </p>
44
 * 
45
 */
46
public class MessageBox extends Dialog {
47
  
48
  private static final int BUTTON_WIDTH = 61;
49
  private static final int HORIZONTAL_DIALOG_UNIT_PER_CHAR = 4;
50
  private static final int MAX_WIDTH = 640;
51
  
52
  private Shell shell;  
53
	private String message = "";
54
	private int result;
55
	
56
  /**
57
   * Constructs a new instance of this class given only its parent.
58
   *
59
   * @param parent a shell which will be the parent of the new instance
60
   *
61
   * @exception IllegalArgumentException <ul>
62
   *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
63
   * </ul>
64
   * @exception SWTException <ul>
65
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
66
   *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
67
   * </ul>
68
   */
69
  public MessageBox( final Shell parent ) {
70
  	this( parent, SWT.OK | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL );
71
  }
72
73
  /**
74
   * Constructs a new instance of this class given its parent
75
   * and a style value describing its behavior and appearance.
76
   * <p>
77
   * The style value is either one of the style constants defined in
78
   * class <code>SWT</code> which is applicable to instances of this
79
   * class, or must be built by <em>bitwise OR</em>'ing together 
80
   * (that is, using the <code>int</code> "|" operator) two or more
81
   * of those <code>SWT</code> style constants. The class description
82
   * lists the style constants that are applicable to the class.
83
   * Style bits are also inherited from superclasses.
84
   *
85
   * @param parent a shell which will be the parent of the new instance
86
   * @param style the style of dialog to construct
87
   *
88
   * @exception IllegalArgumentException <ul>
89
   *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
90
   * </ul>
91
   * @exception SWTException <ul>
92
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
93
   *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
94
   * </ul>
95
   */
96
  public MessageBox( final Shell parent, final int style ) {
97
  	super( parent, checkStyle ( style ) );
98
  	checkSubclass();
99
  }
100
  
101
  static int checkStyle( final int style ) {
102
    int chkStyle = 0;
103
  	int mask = ( SWT.YES | SWT.NO 
104
  	           | SWT.OK | SWT.CANCEL 
105
  	           | SWT.ABORT | SWT.RETRY | SWT.IGNORE );
106
  	int bits = style & mask;
107
  	if( bits == SWT.OK || bits == SWT.CANCEL
108
  	    || bits == ( SWT.OK | SWT.CANCEL ) ) {
109
  	  chkStyle = style;
110
  	} else if( bits == SWT.YES || bits == SWT.NO
111
  	           || bits == ( SWT.YES | SWT.NO )
112
  	           || bits == ( SWT.YES | SWT.NO | SWT.CANCEL ) ) {
113
  	  chkStyle = style;
114
  	} else if( bits == ( SWT.RETRY | SWT.CANCEL )
115
  	        || bits == ( SWT.ABORT | SWT.RETRY | SWT.IGNORE ) ) {
116
  	  chkStyle = style;
117
  	} else {
118
  	  chkStyle = ( style & ~mask ) | SWT.OK;
119
  	}
120
  	return chkStyle;
121
  }
122
123
  /**
124
   * Returns the dialog's message, or an empty string if it does not have one.
125
   * The message is a description of the purpose for which the dialog was opened.
126
   * This message will be visible in the dialog while it is open.
127
   *
128
   * @return the message
129
   */
130
  public String getMessage() {
131
  	return message;
132
  }
133
  
134
  /**
135
   * Sets the dialog's message, which is a description of
136
   * the purpose for which it was opened. This message will be
137
   * visible on the dialog while it is open.
138
   *
139
   * @param string the message
140
   * 
141
   * @exception IllegalArgumentException <ul>
142
   *    <li>ERROR_NULL_ARGUMENT - if the string is null</li>
143
   * </ul>
144
   */
145
  public void setMessage( final String string ) {
146
    if( string == null ) {
147
      error( SWT.ERROR_NULL_ARGUMENT );
148
    }
149
    message = string;
150
  }
151
152
  /**
153
   * Makes the dialog visible and brings it to the front
154
   * of the display.
155
   *
156
   * @return the ID of the button that was selected to dismiss the
157
   *         message box (e.g. SWT.OK, SWT.CANCEL, etc.)
158
   *
159
   * @exception SWTException <ul>
160
   *    <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
161
   *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
162
   * </ul>
163
   */
164
  public int open() {
165
    shell = new Shell( parent, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL );
166
    shell.setText( getText() );
167
    
168
    createControls( shell );
169
    
170
    Rectangle parentSize = parent.getBounds();
171
    Point prefSize = shell.computeSize( SWT.DEFAULT, SWT.DEFAULT );
172
    if( prefSize.x > MAX_WIDTH ) {
173
      prefSize.x = MAX_WIDTH;    
174
    }
175
    shell.setSize( prefSize );
176
177
    int locationX = ( parentSize.width - prefSize.x ) / 2 + parentSize.x;
178
    int locationY = ( parentSize.height - prefSize.y ) / 2 + parentSize.y;
179
    shell.setLocation( new Point( locationX, locationY ) );
180
181
    shell.pack();
182
    shell.open();
183
    
184
    Display display = parent.getDisplay();
185
    while( !shell.isDisposed() ) {
186
      if( !display.readAndDispatch() ) display.sleep();
187
    }
188
    
189
    return result;
190
  }  
191
  
192
  private void createControls( final Composite parent ) {
193
    Display display = parent.getDisplay();
194
    Image icon = null;
195
    parent.setLayout( new GridLayout( 2, false ) );      
196
    
197
    // Icon
198
    int systemImageID = -1;
199
    if( ( style & SWT.ICON_ERROR ) != 0 ) {
200
      systemImageID = SWT.ICON_ERROR;
201
    } else if( ( style & SWT.ICON_INFORMATION ) != 0 ) {
202
      systemImageID = SWT.ICON_INFORMATION;
203
    } else if( ( style & SWT.ICON_QUESTION ) != 0 ) {
204
      systemImageID = SWT.ICON_QUESTION;
205
    } else if( ( style & SWT.ICON_WARNING ) != 0 ) {
206
      systemImageID = SWT.ICON_WARNING;
207
    } else if( ( style & SWT.ICON_WORKING ) != 0 ) {
208
      systemImageID = SWT.ICON_WORKING;
209
    }    
210
    if( systemImageID != -1 ) {      
211
      icon = display.getSystemImage( systemImageID );
212
      createIcon( parent, icon );
213
    }
214
    
215
    // Text
216
    createText( parent, icon );
217
    
218
    // Buttons
219
    Composite buttonComp = new Composite( parent, SWT.NONE );
220
    buttonComp.setLayout( new GridLayout( 0, true ) );
221
    GridData buttonData = new GridData( SWT.CENTER, SWT.CENTER, true, false );
222
    buttonData.horizontalSpan = 2;    
223
    buttonComp.setLayoutData( buttonData );
224
    
225
    if( ( style & SWT.YES ) == SWT.YES ) {
226
      createButton( buttonComp, SWT.getMessage( "SWT_Yes" ), SWT.YES );
227
    }
228
    if( ( style & SWT.NO ) == SWT.NO ) {
229
      createButton( buttonComp, SWT.getMessage( "SWT_No" ), SWT.NO );
230
    }
231
    if( ( style & SWT.OK ) == SWT.OK ) {
232
      createButton( buttonComp, SWT.getMessage( "SWT_OK" ), SWT.OK );
233
    }
234
    if( ( style & SWT.ABORT ) == SWT.ABORT ) {
235
      createButton( buttonComp, SWT.getMessage( "SWT_Abort" ), SWT.ABORT );
236
    }
237
    if( ( style & SWT.RETRY ) == SWT.RETRY ) {
238
      createButton( buttonComp, SWT.getMessage( "SWT_Retry" ), SWT.RETRY );
239
    }
240
    if( ( style & SWT.CANCEL ) == SWT.CANCEL ) {
241
      createButton( buttonComp, SWT.getMessage( "SWT_Cancel" ), SWT.CANCEL );  
242
    }    
243
    if( ( style & SWT.IGNORE ) == SWT.IGNORE ) {
244
      createButton( buttonComp, SWT.getMessage( "SWT_Ignore" ), SWT.IGNORE );
245
    }
246
  }
247
  
248
  private void createButton( final Composite parent, 
249
                             final String btnText, 
250
                             final int resCode ) {
251
    // Increment the number of columns in the button bar
252
    ( ( GridLayout ) parent.getLayout() ).numColumns++;
253
    Button btn = new Button( parent, SWT.PUSH );
254
    // Set button layout data
255
    GridData data = new GridData( GridData.HORIZONTAL_ALIGN_FILL );
256
    int widthHint = convertHorizontalDLUsToPixels( BUTTON_WIDTH );
257
    Point minSize = btn.computeSize( SWT.DEFAULT, SWT.DEFAULT, true );
258
    data.widthHint = Math.max( widthHint, minSize.x );
259
    btn.setLayoutData( data );
260
    // Set text
261
    btn.setText( btnText );    
262
    btn.addSelectionListener( new SelectionAdapter() {
263
      public void widgetSelected( final SelectionEvent event ) {
264
        result = resCode;
265
        shell.close();
266
      }
267
    } );
268
  }
269
  
270
  private void createIcon( final Composite parent, final Image icon ) {    
271
    Label iconLabel = new Label( parent, SWT.CENTER );
272
    // Set label layout data
273
    GridData data = new GridData( SWT.CENTER, SWT.TOP, false, false );
274
    data.widthHint = icon.getBounds().width + 20;
275
    iconLabel.setLayoutData( data );
276
    // Set image
277
    iconLabel.setImage( icon );
278
  }
279
  
280
  private void createText( final Composite parent, final Image icon ) {
281
    Label textLabel = new Label( parent, SWT.WRAP );
282
    String text = getMessage();
283
    Font font = textLabel.getFont();
284
    // Set label layout data
285
    GridData data = new GridData( GridData.HORIZONTAL_ALIGN_FILL );
286
    int iconWidth = icon == null ? 0 : icon.getBounds().width;
287
    int maxTextWidth = MAX_WIDTH - iconWidth - 20;
288
    // Determine the max line length
289
    int maxLineWidth = 0;
290
    StringTokenizer st = new StringTokenizer( text, "\n" );
291
    while( st.hasMoreTokens() ) {
292
      String line = st.nextToken();
293
      int lineWidth = TextSizeDetermination.stringExtent( font, line ).x;
294
      maxLineWidth = Math.max( maxLineWidth, lineWidth );      
295
    }
296
    // Set wrap width
297
    if( maxLineWidth > maxTextWidth ){
298
      data.widthHint = maxTextWidth;
299
    }    
300
    textLabel.setLayoutData( data );
301
    textLabel.setText( text );    
302
  }
303
  
304
  /**
305
   * Returns the number of pixels corresponding to the given number of
306
   * horizontal dialog units.   
307
   * 
308
   * @param dlus
309
   *            the number of horizontal dialog units
310
   * @return the number of pixels
311
   */
312
  private int convertHorizontalDLUsToPixels( final int dlus ) {   
313
    Font dialogFont = shell.getFont();
314
    return ( int)( ( Graphics.getAvgCharWidth( dialogFont ) * dlus 
315
                  + HORIZONTAL_DIALOG_UNIT_PER_CHAR / 2 )  
316
                  / HORIZONTAL_DIALOG_UNIT_PER_CHAR );    
317
  }
318
}

Return to bug 198389