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/swt/SWT.java (+67 lines)
Lines 1114-1147 Link Here
1114
  /**
1114
  /**
1115
   * The <code>MessageBox</code> style constant for error icon
1115
   * The <code>MessageBox</code> style constant for error icon
1116
   * behavior (value is 1).
1116
   * behavior (value is 1).
1117
   * @since 1.2
1117
   */
1118
   */
1118
  public static final int ICON_ERROR = 1;
1119
  public static final int ICON_ERROR = 1;
1119
1120
1120
  /**
1121
  /**
1121
   * The <code>MessageBox</code> style constant for information icon
1122
   * The <code>MessageBox</code> style constant for information icon
1122
   * behavior (value is 1&lt;&lt;1).
1123
   * behavior (value is 1&lt;&lt;1).
1124
   * @since 1.2
1123
   */
1125
   */
1124
  public static final int ICON_INFORMATION = 1 << 1;
1126
  public static final int ICON_INFORMATION = 1 << 1;
1125
1127
1126
  /**
1128
  /**
1127
   * The <code>MessageBox</code> style constant for question icon
1129
   * The <code>MessageBox</code> style constant for question icon
1128
   * behavior (value is 1&lt;&lt;2).
1130
   * behavior (value is 1&lt;&lt;2).
1131
   * @since 1.2
1129
   */
1132
   */
1130
  public static final int ICON_QUESTION = 1 << 2;
1133
  public static final int ICON_QUESTION = 1 << 2;
1131
1134
1132
  /**
1135
  /**
1133
   * The <code>MessageBox</code> style constant for warning icon
1136
   * The <code>MessageBox</code> style constant for warning icon
1134
   * behavior (value is 1&lt;&lt;3).
1137
   * behavior (value is 1&lt;&lt;3).
1138
   * @since 1.2
1135
   */
1139
   */
1136
  public static final int ICON_WARNING = 1 << 3;
1140
  public static final int ICON_WARNING = 1 << 3;
1137
1141
1138
  /**
1142
  /**
1139
   * The <code>MessageBox</code> style constant for "working" icon
1143
   * The <code>MessageBox</code> style constant for "working" icon
1140
   * behavior (value is 1&lt;&lt;4).
1144
   * behavior (value is 1&lt;&lt;4).
1145
   * @since 1.2
1141
   */
1146
   */
1142
  public static final int ICON_WORKING = 1 << 4;
1147
  public static final int ICON_WORKING = 1 << 4;
1143
1148
1144
  /**
1149
  /**
1150
   * The <code>MessageBox</code> style constant for an OK button;
1151
   * valid combinations are OK, OK|CANCEL
1152
   * (value is 1&lt;&lt;5).
1153
   * @since 1.2
1154
   */
1155
  public static final int OK = 1 << 5;
1156
1157
  /**
1158
   * The <code>MessageBox</code> style constant for YES button;
1159
   * valid combinations are YES|NO, YES|NO|CANCEL
1160
   * (value is 1&lt;&lt;6).
1161
   * @since 1.2
1162
   */
1163
  public static final int YES = 1 << 6;
1164
1165
  /**
1166
   * The <code>MessageBox</code> style constant for NO button;
1167
   * valid combinations are YES|NO, YES|NO|CANCEL
1168
   * (value is 1&lt;&lt;7).
1169
   * @since 1.2
1170
   */
1171
  public static final int NO = 1 << 7;
1172
1173
  /**
1174
   * The <code>MessageBox</code> style constant for a CANCEL button;
1175
   * valid combinations are OK|CANCEL, YES|NO|CANCEL, RETRY|CANCEL
1176
   * (value is 1&lt;&lt;8).  This style constant is also used with
1177
   * <code>Text</code> in combination with SEARCH.
1178
   *
1179
   * <p><b>Used By:</b><ul>
1180
   * <li><code>MessageBox</code></li>
1181
   * <li><code>Text</code></li>
1182
   * </ul></p>
1183
   * @since 1.2
1184
   */
1185
  public static final int CANCEL = 1 << 8;
1186
1187
  /**
1188
   * The <code>MessageBox</code> style constant for an ABORT button;
1189
   * the only valid combination is ABORT|RETRY|IGNORE
1190
   * (value is 1&lt;&lt;9).
1191
   * @since 1.2
1192
   */
1193
  public static final int ABORT = 1 << 9;
