Tuesday, April 26, 2011

Re: CellTable sorting : sorting several columns ?

Thanks for your reply.
But how do you get the name or type of a column ??
So far this is what i got, trying to implement your idea :

private class SortDecorator implements ColumnSortEvent.Handler {
        private ColumnSortEvent.Handler innerHandler;
   

        TextColumn<DemandeBean> autoriteColumn;
       
        //public SortDecorator(ColumnSortEvent.Handler innerHandler,
        public SortDecorator( com.google.gwt.user.cellview.client.ColumnSortEvent.Handler handler,
                TextColumn<DemandeBean> colonnetriee
                ) {
            this.autoriteColumn = colonnetriee;
            this.innerHandler = handler;
            GWT.log("entrée dans SortDecorator");
        }
       
        //@Override
        public void onColumnSort(ColumnSortEvent event) {
            GWT.log("entrée dans SortDecorator.onColumnSort");
            GWT.log("entrée dans SortDecorator.onColumnSort 2");
            innerHandler.onColumnSort(event);
            //tableHeader.redrawHeaders();
           
           
            GWT.log("entrée dans SortDecorator.onColumnSort 3");
            if (event.getColumn().equals("autoriteColumn")) {
                GWT.log("entrée dans SortDecorator.onColumnSort 4");
                List<DemandeBean> newData = new ArrayList(demandeTable.getVisibleItems());
                 if (event.isSortAscending()) {
                        GWT.log("Ordre ascendant");
                     Collections.sort(newData, new Comparator<DemandeBean>() {
                            public int compare(DemandeBean o1, DemandeBean o2) {
                                if (o1 == o2) {
                                    return 0;            
                                    }                          
                                if (o1 != null) {          
                                    return (o2 != null) ? o1.getAutorite().compareTo(o2.getAutorite()) : 1;           
                                    }            
                                return -1;       
                                }
                            }
                     );                         
                 } else {
                     GWT.log("Ordre descendant");
                     Collections.sort(newData, new Comparator<DemandeBean>() {
                            public int compare(DemandeBean o1, DemandeBean o2) {
                                if (o1 == o2) {
                                    return 0;            
                                    }                          
                                if (o1 != null) {          
                                    return (o2 != null) ? o2.getAutorite().compareTo(o1.getAutorite()) : 1;           
                                    }            
                                return -1;       
                                }
                            }
                     );
                 }
                 Range range = demandeTable.getVisibleRange();
                int start = range.getStart();
                 demandeTable.setRowData(start, newData);
               
            }
//            else if (event.getColumn().equals(column2)) {
//            }
        }


The call is the following :
        demandeTable.addColumnSortHandler(new SortDecorator(new ColumnSortEvent.Handler(), autoriteColumn) );

the call is incorrect since i get the following error message :
Cannot instantiate the type ColumnSortEvent.Handler

Can you give me a more detailed explanation or a basic sample ?
Thanks

On Mon, Apr 25, 2011 at 3:40 PM, SVR <svrnyc@gmail.com> wrote:
I didnt go through your code :-), but you can create a new class that implements
ColumnSortEvent.Handler and add it using addColumnSortHandler().
You can pass the columns to the class constructor and onColumnSort
you can check the column being sorted like so:
if (event.getColumn().equals(column1)) {
}
else if (event.getColumn().equals(column2)) {
}
and so on.
Hope this helps.


On Fri, Apr 22, 2011 at 1:36 PM, Celinio <cel975@gmail.com> wrote:
Hi,
I am using GWT 2.2 and the new CellTable sorting features, as described here :
http://code.google.com/intl/fr-FR/webtoolkit/doc/latest/DevGuideUiCellTable.html

There is a strange error (Umbrella exception, nothing else) when I use the columnSortHandler.setComparator(...) method.
I commented it out.

