1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.snippets;
15 
16 /*
17  * GridLayout snippet: grow/shrink a wrappable Text's height to show its
18  * content as it changes
19  *
20  * For a list of all SWT example snippets see
21  * http://www.eclipse.org/swt/snippets/
22  */
23 import org.eclipse.swt.*;
24 import org.eclipse.swt.layout.*;
25 import org.eclipse.swt.widgets.*;
26 
27 public class Snippet342 {
28 
main(String[] args)29 public static void main(String[] args) {
30 	final int TEXT_WIDTH = 100;
31 
32 	Display display = new Display();
33 	final Shell shell = new Shell(display);
34 	shell.setText("Snippet 342");
35 	shell.setLayout(new GridLayout());
36 	final Text text = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER);
37 	text.setLayoutData(new GridData(TEXT_WIDTH, SWT.DEFAULT));
38 	text.addListener(SWT.Modify, event -> {
39 		int currentHeight = text.getSize().y;
40 		int preferredHeight = text.computeSize(TEXT_WIDTH, SWT.DEFAULT).y;
41 		if (currentHeight != preferredHeight) {
42 			GridData data = (GridData)text.getLayoutData();
43 			data.heightHint = preferredHeight;
44 			shell.pack();
45 		}
46 	});
47 	shell.pack();
48 	shell.open();
49 	while (!shell.isDisposed()) {
50 		if (!display.readAndDispatch()) display.sleep();
51 	}
52 	display.dispose();
53 }
54 
55 }
56