1194
1195
  /**
1196
   * The <code>MessageBox</code> style constant for a RETRY button;
1197
   *  valid combinations are ABORT|RETRY|IGNORE, RETRY|CANCEL
1198
   * (value is 1&lt;&lt;10).
1199
   * @since 1.2
1200
   */
1201
  public static final int RETRY = 1 << 10;
1202
1203
  /**
1204
   * The <code>MessageBox</code> style constant for an IGNORE button;
1205
   * the only valid combination is ABORT|RETRY|IGNORE
1206
   * (value is 1&lt;&lt;11).
1207
   * @since 1.2
1208
   */
1209
  public static final int IGNORE = 1 << 11;
1210
1211
  /**
1145
   * The <code>Composite</code> constant to indicate that
1212
   * The <code>Composite</code> constant to indicate that
1146
   * an attribute (such as background) is not inherited
1213
   * an attribute (such as background) is not inherited
1147
   * by the children (value is 0).
1214
   * by the children (value is 0).
(-)src/org/eclipse/swt/widgets/MessageBox.java (+320 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
 * @since 1.2
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() ) {
187
        display.sleep();
188
      }
189
    }
190
191
    return result;
192
  }
193
194
  private void createControls( final Composite parent ) {
195
    Display display = parent.getDisplay();
196
    Image icon = null;
197
    parent.setLayout( new GridLayout( 2, false ) );
198
199
    // Icon
200
    int systemImageID = -1;
201
    if( ( style & SWT.ICON_ERROR ) != 0 ) {
202
      systemImageID = SWT.ICON_ERROR;
203
    } else if( ( style & SWT.ICON_INFORMATION ) != 0 ) {
204
      systemImageID = SWT.ICON_INFORMATION;
205
    } else if( ( style & SWT.ICON_QUESTION ) != 0 ) {
206
      systemImageID = SWT.ICON_QUESTION;
207
    } else if( ( style & SWT.ICON_WARNING ) != 0 ) {
208
      systemImageID = SWT.ICON_WARNING;
209
    } else if( ( style & SWT.ICON_WORKING ) != 0 ) {
210
      systemImageID = SWT.ICON_WORKING;
211
    }
212
    if( systemImageID != -1 ) {
213
      icon = display.getSystemImage( systemImageID );
214
      createIcon( parent, icon );
215
    }
216
217
    // Text
218
    createText( parent, icon );
219
220
    // Buttons
221
    Composite buttonComp = new Composite( parent, SWT.NONE );
222
    buttonComp.setLayout( new GridLayout( 0, true ) );
223
    GridData buttonData = new GridData( SWT.CENTER, SWT.CENTER, true, false );
224
    buttonData.horizontalSpan = 2;
225
    buttonComp.setLayoutData( buttonData );
226
227
    if( ( style & SWT.YES ) == SWT.YES ) {
228
      createButton( buttonComp, SWT.getMessage( "SWT_Yes" ), SWT.YES );
229
    }
230
    if( ( style & SWT.NO ) == SWT.NO ) {
231
      createButton( buttonComp, SWT.getMessage( "SWT_No" ), SWT.NO );
232
    }
233
    if( ( style & SWT.OK ) == SWT.OK ) {
234
      createButton( buttonComp, SWT.getMessage( "SWT_OK" ), SWT.OK );
235
    }
236
    if( ( style & SWT.ABORT ) == SWT.ABORT ) {
237
      createButton( buttonComp, SWT.getMessage( "SWT_Abort" ), SWT.ABORT );
238
    }
239
    if( ( style & SWT.RETRY ) == SWT.RETRY ) {
240
      createButton( buttonComp, SWT.getMessage( "SWT_Retry" ), SWT.RETRY );
241
    }
242
    if( ( style & SWT.CANCEL ) == SWT.CANCEL ) {
243
      createButton( buttonComp, SWT.getMessage( "SWT_Cancel" ), SWT.CANCEL );
244
    }
245
    if( ( style & SWT.IGNORE ) == SWT.IGNORE ) {
246
      createButton( buttonComp, SWT.getMessage( "SWT_Ignore" ), SWT.IGNORE );
247
    }
248
  }
249
250
  private void createButton( final Composite parent,
251
                             final String btnText,
252
                             final int resCode ) {
253
    // Increment the number of columns in the button bar
254
    ( ( GridLayout ) parent.getLayout() ).numColumns++;
255
    Button btn = new Button( parent, SWT.PUSH );
256
    // Set button layout data
257
    GridData data = new GridData( GridData.HORIZONTAL_ALIGN_FILL );
258
    int widthHint = convertHorizontalDLUsToPixels( BUTTON_WIDTH );
259
    Point minSize = btn.computeSize( SWT.DEFAULT, SWT.DEFAULT, true );
260
    data.widthHint = Math.max( widthHint, minSize.x );
261
    btn.setLayoutData( data );
262
    // Set text
263
    btn.setText( btnText );
264
    btn.addSelectionListener( new SelectionAdapter() {
265
      public void widgetSelected( final SelectionEvent event ) {
266
        result = resCode;
267
        shell.close();
268
      }
269
    } );
270
  }
271
272
  private void createIcon( final Composite parent, final Image icon ) {
273
    Label iconLabel = new Label( parent, SWT.CENTER );
274
    // Set label layout data
275
    GridData data = new GridData( SWT.CENTER, SWT.TOP, false, false );
276
    data.widthHint = icon.getBounds().width + 20;
277
    iconLabel.setLayoutData( data );
278
    // Set image
279
    iconLabel.setImage( icon );
280
  }
281
282
  private void createText( final Composite parent, final Image icon ) {
283
    Label textLabel = new Label( parent, SWT.WRAP );
284
    String text = getMessage();
285
    Font font = textLabel.getFont();
286
    // Set label layout data
287
    GridData data = new GridData( GridData.HORIZONTAL_ALIGN_FILL );
288
    int iconWidth = icon == null ? 0 : icon.getBounds().width;
289
    int maxTextWidth = MAX_WIDTH - iconWidth - 20;
290
    // Determine the max line length
291
    int maxLineWidth = 0;
292
    StringTokenizer st = new StringTokenizer( text, "\n" );
293
    while( st.hasMoreTokens() ) {
294
      String line = st.nextToken();
295
      int lineWidth = TextSizeDetermination.stringExtent( font, line ).x;
296
      maxLineWidth = Math.max( maxLineWidth, lineWidth );
297
    }
298
    // Set wrap width
299
    if( maxLineWidth > maxTextWidth ){
300
      data.widthHint = maxTextWidth;
301
    }
302
    textLabel.setLayoutData( data );
303
    textLabel.setText( text );
304
  }
305
306
  /**
307
   * Returns the number of pixels corresponding to the given number of
308
   * horizontal dialog units.
309
   *
310
   * @param dlus
311
   *            the number of horizontal dialog units
312
   * @return the number of pixels
313
   */
