Skip to content

Package: EMFFormsSWTLayoutUtil

EMFFormsSWTLayoutUtil

nameinstructionbranchcomplexitylinemethod
adjustParentSize(Control)
M: 64 C: 52
45%
M: 9 C: 11
55%
M: 6 C: 5
45%
M: 14 C: 17
55%
M: 0 C: 1
100%
getLayoutOptimizer()
M: 3 C: 12
80%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 2 C: 5
71%
M: 0 C: 1
100%
getOptimizerService()
M: 9 C: 18
67%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 3 C: 8
73%
M: 0 C: 1
100%
updateLayoutData(Object, int, int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. 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: * Johannes Faltermeier - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.swt.core.layout;
15:
16: import org.eclipse.swt.SWT;
17: import org.eclipse.swt.custom.ScrolledComposite;
18: import org.eclipse.swt.graphics.Point;
19: import org.eclipse.swt.layout.GridData;
20: import org.eclipse.swt.widgets.Composite;
21: import org.eclipse.swt.widgets.Control;
22: import org.eclipse.swt.widgets.ExpandBar;
23: import org.eclipse.swt.widgets.ExpandItem;
24: import org.eclipse.swt.widgets.Shell;
25: import org.osgi.framework.Bundle;
26: import org.osgi.framework.BundleContext;
27: import org.osgi.framework.FrameworkUtil;
28: import org.osgi.framework.ServiceReference;
29:
30: /**
31: * Util class for common SWT-related layout tasks.
32: *
33: * @author jfaltermeier
34: * @since 1.8
35: *
36: */
37: public final class EMFFormsSWTLayoutUtil {
38:
39:         private static EMFFormsSWTLayoutOptimizer layoutOptimizer;
40:
41:         private EMFFormsSWTLayoutUtil() {
42:
43:         }
44:
45:         private static synchronized EMFFormsSWTLayoutOptimizer getLayoutOptimizer() {
46:•                if (layoutOptimizer == null) {
47:                         final EMFFormsSWTLayoutOptimizer optimizerService = getOptimizerService();
48:•                        if (optimizerService != null) {
49:                                 layoutOptimizer = optimizerService;
50:                         } else {
51:                                 layoutOptimizer = new EMFFormsSWTLayoutDelayed();
52:                         }
53:                 }
54:                 return layoutOptimizer;
55:         }
56:
57:         private static EMFFormsSWTLayoutOptimizer getOptimizerService() {
58:                 final Bundle bundle = FrameworkUtil.getBundle(EMFFormsSWTLayoutUtil.class);
59:•                if (bundle == null) {
60:                         return null;
61:                 }
62:                 final BundleContext bundleContext = bundle.getBundleContext();
63:•                if (bundleContext == null) {
64:                         return null;
65:                 }
66:                 final ServiceReference<EMFFormsSWTLayoutOptimizer> serviceReference = bundleContext
67:                         .getServiceReference(EMFFormsSWTLayoutOptimizer.class);
68:•                if (serviceReference != null) {
69:                         return bundleContext.getService(serviceReference);
70:                 }
71:                 return null;
72:         }
73:
74:         /**
75:          * This methods helps to update the size of a parent composite when the size of a child has changed. This is needed
76:          * for {@link ScrolledComposite} and {@link ExpandBar}.
77:          *
78:          * @param control the control with a changed size.
79:          */
80:         public static void adjustParentSize(Control control) {
81:•                if (control.isDisposed()) {
82:                         return;
83:                 }
84:                 Composite parent = control.getParent();
85:•                while (parent != null) {
86:
87:•                        if (ScrolledComposite.class.isInstance(parent)) {
88:                                 final ScrolledComposite scrolledComposite = ScrolledComposite.class.cast(parent);
89:                                 final Control content = scrolledComposite.getContent();
90:•                                if (content == null) {
91:                                         return;
92:                                 }
93:                                 final Point point = content.computeSize(SWT.DEFAULT, SWT.DEFAULT);
94:                                 scrolledComposite.setMinSize(point);
95:•                        } else if (ExpandBar.class.isInstance(parent)) {
96:                                 final ExpandBar bar = ExpandBar.class.cast(parent);
97:                                 int oldBarHeight = 0;
98:                                 int barHeight = 0;
99:•                                for (final ExpandItem item : bar.getItems()) {
100:                                         final Control itemControl = item.getControl();
101:•                                        if (itemControl != null) {
102:                                                 oldBarHeight += item.getHeight();
103:                                                 final int height = itemControl.computeSize(bar.getSize().x, SWT.DEFAULT, true).y;
104:                                                 barHeight += height;
105:                                                 item.setHeight(height);
106:                                         }
107:                                 }
108:•                                if (bar.getItemCount() > 0) {
109:                                         /* only update layout data when there is at least one item */
110:                                         updateLayoutData(bar.getLayoutData(), oldBarHeight, barHeight);
111:                                 }
112:                         }
113:
114:•                        if (parent.getParent() == null) {
115:                                 getLayoutOptimizer().layout(parent);
116:                         }
117:
118:•                        else if (Shell.class.isInstance(parent)) {
119:                                 getLayoutOptimizer().layout(parent);
120:                         }
121:
122:                         parent = parent.getParent();
123:                 }
124:
125:         }
126:
127:         private static void updateLayoutData(final Object layoutData, int oldHeight, int newHeight) {
128:•                if (layoutData instanceof GridData) {
129:                         final GridData gridData = (GridData) layoutData;
130:•                        if (gridData.heightHint == -1) {
131:                                 return;
132:                         }
133:                         final int heightHint = gridData.heightHint - oldHeight + newHeight;
134:                         gridData.heightHint = heightHint;
135:
136:                 }
137:         }
138:
139: }