Skip to content

Package: LayoutProviderHelper

LayoutProviderHelper

nameinstructionbranchcomplexitylinemethod
addLayoutProvider(LayoutProvider)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkProviderLength()
M: 48 C: 5
9%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 7 C: 2
22%
M: 0 C: 1
100%
getColumnLayout(int, boolean)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getColumnLayout(int, boolean, Point)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getLayoutData(SWTGridCell, SWTGridDescription, SWTGridDescription, SWTGridDescription, VElement, EObject, Control)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getLayoutProvider()
M: 1 C: 17
94%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
getSpanningLayoutData(int, int)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
loadClass(String, String)
M: 25 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
readLayoutProviders()
M: 79 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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: * Eugen - initial API and implementation
13: * Martin Fleck - Bug 490708: Add layout provider service
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.spi.swt.layout;
16:
17: import java.lang.reflect.Constructor;
18: import java.lang.reflect.InvocationTargetException;
19: import java.util.ArrayList;
20: import java.util.List;
21:
22: import org.eclipse.core.runtime.IConfigurationElement;
23: import org.eclipse.core.runtime.IExtensionRegistry;
24: import org.eclipse.core.runtime.Platform;
25: import org.eclipse.emf.ecore.EObject;
26: import org.eclipse.emf.ecp.view.internal.swt.Activator;
27: import org.eclipse.emf.ecp.view.spi.model.VElement;
28: import org.eclipse.emfforms.spi.swt.core.layout.LayoutProvider;
29: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
30: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
31: import org.eclipse.swt.graphics.Point;
32: import org.eclipse.swt.widgets.Control;
33: import org.eclipse.swt.widgets.Layout;
34: import org.osgi.framework.Bundle;
35:
36: /**
37: * The helper class allowing an easy access to {@link LayoutProvider LayoutProviders}.
38: *
39: * @author Eugen Neufeld
40: * @since 1.3
41: *
42: */
43: public final class LayoutProviderHelper {
44:         private static final String CLASS_CANNOT_BE_RESOLVED = "%1$s cannot be loaded because bundle %2$s cannot be resolved."; //$NON-NLS-1$
45:         private static final String CLASS = "class"; //$NON-NLS-1$
46:         private static final String EXTENSION_POINT_ID = "org.eclipse.emf.ecp.ui.view.swt.layoutProvider"; //$NON-NLS-1$
47:
48:         private LayoutProviderHelper() {
49:
50:         }
51:
52:         private static List<LayoutProvider> layoutProviders = new ArrayList<LayoutProvider>();
53:
54:         private static synchronized List<LayoutProvider> getLayoutProvider() {
55:•                if (layoutProviders == null || layoutProviders.isEmpty()) {
56:                         final LayoutProvider layoutProviderService = Activator.getDefault().getLayoutProvider();
57:                         // allow service registration and registration through extension points
58:                         // service has precedence over extension points
59:•                        if (layoutProviderService != null) {
60:                                 layoutProviders.add(layoutProviderService);
61:                         } else {
62:                                 readLayoutProviders();
63:                         }
64:                 }
65:                 return layoutProviders;
66:         }
67:
68:         private static void readLayoutProviders() {
69:                 final IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
70:•                if (extensionRegistry == null) {
71:                         return;
72:                 }
73:                 final IConfigurationElement[] controls = extensionRegistry.getConfigurationElementsFor(EXTENSION_POINT_ID);
74:•                for (final IConfigurationElement e : controls) {
75:                         try {
76:                                 final String clazz = e.getAttribute(CLASS);
77:                                 final Class<? extends LayoutProvider> resolvedClass = loadClass(e
78:                                         .getContributor().getName(), clazz);
79:                                 final Constructor<? extends LayoutProvider> controlConstructor = resolvedClass
80:                                         .getConstructor();
81:                                 final LayoutProvider layoutProvider = controlConstructor.newInstance();
82:                                 layoutProviders.add(layoutProvider);
83:                         } catch (final ClassNotFoundException ex) {
84:                                 Activator.log(ex);
85:                         } catch (final NoSuchMethodException ex) {
86:                                 Activator.log(ex);
87:                         } catch (final SecurityException ex) {
88:                                 Activator.log(ex);
89:                         } catch (final InstantiationException ex) {
90:                                 Activator.log(ex);
91:                         } catch (final IllegalAccessException ex) {
92:                                 Activator.log(ex);
93:                         } catch (final IllegalArgumentException ex) {
94:                                 Activator.log(ex);
95:                         } catch (final InvocationTargetException ex) {
96:                                 Activator.log(ex);
97:                         }
98:
99:                 }
100:
101:         }
102:
103:         @SuppressWarnings("unchecked")
104:         private static <T> Class<T> loadClass(String bundleName, String clazz)
105:                 throws ClassNotFoundException {
106:                 final Bundle bundle = Platform.getBundle(bundleName);
107:•                if (bundle == null) {
108:                         throw new ClassNotFoundException(String.format(
109:                                 CLASS_CANNOT_BE_RESOLVED, clazz, bundleName));
110:                 }
111:                 return (Class<T>) bundle.loadClass(clazz);
112:
113:         }
114:
115:         /**
116:          * Eases the access to the layout provider extension point. For the method description.
117:          *
118:          * @see LayoutProvider#getColumnLayout(int, boolean)
119:          *
120:          * @param numColumns the number of columns to create
121:          * @param equalWidth whether the columns should be equal width
122:          * @return the layout to use
123:          */
124:         public static Layout getColumnLayout(int numColumns, boolean equalWidth) {
125:                 checkProviderLength();
126:                 return getLayoutProvider().get(0).getColumnLayout(numColumns, equalWidth);
127:         }
128:
129:         /**
130:          * Eases the access to the layout provider extension point. For the method description.
131:          *
132:          * @see LayoutProvider#getColumnLayout(int, boolean, Point)
133:          *
134:          * @param numColumns the number of columns to create
135:          * @param equalWidth whether the columns should be equal width
136:          * @param margins the margins
137:          * @return the layout to use
138:          * @since 1.7
139:          */
140:         public static Layout getColumnLayout(int numColumns, boolean equalWidth, Point margins) {
141:                 checkProviderLength();
142:                 return getLayoutProvider().get(0).getColumnLayout(numColumns, equalWidth, margins);
143:         }
144:
145:         /**
146:          * Eases the access to the layout provider extension point. For the method description.
147:          *
148:          * @see LayoutProvider#getLayoutData(SWTGridCell, SWTGridDescription, SWTGridDescription, SWTGridDescription,
149:          * VElement, EObject, Control)
150:          *
151:          * @param gridCell the current {@link SWTGridCell}
152:          * @param controlGridDescription the {@link SWTGridDescription} of the rendered {@link VElement}
153:          * @param currentRowGridDescription the {@link SWTGridDescription} of the current row
154:          * @param fullGridDescription the {@link SWTGridDescription} of the whole container
155:          * @param vElement the {@link VElement} which is currently rendered
156:          * @param domainModel The domain model object whose feature is currently rendered
157:          * @param control the rendered {@link Control}
158:          * @return the Object being the layout data to set
159:          * @since 1.6
160:          */
161:         public static Object getLayoutData(SWTGridCell gridCell, SWTGridDescription controlGridDescription,
162:                 SWTGridDescription currentRowGridDescription, SWTGridDescription fullGridDescription, VElement vElement,
163:                 EObject domainModel, Control control) {
164:                 checkProviderLength();
165:                 return getLayoutProvider().get(0).getLayoutData(gridCell, controlGridDescription, currentRowGridDescription,
166:                         fullGridDescription, vElement, domainModel, control);
167:         }
168:
169:         private static void checkProviderLength() {
170:•                if (getLayoutProvider().size() != 1) {
171:                         final StringBuilder sb = new StringBuilder("There must be exactly one LayoutProvider!"); //$NON-NLS-1$
172:                         sb.append("\n"); //$NON-NLS-1$
173:                         sb.append(String.format("Found %1$d providers.", getLayoutProvider().size())); //$NON-NLS-1$
174:•                        for (final LayoutProvider layoutProvider : getLayoutProvider()) {
175:                                 sb.append("\n"); //$NON-NLS-1$
176:                                 sb.append(layoutProvider.toString());
177:                         }
178:                         throw new IllegalStateException(sb.toString());
179:                 }
180:         }
181:
182:         /**
183:          * The layout data for a spanning layout.
184:          *
185:          * @param spanX the horizontal span
186:          * @param spanY the vertical span
187:          * @return a simple spanning layout
188:          */
189:         public static Object getSpanningLayoutData(int spanX, int spanY) {
190:                 checkProviderLength();
191:                 return getLayoutProvider().get(0).getSpanningLayoutData(spanX, spanY);
192:         }
193:
194:         /**
195:          * Allows to add a {@link LayoutProvider} directly.
196:          *
197:          * @param layoutProvider The {@link LayoutProvider}
198:          * @since 1.6
199:          */
200:         public static void addLayoutProvider(LayoutProvider layoutProvider) {
201:                 layoutProviders.add(layoutProvider);
202:         }
203: }