314
  private int convertHorizontalDLUsToPixels( final int dlus ) {
315
    Font dialogFont = shell.getFont();
316
    return ( int)( ( Graphics.getAvgCharWidth( dialogFont ) * dlus
317
                  + HORIZONTAL_DIALOG_UNIT_PER_CHAR / 2 )
318
                  / HORIZONTAL_DIALOG_UNIT_PER_CHAR );
319
  }
320
}
(-)src/org/eclipse/rap/demo/controls/DialogsTab.java (-14 / +213 lines)
Lines 17-24 Link Here
17
import org.eclipse.jface.dialogs.Dialog;
17
import org.eclipse.jface.dialogs.Dialog;
18
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.custom.CTabFolder;
19
import org.eclipse.swt.custom.CTabFolder;
20
import org.eclipse.swt.events.SelectionAdapter;
20
import org.eclipse.swt.events.*;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.layout.GridData;
21
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.layout.GridLayout;
22
import org.eclipse.swt.layout.GridLayout;
24
import org.eclipse.swt.widgets.*;
23
import org.eclipse.swt.widgets.*;
Lines 30-41 Link Here
30
  private Label loginDlgResLabel;
29
  private Label loginDlgResLabel;
31
  private Label messageDlgResLabel;
30
  private Label messageDlgResLabel;
32
  private Label errorDlgResLabel;
31
  private Label errorDlgResLabel;
32
  private Label messageBoxDlgResLabel;
33
34
  private Button okButton, cancelButton;
35
  private Button yesButton, noButton;
36
  private Button retryButton;
37
  private Button abortButton, ignoreButton;
38
  private Button iconErrorButton, iconInformationButton, iconQuestionButton;
39
  private Button iconWarningButton, iconWorkingButton, noIconButton;
40
41
  private Button showMessageBoxDlgButton;
33
42
34
  public DialogsTab( final CTabFolder topFolder ) {
43
  public DialogsTab( final CTabFolder topFolder ) {
35
    super( topFolder, "Dialogs" );
44
    super( topFolder, "Dialogs" );
36
  }
45
  }