If i use the addColumnSortHandler(new ColumnSortEvent.Handler() {  ...} method, it works.

However on one column only. I want to be able to sort the table, on whichever column i click on.

So i'm stuck :
either i figure out what's wrong with the setComparator(..) method
or i use the
addColumnSortHandler(...) and in that case i need to figure out how to retrieve the name of the column
that is currently sorted.
Does anyone know what could be wrong ?
Thanks

Here is the code :
package com.francetelecom.voltage.gestion.presentation.client.mvp.view.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.ft.v.g.presentation.client.GwtApplication;
import com.ft.v.g.presentation.client.mvp.presenter.ListPresenter;
import com.ft.v.g.presentation.client.mvp.view.ListView;
import com.ft.v.g.presentation.client.ui.RecherchePanel;
import com.ft.v.g.presentation.client.ui.SimpleHasFeedback;
import com.ft.v.model.DemandeBean;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.RangeChangeEvent;
import com.google.gwt.view.client.RangeChangeEvent.Handler;


public class DemandesListViewImpl extends Composite implements
ListView<DemandeBean> {
   

    private RecherchePanel rp = new RecherchePanel();

    private final CellTable<DemandeBean> demandeTable;
    private final SimplePager pager;
    private final static int PAGE_SIZE = 7;
    private List<DemandeBean> demandes;
    private SimpleHasFeedback feedback;

    private ListPresenter<DemandeBean> presenter;

    public DemandesListViewImpl() {
       


        feedback = new SimpleHasFeedback();
        // Create a CellTable.
        demandeTable = new CellTable<DemandeBean>();
        demandeTable.getElement().setId("marketlist");
        demandeTable.setPageSize(PAGE_SIZE);
        demandeTable.setRowCount(0, true);
       
        demandeTable.addRangeChangeHandler(new Handler() {

            public void onRangeChange(RangeChangeEvent event) {
    
                Range range = demandeTable.getVisibleRange();
                int start = range.getStart();
                int length = range.getLength();
                List<DemandeBean> toSet = new ArrayList<DemandeBean>(length);
                for (int i = start; i < start + length && i < demandes.size(); i++)
                    toSet.add(demandes.get(i));
                demandeTable.setRowData(start, toSet);

            }
        });

        // Create a Pager to control the table.
        SimplePager.Resources pagerResources = GWT
                .create(SimplePager.Resources.class);
        pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,
                true);
        pager.setDisplay(demandeTable);

        // Create id column.
        TextColumn<DemandeBean> idColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {
                //return demande.getId().toString();               
                return String.valueOf(demande.getIdDemande());
            }

        };

        // renderer for nameColumn
        SafeHtmlRenderer<String> nameColumnRenderer = new AbstractSafeHtmlRenderer<String>() {

            public SafeHtml render(String object) {
            
                SafeHtmlBuilder builder = new SafeHtmlBuilder();
                builder.appendHtmlConstant("<a>");
                builder.appendEscaped(object);
                builder.appendHtmlConstant("</a>");
                return builder.toSafeHtml();
            }

        };
       
        // Create id column.
        TextColumn<DemandeBean> etatColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {
                return demande.getEtatDemande().getNomCourt().toString();               
            }

        };
       

        // Date réception
        Column<DemandeBean, String> dateReceptionColumn = new Column<DemandeBean, String>(
                new ClickableTextCell(nameColumnRenderer)) {

            @Override
            public String getValue(DemandeBean demandeBean) {
               
                //String dateReception = demandeBean.getDateReception().toString();
                //Date converti = VoltageUtils.convertirFormatDate(dateReception);
               
                return demandeBean.getDateReception().toString();
                //return converti.toString();
            }
        };
       
       
