How can to show the row number in Nebula NatTable - eclipse-rcp

I can show a CSV content to a NatTable using those code:
IDataProvider bodyDataProvider = ...;
DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
SelectionLayer selectionLayer = new SelectionLayer(bodyDataLayer);
ViewportLayer viewportLayer = new ViewportLayer(selectionLayer);
viewportLayer.setRegionName(GridRegion.BODY);
natTable.setLayer(viewportLayer);
How can we show the row number? I tried to used GridLayer but there are no column header. Please help!

I resolved this. I used CompositeLayer:
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
DataLayer rowHeaderDataLayer = new DataLayer(rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(
rowHeaderDataLayer,
viewportLayer,
selectionLayer);
CompositeLayer compositeLayer = new CompositeLayer(2, 1);
compositeLayer.setChildLayer(GridRegion.ROW_HEADER, rowHeaderLayer, 0, 0);
compositeLayer.setChildLayer(GridRegion.BODY, viewportLayer, 1, 0);
natTable.setLayer(compositeLayer);

Related

NatTable filtering row header layer for dynamic list

When I use dynamic list for Nattable the filtering via filter header layer it does not work for me.
I want to be able to filter all columns. I have respective code for each column in the filter row configuration class and it works if the input is a static list.
Attached my code snippet:
public Control createPeopleTable(Composite parent) {
// create a new ConfigRegistry which will be needed for GlazedLists
configRegistry = new ConfigRegistry();
peopleTableColumns = PeopleTableColumnDefinition.getPeopleTableColumns();
final Map<String, String> propertyToLabelMap = new HashMap<>();
final String[] propertyNames = new String[peopleTableColumns.size()];
for (int i = 0; i < peopleTableColumns.size(); ++i) {
final PeopleTableColumnDefinition col = peopleTableColumns.get(i);
propertyNames[i] = col.getProperty();
propertyToLabelMap.put(propertyNames[i], col.getDisplayName());
}
columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);columnPropertyAccessor = new ExtendedReflectiveColumnPropertyAccessor<>(propertyNames);
peopleList = new ArrayList<>();
eventList = GlazedLists.eventList(peopleList);
dynamicValues = GlazedLists.threadSafeList(eventList);
bodyLayerStack = new BodyLayerStack(dynamicValues, columnPropertyAccessor);
.....
IFilterStrategy<People> filterStrategy = new DefaultGlazedListsFilterStrategy<>(
bodyLayerStack.getFilterList(), columnPropertyAccessor, configRegistry);
// Note: The column header layer is wrapped in a filter row composite.
// This plugs in the filter row functionality
FilterRowHeaderComposite<People> filterRowHeaderLayer = new FilterRowHeaderComposite<>(filterStrategy,
sortHeaderLayer, bodyLayerStack.getBodyDataProvider(), configRegistry);
// build the row header layer
IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyLayerStack.getBodyDataProvider());
DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayerStack,
bodyLayerStack.getSelectionLayer());
// build the corner layer
IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider,
rowHeaderDataProvider);
DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, filterRowHeaderLayer);
// build the grid layer
GridLayer gridLayer = new GridLayer(bodyLayerStack, filterRowHeaderLayer, rowHeaderLayer, cornerLayer);
// turn the auto configuration off as we want to add our header menu
// configuration
natTable = new NatTable(parent, gridLayer, false);
.....
natTable.addConfiguration(new FilterRowConfiguration());
natTable.configure();
...
return natTable;
}
class FilterRowConfiguration extends AbstractRegistryConfiguration {
#Override
public void configureRegistry(IConfigRegistry configRegistry) {
// register the FilterRowTextCellEditor in the first column which
// immediately commits on key press
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new FilterRowTextCellEditor(),
DisplayMode.NORMAL, FilterRowDataLayer.FILTER_ROW_COLUMN_LABEL_PREFIX
+ PeopleTableColumnDefinition.Name.columnIndex());
.....
}
class BodyLayerStack extends AbstractLayerTransform {
private FilterList<People> filterList;
private final SelectionLayer selectionLayer;
private SortedList<People> sortedList;
private ViewportLayer viewportLayer;
public BodyLayerStack(EventList<People> values,
IColumnPropertyAccessor<People> columnPropertyAccessor) {
// use the SortedList constructor with 'null' for the Comparator
// because the Comparator
// will be set by configuration
sortedList = new SortedList<>(values, null);
// wrap the SortedList with the FilterList
filterList = new FilterList<>(sortedList);
peopleListDataProvider = new PeopleListDataProvider<>(filterList, columnPropertyAccessor);
bodyDataLayer = new DataLayer(peopleListDataProvider);
final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(
bodyDataLayer);
for (int i = 0; i < peopleTableColumns.size(); i++) {
PeopleTableColumnDefinition peopleTableColumnDefinition = peopleTableColumns.get(i);
columnLabelAccumulator.registerColumnOverrides(i, peopleTableColumnDefinition.getTableLabel());
}
bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
// layer for event handling of GlazedLists and PropertyChanges
GlazedListsEventLayer<People> glazedListsEventLayer = new GlazedListsEventLayer<>(bodyDataLayer,
filterList);
selectionLayer = new SelectionLayer(glazedListsEventLayer);
viewportLayer = new ViewportLayer(selectionLayer);
FreezeLayer freezeLayer = new FreezeLayer(selectionLayer);
compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer, viewportLayer, selectionLayer);
setUnderlyingLayer(compositeFreezeLayer);
}
public SortedList<People> getSortedList() {
return sortedList;
}
public SelectionLayer getSelectionLayer() {
return this.selectionLayer;
}
public FilterList<People> getFilterList() {
return this.filterList;
}
public PeopleListDataProvider<People> getBodyDataProvider() {
return peopleListDataProvider;
}
}
public void setListOfPeople(List<People> listOfPeople) {
this.eventList.getReadWriteLock().writeLock().lock();
this.dynamicValues.getReadWriteLock().writeLock().lock();
try {
peopleList.clear();
eventList.clear();
dynamicValues.clear();
peopleList.addAll(listOfPeople);
eventList.addAll(listOfPeople);
dynamicValues.addAll(listOfPeople);
peopleListDataProvider.setPeopleList(peopleList);
} finally {
eventList.getReadWriteLock().writeLock().unlock();
dynamicValues.getReadWriteLock().writeLock().unlock();
}
}
It seems you haven't understood the principles of GlazedLists and Java object references.
First you don't have to call clear() and addAll() on all lists. The GlazedLists are views on the base list, so it should be enough to call on the EventList or a higher list like the FilterList.
Second mistake is that you set another list on the IDataProvider. And that list is not the FilterList. So actually you disable the filter as the FilterList is doing the filter magic. And setting the list is not necessary as you performed a list modification. So why changing it then?

