Search in sources :

Example 11 with EJRWTAbstractTableSorter

use of org.entirej.applicationframework.rwt.table.EJRWTAbstractTableSorter in project rap by entirej.

the class EJRWTTextItemRenderer method getColumnSorter.

@Override
public EJRWTAbstractTableSorter getColumnSorter(final EJScreenItemProperties item, EJScreenItemController controller) {
    final Collator compareCollator = Collator.getInstance();
    return new EJRWTAbstractTableSorter() {

        @Override
        public int compare(Viewer viewer, Object e1, Object e2) {
            if (e1 instanceof EJDataRecord && e2 instanceof EJDataRecord) {
                EJDataRecord d1 = (EJDataRecord) e1;
                EJDataRecord d2 = (EJDataRecord) e2;
                if (d1 != null && d2 != null) {
                    Object value1 = d1.getValue(item.getReferencedItemName());
                    Object value2 = d2.getValue(item.getReferencedItemName());
                    if (value1 == null && value2 == null) {
                        return 0;
                    }
                    if (value1 == null && value2 != null) {
                        return -1;
                    }
                    if (value1 != null && value2 == null) {
                        return 1;
                    }
                    return compareCollator.compare(value1, value2);
                }
            }
            return 0;
        }

        @Override
        public int compareNumber(Viewer viewer, Object e1, Object e2, DecimalFormat frm) {
            if (e1 instanceof EJDataRecord && e2 instanceof EJDataRecord) {
                EJDataRecord d1 = (EJDataRecord) e1;
                EJDataRecord d2 = (EJDataRecord) e2;
                if (d1 != null && d2 != null) {
                    Object value1 = d1.getValue(item.getReferencedItemName());
                    Object value2 = d2.getValue(item.getReferencedItemName());
                    if (value1 == null && value2 == null) {
                        return 0;
                    }
                    if (value1 == null && value2 != null) {
                        return -1;
                    }
                    if (value1 != null && value2 == null) {
                        return 1;
                    }
                    if (value1 instanceof String && value2 instanceof String) {
                        try {
                            Number dv1 = frm.parse((String) value1);
                            Number dv2 = frm.parse((String) value2);
                            return Double.compare(dv1.doubleValue(), dv2.doubleValue());
                        } catch (NumberFormatException f) {
                            return compareCollator.compare(value1, value2);
                        } catch (ParseException e) {
                            return compareCollator.compare(value1, value2);
                        }
                    }
                    return compareCollator.compare(value1, value2);
                }
            }
            return 0;
        }

        @Override
        public int compareDate(Viewer viewer, Object e1, Object e2, DateFormat format) {
            if (e1 instanceof EJDataRecord && e2 instanceof EJDataRecord) {
                EJDataRecord d1 = (EJDataRecord) e1;
                EJDataRecord d2 = (EJDataRecord) e2;
                if (d1 != null && d2 != null) {
                    Object value1 = d1.getValue(item.getReferencedItemName());
                    Object value2 = d2.getValue(item.getReferencedItemName());
                    if (value1 == null && value2 == null) {
                        return 0;
                    }
                    if (value1 == null && value2 != null) {
                        return -1;
                    }
                    if (value1 != null && value2 == null) {
                        return 1;
                    }
                    if (value1 instanceof String && value2 instanceof String) {
                        try {
                            Date dv1 = format.parse((String) value1);
                            Date dv2 = format.parse((String) value2);
                            return dv1.compareTo(dv2);
                        } catch (ParseException e) {
                        // ignore
                        }
                    }
                    return compareCollator.compare(value1, value2);
                }
            }
            return 0;
        }
    };
}
Also used : DecimalFormat(java.text.DecimalFormat) DateFormat(java.text.DateFormat) Viewer(org.eclipse.jface.viewers.Viewer) ParseException(java.text.ParseException) EJDataRecord(org.entirej.framework.core.data.EJDataRecord) Date(java.util.Date) Collator(java.text.Collator) EJRWTAbstractTableSorter(org.entirej.applicationframework.rwt.table.EJRWTAbstractTableSorter)

Aggregations

EJRWTAbstractTableSorter (org.entirej.applicationframework.rwt.table.EJRWTAbstractTableSorter)11 EJDataRecord (org.entirej.framework.core.data.EJDataRecord)8 Viewer (org.eclipse.jface.viewers.Viewer)6 ColumnLabelProvider (org.eclipse.jface.viewers.ColumnLabelProvider)5 EJFrameworkExtensionProperties (org.entirej.framework.core.properties.definitions.interfaces.EJFrameworkExtensionProperties)5 Font (org.eclipse.swt.graphics.Font)4 EJRWTAppItemRenderer (org.entirej.applicationframework.rwt.renderer.interfaces.EJRWTAppItemRenderer)4 EJScreenItemController (org.entirej.framework.core.interfaces.EJScreenItemController)4 EJManagedItemRendererWrapper (org.entirej.framework.core.renderers.EJManagedItemRendererWrapper)4 Collator (java.text.Collator)2 TableViewerColumn (org.eclipse.jface.viewers.TableViewerColumn)2 Point (org.eclipse.swt.graphics.Point)2 TableColumn (org.eclipse.swt.widgets.TableColumn)2 EJRWTTableSortSelectionListener (org.entirej.applicationframework.rwt.table.EJRWTTableSortSelectionListener)2 HtmlBaseColumnLabelProvider (org.entirej.applicationframework.rwt.table.HtmlBaseColumnLabelProvider)2 EJCoreVisualAttributeProperties (org.entirej.framework.core.properties.EJCoreVisualAttributeProperties)2 DateFormat (java.text.DateFormat)1 DecimalFormat (java.text.DecimalFormat)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1