Re: [Microba-controls] commitEdit of a null Date value
Brought to you by:
michaelbaranov
From: Michael B. <mic...@gm...> - 2007-10-20 07:22:38
|
Chris, I just have checked the behavior of DatePicker with a quick snippet and I am able to set null date to it programmatically just OK out of the box! Can you please double-check it and send me a quick snippet to illustrate the failure (I'm confused with your description right now)? Here is my snippet to start with: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyVetoException; import javax.swing.JButton; import javax.swing.JFrame; import com.michaelbaranov.microba.calendar.DatePicker; public class Snippet { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLayout(new FlowLayout()); final DatePicker picker = new DatePicker(); picker.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(picker.getDate()); } }); JButton btn = new JButton("NULL"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { picker.setDate(null); } catch (PropertyVetoException e1) { System.out.println("Could not set null date."); e1.printStackTrace(); } } }); frame.add(picker); frame.add(btn); frame.setSize(640, 480); frame.setVisible(true); } } Cheers! Chris Murphy wrote: > > Hi Michael, > > Michael Baranov wrote: >> Chris, >> >> Thanks for digging in! >> One more question before we go on: DateFormat vs. DateFormatter. >> You're using DateFormatter and even suggest to update the API for it. >> But what's the purpose? What's the difference? Can't you do the same >> null/empty check with DateFormat? DateFormat is in the API already. >> > Maybe you can! But then I would have thought Sun's workaround would be > on DateFormat if that was the case. To tell you the truth I don't > really want to become a > DatePicker/JFormattedText/DateFormatter/DateFormat expert right now; I > just want to use it. >> About the deep-buried UI: it was the initial idea to hide the >> implementation, the fact that DatePicker is really composed of a text >> edit and a button. Maybe it's a good idea to expose the parts from >> their UI delegates and make them accessible like this: >> ( (DatePickerUI)picker.getUI() ).getButton() >> // etc... >> Is this what you mean? > Yes. You could do worse than take a look at the source of > http://web.ukonline.co.uk/mseries/ and borrow some of the good ideas > there from there - if there are any. I've never created a composite > component with its own UI. >> >> Chris, please get the latest sources from sourceforge. If you can't >> make a patch for me, plz send me back the modified sources. > I'm pretty sure I got the latest source. Am attaching the source file > I'm using currently. > > I have it on my list of things to do to compare MDateEntryField with > DatePicker a bit more. Things I will look at are focusing and > validation and also how well they work when they are XMLEncoded. Will > get back to you then ... > > - Chris >> >> Cheers! >> Michael. >>> Hi Michael, >>> >>> Send me your latest source file for BasicDatePickerUI and I'll do a >>> patch if you like (have never done such a thing before); however all >>> that needs to be done is to use this new inner class >>> NullCapableDateFormatter instead of DateFormatter as your code has it. I >>> have since made the new inner class private and static as well - but >>> haven't really tested it. Apart from that all the necessary code is as >>> sent. Alternatively you could change the code so that API users can put >>> in their own DateFormatter. One small problem with this I see is that >>> your UIs seem to be hard to get to - there is no reference to them from >>> the controller as I would expect. I have had to code this method: >>> >>> public static Component getDisplay( DatePicker dp) >>> >>> { >>> >>> Component result; >>> >>> Component components[] = dp.getComponents(); >>> >>> //Print.prArray( components, "Which one is a text field?"); >>> >>> //A: >>> >>> //value: null, class: com.michaelbaranov.microba.calendar.ui.basic.BasicDatePickerUI$MyJFormattedTextField >>> >>> //value: null, class: javax.swing.JButton >>> >>> result = components[0]; >>> >>> return result; >>> >>> } >>> >>> Is there a better way? >>> >>> I experienced the problem when programmatically setting the date to >>> null. What would happen was that a null wouldn't get through the >>> commitEdit() validation (which presumably happened at the time of a >>> focus change), and the last successfully committed value would come back >>> to being the actual value. This was a difficult thing to track down, and >>> indeed also exists on MDateEntryField, and was the reason that I came to >>> your Component - hoping it would allow null date values. >>> >>> - Chris Murphy (www.strandz.org) >>> >>> Michael Baranov wrote: >>> >>>> Chris, >>>> >>>> Thanks for the path! But can you describe the effect of the change >>>> from the point of view of a user? What was wrong (but now OK)? Can you >>>> please send me a regular machine-processable patch for >>>> BasicDatePicketUI against the latest sources (so I ca patch a local >>>> copy free of mistakes)? >>>> >>>> Thanks for using Microba and contributing! >>>> >>>> Chris Murphy wrote: >>>> >>>>> Hi Michael, >>>>> >>>>> I found something for your consideration, which has required changing >>>>> the microba source as configuration was not possible. The problem is >>>>> that JFormattedTextField does not properly support the concept of a >>>>> null date. A null date can't be committed (commitEdit()) to a >>>>> JFormattedTextField. As you can see from the code Sun's bug database >>>>> offers a workaround that I've implemented on your code. This code is >>>>> from changes I've made to BasicDatePickerUI: >>>>> >>>>> private DefaultFormatterFactory createFormatterFactory() { >>>>> return new DefaultFormatterFactory(new >>>>> NullCapableDateFormatter(peer >>>>> .getDateFormat())); >>>>> } >>>>> >>>>> >>>>> /** >>>>> * To do this enhancement: >>>>> * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4745048 >>>>> */ >>>>> public class NullCapableDateFormatter extends DateFormatter >>>>> { >>>>> public NullCapableDateFormatter(DateFormat dateFormat) >>>>> { >>>>> super( dateFormat); >>>>> } >>>>> >>>>> public Object stringToValue(String string) >>>>> throws ParseException >>>>> { >>>>> if(string == null || string.length() == 0) >>>>> { >>>>> return null; >>>>> } >>>>> return super.stringToValue(string); >>>>> } >>>>> } >>>>> >>>>> >>>>> thanks - Chris >>>>> >>>>> ------------------------------------------------------------------------- >>>>> >>>>> This SF.net email is sponsored by: Splunk Inc. >>>>> Still grepping through log files to find problems? Stop. >>>>> Now Search log events and configuration files using AJAX and a browser. >>>>> Download your FREE copy of Splunk now >> http://get.splunk.com/ >>>>> _______________________________________________ >>>>> Microba-controls mailing list >>>>> Mic...@li... >>>>> https://lists.sourceforge.net/lists/listinfo/microba-controls >>>>> >>>>> >>>>> >>> >>> ------------------------------------------------------------------------- >>> This SF.net email is sponsored by: Splunk Inc. >>> Still grepping through log files to find problems? Stop. >>> Now Search log events and configuration files using AJAX and a browser. >>> Download your FREE copy of Splunk now >> http://get.splunk.com/ >>> _______________________________________________ >>> Microba-controls mailing list >>> Mic...@li... >>> https://lists.sourceforge.net/lists/listinfo/microba-controls >>> >>> -- Michael Baranov |