ITextSharp v5.5.13.0 XMLWorker Turkish character problem

I used iTextSharp and all Turkish character disappeared.
Also html inline css attributes work on table element but not working on div element.
I tried lots of encoding convert sample code but not found any results.
My sample code:
public static byte[] HtmlToPdfItextSharp(string HTMLCONTENTSTRING, List<string> cssFiles = null)
{
using (var ms = new MemoryStream())
{
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 10, 10);
BaseFont STF_Helvetica_Turkish = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "CP1254", BaseFont.NOT_EMBEDDED);
Font fontNormal = new Font(STF_Helvetica_Turkish, 12, Font.NORMAL, BaseColor.BLACK);
string fontPath = Path.Combine(Path.Combine(Server.MapPath("~/App_Data/Pdf/arial.ttf")));
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.UseUnicode = true;
fontProvider.Register(fontPath);
CssAppliers ca = new CssAppliersImpl(fontProvider);
var pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
pdfWriter.DirectContent.SetFontAndSize(STF_Helvetica_Turkish, 12);
pdfWriter.CloseStream = false;
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssFiles.ForEach(e => cssResolver.AddCssFile(e, true));
var pp = new PdfWriterPipeline(pdfDoc, pdfWriter);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, pp));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(worker);
parser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(HTMLCONTENTSTRING)));
pdfDoc.Close();
return ms.GetBuffer();
}
}
I updated my code and add stylesheet file (with font familiy:arial;) and i solved character
but it takes too long time
My new updated function like:
public static byte[] HtmlToPdfItextSharp(string HTMLCONTENTSTRING, List<string> cssFiles = null)
{
using (var ms= new MemoryStream())
{
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 7, 10);
var pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);
pdfWriter.CloseStream = false;
pdfDoc.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssFiles.ForEach(e => cssResolver.AddCssFile(e, true));
var pp = new PdfWriterPipeline(pdfDoc, pdfWriter);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, pp));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(worker);
parser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(pHtmlIcerik)));
pdfDoc.Close();
return ms.ToArray();
}
}
Css code :
body {
font-family:Arial;
}
table{
font-family:Arial;
}
td{
font-family:Arial;
}

Duplicate Fields using itextsharp

I have this code to create TextFields
public void MssCreateTextField(byte[] ssPdf, RCRectangleRecord ssRectangle, string ssName, int ssFontSize, string ssValue, int ssPage, out byte[] ssPdfOut, bool ssIsMultiline) {
PdfReader reader = new PdfReader(ssPdf);
ssPdfOut = null;
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
/*TextField tField = new TextField(stamper.Writer, new iTextSharp.text.Rectangle((float)ssRectangle.ssSTRectangle.ssllx, (float)ssRectangle.ssSTRectangle.sslly, (float)ssRectangle.ssSTRectangle.ssurx, (float)ssRectangle.ssSTRectangle.ssury), ssName);
if (ssValue!="")
tField.Text = ssValue;
if (ssIsMultiline)
tField.Options = TextField.MULTILINE;
tField.FontSize = ssFontSize;*/
PdfFormField tField = PdfFormField.CreateTextField(stamper.Writer, ssIsMultiline, false, 50);
tField.FieldName = ssName;
tField.SetWidget(new iTextSharp.text.Rectangle((float)ssRectangle.ssSTRectangle.ssllx, (float)ssRectangle.ssSTRectangle.sslly, (float)ssRectangle.ssSTRectangle.ssurx, (float)ssRectangle.ssSTRectangle.ssury), PdfAnnotation.HIGHLIGHT_TOGGLE);
stamper.FormFlattening = false;
stamper.AddAnnotation(tField, ssPage);
stamper.Close();
reader.Close();
ssPdfOut = output.ToArray();
}
As you can see i have some code commented as an alternative but the two different ways are producing the same result.
What i am trying to achieve is create two textfields with the same name to when editing one it edits the others two. This two codes do that (in the browsers and pdfescape site) excepting in the adobe acrobat reader. In the acrobat reader i get just the first field visible and the others hidden i dont know why...
If you want to add two text field visualizations which represent the same content, you have to add them as two widgets of the same field and not two distinct fields, e.g. like this:
public void CreateDoubleTextField(byte[] ssPdf, Rectangle ssRectangle1, Rectangle ssRectangle2, string ssName, int ssFontSize, string ssValue, int ssPage, out byte[] ssPdfOut, bool ssIsMultiline)
{
PdfReader reader = new PdfReader(ssPdf);
ssPdfOut = null;
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
PdfFormField tField = PdfFormField.CreateTextField(stamper.Writer, ssIsMultiline, false, 50);
tField.FieldName = ssName;
PdfFormField widget1 = PdfFormField.CreateEmpty(stamper.Writer);
widget1.SetWidget(ssRectangle1, PdfAnnotation.HIGHLIGHT_TOGGLE);
PdfFormField widget2 = PdfFormField.CreateEmpty(stamper.Writer);
widget2.SetWidget(ssRectangle2, PdfAnnotation.HIGHLIGHT_TOGGLE);
tField.AddKid(widget1);
tField.AddKid(widget2);
stamper.FormFlattening = false;
stamper.AddAnnotation(tField, ssPage);
stamper.Close();
reader.Close();
ssPdfOut = output.ToArray();
}
(As I don't have that RCRectangleRecord, I use the iTextSharp Rectangle class as argument.)
This gives you two field visualizations in Adobe Acrobat Reader; after editing one of them and switching focus (e.g. clicking somewhere outside that visualization), the other visualization duplicates the content.
Now i have this and i can create two fields when the list has more than one Rectangle but for some reason i dont know how the two fields appears without the name!!
PdfReader reader = new PdfReader(ssPdf);
ssPdfOut = null;
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
TextField tField;
if (ssRectangle.Count==1){
tField= new TextField(stamper.Writer, new iTextSharp.text.Rectangle((float)ssRectangle[0].ssSTRectangle.ssllx, (float)ssRectangle[0].ssSTRectangle.sslly, (float)ssRectangle[0].ssSTRectangle.ssurx, (float)ssRectangle[0].ssSTRectangle.ssury), ssName);
if (ssValue!="")
tField.Text = ssValue;
if (ssIsMultiline)
tField.Options = TextField.MULTILINE;
tField.FontSize = ssFontSize;
tField.FieldName = ssName;
stamper.AddAnnotation(tField.GetTextField(), ssPage);
}
else
{
PdfFormField PtField = PdfFormField.CreateTextField(stamper.Writer, ssIsMultiline, false, 250);
PtField.Name=ssName;
foreach (RCRectangleRecord item in ssRectangle)
{
/*
tField=new TextField(stamper.Writer, new iTextSharp.text.Rectangle((float)ssRectangle[0].ssSTRectangle.ssllx, (float)ssRectangle[0].ssSTRectangle.sslly, (float)ssRectangle[0].ssSTRectangle.ssurx, (float)ssRectangle[0].ssSTRectangle.ssury), ssName);
tField.FieldName = ssName;
PtField.AddKid(tField.GetTextField());*/
PdfFormField widget = PdfFormField.CreateEmpty(stamper.Writer);
widget.SetWidget(new Rectangle((float)item.ssSTRectangle.ssllx, (float)item.ssSTRectangle.sslly, (float)item.ssSTRectangle.ssurx, (float)item.ssSTRectangle.ssury), PdfAnnotation.HIGHLIGHT_TOGGLE);
widget.Name = ssName;
PtField.AddKid(widget);
}
stamper.AddAnnotation(PtField, ssPage);
}
stamper.FormFlattening = false;
stamper.Close();
reader.Close();
ssPdfOut = output.ToArray();

SWT CTabFolder doesn't display when running from eclipse

I am creating an app using WindowBuilder in eclipse 4.4.2. I've added CTabFolders and corresponding CTabItems. The tabs are displayed properly when Click the preview button(Quickly test/preview the window without compiling or running it) in windowbuilder.
Screen Shot===> http://i.imgur.com/Sx9TQrL.jpg
But the real problem is when I run the app,the tabs are not displayed.
Screen Shot===> http://i.imgur.com/OP7WY8W.jpg
Code
public class Dashboard {
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setImage(SWTResourceManager.getImage(Dashboard.class,
"/com/sun/java/swing/plaf/windows/icons/Computer.gif"));
shell.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BORDER));
shell.setSize(1080, 700);
shell.setLayout(new GridLayout(2, false));
/**
* Base Layout
* **/
Composite content = new Composite(shell, SWT.BORDER);
content.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
content.setLayout(new StackLayout());
// content.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
// false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CTabFolder tabFolder = new CTabFolder(content, SWT.CLOSE);
tabFolder.setTabHeight(28);
tabFolder.setSimple(false);
tabFolder.setSelectionForeground(SWTResourceManager
.getColor(SWT.COLOR_BLACK));
tabFolder.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
CTabItem homeTab = new CTabItem(tabFolder, SWT.NONE);
homeTab.setText("Home");
Composite composite_4 = new Composite(tabFolder, SWT.NONE);
homeTab.setControl(composite_4);
Label lblHome = new Label(composite_4, SWT.NONE);
lblHome.setBounds(185, 105, 55, 15);
lblHome.setText("Home");
final CTabItem empTab = new CTabItem(tabFolder, SWT.CLOSE);
empTab.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
empTab.setText("Employees");
Composite composite_5 = new Composite(tabFolder, SWT.NONE);
empTab.setControl(composite_5);
Label lblEmployees = new Label(composite_5, SWT.NONE);
lblEmployees.setBounds(155, 102, 55, 15);
lblEmployees.setText("Employees");
CTabItem workTab = new CTabItem(tabFolder, SWT.CLOSE);
workTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/worksite.png"));
workTab.setText("Work Site");
Composite composite_6 = new Composite(tabFolder, SWT.NONE);
workTab.setControl(composite_6);
Label lblWorkSite = new Label(composite_6, SWT.NONE);
lblWorkSite.setBounds(222, 123, 55, 15);
lblWorkSite.setText("Work site");
CTabItem paymentTab = new CTabItem(tabFolder, SWT.NONE);
paymentTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/payments.png"));
paymentTab.setText("Payments");
Composite composite_7 = new Composite(tabFolder, SWT.NONE);
paymentTab.setControl(composite_7);
Label lblPayments = new Label(composite_7, SWT.NONE);
lblPayments.setBounds(185, 176, 55, 15);
lblPayments.setText("Payments");
CTabItem toolsTab = new CTabItem(tabFolder, SWT.NONE);
toolsTab.setImage(SWTResourceManager.getImage(Dashboard.class, "/resource/icons/tools.png"));
toolsTab.setShowClose(true);
toolsTab.setText("Tools");
Composite composite_8 = new Composite(tabFolder, SWT.NONE);
toolsTab.setControl(composite_8);
Label lblTools = new Label(composite_8, SWT.NONE);
lblTools.setBounds(264, 125, 55, 15);
lblTools.setText("Tools");
/**
* MenuBar
* **/
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setImage(null);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
mntmExit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/close.png"));
mntmExit.setText("Exit");
MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
mntmHelp.setText("Help");
Menu menu_2 = new Menu(mntmHelp);
mntmHelp.setMenu(menu_2);
MenuItem mntmAbout = new MenuItem(menu_2, SWT.NONE);
mntmAbout.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
About abdialog = new About(shell, 0);
abdialog.open();
}
});
mntmAbout.setText("About");
Composite sidebar = new Composite(shell, SWT.NONE);
sidebar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
1));
sidebar.setBackground(SWTResourceManager.getColor(112, 128, 144));
sidebar.setLayout(new GridLayout(1, false));
ExpandBar expandBar = new ExpandBar(sidebar, SWT.NONE);
expandBar.setBackground(SWTResourceManager.getColor(112, 128, 144));
expandBar.setTouchEnabled(true);
GridData gd_expandBar = new GridData(SWT.CENTER, SWT.CENTER, false,
false, 1, 1);
gd_expandBar.heightHint = 619;
expandBar.setLayoutData(gd_expandBar);
ExpandItem xpndtmEmployeeDetails = new ExpandItem(expandBar, SWT.NONE,
0);
xpndtmEmployeeDetails.setExpanded(true);
xpndtmEmployeeDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
xpndtmEmployeeDetails.setText("Employee Details");
Composite expandbarComposite = new Composite(expandBar, SWT.NONE);
xpndtmEmployeeDetails.setControl(expandbarComposite);
xpndtmEmployeeDetails.setHeight(100);
expandbarComposite.setLayout(new FormLayout());
expandbarComposite.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
Button btnAddNew = new Button(expandbarComposite, SWT.NONE);
btnAddNew.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew = new FormData();
fd_btnAddNew.right = new FormAttachment(100, -10);
fd_btnAddNew.left = new FormAttachment(0, 38);
btnAddNew.setLayoutData(fd_btnAddNew);
btnAddNew.setText("Add New");
Button btnEdit = new Button(expandbarComposite, SWT.FLAT | SWT.CENTER);
btnEdit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
btnEdit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
fd_btnAddNew.bottom = new FormAttachment(btnEdit, -6);
FormData fd_btnEdit = new FormData();
fd_btnEdit.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
fd_btnEdit.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnEdit.bottom = new FormAttachment(100);
btnEdit.setLayoutData(fd_btnEdit);
btnEdit.setText("Edit");
/**
* View Employees Button
* **/
Button btnViewEmployees = new Button(expandbarComposite, SWT.NONE);
btnViewEmployees.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
tabFolder.setSelection(empTab);
}
});
FormData fd_btnViewEmployees = new FormData();
fd_btnViewEmployees.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnViewEmployees.top = new FormAttachment(0, 6);
fd_btnViewEmployees.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
btnViewEmployees.setLayoutData(fd_btnViewEmployees);
btnViewEmployees.setText("View Employees");
ExpandItem xpndtmWorkSiteDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/worksite.png"));
xpndtmWorkSiteDetails.setText("Work Site Details");
Composite composite = new Composite(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setControl(composite);
xpndtmWorkSiteDetails.setHeight(100);
composite.setLayout(new FormLayout());
Button btnAddNew_1 = new Button(composite, SWT.NONE);
btnAddNew_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_1 = new FormData();
fd_btnAddNew_1.right = new FormAttachment(100, -10);
fd_btnAddNew_1.top = new FormAttachment(0, 37);
btnAddNew_1.setLayoutData(fd_btnAddNew_1);
btnAddNew_1.setText("Add New");
Button btnEdit_1 = new Button(composite, SWT.NONE);
fd_btnAddNew_1.bottom = new FormAttachment(btnEdit_1, -6);
fd_btnAddNew_1.left = new FormAttachment(btnEdit_1, 0, SWT.LEFT);
btnEdit_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEdit_1 = new FormData();
fd_btnEdit_1.left = new FormAttachment(0, 40);
fd_btnEdit_1.right = new FormAttachment(100, -10);
fd_btnEdit_1.bottom = new FormAttachment(100);
btnEdit_1.setLayoutData(fd_btnEdit_1);
btnEdit_1.setText("Edit");
Button btnViewWorks = new Button(composite, SWT.NONE);
FormData fd_btnViewWorks = new FormData();
fd_btnViewWorks.top = new FormAttachment(0, 6);
fd_btnViewWorks.left = new FormAttachment(btnAddNew_1, 0, SWT.LEFT);
fd_btnViewWorks.right = new FormAttachment(100, -10);
btnViewWorks.setLayoutData(fd_btnViewWorks);
btnViewWorks.setText("View Works");
ExpandItem xpndtmPaymentDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmPaymentDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/payments.png"));
xpndtmPaymentDetails.setText("Payment Details");
Composite composite_1 = new Composite(expandBar, SWT.NONE);
xpndtmPaymentDetails.setControl(composite_1);
xpndtmPaymentDetails.setHeight(100);
composite_1.setLayout(new FormLayout());
Button btnAddNew_2 = new Button(composite_1, SWT.NONE);
btnAddNew_2.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_2 = new FormData();
fd_btnAddNew_2.left = new FormAttachment(0, 44);
btnAddNew_2.setLayoutData(fd_btnAddNew_2);
btnAddNew_2.setText("Add New");
Button btnEditPayments = new Button(composite_1, SWT.NONE);
fd_btnAddNew_2.bottom = new FormAttachment(btnEditPayments, -6);
fd_btnAddNew_2.right = new FormAttachment(btnEditPayments, 0, SWT.RIGHT);
btnEditPayments.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEditPayments = new FormData();
fd_btnEditPayments.left = new FormAttachment(0, 44);
fd_btnEditPayments.right = new FormAttachment(100, -10);
fd_btnEditPayments.bottom = new FormAttachment(100);
btnEditPayments.setLayoutData(fd_btnEditPayments);
btnEditPayments.setText("Edit Payments");
Button btnViewPayments = new Button(composite_1, SWT.NONE);
FormData fd_btnViewPayments = new FormData();
fd_btnViewPayments.right = new FormAttachment(btnAddNew_2, 0, SWT.RIGHT);
fd_btnViewPayments.top = new FormAttachment(0, 6);
fd_btnViewPayments.left = new FormAttachment(btnAddNew_2, 0, SWT.LEFT);
btnViewPayments.setLayoutData(fd_btnViewPayments);
btnViewPayments.setText("View Payments");
ExpandItem xpndtmOurTools = new ExpandItem(expandBar, SWT.NONE);
xpndtmOurTools.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/tools.png"));
xpndtmOurTools.setText("Tools Available");
Composite composite_3 = new Composite(expandBar, SWT.NONE);
xpndtmOurTools.setControl(composite_3);
xpndtmOurTools.setHeight(80);
composite_3.setLayout(new FormLayout());
Button btnViewTools = new Button(composite_3, SWT.NONE);
FormData fd_btnViewTools = new FormData();
fd_btnViewTools.top = new FormAttachment(0, 10);
fd_btnViewTools.right = new FormAttachment(100, -10);
fd_btnViewTools.left = new FormAttachment(100, -131);
btnViewTools.setLayoutData(fd_btnViewTools);
btnViewTools.setText("View Tools");
Button btnAddNew_3 = new Button(composite_3, SWT.NONE);
btnAddNew_3.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Add tools");
}
});
FormData fd_btnAddNew_3 = new FormData();
fd_btnAddNew_3.right = new FormAttachment(btnViewTools, 0, SWT.RIGHT);
fd_btnAddNew_3.top = new FormAttachment(btnViewTools, 6);
fd_btnAddNew_3.left = new FormAttachment(btnViewTools, 0, SWT.LEFT);
btnAddNew_3.setLayoutData(fd_btnAddNew_3);
btnAddNew_3.setText("Add New");
ExpandItem xpndtmReports = new ExpandItem(expandBar, SWT.NONE);
xpndtmReports.setExpanded(true);
xpndtmReports.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/reports.png"));
xpndtmReports.setText("Reports");
xpndtmReports.setHeight(80);
Composite composite_2 = new Composite(expandBar, SWT.NONE);
xpndtmReports.setControl(composite_2);
composite_2.setLayout(new FormLayout());
Button btnGenerateReport = new Button(composite_2, SWT.NONE);
btnGenerateReport.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/gen_report.png"));
FormData fd_btnGenerateReport = new FormData();
fd_btnGenerateReport.top = new FormAttachment(0, 10);
fd_btnGenerateReport.left = new FormAttachment(0, 10);
fd_btnGenerateReport.right = new FormAttachment(100, -28);
btnGenerateReport.setLayoutData(fd_btnGenerateReport);
btnGenerateReport.setText("Generate Report");
xpndtmReports.setHeight(100);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Can anyone suggest a solution. Thanks in advance.
And Finally I've figured the problem in the code. The composite in the base layout was assigned with the StackLayout as follows.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new StackLayout());
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
When I changed the StackLayout to GridLayout, I got a working solution.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Unable to left-align nested tables inside a cell

