Skip to content

Package: LabelSWTRenderer$1

LabelSWTRenderer$1

nameinstructionbranchcomplexitylinemethod
widgetDisposed(DisposeEvent)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2011-2013 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 Neufeld - initial API and implementation
13: */
14: package org.eclipse.emf.ecp.view.spi.label.swt;
15:
16: import java.util.HashMap;
17: import java.util.Map;
18: import java.util.Set;
19:
20: import javax.inject.Inject;
21:
22: import org.eclipse.core.databinding.Binding;
23: import org.eclipse.core.databinding.DataBindingContext;
24: import org.eclipse.core.databinding.observable.value.IObservableValue;
25: import org.eclipse.emf.databinding.EMFDataBindingContext;
26: import org.eclipse.emf.databinding.edit.EMFEditObservables;
27: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
28: import org.eclipse.emf.ecp.view.spi.label.model.VLabel;
29: import org.eclipse.emf.ecp.view.spi.label.model.VLabelStyle;
30: import org.eclipse.emf.ecp.view.spi.model.VViewPackage;
31: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
32: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
33: import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport;
34: import org.eclipse.emf.ecp.view.template.model.VTStyleProperty;
35: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
36: import org.eclipse.emf.ecp.view.template.style.fontProperties.model.VTFontPropertiesFactory;
37: import org.eclipse.emf.ecp.view.template.style.fontProperties.model.VTFontPropertiesStyleProperty;
38: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
39: import org.eclipse.emfforms.spi.common.report.ReportService;
40: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
41: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
42: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
43: import org.eclipse.emfforms.spi.swt.core.layout.GridDescriptionFactory;
44: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
45: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
46: import org.eclipse.jface.databinding.swt.ISWTObservableValue;
47: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
48: import org.eclipse.swt.SWT;
49: import org.eclipse.swt.events.DisposeEvent;
50: import org.eclipse.swt.events.DisposeListener;
51: import org.eclipse.swt.graphics.Color;
52: import org.eclipse.swt.graphics.Font;
53: import org.eclipse.swt.graphics.FontData;
54: import org.eclipse.swt.widgets.Composite;
55: import org.eclipse.swt.widgets.Control;
56: import org.eclipse.swt.widgets.Display;
57: import org.eclipse.swt.widgets.Label;
58:
59: /**
60: * Renders an {@link VLabel} to a SWT {@link Label}.
61: *
62: * @since 1.5
63: *
64: */
65: public class LabelSWTRenderer extends AbstractSWTRenderer<VLabel> {
66:         private final EMFDataBindingContext dbc;
67:         private final EMFFormsDatabinding emfFormsDatabinding;
68:         private final VTViewTemplateProvider vtViewTemplateProvider;
69:
70:         /**
71:          * Default Constructor.
72:          *
73:          * @param vElement the view element to be rendered
74:          * @param viewContext The view model context
75:          * @param reportService the ReportService to use
76:          * @param emfFormsDatabinding the EMFFormsDatabinding to use
77:          * @param vtViewTemplateProvider the VTViewTemplateProvider to use
78:          * @since 1.6
79:          */
80:         @Inject
81:         public LabelSWTRenderer(final VLabel vElement, final ViewModelContext viewContext, ReportService reportService,
82:                 EMFFormsDatabinding emfFormsDatabinding, VTViewTemplateProvider vtViewTemplateProvider) {
83:                 super(vElement, viewContext, reportService);
84:                 this.emfFormsDatabinding = emfFormsDatabinding;
85:                 this.vtViewTemplateProvider = vtViewTemplateProvider;
86:                 dbc = new EMFDataBindingContext();
87:         }
88:
89:         private SWTGridDescription rendererGridDescription;
90:         private Font font;
91:         private org.eclipse.swt.graphics.Color labelColor;
92:
93:         private Map<VLabelStyle, VTFontPropertiesStyleProperty> defaultStyles;
94:         private Composite parent;
95:         private EMFDataBindingContext dataBindingContext;
96:
97:         /**
98:          * {@inheritDoc}
99:          *
100:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#dispose()
101:          */
102:         @Override
103:         protected void dispose() {
104:                 rendererGridDescription = null;
105:                 if (font != null) {
106:                         font.dispose();
107:                 }
108:                 if (labelColor != null) {
109:                         labelColor.dispose();
110:                 }
111:                 if (dataBindingContext != null) {
112:                         dataBindingContext.dispose();
113:                         dataBindingContext = null;
114:                 }
115:                 dbc.dispose();
116:                 super.dispose();
117:         }
118:
119:         /**
120:          * {@inheritDoc}
121:          *
122:          * @see org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer#getGridDescription(SWTGridDescription)
123:          */
124:         @Override
125:         public SWTGridDescription getGridDescription(SWTGridDescription gridDescription) {
126:                 if (rendererGridDescription == null) {
127:                         rendererGridDescription = GridDescriptionFactory.INSTANCE.createSimpleGrid(1, 1, this);
128:                         final SWTGridCell gridCell = rendererGridDescription.getGrid().get(0);
129:                         gridCell.setVerticalGrab(false);
130:                         gridCell.setVerticalFill(false);
131:                         gridCell.setHorizontalFill(true);
132:                         gridCell.setHorizontalGrab(true);
133:                 }
134:                 return rendererGridDescription;
135:         }
136:
137:         @Override
138:         protected Control renderControl(SWTGridCell cell, Composite parent)
139:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
140:                 this.parent = parent;
141:                 final Label label;
142:                 if (getVElement().getStyle() == VLabelStyle.SEPARATOR) {
143:                         label = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
144:                 } else {
145:                         label = new Label(parent, SWT.NONE);
146:                         setText(label);
147:                         applyStyle(label);
148:                 }
149:                 return label;
150:         }
151:
152:         private void setText(Label label) {
153:                 if (getVElement().getDomainModelReference() != null) {
154:                         try {
155:                                 final IObservableValue observableValue = emfFormsDatabinding
156:                                         .getObservableValue(getVElement().getDomainModelReference(),
157:                                                 getViewModelContext().getDomainModel());
158:                                 final ISWTObservableValue observeText = WidgetProperties.text().observe(label);
159:                                 final Binding binding = getDataBindingContext().bindValue(observeText, observableValue);
160:
161:                                 label.addDisposeListener(new DisposeListener() {
162:                                         @Override
163:                                         public void widgetDisposed(DisposeEvent e) {
164:                                                 binding.dispose();
165:                                         }
166:                                 });
167:                         } catch (final DatabindingFailedException ex) {
168:                                 getReportService().report(new RenderingFailedReport(ex));
169:                                 label.setText(ex.getMessage());
170:                         }
171:                 } else {
172:                         final IObservableValue modelValue = EMFEditObservables.observeValue(
173:                                 AdapterFactoryEditingDomain.getEditingDomainFor(getVElement()), getVElement(),
174:                                 VViewPackage.eINSTANCE.getElement_Label());
175:                         final IObservableValue targetValue = WidgetProperties.text().observe(label);
176:
177:                         dbc.bindValue(targetValue, modelValue);
178:                 }
179:         }
180:
181:         private DataBindingContext getDataBindingContext() {
182:                 if (dataBindingContext == null) {
183:                         dataBindingContext = new EMFDataBindingContext();
184:                 }
185:                 return dataBindingContext;
186:         }
187:
188:         /**
189:          * Applies the style defined in the StyleProperty.
190:          *
191:          * @param label the {@link Label} to apply the styles onto
192:          */
193:         protected void applyStyle(Label label) {
194:                 final VTFontPropertiesStyleProperty fontProperties = getFontProperty();
195:                 if (fontProperties == null) {
196:                         return;
197:                 }
198:                 final FontData[] fD = label.getFont().getFontData();
199:
200:                 int style = SWT.NORMAL;
201:                 if (fontProperties.isBold()) {
202:                         style = style | SWT.BOLD;
203:                 }
204:                 if (fontProperties.isItalic()) {
205:                         style = style | SWT.ITALIC;
206:                 }
207:
208:                 fD[0].setStyle(style);
209:
210:                 fD[0].setHeight(fontProperties.getHeight());
211:
212:                 if (font != null) {
213:                         font.dispose();
214:                 }
215:                 if (labelColor != null) {
216:                         labelColor.dispose();
217:                 }
218:                 String fontName = fontProperties.getFontName();
219:                 if (fontName == null) {
220:                         fontName = getDefaultFontName(label);
221:                 }
222:                 font = new Font(label.getDisplay(), fontName, fontProperties.getHeight(), style);
223:                 label.setFont(font);
224:                 labelColor = getColor(fontProperties.getColorHEX());
225:                 label.setForeground(labelColor);
226:
227:         }
228:
229:         private Color getColor(String colorHex) {
230:                 final String redString = colorHex.substring(0, 2);
231:                 final String greenString = colorHex.substring(2, 4);
232:                 final String blueString = colorHex.substring(4, 6);
233:                 final int red = Integer.parseInt(redString, 16);
234:                 final int green = Integer.parseInt(greenString, 16);
235:                 final int blue = Integer.parseInt(blueString, 16);
236:                 return new Color(Display.getDefault(), red, green, blue);
237:         }
238:
239:         private VTFontPropertiesStyleProperty getFontProperty() {
240:                 VTFontPropertiesStyleProperty fontProperties;
241:                 final Set<VTStyleProperty> styleProperties = vtViewTemplateProvider
242:                         .getStyleProperties(getVElement(), getViewModelContext());
243:                 for (final VTStyleProperty styleProperty : styleProperties) {
244:                         if (VTFontPropertiesStyleProperty.class.isInstance(styleProperty)) {
245:                                 fontProperties = VTFontPropertiesStyleProperty.class
246:                                         .cast(styleProperty);
247:                                 return fontProperties;
248:                         }
249:                 }
250:
251:                 fontProperties = getDefaultFontProperties().get(getVElement().getStyle());
252:                 return fontProperties;
253:         }
254:
255:         private Map<VLabelStyle, VTFontPropertiesStyleProperty> getDefaultFontProperties() {
256:                 if (defaultStyles == null) {
257:                         defaultStyles = new HashMap<VLabelStyle, VTFontPropertiesStyleProperty>();
258:                         final String defaultFontName = parent.getDisplay().getSystemFont().getFontData()[0].getName();
259:                         defaultStyles.put(VLabelStyle.H0, createStyle(false, false, "000000", defaultFontName, 10)); //$NON-NLS-1$
260:                         defaultStyles.put(VLabelStyle.H1, createStyle(false, false, "000000", defaultFontName, 12)); //$NON-NLS-1$
261:                         defaultStyles.put(VLabelStyle.H2, createStyle(false, false, "000000", defaultFontName, 14)); //$NON-NLS-1$
262:                         defaultStyles.put(VLabelStyle.H3, createStyle(true, true, "000000", defaultFontName, 10)); //$NON-NLS-1$
263:                         defaultStyles.put(VLabelStyle.H4, createStyle(true, false, "000000", defaultFontName, 10)); //$NON-NLS-1$
264:                         defaultStyles.put(VLabelStyle.H5, createStyle(true, false, "000000", defaultFontName, 12)); //$NON-NLS-1$
265:                         defaultStyles.put(VLabelStyle.H6, createStyle(true, false, "000000", defaultFontName, 14)); //$NON-NLS-1$
266:                         defaultStyles.put(VLabelStyle.H7, createStyle(false, true, "000000", defaultFontName, 10)); //$NON-NLS-1$
267:                         defaultStyles.put(VLabelStyle.H8, createStyle(false, true, "000000", defaultFontName, 12)); //$NON-NLS-1$
268:                         defaultStyles.put(VLabelStyle.H9, createStyle(false, true, "000000", defaultFontName, 14)); //$NON-NLS-1$
269:                 }
270:                 return defaultStyles;
271:         }
272:
273:         private VTFontPropertiesStyleProperty createStyle(boolean bold, boolean italic, String colorHex, String fontName,
274:                 int fontHeight) {
275:
276:                 final VTFontPropertiesStyleProperty fontProp = VTFontPropertiesFactory.eINSTANCE
277:                         .createFontPropertiesStyleProperty();
278:
279:                 fontProp.setBold(bold);
280:                 fontProp.setColorHEX(colorHex); // "ff0000"
281:                 fontProp.setFontName(fontName);
282:                 fontProp.setHeight(fontHeight);
283:                 fontProp.setItalic(italic);
284:
285:                 return fontProp;
286:
287:         }
288: }