Home » Eclipse Projects » NatTable » Vertical Text in Columnheader(Vertical Text in Columnheader)
| | | |
Re: Vertical Text in Columnheader [message #916943 is a reply to message #915994] |
Wed, 19 September 2012 17:06 |
dhruba kumar Messages: 51 Registered: September 2012 |
Member |
|
|
Hi Dirk, I ve used vertical textpainter but text is not showing like this
C
O
L
F
I
R
S
T
I ve also tried to Create own Painter ::
package myapp.mygrid2;
import net.sourceforge.nattable.config.IConfigRegistry;
import net.sourceforge.nattable.layer.ILayer;
import net.sourceforge.nattable.layer.cell.LayerCell;
import net.sourceforge.nattable.painter.cell.AbstractTextPainter;
import net.sourceforge.nattable.painter.cell.GraphicsUtils;
import net.sourceforge.nattable.resize.command.ColumnResizeCommand;
import net.sourceforge.nattable.resize.command.RowResizeCommand;
import net.sourceforge.nattable.style.CellStyleUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
// Referenced classes of package net.sourceforge.nattable.painter.cell:
// AbstractTextPainter, GraphicsUtils
public class TextVerticalPainter extends AbstractTextPainter
{
public TextVerticalPainter()
{
this(false, true);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg)
{
this(wrapText, paintBg, 0);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg, int spacing)
{
this(wrapText, paintBg, spacing, false);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg, boolean calculate)
{
this(wrapText, paintBg, 0, calculate);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg, int spacing, boolean calculate)
{
super(wrapText, paintBg, spacing, calculate);
}
public int getPreferredWidth(LayerCell cell, GC gc, IConfigRegistry configRegistry)
{
setupGCFromConfig(gc, CellStyleUtil.getCellStyle(cell, configRegistry));
return gc.textExtent(convertDataType(cell, configRegistry)).x;
}
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry)
{
setupGCFromConfig(gc, CellStyleUtil.getCellStyle(cell, configRegistry));
return getLengthFromCache(gc, convertDataType(cell, configRegistry)) + spacing * + 1;
}
public void paintCell(LayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry)
{
/*Paint cell Logic for Painting
C
O
L
T
X
T
*/
}
protected void setNewMinLength(LayerCell cell, int contentHeight)
{
int cellLength = cell.getBounds().height;
if(cellLength < contentHeight)
{
ILayer layer = cell.getLayer();
int row = layer.getRowIndexByPosition(cell.getRowPosition());
layer.doCommand(new RowResizeCommand(layer, row, contentHeight));
}
}
public String insertNewLine(String txt)
{
String newtxt="";
for(int i=0;i<txt.length();i++)
newtxt+=txt.charAt(i)+"\n";
return newtxt;
}
protected int calculatePadding(LayerCell cell, int availableLength)
{
return cell.getBounds().height - availableLength;
}
}
pls guide for the paint cell logic.
|
|
| |
Re: Vertical Text in Columnheader [message #917653 is a reply to message #917520] |
Thu, 20 September 2012 09:41 |
dhruba kumar Messages: 51 Registered: September 2012 |
Member |
|
|
Hi Dirk Thanks I ve created custom Vertical Text Painter as per your advice
The code is:
import net.sourceforge.nattable.config.IConfigRegistry;
import net.sourceforge.nattable.layer.ILayer;
import net.sourceforge.nattable.layer.cell.LayerCell;
import net.sourceforge.nattable.painter.cell.AbstractTextPainter;
import net.sourceforge.nattable.painter.cell.GraphicsUtils;
import net.sourceforge.nattable.resize.command.ColumnResizeCommand;
import net.sourceforge.nattable.resize.command.RowResizeCommand;
import net.sourceforge.nattable.style.CellStyleUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
// Referenced classes of package net.sourceforge.nattable.painter.cell:
// AbstractTextPainter, GraphicsUtils
public class TextVerticalPainter extends AbstractTextPainter
{
public TextVerticalPainter()
{
this(false, true);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg)
{
this(wrapText, paintBg, 0);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg, int spacing)
{
this(wrapText, paintBg, spacing, false);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg, boolean calculate)
{
this(wrapText, paintBg, 0, calculate);
}
public TextVerticalPainter(boolean wrapText, boolean paintBg, int spacing, boolean calculate)
{
super(wrapText, paintBg, spacing, calculate);
}
public int getPreferredWidth(LayerCell cell, GC gc, IConfigRegistry configRegistry)
{
setupGCFromConfig(gc, CellStyleUtil.getCellStyle(cell, configRegistry));
return gc.textExtent(/*insertNewLine*/(convertDataType(cell, configRegistry))).x;
}
public int getPreferredHeight(LayerCell cell, GC gc, IConfigRegistry configRegistry)
{
setupGCFromConfig(gc, CellStyleUtil.getCellStyle(cell, configRegistry));
return getLengthFromCache(gc, /*insertNewLine*/(convertDataType(cell, configRegistry))) + spacing * + 1;
}
public void paintCell(LayerCell cell, GC gc, Rectangle rectangle, IConfigRegistry configRegistry)
{
if(paintBg)
{
super.paintCell(cell, gc, rectangle, configRegistry);
}
// if(paintFg)
{
Rectangle originalClipping = gc.getClipping();
gc.setClipping(rectangle.intersection(originalClipping));
net.sourceforge.nattable.style.IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
setupGCFromConfig(gc, cellStyle);
int fontHeight = gc.getFontMetrics().getHeight();
String text = convertDataType(cell, configRegistry);
text = insertNewLine(getTextToDisplay(cell, gc, rectangle.width, text));
int numberOfNewLines = getNumberOfNewLines(text);
int contentHeight = fontHeight * numberOfNewLines;
int contentToCellDiff = cell.getBounds().height - rectangle.height;
if(contentHeight > rectangle.height && calculate)
{
ILayer layer = cell.getLayer();
layer.doCommand(new RowResizeCommand(layer, cell.getRowPosition(), contentHeight + spacing * 2 + contentToCellDiff));
}
if(numberOfNewLines == 1)
{
int contentWidth = Math.min(getLengthFromCache(gc, text), rectangle.width);
gc.drawText(text, rectangle.x + CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, rectangle, contentWidth + spacing), rectangle.y + CellStyleUtil.getVerticalAlignmentPadding(cellStyle, rectangle, contentHeight + spacing), 3);
} else
{
int yStartPos = rectangle.y + CellStyleUtil.getVerticalAlignmentPadding(cellStyle, rectangle, contentHeight);
String lines[] = text.split("\n");
String as[];
int j = (as = lines).length;
for(int i = 0; i < j; i++)
{
String line = as[i];
int lineContentWidth = Math.min(getLengthFromCache(gc, line), rectangle.width);
gc.drawText(line, rectangle.x + CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, rectangle, lineContentWidth + spacing), yStartPos + spacing, 3);
yStartPos += fontHeight;
}
}
gc.setClipping(originalClipping);
}
}
protected void setNewMinLength(LayerCell cell, int contentHeight)
{
int cellLength = cell.getBounds().height;
if(cellLength < contentHeight)
{
ILayer layer = cell.getLayer();
int row = layer.getRowIndexByPosition(cell.getRowPosition());
layer.doCommand(new RowResizeCommand(layer, row, contentHeight));
}
}
public String insertNewLine(String txt)
{
String newtxt="";
for(int i=0;i<txt.length();i++)
newtxt+=txt.charAt(i)+"\n";
return newtxt;
}
protected int calculatePadding(LayerCell cell, int availableLength)
{
return cell.getBounds().height - availableLength;
}
}
When Test this custom Painter with example TextPainter_example it works as my expectation.
But I am acctually using NattableBuilder Extension and when I use this with NattableBuilder ,the header only shows signgle character.
I am using this code:
rightbuilder = new NatTableBuilder(...);
ColumnHeaderLayer columnHeaderLayer= rightbuilder.getColumnHeaderLayerStack().getColumnHeaderLayer();
columnHeaderLayer.addConfiguration(new DefaultColumnHeaderLayerConfiguration() {
protected void addColumnHeaderStyleConfig() {
addConfiguration(new DefaultColumnHeaderStyleConfiguration() {
{
cellPainter = new BeveledBorderDecorator(new TextVerticalPainter(true,true,true));
}
});
}
});
pls help
|
|
| | | |
Goto Forum:
Current Time: Sat Sep 21 05:29:15 GMT 2024
Powered by FUDForum. Page generated in 0.04201 seconds
|