Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Eclipse plugin Implementation( Sorting table column on clicking table header)
icon2.gif  Eclipse plugin Implementation [message #1698372] Mon, 15 June 2015 07:38 Go to next message
Amit Gupta is currently offline Amit GuptaFriend
Messages: 3
Registered: June 2015
Junior Member
Hi All,

I am getting error on implementing Sorting table on clicking Table Header in Swt.
getting null pointer exception. I am using table viewer column for creating table and try to sort it on clicking table Header of the column. Pleas find below my snippet...

tvAss1 = new TableViewer(composite1,SWT.FULL_SELECTION | SWT.BORDER);
tvAss1.setContentProvider( new RecetnlyAddContentProvider());
tvAss1.setLabelProvider( new RecentlyAddLabelProvider());

// Set up the table
final int tableWidth = 300;
final Table table = tvAss1.getTable();
table.setLayoutData(new GridData(GridData.FILL_BOTH));
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(25, 150, tableWidth, 200);

stC1 = new TableViewerColumn(tvAss1, SWT.NONE);
stC1.getColumn().setText("Component Name");
stC1.getColumn().setResizable(true);
stC1.getColumn().setToolTipText(COMPONENT_NAME);
stC1.getColumn().setWidth(tableWidth/3);
stC1.setLabelProvider(new LinkRecentColumnLabelProvider("72"));

TableColumn tc = new TableColumn(table, SWT.LEFT);
tc.setText(TYPE);
tc.setWidth(tableWidth / 3);

TableColumn tc1 = new TableColumn(table, SWT.LEFT | SWT.CHECK);
tc1.setText(DATE);
tc1.setWidth(tableWidth / 3);

table.setData("Asset Store");
tvAss1.setColumnProperties(PROPS);
tvAss1.refresh();
tvAss1.setInput(recentComponent);
table.setHeaderVisible(true);
table.setLinesVisible(true);
//TableUtil.packAndFillLastColumn(table);
//searchTableViewer.setContentProvider(new SearchDetailContentProvider());
TableColumn[] columns = tvAss1.getTable().getColumns();
for (final TableColumn column : columns) {
column.addListener(SWT.Selection, new Listener() {

@Override
public void handleEvent(Event arg0) {
int index = tvAss1.getTable().indexOf(column);
sortRecentAddTable(column, index);
}

});
}


private void sortRecentAddTable(final TableColumn column, final int index){
log.info("SortingRecentTable");
recentComparator.setColumn(index);
int dir = recentComparator.getDirection();
Collections.sort(csd1, recentComparator);
addData("first", first, last, prev, next, pageNoLabel, pageText1,pageText2, recordCount);
}



public class RecentTableComparator implements Comparator<RecentComponent>{

private int recentIndex;
private static final int DESCENDING =1;
private int direction = DESCENDING;

public RecentTableComparator() {
this.recentIndex = 0;
direction = DESCENDING;
}
public int getDirection(){
return direction == 1 ? SWT.DOWN : SWT.UP;
}
public void setColumn(int column){
if(column == this.recentIndex){
direction = 1 - direction;
}
else{
this.recentIndex = column;
direction = DESCENDING;
}
}


@Override
public int compare(RecentComponent Obj1, RecentComponent Obj2) {

int rc =0;
switch (recentIndex) {
case 0:
rc = Obj1.getComponentName().compareTo(Obj2.getComponentName());
break;
case 1:
rc = Obj1.getComponentType().compareTo(Obj2.getComponentType());
break;
case 2:
rc = Obj1.getPublishDate().compareTo(Obj2.getPublishDate());
break;
default:
rc=0;
}
if(direction == DESCENDING){
rc = -rc;
}
return rc;
}


}
Please guide me and provide me some solution. It will be very helpful for me.


Regards
Amit Gupta
Re: Eclipse plugin Implementation [message #1698550 is a reply to message #1698372] Tue, 16 June 2015 11:27 Go to previous messageGo to next message
Amit Gupta is currently offline Amit GuptaFriend
Messages: 3
Registered: June 2015
Junior Member

Hi Ravi ,

please find below... its my new implementation....

tvAss1 = new TableViewer(composite1,SWT.FULL_SELECTION | SWT.BORDER);
tvAss1.setContentProvider( new RecetnlyAddContentProvider());
tvAss1.setLabelProvider( new RecentlyAddLabelProvider());
final int tableWidth = 300;
stC1 = new TableViewerColumn(tvAss1, SWT.NONE);
stC1.getColumn().setText("Component Name");
stC1.getColumn().setResizable(true);
stC1.getColumn().setToolTipText(COMPONENT_NAME);
stC1.getColumn().setWidth(tableWidth/3);
stC1.setLabelProvider(new LinkRecentColumnLabelProvider("72"));

// Set up the table

final Table table = tvAss1.getTable();
table.setLayoutData(new GridData(GridData.FILL_BOTH));
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(25, 150, tableWidth, 200);



TableColumn column1 = new TableColumn(table, SWT.LEFT);
column1.setText(COMPONENT_NAME);
column1.setWidth(tableWidth / 3);

TableColumn column2 = new TableColumn(table, SWT.LEFT);
column2.setText(TYPE);
column2.setWidth(tableWidth / 3);

TableColumn column3 = new TableColumn(table, SWT.LEFT | SWT.CHECK);
column3.setText(DATE);
column3.setWidth(tableWidth / 3);

table.setData("Asset Store");
tvAss1.setColumnProperties(PROPS);
tvAss1.refresh();
tvAss1.setInput(recentComponent);
table.setHeaderVisible(true);
table.setLinesVisible(true);
//TableUtil.packAndFillLastColumn(table);
//searchTableViewer.setContentProvider(new SearchDetailContentProvider());
/*TableColumn[] columns = tvAss1.getTable().getColumns();
for (final TableColumn column : columns) {
column.addListener(SWT.Selection, new Listener() {

@Override
public void handleEvent(Event arg0) {
int index = tvAss1.getTable().indexOf(column);
sortRecentAddTable(column, index);
}

});
}*/
column1.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
// sort column 1
TableItem[] items = table.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 2; i < items.length; i++) {
String value1 = items[i].getText(0);
for (int j = 1; j < i; j++) {
String value2 = items[j].getText(0);
for (int k = 0; k < i; k++) {
String value3 = items[k].getText(0);
if (collator.compare(value1, value2) < 0) {
String[] values = { items[i].getText(0),
items[i].getText(1), items[i].getText(2)};
items[i].dispose();
TableItem item = new TableItem(table, SWT.NONE, k);
item.setText(values);
items = table.getItems();
break;
}
}
}
}
}

});

column2.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
// sort column 2
TableItem[] items = table.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(1);
for (int j = 0; j < i; j++) {
String value2 = items[j].getText(1);
for (int k = 0; k < i; k++) {
String value3 = items[k].getText(1);
if (collator.compare(value1, value2) < 0) {
String[] values = { items[i].getText(0),
items[i].getText(1), items[i].getText(2) };
items[i].dispose();
TableItem item = new TableItem(table, SWT.NONE, k);
item.setText(values);
items = table.getItems();
break;
}
}
}
}
}
});
column3.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
// sort column 2
TableItem[] items = table.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(2);
for (int j = 0; j < i; j++) {
String value2 = items[j].getText(2);
for (int k = 0; k < i; k++) {
String value3 = items[k].getText(2);
if (collator.compare(value1, value2) < 0) {
String[] values = { items[i].getText(0),
items[i].getText(1), items[i].getText(2) };
items[i].dispose();
TableItem item = new TableItem(table, SWT.NONE, j);
item.setText(values);
items = table.getItems();
break;
}
}
}
}
}
});
Re: Eclipse plugin Implementation [message #1698553 is a reply to message #1698550] Tue, 16 June 2015 11:47 Go to previous message
Amit Gupta is currently offline Amit GuptaFriend
Messages: 3
Registered: June 2015
Junior Member
If I commented below code--
/*TableColumn column1 = new TableColumn(table, SWT.LEFT);
column1.setText(COMPONENT_NAME);
column1.setWidth(tableWidth / 3);*/

I am getting error here...
in this line---- column1.addListener(SWT.Selection, new Listener() {

check it...
Previous Topic:Retrieve plugins
Next Topic:Download Problem
Goto Forum:
  


Current Time: Sun Jul 21 04:11:20 GMT 2019

Powered by FUDForum. Page generated in 0.03032 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top