/*
        // Create name column.
        Column<DemandeBean, String> nameColumn = new Column<DemandeBean, String>(
                new ClickableTextCell(nameColumnRenderer)) {

            @Override
            public String getValue(DemandeBean demandeBean) {
                return demandeBean.getName();
            }
        };

        nameColumn.setFieldUpdater(new FieldUpdater<DemandeBean, String>() {
            public void update(int index, DemandeBean demandeBean, String value) {
                if (presenter != null) {
                    presenter.onEditClicked(demandeBean);
                }
            }
        }

        );
*/       

        // renderer for nameColumn
        SafeHtmlRenderer<String> deleteColumnRenderer = new AbstractSafeHtmlRenderer<String>() {

            public SafeHtml render(String object) {
                SafeHtmlBuilder builder = new SafeHtmlBuilder();
           
                builder.appendHtmlConstant("<a><img src=\"images/btnDelete.gif\"/></a>");
                return builder.toSafeHtml();
            }

        };

        // delete column.
        Column<DemandeBean, String> deleteColumn = new Column<DemandeBean, String>(
                new ClickableTextCell(deleteColumnRenderer)) {

            @Override
            public String getValue(DemandeBean demandeBean) {
                return GwtApplication.constantes.button_delete();
            }
        };

        deleteColumn.setFieldUpdater(new FieldUpdater<DemandeBean, String>() {
            public void update(int index, DemandeBean demandeBean, String value) {
                if (presenter != null) {
                    presenter.onDeleteClicked(demandeBean);
                }
            }
        }

        );

        /*
        // Create description column.
        TextColumn<DemandeBean> descriptionColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {
                //return demande.getDescription();               
                return demande.getAutorite();
            }
        };
        */
       
       
       
        // Colonne TYPE
        TextColumn<DemandeBean> typeColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {               
                return demande.getTypeDemande();
            }
        };
       
        // Colonne MEDIA
        TextColumn<DemandeBean> mediaColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {               
                return demande.getTypeMedia();
            }
        };
        mediaColumn.setSortable(true);
   
        // Colonne Autorite
        TextColumn<DemandeBean> autoriteColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {               
                return demande.getAutorite();
            }
        };

        autoriteColumn.setSortable(true);

       
        /*
       
          List<DemandeBean> newData = new ArrayList(demandeTable.getVisibleItems());
       
        //ListHandler<DemandeBean> columnSortHandler = new ListHandler<DemandeBean>(demandes);
          ListHandler<DemandeBean> columnSortHandler = new ListHandler<DemandeBean>(newData);
          GWT.log("taille newData : " + newData.size());
         
         
       
        columnSortHandler.setComparator(mediaColumn,  new Comparator<DemandeBean>() {         
            public int compare(DemandeBean o1, DemandeBean o2) {               
                GWT.log("appel compare");               
                if (o1 == o2) {             
                    return 0;           
                }            
                // Compare the name columns.           
                if (o1 != null) {              
                    return (o2 != null) ? o1.getTypeMedia().compareTo(o2.getTypeMedia()) : 1;          
           
                    }           
                return -1;          
                }        
            });
        demandeTable.addColumnSortHandler(columnSortHandler);

        */
       
       
        demandeTable.addColumnSortHandler(new ColumnSortEvent.Handler() {           
           public void onColumnSort(ColumnSortEvent event) {           
              
               // La colonne triée
               Column a = event.getColumn();
              
              
                GWT.log("appel de onColumnSort");
                 List<DemandeBean> newData = new ArrayList(demandeTable.getVisibleItems());
        
                 if (event.isSortAscending()) {
                        GWT.log("Ordre ascendant");
                     Collections.sort(newData, new Comparator<DemandeBean>() {
                            public int compare(DemandeBean o1, DemandeBean o2) {
                                if (o1 == o2) {
                                    return 0;            
                                    }                          
                                if (o1 != null) {          
                                    return (o2 != null) ? o1.getAutorite().compareTo(o2.getAutorite()) : 1;           
                                    }            
                                return -1;       
                                }
                            }
                     );                         
                 } else {
                     GWT.log("Ordre descendant");
                     Collections.sort(newData, new Comparator<DemandeBean>() {
                            public int compare(DemandeBean o1, DemandeBean o2) {
                                if (o1 == o2) {
                                    return 0;            
                                    }                          
                                if (o1 != null) {          
                                    return (o2 != null) ? o2.getAutorite().compareTo(o1.getAutorite()) : 1;           
                                    }            
                                return -1;       
                                }
                            }
                     );
                 }
                 Range range = demandeTable.getVisibleRange();
                int start = range.getStart();
                 demandeTable.setRowData(start, newData);
               }               
             });
             
           
       
        // Colonne operateur
        TextColumn<DemandeBean> operateurColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {
                //return demande.getDescription();               
                return demande.getOperateur();
            }
        };
       
        // Colonne operateur
        TextColumn<DemandeBean> urgenceColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {
                //return demande.getDescription();               
                return String.valueOf(demande.getNiveauUrgenceAutorite());
            }
        };
       
        // Colonne operateur
        TextColumn<DemandeBean> prestationColumn = new TextColumn<DemandeBean>() {

            @Override
            public String getValue(DemandeBean demande) {
                //return demande.getDescription();               
                return String.valueOf(demande.getPrestation());
            }
        };
       
        /*
        demandeTable.addColumn(idColumn,
                GwtApplication.constantes.column_marketId());
        demandeTable.addColumn(nameColumn,
                GwtApplication.constantes.column_marketName());
        demandeTable.addColumn(descriptionColumn,
                GwtApplication.constantes.column_marketDescription());
        demandeTable.addColumn(deleteColumn, "");
        */
       
        demandeTable.addColumn(deleteColumn, "");
        demandeTable.addColumn(etatColumn, "Etat");
        demandeTable.addColumn(idColumn, "N° demande");
        demandeTable.addColumn(autoriteColumn,"Autorité");

        demandeTable.getColumnSortList().push(autoriteColumn);
       
       
        demandeTable.addColumn(typeColumn,"Type");
        demandeTable.addColumn(prestationColumn,"Code");
        demandeTable.addColumn(mediaColumn, "Media");
        demandeTable.addColumn(urgenceColumn,"Urgence");
   
        demandeTable.addColumn(dateReceptionColumn,"Date réception");
        //demandeTable.addColumn(descriptionColumn,"ID_AUTORITE");
   
        //demandeTable.addColumn(operateurColumn,"Opérateur");
       
   
        initWidget(getDisplay());
    }

    private Widget getDisplay() {

        // main panel is content panel

        // content panel configuration
       
        FlowPanel content = new FlowPanel();
        content.getElement().setId("content");
        content.add(new HTML("<center><h2>"
                + GwtApplication.constantes.corbeilleDemandes_title() + "</h2></center>"));
       
        content.add(rp);
       
        content.add(new HTML("<h4>"
                + GwtApplication.constantes.marketPage_result() + "</h4>"));
        content.add(feedback);
       
       
       
       
       
        content.add(demandeTable);
        content.add(pager);
       
       
        content.setWidth("100%");
       
       
       
        /*
        FlowPanel content = new FlowPanel();
        PaginatedTable table = new PaginatedTable(new TableTestModel());
        content.add(table);
        */
       
        return content;

    }

    public void setData(List<DemandeBean> demandes) {
       
        if (null != demandes) {
            GWT.log("setData : demandes pas nul");
        } else {
            GWT.log("setData : demandes est nul");
        }
        this.demandes = demandes;
        demandeTable.setRowCount(demandes.size(), true);
        demandeTable.setRowData(0, demandes);
        demandeTable.setVisible(!this.demandes.isEmpty());
        pager.setVisible(!this.demandes.isEmpty());
    }

    public void setPresenter(ListPresenter<DemandeBean> presenter) {
        this.presenter = presenter;
    }

    public void reset() {
        feedback.clear();
    }

    public void error() {
        feedback.error(GwtApplication.constantes.error_internal());
    }

    public void noData() {
        this.setData(new ArrayList<DemandeBean>());
        feedback.info(GwtApplication.messages.list_counter_none());
    }

    public boolean confirmDeletion(DemandeBean demandeBean) {
        return feedback.confirm(GwtApplication.messages
                .deleteMarketPage_confirm(String.valueOf(demandeBean.getId())));

    }

}


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment