Bug 194826 - [Databinding] Allow Control refresh after Conversion
Summary: [Databinding] Allow Control refresh after Conversion
Status: RESOLVED DUPLICATE of bug 238222
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Boris Bokowski CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-06-28 15:17 EDT by Thomas Schindl CLA
Modified: 2008-07-31 16:20 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Schindl CLA 2007-06-28 15:17:02 EDT
Suppose you have a DateField has a default representation format und multiple input formats.

Presentation-Format: dd.MM.yyyy
Input-Format: dd.MM.yyyy, ddMMyy

If the user enters the value in ddMMyy after having converted to date the field should update the Text-Field to show the Date in dd.MM.yyyy.
Comment 1 Brad Reynolds CLA 2007-06-30 18:29:25 EDT
Out of curiosity is there any reason why you're not using the SWT Date control (other than the fact that we haven't created an observable for it yet)?
Comment 2 Brad Reynolds CLA 2007-07-01 14:37:02 EDT
(In reply to comment #0)
> If the user enters the value in ddMMyy after having converted to date the field
> should update the Text-Field to show the Date in dd.MM.yyyy.

Also I'm curious how you see this working in regards to the user experience.  If you allow the user to enter data, the control displays it, format the text, and then update the control you have the potential of the cursor jumping around, losing selection, etc. while the user is entering text.  

The current design of Bindings isn't to be a controller in the MVC sense of the term.  A true controller would receive the input before it is displayed in order to ensure that formatting and masking occur.  Is that what you're talking about or did you have something else in mind?
Comment 3 Thomas Schindl CLA 2007-07-02 02:26:09 EDT
Brad,

ad 1: Yes I'd like people to have more flexibility to enter dates which is not easily possible using the DateTime-Control SWT offeres or do they also allow to enter users values in different formats? Another point is that I'd like the lib to be compatible with SWT 3.2.

ad2: Well I'm doing the conversion not while the user is typing afterwards SWT.FocusOut.

ad3: Yes that's what I'm trying to achieve and I think that this is a fairly common use case and it would make sense IMHO that Databing offers API to make things like this work.

Just for reference how I solved this issue currently is the following:

public static BoundTextFieldControl createDate(Composite parent, int style,
    String detailAttributeName, String[] formats) {
    UpdateValueStrategy modelToTarget = new UpdateValueStrategy();
    modelToTarget.setConverter(new DateToStringConverter(formats[0]));

    UpdateValueStrategy targetToModel = new UpdateValueStrategy();
    StringToDateConverter converter = new StringToDateConverter(formats);
    targetToModel.setConverter(converter);

    BoundTextFieldControl control = create(parent, style,
	detailAttributeName, new Class[] { Date.class }, modelToTarget,
	targetToModel, SWT.FocusOut);
    converter.setControl(control);

    return control;
}

private static class StringToDateConverter implements IConverter {
    private SimpleDateFormat[] sFormats;
    private BoundTextFieldControl control;

    private StringToDateConverter(String[] formats) {
        this.sFormats = new SimpleDateFormat[formats.length];
    
        for (int i = 0; i < formats.length; i++) {
            this.sFormats[i] = new SimpleDateFormat(formats[i]);
        }
   }

   private void setControl(BoundTextFieldControl control) {
       this.control = control;
   }

   public Object convert(Object fromObject) {
       Date date = null;
       boolean first = true;

       if (!fromObject.toString().trim().equals("")) {
           for (SimpleDateFormat format : sFormats) {
               try {
                   date = format.parse(fromObject.toString());
               } catch (ParseException e) {
               }

               if (date != null) {
                   break;
               }

               first = false;
           }
       }

       if (date == null) {
           control.setText("");
       } else if (!first) {
           control.setText(sFormats[0].format(date));
       }

       return date;
    }

    public Object getFromType() {
        return String.class;
    }

    public Object getToType() {
        return Date.class;
    }
}

private static class DateToStringConverter implements IConverter {
    private SimpleDateFormat format;

    private DateToStringConverter(String format) {
        this.format = new SimpleDateFormat(format);
    }

    public Object convert(Object fromObject) {
        if (fromObject == null) {
            return "";
	}

	return format.format((Date) fromObject);
    }

    public Object getFromType() {
        return Date.class;
    }

    public Object getToType() {
        return String.class;
    }
}
Comment 4 Brad Reynolds CLA 2007-07-02 21:20:50 EDT
(In reply to comment #3)
> Brad,
> 
> ad 1: Yes I'd like people to have more flexibility to enter dates which is not
> easily possible using the DateTime-Control SWT offeres or do they also allow to
> enter users values in different formats? 

I'm not sure I understand what this means.  What's wrong with the format?  I know it accepts SHORT, MEDIUM, or LONG styles.  It also looks like it gets the format from the system but I'm not intimately familiar with the code.

> Another point is that I'd like the lib
> to be compatible with SWT 3.2.

I undestand.  Will it be a while before you move to 3.3?
 
> ad3: Yes that's what I'm trying to achieve and I think that this is a fairly
> common use case and it would make sense IMHO that Databing offers API to make
> things like this work.

What other use cases do you have that need this?  My worry is that this isn't what the library was designed to do at this point.  Masking is something that we expect to occur outside of the binding.  Also could you just tell the binding to update the target via binding.updateModelToTarget()?
Comment 5 Thomas Schindl CLA 2007-07-03 03:21:37 EDT
(In reply to comment #4)
> (In reply to comment #3)
> > Brad,
> > 
> > ad 1: Yes I'd like people to have more flexibility to enter dates which is not
> > easily possible using the DateTime-Control SWT offeres or do they also allow to
> > enter users values in different formats? 
> 
> I'm not sure I understand what this means.  What's wrong with the format?  I
> know it accepts SHORT, MEDIUM, or LONG styles.  It also looks like it gets the
> format from the system but I'm not intimately familiar with the code.

I want the give users the possibility to use different formats and as stay flexible as possible e.g. input is most often done using ddMMyy but this format is not good for presentation where (dd.MM.yyyy) is a much better format, as far as I see DateTime doesn't support this kind of thing (input-format = presentation-format). Additionally I'd give my user the possibility to use the format they want to (say in the morning he/she likes to enter dates in ddMMyy and in the evening dd.MM.yy :-)

> 
> > Another point is that I'd like the lib
> > to be compatible with SWT 3.2.
> 
> I undestand.  Will it be a while before you move to 3.3?
> 

Well I fact, I'm using 3.3 but this won't solve the usability issues from above because users are already familiar with this possibility and taking it a way will drive them (and as a result me) crazy :-)

> > ad3: Yes that's what I'm trying to achieve and I think that this is a fairly
> > common use case and it would make sense IMHO that Databing offers API to make
> > things like this work.
> 
> What other use cases do you have that need this?  My worry is that this isn't
> what the library was designed to do at this point.  Masking is something that
> we expect to occur outside of the binding.  Also could you just tell the

but isn't this the purpose of IConverter

> binding to update the target via binding.updateModelToTarget()?
> 

Here are some use cases:
- Formatted Number input: User Enters in 10000 => Presentation: $10,000.00 (or in austrian locale €10.000,00)
- Rounding (of e.g. hours): User Enters 9.15 in a field where the maximum smallest fraction is 0.25 => IConverter transforms to 9.25 and updates the presentation

I think binding.updateModelToTarget() is the thing I searched for.
Comment 6 Brad Reynolds CLA 2007-07-04 13:10:12 EDT
(In reply to comment #5)
> > What other use cases do you have that need this?  My worry is that this isn't
> > what the library was designed to do at this point.  Masking is something that
> > we expect to occur outside of the binding.  Also could you just tell the
> 
> but isn't this the purpose of IConverter

The original intent of IConverter is to convert from one type to another.  When you throw strings in the mix we get into these sort of issues because it's now a formatter as well.  If the API of the control was more aligned with something like DateTime you wouldn't need to deal with formatting in the converter thus avoiding these types of issues.

> I think binding.updateModelToTarget() is the thing I searched for.

So are you saying you have what you need or is there still a gap to fill?  
Comment 7 Ovidio Mallo CLA 2008-07-27 13:55:35 EDT
I'd say this is a duplicate of bug #238222.
Comment 8 Boris Bokowski CLA 2008-07-31 16:20:41 EDT

*** This bug has been marked as a duplicate of bug 238222 ***