the following code does produce the table and the nested table.
However, the nested table is always aligned in the middle.
I have no clue how to achieve horizontal alignment properly.
The following method is simply looping through a list.
when it's a simple question, I will add an TextField.
if it's a question containing multiple answers, i will inject a nested table with the checkboxes and the values.
private PdfPTable CreateQuestionTable(RLQuestionRecordList questions)
{
PdfPCell cell;
PdfPTable table = new PdfPTable(2);
//table.SetWidths(new int[]{ 50, 50 });
table.WidthPercentage = 100;
table.SpacingBefore = 20;
table.SpacingAfter = 20;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
foreach (RCQuestionRecord q in questions)
{
//add question to the table
cell = new PdfPCell(new Phrase(q.ssSTQuestion.ssName, _Normal));
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
table.AddCell(cell);
//add answer to the table.
//add generate time we don;t know where the table will be,
//hence textfields will be generated after the pdf has been generated..
if (q.ssSTQuestion.ssListOfAnswers.Length > 0)
{
// we have radiobuttons, so we add a table inside the cell
cell = new PdfPCell();
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
//we cannot align the table to the left in the cell for some weird reason...
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.AddElement(CreateCheckboxTable(q));
table.AddCell(cell);
}
else
{
// we have simple textfield, so add that to the cell
cell = new PdfPCell();
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 5.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
//simple textfield
cell.CellEvent = new OOMTextField(string.Format("question_{0}", q.ssSTQuestion.ssQuestionId), q.ssSTQuestion.ssLength, q.ssSTQuestion.ssValue, bf);
table.AddCell(cell);
}
}
return table;
}
This is the nested table I want to insert into the cell above.
/// <summary>
///
/// </summary>
/// <param name="question"></param>
/// <returns></returns>
private PdfPTable CreateCheckboxTable(RCQuestionRecord question)
{
PdfPCell cell;
int numCells = question.ssSTQuestion.ssListOfAnswers.Length;
PdfPTable table = new PdfPTable(numCells);
float[] widths = new float[numCells];
int currentColumn = 0;
//table.SetWidths(new int[]{ 50, 50 });
foreach (RCAnswerRecord a in question.ssSTQuestion.ssListOfAnswers) {
//checkbox
cell = new PdfPCell(new Phrase(a.ssSTAnswer.ssLabel, _Normal));
cell.Border = Rectangle.NO_BORDER;
cell.Padding = 0.0f;
cell.PaddingLeft = 20.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_CENTER;
cell.CellEvent = new OOMCheckBox(string.Format("question_{0}", question.ssSTQuestion.ssQuestionId), a.ssSTAnswer.ssIsSelected, a.ssSTAnswer.ssLabel, bf);
//checkbox
table.AddCell(cell);
widths[currentColumn++] = 20.0f + bf.GetWidthPoint(a.ssSTAnswer.ssLabel, 11);
}
table.SetTotalWidth(widths);
table.LockedWidth = true;
table.SpacingBefore = 0;
return table;
}
What am I missing to align the nested tables totally to the left inside the cell?
Please take a look at the PDF document named nested_tables_aligned.pdf:
The outer table in this example has three columns and one row. Each cell in this outer table contains an inner table. The first inner table is left aligned, the second one is center aligned, the third one is right aligned.
This PDF was created with the Java example named NestedTablesAligned:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
float[] columnWidths = {200f, 200f, 200f};
PdfPTable table = new PdfPTable(columnWidths);
table.setTotalWidth(600f);
table.setLockedWidth(true);
buildNestedTables(table);
document.add(table);
document.close();
}
private void buildNestedTables(PdfPTable outerTable) {
PdfPTable innerTable1 = new PdfPTable(1);
innerTable1.setTotalWidth(100f);
innerTable1.setLockedWidth(true);
innerTable1.setHorizontalAlignment(Element.ALIGN_LEFT);
innerTable1.addCell("Cell 1");
innerTable1.addCell("Cell 2");
outerTable.addCell(innerTable1);
PdfPTable innerTable2 = new PdfPTable(2);
innerTable2.setTotalWidth(100f);
innerTable2.setLockedWidth(true);
innerTable2.setHorizontalAlignment(Element.ALIGN_CENTER);
innerTable2.addCell("Cell 3");
innerTable2.addCell("Cell 4");
outerTable.addCell(innerTable2);
PdfPTable innerTable3 = new PdfPTable(2);
innerTable3.setTotalWidth(100f);
innerTable3.setLockedWidth(true);
innerTable3.setHorizontalAlignment(Element.ALIGN_RIGHT);
innerTable3.addCell("Cell 5");
innerTable3.addCell("Cell 6");
outerTable.addCell(innerTable3);
}
As you can see, I define the alignment of the table at the level of the table, not at the level of the cell. The sample principle applies to iTextSharp. In you C# example, you define the aligned for the PdfPCell instead of for the PdfPTable. Change this and your problem will be solved.