37
46
38
  protected void createStyleControls( final Composite parent ) {
47
  protected void createStyleControls( final Composite parent ) {
48
    parent.setLayout( new GridLayout( 1, true ) );
49
50
    createMessageBoxStyleControls( parent );
39
  }
51
  }
40
52
41
  protected void createExampleControls( final Composite parent ) {
53
  protected void createExampleControls( final Composite parent ) {
Lines 44-50 Link Here
44
    group1.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
56
    group1.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
45
    group1.setText( "JFace Dialogs" );
57
    group1.setText( "JFace Dialogs" );
46
    group1.setLayout( new GridLayout( 3, true ) );
58
    group1.setLayout( new GridLayout( 3, true ) );
47
    
59
48
    // JFace input dialog
60
    // JFace input dialog
49
    Button showInputDlgButton = new Button( group1, SWT.PUSH );
61
    Button showInputDlgButton = new Button( group1, SWT.PUSH );
50
    showInputDlgButton.setText( "Input Dialog" );
62
    showInputDlgButton.setText( "Input Dialog" );
Lines 55-67 Link Here
55
    } );
67
    } );
56
    showInputDlgButton.setLayoutData( createGridDataFillBoth() );
68
    showInputDlgButton.setLayoutData( createGridDataFillBoth() );
57
    insertSpaceLabels( group1, 2 );
69
    insertSpaceLabels( group1, 2 );
58
    
70
59
    inputDlgResLabel = new Label( group1, SWT.WRAP );
71
    inputDlgResLabel = new Label( group1, SWT.WRAP );
60
    inputDlgResLabel.setText( "Result:" );
72
    inputDlgResLabel.setText( "Result:" );
61
    GridData gdInputDlgResLabel = new GridData();
73
    GridData gdInputDlgResLabel = new GridData();
62
    gdInputDlgResLabel.horizontalSpan = 3;
74
    gdInputDlgResLabel.horizontalSpan = 3;
63
    inputDlgResLabel.setLayoutData( gdInputDlgResLabel );
75
    inputDlgResLabel.setLayoutData( gdInputDlgResLabel );
64
    
76
65
    Button showMessageInfoDlgButton = new Button( group1, SWT.PUSH );
77
    Button showMessageInfoDlgButton = new Button( group1, SWT.PUSH );
66
    showMessageInfoDlgButton.setLayoutData( createGridDataFillBoth() );
78
    showMessageInfoDlgButton.setLayoutData( createGridDataFillBoth() );
67
    showMessageInfoDlgButton.setText( "Info Message" );
79
    showMessageInfoDlgButton.setText( "Info Message" );
Lines 70-76 Link Here
70
        showMessageDialogInfo();
82
        showMessageDialogInfo();
71
      }
83
      }
72
    } );
84
    } );
73
    
85
74
    Button showMessageWarningDlgButton = new Button( group1, SWT.PUSH );
86
    Button showMessageWarningDlgButton = new Button( group1, SWT.PUSH );
75
    showMessageWarningDlgButton.setLayoutData( createGridDataFillBoth() );
87
    showMessageWarningDlgButton.setLayoutData( createGridDataFillBoth() );
76
    showMessageWarningDlgButton.setText( "Warning Dialog" );
88
    showMessageWarningDlgButton.setText( "Warning Dialog" );
Lines 87-93 Link Here
87
        showMessageDialogError();
99
        showMessageDialogError();
88
      }
100
      }
89
    } );
101
    } );
90
    
102
91
    Button showMessageQuestionDlgButton = new Button( group1, SWT.PUSH );
103
    Button showMessageQuestionDlgButton = new Button( group1, SWT.PUSH );
92
    showMessageQuestionDlgButton.setLayoutData( createGridDataFillBoth() );
104
    showMessageQuestionDlgButton.setLayoutData( createGridDataFillBoth() );
93
    showMessageQuestionDlgButton.setText( "Question Dialog" );
105
    showMessageQuestionDlgButton.setText( "Question Dialog" );
Lines 96-102 Link Here
96
        showMessageDialogQuestion();
108
        showMessageDialogQuestion();
97
      }
109
      }
98
    } );
110
    } );
99
    
111
100
    Button showMessageConfirmDlgButton = new Button( group1, SWT.PUSH );
112
    Button showMessageConfirmDlgButton = new Button( group1, SWT.PUSH );
