Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 186042 Details for
Bug 333508
Grid: support different truncation styles [with patch]
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Suggested patch
grid-truncation-style.patch (text/plain), 17.34 KB, created by
Hannes Erven
on 2011-01-04 14:58:06 EST
(
hide
)
Description:
Suggested patch
Filename:
MIME Type:
Creator:
Hannes Erven
Created:
2011-01-04 14:58:06 EST
Size:
17.34 KB
patch
obsolete
>Index: src/org/eclipse/nebula/widgets/grid/GridCellRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridCellRenderer.java,v >retrieving revision 1.4 >diff -u -r1.4 GridCellRenderer.java >--- src/org/eclipse/nebula/widgets/grid/GridCellRenderer.java 18 Oct 2007 21:13:23 -0000 1.4 >+++ src/org/eclipse/nebula/widgets/grid/GridCellRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -49,6 +49,17 @@ > private boolean wordWrap = false; > > private boolean dragging = false; >+ >+ private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE; >+ >+ /** Truncate at the end */ >+ public static final int TRUNCATE_END=3; >+ >+ /** Truncate in the middle (equal number of chars before/after the truncation) */ >+ public static final int TRUNCATE_MIDDLE=2; >+ >+ /** Truncate at the start */ >+ public static final int TRUNCATE_START=1; > > /** > * @return Returns the row. >@@ -261,4 +272,23 @@ > { > this.dragging = dragging; > } >+ >+ /** >+ * Get the truncation style >+ * @return . >+ */ >+ public int getTruncationStyle() { >+ return truncationStyle; >+ } >+ >+ /** >+ * Set the truncation style to use when cell content is too large. >+ * @see GridCellRenderer#TRUNCATE_END >+ * @see GridCellRenderer#TRUNCATE_MIDDLE >+ * @see GridCellRenderer#TRUNCATE_START >+ * @param truncationStyle >+ */ >+ public void setTruncationStyle(int truncationStyle) { >+ this.truncationStyle = truncationStyle; >+ } > } >Index: src/org/eclipse/nebula/widgets/grid/GridFooterRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridFooterRenderer.java,v >retrieving revision 1.3 >diff -u -r1.3 GridFooterRenderer.java >--- src/org/eclipse/nebula/widgets/grid/GridFooterRenderer.java 24 Apr 2009 14:07:19 -0000 1.3 >+++ src/org/eclipse/nebula/widgets/grid/GridFooterRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -24,6 +24,8 @@ > */ > public abstract class GridFooterRenderer extends AbstractInternalWidget > { >+ private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE; >+ > /** > * Returns the bounds of the text in the cell. This is used when displaying in-place tooltips. > * If <code>null</code> is returned here, in-place tooltips will not be displayed. If the >@@ -39,4 +41,23 @@ > { > return null; > } >+ >+ /** >+ * Get the truncation style >+ * @return . >+ */ >+ public int getTruncationStyle() { >+ return truncationStyle; >+ } >+ >+ /** >+ * Set the truncation style to use when cell content is too large. >+ * @see GridCellRenderer#TRUNCATE_END >+ * @see GridCellRenderer#TRUNCATE_MIDDLE >+ * @see GridCellRenderer#TRUNCATE_START >+ * @param truncationStyle >+ */ >+ public void setTruncationStyle(int truncationStyle) { >+ this.truncationStyle = truncationStyle; >+ } > } >Index: src/org/eclipse/nebula/widgets/grid/GridHeaderRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/GridHeaderRenderer.java,v >retrieving revision 1.5 >diff -u -r1.5 GridHeaderRenderer.java >--- src/org/eclipse/nebula/widgets/grid/GridHeaderRenderer.java 6 Jun 2009 11:13:56 -0000 1.5 >+++ src/org/eclipse/nebula/widgets/grid/GridHeaderRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -28,6 +28,8 @@ > { > private boolean wordWrap = false; > >+ private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE; >+ > /** > * Returns the bounds of the text in the cell. This is used when displaying in-place tooltips. > * If <code>null</code> is returned here, in-place tooltips will not be displayed. If the >@@ -79,5 +81,25 @@ > public void setWordWrap(boolean wordWrap) > { > this.wordWrap = wordWrap; >- } >+ } >+ >+ /** >+ * Get the truncation style >+ * @return . >+ */ >+ public int getTruncationStyle() { >+ return truncationStyle; >+ } >+ >+ /** >+ * Set the truncation style to use when cell content is too large. >+ * @see GridCellRenderer#TRUNCATE_END >+ * @see GridCellRenderer#TRUNCATE_MIDDLE >+ * @see GridCellRenderer#TRUNCATE_START >+ * @param truncationStyle >+ */ >+ public void setTruncationStyle(int truncationStyle) { >+ this.truncationStyle = truncationStyle; >+ } >+ > } >Index: src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnGroupHeaderRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnGroupHeaderRenderer.java,v >retrieving revision 1.5 >diff -u -r1.5 DefaultColumnGroupHeaderRenderer.java >--- src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnGroupHeaderRenderer.java 18 Nov 2009 15:25:50 -0000 1.5 >+++ src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnGroupHeaderRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -87,7 +87,7 @@ > gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND)); > if (!isWordWrap()) > { >- gc.drawString(TextUtils.getShortString(gc, group.getText(), width), getBounds().x + x, >+ gc.drawString(TextUtils.getShortString(gc, group.getText(), width, getTruncationStyle()), getBounds().x + x, > getBounds().y + topMargin); > } > else >Index: src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnHeaderRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnHeaderRenderer.java,v >retrieving revision 1.8 >diff -u -r1.8 DefaultColumnHeaderRenderer.java >--- src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnHeaderRenderer.java 18 Nov 2009 15:25:50 -0000 1.8 >+++ src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnHeaderRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -169,7 +169,7 @@ > > if (!isWordWrap()) > { >- text = TextUtils.getShortString(gc, text, width); >+ text = TextUtils.getShortString(gc, text, width, getTruncationStyle()); > //y -= gc.getFontMetrics().getHeight(); > } > >Index: src/org/eclipse/nebula/widgets/grid/internal/DefaultCellRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/internal/DefaultCellRenderer.java,v >retrieving revision 1.15 >diff -u -r1.15 DefaultCellRenderer.java >--- src/org/eclipse/nebula/widgets/grid/internal/DefaultCellRenderer.java 18 Nov 2009 14:57:07 -0000 1.15 >+++ src/org/eclipse/nebula/widgets/grid/internal/DefaultCellRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -201,7 +201,7 @@ > > if (!isWordWrap()) > { >- String text = TextUtils.getShortString(gc, item.getText(getColumn()), width); >+ String text = TextUtils.getShortString(gc, item.getText(getColumn()), width, getTruncationStyle()); > > if (getAlignment() == SWT.RIGHT) > { >Index: src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnFooterRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnFooterRenderer.java,v >retrieving revision 1.3 >diff -u -r1.3 DefaultColumnFooterRenderer.java >--- src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnFooterRenderer.java 18 Nov 2009 15:25:50 -0000 1.3 >+++ src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnFooterRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -112,7 +112,7 @@ > > int y = getBounds().y + getBounds().height - bottomMargin - gc.getFontMetrics().getHeight(); > >- String text = TextUtils.getShortString(gc, column.getFooterText(), width); >+ String text = TextUtils.getShortString(gc, column.getFooterText(), width, getTruncationStyle()); > > if (column.getAlignment() == SWT.RIGHT) > { >Index: src/org/eclipse/nebula/widgets/grid/internal/DefaultRowHeaderRenderer.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/internal/DefaultRowHeaderRenderer.java,v >retrieving revision 1.6 >diff -u -r1.6 DefaultRowHeaderRenderer.java >--- src/org/eclipse/nebula/widgets/grid/internal/DefaultRowHeaderRenderer.java 9 Jul 2009 22:03:06 -0000 1.6 >+++ src/org/eclipse/nebula/widgets/grid/internal/DefaultRowHeaderRenderer.java 4 Jan 2011 19:45:12 -0000 >@@ -13,6 +13,7 @@ > package org.eclipse.nebula.widgets.grid.internal; > > import org.eclipse.nebula.widgets.grid.AbstractRenderer; >+import org.eclipse.nebula.widgets.grid.GridCellRenderer; > import org.eclipse.nebula.widgets.grid.GridColumn; > import org.eclipse.nebula.widgets.grid.GridItem; > import org.eclipse.swt.SWT; >@@ -42,7 +43,9 @@ > int bottomMargin = 3; > > private TextLayout textLayout; >- >+ >+ private int truncationStyle = GridCellRenderer.TRUNCATE_MIDDLE; >+ > /** > * {@inheritDoc} > */ >@@ -168,7 +171,7 @@ > if (!item.getParent().isWordWrapHeader()) > { > y += (getBounds().height - gc.stringExtent(text).y) / 2; >- gc.drawString(TextUtils.getShortString(gc, text, width), getBounds().x + x + selectionOffset, y + selectionOffset, true); >+ gc.drawString(TextUtils.getShortString(gc, text, width, this.truncationStyle), getBounds().x + x + selectionOffset, y + selectionOffset, true); > } > else > { >@@ -275,4 +278,24 @@ > }); > } > } >+ >+ /** >+ * Get the truncation style >+ * @return . >+ */ >+ public int getTruncationStyle() { >+ return truncationStyle; >+ } >+ >+ /** >+ * Set the truncation style to use when cell content is too large. >+ * @see GridCellRenderer#TRUNCATE_END >+ * @see GridCellRenderer#TRUNCATE_MIDDLE >+ * @see GridCellRenderer#TRUNCATE_START >+ * @param truncationStyle >+ */ >+ public void setTruncationStyle(int truncationStyle) { >+ this.truncationStyle = truncationStyle; >+ } >+ > } >Index: src/org/eclipse/nebula/widgets/grid/internal/TextUtils.java >=================================================================== >RCS file: /cvsroot/technology/org.eclipse.swt.nebula/org.eclipse.nebula.widgets.grid/src/org/eclipse/nebula/widgets/grid/internal/TextUtils.java,v >retrieving revision 1.1 >diff -u -r1.1 TextUtils.java >--- src/org/eclipse/nebula/widgets/grid/internal/TextUtils.java 1 Mar 2007 20:09:59 -0000 1.1 >+++ src/org/eclipse/nebula/widgets/grid/internal/TextUtils.java 4 Jan 2011 19:45:13 -0000 >@@ -10,7 +10,9 @@ > *******************************************************************************/ > package org.eclipse.nebula.widgets.grid.internal; > >+import org.eclipse.nebula.widgets.grid.GridCellRenderer; > import org.eclipse.swt.graphics.GC; >+import org.eclipse.swt.graphics.Point; > > /** > * Utility class to provide common operations on strings not supported by the >@@ -21,36 +23,79 @@ > */ > public class TextUtils > { >+ /** >+ * Shortens a supplied string so that it fits within the area specified by >+ * the width argument. Strings that have been shorted have an "..." attached >+ * to the end of the string. The width is computed using the >+ * {@link GC#textExtent(String)}. >+ * >+ * @param gc GC used to perform calculation. >+ * @param t text to modify. >+ * @param width Pixels to display. >+ * @param style truncation style. see {@link GridCellRenderer#TRUNCATE_END}, {@link GridCellRenderer#TRUNCATE_MIDDLE}, {@link GridCellRenderer#TRUNCATE_START} >+ * @return shortened string that fits in area specified. >+ */ >+ public static String getShortText(GC gc, String t, int width, int style) >+ { >+ return getShortX(gc, t, width, style, false); >+ } > > /** > * Shortens a supplied string so that it fits within the area specified by > * the width argument. Strings that have been shorted have an "..." attached > * to the end of the string. The width is computed using the >- * {@link GC#textExtent(String)}. >+ * {@link GC#stringExtent(String)}. > * > * @param gc GC used to perform calculation. > * @param t text to modify. > * @param width Pixels to display. >+ * @param style truncation style. see {@link GridCellRenderer#TRUNCATE_END}, {@link GridCellRenderer#TRUNCATE_MIDDLE}, {@link GridCellRenderer#TRUNCATE_START} > * @return shortened string that fits in area specified. > */ >- public static String getShortText(GC gc, String t, int width) >+ public static String getShortString(GC gc, String t, int width, int style) > { >+ return getShortX(gc, t, width, style, true); >+ } >+ >+ /** >+ * This is the actiual handler for the text shortening. >+ * @param gc >+ * @param t >+ * @param width >+ * @param style >+ * @param exTx see {@link #getMeasure(GC, String, boolean)} >+ * @return . >+ */ >+ private static String getShortX(GC gc, String t, int width, int style, boolean exTx){ > if (t == null) > { > return null; > } > >- if (t.equals("")) >+ if (t.length()==0) > { > return ""; > } > >- if (width >= gc.textExtent(t).x) >+ if (width >= getMeasure(gc,t,exTx).x) > { > return t; > } >+ >+ switch(style){ >+ case GridCellRenderer.TRUNCATE_MIDDLE: >+ return getShortXMid(gc, t, width, exTx); >+ case GridCellRenderer.TRUNCATE_END: >+ return getShortXEnd(gc, t, width, exTx); >+ case GridCellRenderer.TRUNCATE_START: >+ return getShortXStart(gc, t, width, exTx); >+ default: >+ throw new IllegalArgumentException("Unknown truncation style '"+style+"'! Please use one of the TRUNCATION_* constants."); >+ } >+ } > >- int w = gc.textExtent("...").x; >+ private static String getShortXMid(GC gc, String t, int width, boolean exTx){ >+ int w = getMeasure(gc,"...",exTx).x; > String text = t; > int l = text.length(); > int pivot = l / 2; >@@ -61,8 +106,8 @@ > { > String s1 = text.substring(0, s); > String s2 = text.substring(e, l); >- int l1 = gc.textExtent(s1).x; >- int l2 = gc.textExtent(s2).x; >+ int l1 = getMeasure(gc,s1,exTx).x; >+ int l2 = getMeasure(gc,s2,exTx).x; > if (l1 + w + l2 < width) > { > text = s1 + "..." + s2; >@@ -80,68 +125,64 @@ > return text; > } > >- /** >- * Shortens a supplied string so that it fits within the area specified by >- * the width argument. Strings that have been shorted have an "..." attached >- * to the end of the string. The width is computed using the >- * {@link GC#stringExtent(String)}. >- * >- * @param gc GC used to perform calculation. >- * @param t text to modify. >- * @param width Pixels to display. >- * @return shortened string that fits in area specified. >- */ >- public static String getShortString(GC gc, String t, int width) >- { >+ private static String getShortXEnd(GC gc, String t, int width, boolean exTx){ >+ int w = getMeasure(gc,"...",exTx).x; > >- if (t == null) >- { >- return null; >- } >+ String text = t; >+ int l = text.length(); > >- if (t.equals("")) >- { >- return ""; >- } >+ for(int i=l; i>0; i--){ >+ String s1 = text.substring(0, i); >+ int l1 = getMeasure(gc,s1,exTx).x; > >- if (width >= gc.stringExtent(t).x) >- { >- return t; >+ if (l1 + w < width) >+ { >+ text = s1 + "..."; >+ break; >+ } > } > >- int w = gc.stringExtent("...").x; >+ return text; >+ } >+ >+ private static String getShortXStart(GC gc, String t, int width, boolean exTx){ >+ int w = getMeasure(gc,"...",exTx).x; >+ > String text = t; > int l = text.length(); >- int pivot = l / 2; >- int s = pivot; >- int e = pivot + 1; >- while (s >= 0 && e < l) >- { >- String s1 = text.substring(0, s); >- String s2 = text.substring(e, l); >- int l1 = gc.stringExtent(s1).x; >- int l2 = gc.stringExtent(s2).x; >- if (l1 + w + l2 < width) >+ >+ for(int i=l; i>0; i--){ >+ String s1 = text.substring(l-i, l); >+ int l1 = getMeasure(gc,s1,exTx).x; >+ >+ if (l1 + w < width) > { >- text = s1 + "..." + s2; >+ text = "..."+s1; > break; > } >- s--; >- e++; >- } >- >- if (s == 0 || e == l) >- { >- text = text.substring(0, 1) + "..." + text.substring(l - 1, l); > } > > return text; > } > > /** >+ * Calls gc.stringExtent() or gc.textExtent() >+ * @param gc >+ * @param t >+ * @param exTx true => stringExtent, false => textExtent >+ * @return . >+ */ >+ private static Point getMeasure(GC gc, String t, boolean exTx){ >+ if (exTx) >+ return gc.stringExtent(t); >+ return gc.textExtent(t); >+ } >+ >+ /** > * Protected constructor to prevent instantiation. > */ > protected TextUtils() > { >+ // nothing > } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 333508
: 186042