Set case insensitive as the default: possible?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Set case insensitive as the default: possible?

Andrea Aime-2
Hi,
I'm wondering if there is any way to make case insensitive comparators
the default for Strings. I have quite a group of users that don't pay
attention to casing when entering data and are then confused by the
case sensitive sorting.
I there any simple way to change the default sorting without having to
go table by table, field by field?

Best regards
Andrea Aime


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Set case insensitive as the default: possible?

Jesse Wilson

Andrea --

On 28-Jul-05, at 1:01 AM, Andrea Aime wrote:
> I there any simple way to change the default sorting without having to
> go table by table, field by field?

Well there's no global flag that says 'CASE INSENSITIVE' but there's
an easy way to get to there from here. First, create a helper class for
decorating TableFormats. This implementation should do that for you:

public class DecoratingTableFormat implements TableFormat,  
WritableTableFormat AdvancedTableFormat {
     protected final TableFormat target;
     protected final AdvancedTableFormat advanced;
     protected final WritableTableFormat writable;
     public DecoratingTableFormat(TableFormat target) {
         this.target = target;
         it(target instanceof AdvancedTableFormat) this.advanced =  
(AdvancedTableFormat)target; else advanced = null;
         if(target instanceof WritableTableFormat) this.writable =  
(WritableTableFormat)target; else writable = null;
     }
     public int getColumnCount() { return target.getColumnCount(); }
     public String getColumnName(int column) { return  
target.getColumnName(column); }
     public Object getColumnValue(Object baseObject, int column)  
{ return target. getColumnValue(baseObject, column); }
     public boolean isEditable(Object baseObject, int column)  
{ return writable == null ? false : writable.isEditable(baseObject,  
column); }
     public Object setColumnValue(Object baseObject, Object  
editedValue, int column) {
         return writable == null ? throw new IllegalStateException
() : writable.setColumnValue(baseObject, editedValue, column); }
     public Class getColumnClass(int column) { return advanced ==  
null ? Object.class : advanced.getColumnClass(column); }
     public Comparator getColumnComparator(int column) { return  
advanced == null ? ComparableComparator : advanced.getColumnComparator
(column); }
}

Then you'll need to create a decoration that replaces  
ComparableComparator
with String.CASE_INSENSITIVE_ORDER whenever the column class is  
String.class.

public class CaseInsensitiveTableFormat extends DecoratingTableFormat {
      public CaseInsensitiveTableFormat(AdvancedTableFormat  
tableFormat) {
         super(tableFormat);
     }
     public Comparator getColumnComparator(int column) {
         if(getColumnClass(column) == String.class()) return  
String.CASE_INSENSITIVE_ORDER;
         else return super.getColumnComparator(column);
     }
}

Finally you just need to wrap all of your TableFormat instances
with CaseInsensitiveTableFormat, immediately before specifying
them to your EventTableModel.

This is a great demo of the decorator pattern & possibly a great
thing to cover in an issue of Glazed Lists Developer!

Cheers,
Jesse


smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Set case insensitive as the default: possible?

Andrea Aime-2
Jesse,
thank you so much for the prompt an insightful replies :-)

Best regards
Andrea Aime



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]