From e0952569189572c425c92c93cb2f83e0efcfd619 Mon Sep 17 00:00:00 2001 From: Dirk Fauth Date: Wed, 21 Sep 2016 23:27:50 +0200 Subject: Bug 501951 - Add word wrapping support in text painters Change-Id: Ifb64418541c2e17bb7b381aa28bb0e4d3491662e Signed-off-by: Dirk Fauth --- .../nattable/painter/cell/AbstractTextPainter.java | 84 +++++++++++++++++++++- ...pse.nebula.widgets.nattable.examples.e4.product | 2 +- .../feature.xml | 2 +- .../feature.xml | 2 +- .../META-INF/MANIFEST.MF | 6 +- .../plugin.xml | 6 ++ .../extension/e4/css/NatTableCSSConstants.java | 10 +++ .../extension/e4/css/NatTableCSSHandler.java | 23 ++++++ .../e4/painterfactory/CellPainterFactory.java | 8 ++- .../nattable/extension/poi/PoiExcelExporter.java | 3 +- .../category.xml | 12 ++-- 11 files changed, 141 insertions(+), 17 deletions(-) diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/AbstractTextPainter.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/AbstractTextPainter.java index f9108ff1..7fc026a3 100644 --- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/AbstractTextPainter.java +++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/painter/cell/AbstractTextPainter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012, 2015 Original authors and others. + * Copyright (c) 2012, 2016 Original authors and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -49,6 +49,10 @@ public abstract class AbstractTextPainter extends BackgroundPainter { public static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$ + /** + * @since 1.5 + */ + protected boolean wordWrapping = false; protected boolean wrapText; protected final boolean paintBg; protected boolean paintFg = true; @@ -334,7 +338,31 @@ public abstract class AbstractTextPainter extends BackgroundPainter { // take the whole width of the text int textLength = getLengthFromCache(gc, text); - if (this.calculateByTextLength && this.wrapText) { + + if (this.wordWrapping) { + if (availableLength < textLength) { + String[] lines = text.split(NEW_LINE_REGEX); + for (String textLine : lines) { + if (output.length() > 0) { + output.append(LINE_SEPARATOR); + } + + StringBuilder line = new StringBuilder(); + for (char c : textLine.toCharArray()) { + line.append(c); + int length = getLengthFromCache(gc, line.toString()); + if (length >= availableLength) { + output.append(line.substring(0, line.length() - 1)).append(LINE_SEPARATOR); + line = new StringBuilder(); + line.append(c); + } + } + output.append(line); + } + } else { + output.append(text); + } + } else if (this.calculateByTextLength && this.wrapText) { if (availableLength < textLength) { // calculate length by finding the longest word in text textLength = (availableLength - (2 * this.spacing)); @@ -397,7 +425,7 @@ public abstract class AbstractTextPainter extends BackgroundPainter { } /** - * This method gets only called if word wrapping is enabled. Concatenates + * This method gets only called if text wrapping is enabled. Concatenates * the two given words by taking the availableSpace into account. If * concatenating those two words with a space as delimiter does fit into the * available space the return value is exactly this. Else instead of a space @@ -716,4 +744,54 @@ public abstract class AbstractTextPainter extends BackgroundPainter { } } + /** + * Return whether word wrapping is enabled or not. + *

+ * Word wrapping is the wrapping behavior similar to spreadsheet + * applications where words are wrapped if there is not enough space. Text + * wrapping on the other hand only wraps whole words. + *

+ *

+ * Enabling this feature could result in slow rendering performance. It is + * therefore disabled by default. + *

+ *

+ * Note: If word wrapping is enabled, features like automatic size + * calculation by text length and text wrapping are ignored. + *

+ * + * @return true if word wrapping is enabled, false + * if not. + * + * @since 1.5 + */ + public boolean isWordWrapping() { + return this.wordWrapping; + } + + /** + * Configure whether word wrapping should be enabled or not. + *

+ * Word wrapping is the wrapping behavior similar to spreadsheet + * applications where words are wrapped if there is not enough space. Text + * wrapping on the other hand only wraps whole words. + *

+ *

+ * Enabling this feature could result in slow rendering performance. It is + * therefore disabled by default. + *

+ *

+ * Note: If word wrapping is enabled, features like automatic size + * calculation by text length and text wrapping are ignored. + *

+ * + * @param wordWrapping + * true to enable word wrapping, false + * to disable it. + * + * @since 1.5 + */ + public void setWordWrapping(boolean wordWrapping) { + this.wordWrapping = wordWrapping; + } } diff --git a/org.eclipse.nebula.widgets.nattable.examples.e4.product/org.eclipse.nebula.widgets.nattable.examples.e4.product b/org.eclipse.nebula.widgets.nattable.examples.e4.product/org.eclipse.nebula.widgets.nattable.examples.e4.product index 552796a8..31b6d73a 100644 --- a/org.eclipse.nebula.widgets.nattable.examples.e4.product/org.eclipse.nebula.widgets.nattable.examples.e4.product +++ b/org.eclipse.nebula.widgets.nattable.examples.e4.product/org.eclipse.nebula.widgets.nattable.examples.e4.product @@ -31,7 +31,7 @@ - + diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4.feature/feature.xml b/org.eclipse.nebula.widgets.nattable.extension.e4.feature/feature.xml index 61340c6f..4a185d61 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4.feature/feature.xml +++ b/org.eclipse.nebula.widgets.nattable.extension.e4.feature/feature.xml @@ -2,7 +2,7 @@ + version="1.1.0.qualifier"> NatTable Eclipse 4 Extension diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4.source.feature/feature.xml b/org.eclipse.nebula.widgets.nattable.extension.e4.source.feature/feature.xml index d9283d3b..4a002874 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4.source.feature/feature.xml +++ b/org.eclipse.nebula.widgets.nattable.extension.e4.source.feature/feature.xml @@ -2,7 +2,7 @@ + version="1.1.0.qualifier"> NatTable Eclipse 4 Extension Source diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF b/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF index f9947ac3..a071b315 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: NatTable extension for Eclipse 4 Bundle-SymbolicName: org.eclipse.nebula.widgets.nattable.extension.e4;singleton:=true -Bundle-Version: 1.0.0.qualifier +Bundle-Version: 1.1.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Require-Bundle: org.eclipse.swt;bundle-version="3.103.0", org.eclipse.e4.ui.css.core;bundle-version="0.11.0", @@ -38,6 +38,6 @@ Import-Package: javax.inject;version="1.0.0", org.eclipse.nebula.widgets.nattable.ui.util;version="[1.4.0,2.0.0)", org.eclipse.nebula.widgets.nattable.util;version="[1.4.0,2.0.0)" Bundle-ActivationPolicy: lazy -Export-Package: org.eclipse.nebula.widgets.nattable.extension.e4.css;version="1.0.0", - org.eclipse.nebula.widgets.nattable.extension.e4.painterfactory;version="1.0.0", +Export-Package: org.eclipse.nebula.widgets.nattable.extension.e4.css;version="1.1.0", + org.eclipse.nebula.widgets.nattable.extension.e4.painterfactory;version="1.1.0", org.eclipse.nebula.widgets.nattable.extension.e4.selection;version="1.0.0" diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml b/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml index 1f5188bd..19d4d583 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml @@ -96,6 +96,9 @@ + + @@ -250,6 +253,9 @@ + + diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSConstants.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSConstants.java index 92e4179f..24159bab 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSConstants.java +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSConstants.java @@ -184,6 +184,16 @@ public interface NatTableCSSConstants { *

*/ String RENDER_GRID_LINES = "render-grid-lines"; + /** + * CSS property to specify whether words should automatically or not. + * Default is false. + *

+ * Available values: true, false + *

+ * + * @since 1.1 + */ + String WORD_WRAP = "word-wrap"; /** * CSS property to specify whether text should automatically wrapped between * words or not. Default is false. diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSHandler.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSHandler.java index fbd02588..162a33d9 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSHandler.java +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/css/NatTableCSSHandler.java @@ -536,6 +536,11 @@ public class NatTableCSSHandler implements ICSSPropertyHandler, ICSSPropertyHand newColor, displayMode, label); + } else if (NatTableCSSConstants.WORD_WRAP.equalsIgnoreCase(property) + && (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE)) { + NatTableCSSHelper + .getPainterProperties(context, displayMode) + .put(NatTableCSSConstants.WORD_WRAP, NatTableCSSHelper.getBoolean(value, false)); } else if (NatTableCSSConstants.TEXT_WRAP.equalsIgnoreCase(property) && (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE)) { NatTableCSSHelper @@ -1190,6 +1195,24 @@ public class NatTableCSSHandler implements ICSSPropertyHandler, ICSSPropertyHand style.getAttributeValue(CellStyleAttributes.FOREGROUND_COLOR), engine, null); + } else if (NatTableCSSConstants.WORD_WRAP.equalsIgnoreCase(property)) { + Boolean wrap = Boolean.FALSE; + + ICellPainter painter = natTable.getConfigRegistry().getConfigAttribute( + CellConfigAttributes.CELL_PAINTER, + displayMode, + label); + if (painter != null) { + while (painter instanceof CellPainterWrapper) { + painter = ((CellPainterWrapper) painter).getWrappedPainter(); + } + } + + if (painter instanceof AbstractTextPainter) { + wrap = ((AbstractTextPainter) painter).isWordWrapping(); + } + + return wrap.toString(); } else if (NatTableCSSConstants.TEXT_WRAP.equalsIgnoreCase(property)) { Boolean wrap = Boolean.FALSE; diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/painterfactory/CellPainterFactory.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/painterfactory/CellPainterFactory.java index 16b5a364..44c334c0 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/painterfactory/CellPainterFactory.java +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/painterfactory/CellPainterFactory.java @@ -315,7 +315,7 @@ public class CellPainterFactory { */ public ICellPainter getCellPainter(List painterValues, Map painterProperties) { String backgroundKey = null; - List decoratorKeys = new ArrayList(); + List decoratorKeys = new ArrayList<>(); String contentKey = null; if (isBackgroundPainterKey(painterValues.get(0))) { @@ -493,6 +493,12 @@ public class CellPainterFactory { * The painter properties to apply. */ public void initTextPainter(AbstractTextPainter painter, Map painterProperties) { + boolean wrapWord = false; + if (painterProperties.containsKey(NatTableCSSConstants.WORD_WRAP)) { + wrapWord = (Boolean) painterProperties.get(NatTableCSSConstants.WORD_WRAP); + } + painter.setWordWrapping(wrapWord); + boolean wrapText = false; if (painterProperties.containsKey(NatTableCSSConstants.TEXT_WRAP)) { wrapText = (Boolean) painterProperties.get(NatTableCSSConstants.TEXT_WRAP); diff --git a/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java b/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java index 823de5b8..10fe2fa4 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java +++ b/org.eclipse.nebula.widgets.nattable.extension.poi/src/org/eclipse/nebula/widgets/nattable/extension/poi/PoiExcelExporter.java @@ -273,7 +273,8 @@ public abstract class PoiExcelExporter implements ILayerExporter { private boolean wrapText(ICellPainter cellPainter) { if (cellPainter instanceof AbstractTextPainter) { - return ((AbstractTextPainter) cellPainter).isWrapText(); + return ((AbstractTextPainter) cellPainter).isWordWrapping() + || ((AbstractTextPainter) cellPainter).isWrapText(); } else if (cellPainter instanceof CellPainterWrapper) { return wrapText(((CellPainterWrapper) cellPainter).getWrappedPainter()); } else if (cellPainter instanceof CellPainterDecorator) { diff --git a/org.eclipse.nebula.widgets.nattable.updatesite/category.xml b/org.eclipse.nebula.widgets.nattable.updatesite/category.xml index 023163db..17527c43 100644 --- a/org.eclipse.nebula.widgets.nattable.updatesite/category.xml +++ b/org.eclipse.nebula.widgets.nattable.updatesite/category.xml @@ -12,12 +12,6 @@ - - - - - - @@ -30,6 +24,12 @@ + + + + + + NatTable Core plugin -- cgit v1.2.3