101
    showMessageConfirmDlgButton.setLayoutData( createGridDataFillBoth() );
113
    showMessageConfirmDlgButton.setLayoutData( createGridDataFillBoth() );
102
    showMessageConfirmDlgButton.setText( "Confirm Message" );
114
    showMessageConfirmDlgButton.setText( "Confirm Message" );
Lines 106-116 Link Here
106
      }
118
      }
107
    } );
119
    } );
108
    insertSpaceLabels( group1, 1 );
120
    insertSpaceLabels( group1, 1 );
109
    
121
110
    messageDlgResLabel = new Label( group1, SWT.WRAP );
122
    messageDlgResLabel = new Label( group1, SWT.WRAP );
111
    messageDlgResLabel.setText( "Result:" );
123
    messageDlgResLabel.setText( "Result:" );
112
    insertSpaceLabels( group1, 2 );
124
    insertSpaceLabels( group1, 2 );
113
    
125
114
    Button showErrorDlgButton = new Button( group1, SWT.PUSH );
126
    Button showErrorDlgButton = new Button( group1, SWT.PUSH );
115
    showErrorDlgButton.setLayoutData( createGridDataFillBoth() );
127
    showErrorDlgButton.setLayoutData( createGridDataFillBoth() );
116
    showErrorDlgButton.setText( "Error Dialog" );
128
    showErrorDlgButton.setText( "Error Dialog" );
Lines 124-131 Link Here
124
    errorDlgResLabel = new Label( group1, SWT.WRAP );
136
    errorDlgResLabel = new Label( group1, SWT.WRAP );
125
    errorDlgResLabel.setText( "Result:" );
137
    errorDlgResLabel.setText( "Result:" );
126
    insertSpaceLabels( group1, 2 );
138
    insertSpaceLabels( group1, 2 );
127
    
139
128
    
140
129
    Group group2 = new Group( parent, SWT.NONE );
141
    Group group2 = new Group( parent, SWT.NONE );
130
    group2.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
142
    group2.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
131
    group2.setText( "Custom Dialogs" );
143
    group2.setText( "Custom Dialogs" );
Lines 140-148 Link Here
140
    } );
152
    } );
141
    showLoginDlgButton.setLayoutData( createGridDataFillBoth() );
153
    showLoginDlgButton.setLayoutData( createGridDataFillBoth() );
142
    insertSpaceLabels( group2, 2 );
154
    insertSpaceLabels( group2, 2 );
143
    
155
144
    loginDlgResLabel = new Label( group2, SWT.WRAP );
156
    loginDlgResLabel = new Label( group2, SWT.WRAP );
145
    loginDlgResLabel.setText( "Result:" );
157
    loginDlgResLabel.setText( "Result:" );
158
159
    Group group3 = new Group( parent, SWT.NONE );
160
    group3.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
161
    group3.setText( "SWT Dialogs" );
162
    group3.setLayout( new GridLayout( 3, true ) );
163
164
    showMessageBoxDlgButton = new Button( group3, SWT.PUSH );
165
    showMessageBoxDlgButton.setText( "MessageBox Dialog" );
166
    showMessageBoxDlgButton.addSelectionListener( new SelectionAdapter() {
167
168
      public void widgetSelected( final SelectionEvent event ) {
169
        showMessageBoxDialog();
170
      }
171
    } );
172
    showMessageBoxDlgButton.setLayoutData( createGridDataFillBoth() );
173
    insertSpaceLabels( group3, 2 );
174
175
    messageBoxDlgResLabel = new Label( group3, SWT.WRAP );
176
    messageBoxDlgResLabel.setText( "Result:" );
146
  }
177
  }
