1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat 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  *     Red Hat - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.tests.gtk.snippets;
15 
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.layout.FillLayout;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25 
26 public class Bug299492_LabelCenterWrap {
27 
main(String[] args)28 	public static void main(String[] args) {
29 		Display display = new Display();
30 		Shell shell = new Shell(display);
31 
32 		createLabel1(shell);
33 //        createLabel2(widget.shell);
34 
35 		shell.pack();
36 		shell.open();
37 		while(!shell.isDisposed()) {
38 			if(!display.readAndDispatch())
39 				display.sleep();
40 		}
41 		display.dispose();
42 	}
43 
44 	// this is inspired from my real-world application code
createLabel1(Shell shell)45 	private static void createLabel1(Shell shell) {
46 		shell.setLayout(new GridLayout(1, true));
47 		Label label = new Label(shell, SWT.HORIZONTAL | SWT.CENTER | SWT.WRAP);
48 		label.setText("Very Very Very Long String");
49 		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
50 //        createLabelInsideComposite(widget.shell);
51 	}
52 
53 	// this doesn't work either
54 	@SuppressWarnings("unused")
createLabel2(Shell shell)55 	private static void createLabel2(Shell shell) {
56 		shell.setLayout(new FillLayout());
57 
58 		Label label = new Label(shell, SWT.HORIZONTAL | SWT.CENTER | SWT.WRAP);
59 		label.setText("Very Very Very Long String");
60 //        createLabelInsideComposite(widget.shell);
61 	}
62 
63 	// this attempt to workaround the problem by creating two composites
64 	// to grab the extra horizontal space doesn't work
65 	@SuppressWarnings("unused")
createLabelInsideComposite(Shell shell)66 	private static void createLabelInsideComposite(Shell shell) {
67 		Composite parent = new Composite(shell, SWT.NONE);
68 		GridLayout gridLayout = new GridLayout(3, false);
69 		gridLayout.marginWidth = 0;
70 		gridLayout.marginHeight = 0;
71 		gridLayout.horizontalSpacing = 0;
72 		gridLayout.verticalSpacing = 0;
73 		parent.setLayout(gridLayout);
74 
75 		Composite leftPanel = new Composite(parent, SWT.NONE);
76 		leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
77 
78 		Label label = new Label(parent, SWT.HORIZONTAL | SWT.CENTER | SWT.WRAP);
79 		label.setText("Very Very Very Long String");
80 		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
81 
82 		Composite rightPanel = new Composite(parent, SWT.NONE);
83 		rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
84 	}
85 
86 }