147
178
148
  private GridData createGridDataFillBoth() {
179
  private GridData createGridDataFillBoth() {
Lines 157-163 Link Here
157
188
158
  private void showInputDialog() {
189
  private void showInputDialog() {
159
    final IInputValidator val = new IInputValidator() {
190
    final IInputValidator val = new IInputValidator() {
160
      public String isValid( String newText ) {
191
      public String isValid( final String newText ) {
161
        String result = null;
192
        String result = null;
162
        if( newText.length() < 5 ) {
193
        if( newText.length() < 5 ) {
163
          result = "Input text too short!";
194
          result = "Input text too short!";
Lines 265-268 Link Here
265
    }
296
    }
266
    return result ;
297
    return result ;
267
  }
298
  }
299
300
  private void showMessageBoxDialog() {
301
    int style = getStyle();
302
    if( okButton.getEnabled() && okButton.getSelection() ) {
303
      style |= SWT.OK;
304
    }
305
    if( cancelButton.getEnabled() && cancelButton.getSelection() ) {
306
      style |= SWT.CANCEL;
307
    }
308
    if( yesButton.getEnabled() && yesButton.getSelection() ) {
309
      style |= SWT.YES;
310
    }
311
    if( noButton.getEnabled() && noButton.getSelection() ) {
312
      style |= SWT.NO;
313
    }
314
    if( retryButton.getEnabled() && retryButton.getSelection() ) {
315
      style |= SWT.RETRY;
316
    }
317
    if( abortButton.getEnabled() && abortButton.getSelection() ) {
318
      style |= SWT.ABORT;
319
    }
320
    if( ignoreButton.getEnabled() && ignoreButton.getSelection() ) {
321
      style |= SWT.IGNORE;
322
    }
323
    if( iconErrorButton.getEnabled() && iconErrorButton.getSelection() ) {
324
      style |= SWT.ICON_ERROR;
325
    }
326
    if( iconInformationButton.getEnabled()
327
        && iconInformationButton.getSelection() ) {
328
      style |= SWT.ICON_INFORMATION;
329
    }
330
    if( iconQuestionButton.getEnabled() && iconQuestionButton.getSelection() ) {
331
      style |= SWT.ICON_QUESTION;
332
    }
333
    if( iconWarningButton.getEnabled() && iconWarningButton.getSelection() ) {
334
      style |= SWT.ICON_WARNING;
335
    }
336
    if( iconWorkingButton.getEnabled() && iconWorkingButton.getSelection() ) {
337
      style |= SWT.ICON_WORKING;
338
    }
339
340
    String title = "MessageBox Title";
341
    String mesg = "Lorem ipsum dolor sit amet consectetuer adipiscing elit.";
342
    MessageBox mb = new MessageBox( getShell(), style );
343
    mb.setText( title );
344
    mb.setMessage( mesg );
345
    int result = mb.open();
346
    String strResult = "";
347
    switch( result ) {
348
      case SWT.OK:
349
        strResult = "SWT.OK";
350
      break;
351
      case SWT.YES:
352
        strResult = "SWT.YES";
353
      break;
354
      case SWT.NO:
355
        strResult = "SWT.NO";
356
      break;
357
      case SWT.CANCEL:
358
        strResult = "SWT.CANCEL";
359
      break;
360
      case SWT.ABORT:
361
        strResult = "SWT.ABORT";
362
      break;
363
      case SWT.RETRY:
364
        strResult = "SWT.RETRY";
365
      break;
366
      case SWT.IGNORE:
367
        strResult = "SWT.IGNORE";
368
      break;
369
      default:
370
        strResult = "" + result;
371
      break;
372
    }
373
    messageBoxDlgResLabel.setText( "Result: " + strResult );
374
    messageBoxDlgResLabel.pack();
375
  }
376
377
  private void createMessageBoxStyleControls( final Composite parent ) {
378
    Group buttonStyleGroup = new Group( parent, SWT.NONE );
379
    buttonStyleGroup.setLayout( new GridLayout() );
380
    buttonStyleGroup.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_FILL
381
                                                  | GridData.VERTICAL_ALIGN_FILL ) );
382
    buttonStyleGroup.setText( "SWT MessageBox Styles" );
383
384
    okButton = new Button( buttonStyleGroup, SWT.CHECK );
385
    okButton.setText( "SWT.OK" );
386
    cancelButton = new Button( buttonStyleGroup, SWT.CHECK );
387
    cancelButton.setText( "SWT.CANCEL" );
388
    yesButton = new Button( buttonStyleGroup, SWT.CHECK );
389
    yesButton.setText( "SWT.YES" );
390
    noButton = new Button( buttonStyleGroup, SWT.CHECK );
391
    noButton.setText( "SWT.NO" );
392
    retryButton = new Button( buttonStyleGroup, SWT.CHECK );
393
    retryButton.setText( "SWT.RETRY" );
394
    abortButton = new Button( buttonStyleGroup, SWT.CHECK );
395
    abortButton.setText( "SWT.ABORT" );
396
    ignoreButton = new Button( buttonStyleGroup, SWT.CHECK );
397
    ignoreButton.setText( "SWT.IGNORE" );
398
399
    Group iconStyleGroup = new Group( parent, SWT.NONE );
400
    iconStyleGroup.setLayout( new GridLayout() );
401
    iconStyleGroup.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_FILL
402
                                                | GridData.VERTICAL_ALIGN_FILL ) );
403
    iconStyleGroup.setText( "SWT MessageBox Icon Styles" );
404
405
    iconErrorButton = new Button( iconStyleGroup, SWT.RADIO );
406
    iconErrorButton.setText( "SWT.ICON_ERROR" );
407
    iconInformationButton = new Button( iconStyleGroup, SWT.RADIO );
408
    iconInformationButton.setText( "SWT.ICON_INFORMATION" );
409
    iconQuestionButton = new Button( iconStyleGroup, SWT.RADIO );
410
    iconQuestionButton.setText( "SWT.ICON_QUESTION" );
411
    iconWarningButton = new Button( iconStyleGroup, SWT.RADIO );
412
    iconWarningButton.setText( "SWT.ICON_WARNING" );
413
    iconWorkingButton = new Button( iconStyleGroup, SWT.RADIO );
414
    iconWorkingButton.setText( "SWT.ICON_WORKING" );
415
    noIconButton = new Button( iconStyleGroup, SWT.RADIO );
416
    noIconButton.setText( "No Icon" );
417
418
    SelectionListener buttonStyleListener = new SelectionAdapter() {
419
420
      public void widgetSelected( final SelectionEvent event ) {
421
        buttonStyleSelected( event );
422
      }
423
    };
424
425
    okButton.addSelectionListener( buttonStyleListener );
426
    cancelButton.addSelectionListener( buttonStyleListener );
427
    yesButton.addSelectionListener( buttonStyleListener );
428
    noButton.addSelectionListener( buttonStyleListener );
429
    retryButton.addSelectionListener( buttonStyleListener );
430
    abortButton.addSelectionListener( buttonStyleListener );
431
    ignoreButton.addSelectionListener( buttonStyleListener );
432
433
    noIconButton.setSelection( true );
434
  }
435
436
  private void buttonStyleSelected( final SelectionEvent event ) {
437
    boolean ok = okButton.getSelection();
438
    boolean cancel = cancelButton.getSelection();
439
    boolean yes = yesButton.getSelection();
440
    boolean no = noButton.getSelection();
441
    boolean abort = abortButton.getSelection();
442
    boolean retry = retryButton.getSelection();
443
    boolean ignore = ignoreButton.getSelection();
444
445
    okButton.setEnabled( !( yes || no || retry || abort || ignore ) );
446
    cancelButton.setEnabled( !( abort || ignore || ( yes != no ) ) );
447
    yesButton.setEnabled( !( ok || retry || abort || ignore || ( cancel && !yes && !no ) ) );
448
    noButton.setEnabled( !( ok || retry || abort || ignore || ( cancel && !yes && !no ) ) );
449
    retryButton.setEnabled( !( ok || yes || no ) );
450
    abortButton.setEnabled( !( ok || cancel || yes || no ) );
451
    ignoreButton.setEnabled( !( ok || cancel || yes || no ) );
452
453
    showMessageBoxDlgButton.setEnabled( !( ok
454
                                           || cancel
455
                                           || yes
456
                                           || no
457
                                           || retry
458
                                           || abort || ignore )
459
                                        || ok
460
                                        || ( ok && cancel )
461
                                        || ( yes && no )
462
                                        || ( yes && no && cancel )
463
                                        || ( retry && cancel )
464
                                        || ( abort && retry && ignore ) );
465
466
  }
268
}
467
}
(-)src/org/eclipse/swt/widgets/MessageBox_Test.java (+100 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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
12
package org.eclipse.swt.widgets;
13
14
import junit.framework.TestCase;
15
16
import org.eclipse.swt.RWTFixture;
17
import org.eclipse.swt.SWT;
18
19
public class MessageBox_Test extends TestCase {
20
21
  protected void setUp() throws Exception {
22
    RWTFixture.setUp();
23
  }
24
25
  protected void tearDown() throws Exception {
26
    RWTFixture.tearDown();
27
  }
28
29
  public void testMessage() {
30
    Display display = new Display();
31
    Shell shell = new Shell( display , SWT.NONE );
32
    String mesg = "Lorem ipsum dolor sit amet consectetuer adipiscing elit.";
33
    MessageBox mb = new MessageBox( shell, SWT.NONE );
34
    mb.setMessage( mesg );
35
    assertEquals( mesg, mb.getMessage() );
36
  }
37
38
  public void testText() {
39
    Display display = new Display();
40
    Shell shell = new Shell( display , SWT.NONE );
41
    String title = "MessageBox Title";
42
    MessageBox mb = new MessageBox( shell, SWT.NONE );
43
    mb.setText( title );
44
    assertEquals( title, mb.getText() );
45
  }
46
47
  public void testStyle() {
48
    Display display = new Display();
49
    Shell shell = new Shell( display , SWT.NONE );
50
    // Test SWT.NONE
51
    MessageBox mb = new MessageBox( shell, SWT.NONE );
52
    assertTrue( ( mb.getStyle() & SWT.OK ) != 0 );
53
54
    // Test SWT.OK | SWT.CANCEL
55
    mb = new MessageBox( shell, SWT.OK | SWT.CANCEL );
56
    assertTrue( ( mb.getStyle() & SWT.OK ) != 0 );
57
    assertTrue( ( mb.getStyle() & SWT.CANCEL ) != 0 );
58
    mb = new MessageBox( shell, SWT.OK | SWT.CANCEL | SWT.YES );
59
    assertTrue( ( mb.getStyle() & SWT.OK ) != 0 );
60
    assertTrue( ( mb.getStyle() & SWT.CANCEL ) == 0 );
61
    assertTrue( ( mb.getStyle() & SWT.YES ) == 0 );
62
63
    // Test SWT.YES | SWT.NO | SWT.CANCEL
64
    mb = new MessageBox( shell, SWT.YES );
65
    assertTrue( ( mb.getStyle() & SWT.YES ) != 0 );
66
    mb = new MessageBox( shell, SWT.NO );
67
    assertTrue( ( mb.getStyle() & SWT.NO ) != 0 );
68
    mb = new MessageBox( shell, SWT.YES | SWT.NO );
69
    assertTrue( ( mb.getStyle() & SWT.YES ) != 0 );
70
    assertTrue( ( mb.getStyle() & SWT.NO ) != 0 );
71
    mb = new MessageBox( shell, SWT.YES | SWT.NO | SWT.CANCEL );
72
    assertTrue( ( mb.getStyle() & SWT.YES ) != 0 );
73
    assertTrue( ( mb.getStyle() & SWT.NO ) != 0 );
74
    assertTrue( ( mb.getStyle() & SWT.CANCEL ) != 0 );
75
    mb = new MessageBox( shell, SWT.YES | SWT.CANCEL );
76
    assertTrue( ( mb.getStyle() & SWT.OK ) != 0 );
77
    assertTrue( ( mb.getStyle() & SWT.YES ) == 0 );
78
    assertTrue( ( mb.getStyle() & SWT.NO ) == 0 );
79
    assertTrue( ( mb.getStyle() & SWT.CANCEL ) == 0 );
80
    mb = new MessageBox( shell, SWT.NO | SWT.CANCEL );
81
    assertTrue( ( mb.getStyle() & SWT.OK ) != 0 );
82
    assertTrue( ( mb.getStyle() & SWT.YES ) == 0 );
83
    assertTrue( ( mb.getStyle() & SWT.NO ) == 0 );
84
    assertTrue( ( mb.getStyle() & SWT.CANCEL ) == 0 );
85
86
    // Test SWT.ABORT | SWT.RETRY | SWT.IGNORE
87
    mb = new MessageBox( shell, SWT.ABORT | SWT.RETRY | SWT.IGNORE );
88
    assertTrue( ( mb.getStyle() & SWT.ABORT ) != 0 );
89
    assertTrue( ( mb.getStyle() & SWT.RETRY ) != 0 );
90
    assertTrue( ( mb.getStyle() & SWT.IGNORE ) != 0 );
91
    mb = new MessageBox( shell, SWT.CANCEL | SWT.RETRY );
92
    assertTrue( ( mb.getStyle() & SWT.CANCEL ) != 0 );
93
    assertTrue( ( mb.getStyle() & SWT.RETRY ) != 0 );
94
    mb = new MessageBox( shell, SWT.YES | SWT.RETRY | SWT.IGNORE );
95
    assertTrue( ( mb.getStyle() & SWT.OK ) != 0 );
96
    assertTrue( ( mb.getStyle() & SWT.YES ) == 0 );
97
    assertTrue( ( mb.getStyle() & SWT.RETRY ) == 0 );
98
    assertTrue( ( mb.getStyle() & SWT.IGNORE ) == 0 );
99
  }
100
}

Return to bug 198389