Results 1 to 10 of 10

Thread: Date Format Control within Forms

  1. #1
    Join Date
    Oct 2005
    Posts
    12

    Default Date Format Control within Forms

    Hi all,

    I am developing a spring rich client application. Within it there are several forms, that contain date fields. With some of the Fields I want to display them in the form of dd.MM.yy in others I want to display them dd.MM.yyyy and within others in the form of dd.MM.yyyy HH:mm.

    Now I have searched the forums and read on atlassian that one would have to create his own PropertyEditor. Since I want to use the JCalendar Component, I have tried the example on atlassian.

    From that point on, I did not know where to hook the JDateChooserPropertyEditor to my FormFields, so I took of searching the Forum to find things like:

    Code:
    getFormModel().bind(Date.class, new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy")))
    but this does not seem to work.

    here is my form class...

    Code:
    import javax.swing.JComponent;
    
    import org.springframework.binding.form.FormModel;
    import org.springframework.richclient.form.AbstractForm;
    import org.springframework.richclient.form.builder.TableFormBuilder;
    
    public class PersonForm extends AbstractForm
    {
    
        public final static String formName = "person.form";
    
        public PersonForm(FormModel formModel)
        {
            super(formModel, formName);
        }
    
        protected JComponent createFormControl()
        {
            //        TableFormBuilder formBuilder = new TableFormBuilder(new SwingBindingFactory(getFormModel()));
            TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());
            formBuilder.addSeparator("Name");
            formBuilder.row();
            formBuilder.add("firstName");
            formBuilder.add("lastName");
            formBuilder.row();
            formBuilder.add("birthDate");
            formBuilder.row();
            formBuilder.addSeparator("Adresse");
            formBuilder.row();
            formBuilder.add("street");
            formBuilder.row();
            formBuilder.add("zip");
            formBuilder.add("city");
            formBuilder.row();
            formBuilder.addSeparator("Kontaktdaten");
            formBuilder.row();
            formBuilder.add("phone");
            formBuilder.row();
            formBuilder.add("phone2");
            formBuilder.row();
            formBuilder.add("email");
            return formBuilder.getForm();
        }
    
    }
    plus another question which constructor is correct? The one creating a new SwingBindingFactory, or getBindingFactory()???

    AND another one I have this form on my StartView class, which contains a JSplitPane with a JTree on the Left Side, and the PropertyForm (PersonForm) from above on the right. (added by the Pattern Pete deBruycker suggested).

    Now my question is, how would i add the Command Buttons to the Bottom of the Property Panel?

    Here is the code for my StartView...

    Code:
    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Component;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTree;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreeCellRenderer;
    
    import org.springframework.binding.form.FormModel;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.richclient.application.support.AbstractView;
    import org.springframework.richclient.dialog.FormBackedDialogPage;
    import org.springframework.richclient.form.FormModelHelper;
    import org.springframework.richclient.progress.TreeStatusBarUpdater;
    import org.springframework.richclient.tree.FocusableTreeCellRenderer;
    import org.springframework.richclient.util.Assert;
    
    import de.byteaction.diamonds.forms.PersonForm;
    import de.byteaction.diamonds.mappings.Person;
    import de.byteaction.diamonds.mappings.PersonDao;
    
    public class StartView extends AbstractView implements ApplicationListener
    {
    
        private JTree            personTree;
    
        private PersonDao        personDao;
    
        private DefaultTreeModel personsTreeModel;
    
        protected JComponent createControl()
        {
            JPanel treePanel = new JPanel(new BorderLayout());
            formPanel = new JPanel(new CardLayout());
            JSplitPane contentPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            createOwnerManagerTree();
            JScrollPane sp = new JScrollPane(personTree);
            treePanel.add(sp, BorderLayout.CENTER);
            contentPane.add(treePanel);
            formPanel.add(new JPanel(), "BLANK");
            contentPane.add(formPanel);
            return contentPane;
        }
    
        private void createOwnerManagerTree()
        {
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Personen");
            List persons = personDao.getAll();
            for (Iterator i = persons.iterator(); i.hasNext();)
            {
                Person person = (Person) i.next();
                DefaultMutableTreeNode personNode = new DefaultMutableTreeNode(person);
                rootNode.add(personNode);
            }
            this.personsTreeModel = new DefaultTreeModel(rootNode);
            this.personTree = new JTree(personsTreeModel);
            personTree.setShowsRootHandles(true);
            personTree.addTreeSelectionListener(new TreeStatusBarUpdater(getStatusBar()) {
    
                public String getSelectedObjectName()
                {
                    Person selectedPerson = getSelectedPerson();
                    if (selectedPerson != null)
                    {
                        FormModel formModel = FormModelHelper.createFormModel(selectedPerson, PersonForm.formName);
                        PersonForm form = new PersonForm(formModel);
                        FormBackedDialogPage page = new FormBackedDialogPage(form);
                        CardLayout cardLayout = (CardLayout) formPanel.getLayout();
                        JPanel panel = new JPanel(new BorderLayout());
                        panel.add(form.getControl(), BorderLayout.CENTER);
                        
                        formPanel.add(panel, "PROPERTIES");
                        cardLayout.show(formPanel, "PROPERTIES");
                        //                    createDialog(selectedPerson);
                        return selectedPerson.getFirstName() + " " + selectedPerson.getLastName() + " " + selectedPerson.getPhone() + " " + selectedPerson.getPhone2();
                    }
                    else
                    {
                        CardLayout cardLayout = (CardLayout) formPanel.getLayout();
                        cardLayout.show(formPanel, "BLANK");
                        return "Personen";
                    }
                }
    
                private void createDialog(Person selectedPerson)
                {
                    FormModel formModel = FormModelHelper.createFormModel(selectedPerson, PersonForm.formName);
                    PersonForm form = new PersonForm(formModel);
    
                    //                FormBackedDialogPage page = new FormBackedDialogPage(form);
                    //
                    //                TitledPageApplicationDialog dialog = new TitledPageApplicationDialog(page, getWindowControl()) {
                    //
                    //                    protected void onAboutToShow()
                    //                    {
                    //                        //                        setEnabled(compositePage.isPageComplete());
                    //                    }
                    //
                    //                    protected boolean onFinish()
                    //                    {
                    //                        //                        ownerFormModel.commit();
                    //                        //                        clinic.storeOwner(owner);
                    //                        //                        ownersTreeModel.nodeChanged(getSelectedOwnerNode());
                    //                        return true;
                    //                    }
                    //                };
                    //                dialog.showDialog();
                }
            });
            personTree.addTreeSelectionListener(new TreeSelectionListener() {
    
                public void valueChanged(TreeSelectionEvent e)
                {
                    //                updateCommands();
                }
            });
            //        personTree.addMouseListener(new PopupMenuMouseListener() {
            //            protected boolean onAboutToShow(MouseEvent e) {
            //                return !isRootOrNothingSelected();
            //            }
            //
            //            protected JPopupMenu getPopupMenu() {
            //                return getSelectedOwner() != null ? createOwnerPopupContextMenu() : createPetPopupContextMenu();
            //            }
            //        });
            //        ownersTree.addMouseListener(new MouseAdapter() {
            //            public void mouseClicked(MouseEvent e) {
            //                if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1 && propertiesExecutor.isEnabled()) {
            //                    propertiesExecutor.execute();
            //                }
            //            }
            //        });
            personTree.setCellRenderer(getTreeCellRenderer());
            personTree.setRootVisible(true);
        }
    
        public void onApplicationEvent(ApplicationEvent event)
        {
            // TODO Auto-generated method stub
    
        }
    
        public void setPerson(PersonDao dao)
        {
            Assert.notNull(dao, "We need a Person Dao");
            personDao = dao;
        }
    
        private Person getSelectedPerson()
        {
            DefaultMutableTreeNode node = getSelectedPersonNode();
            if (node != null)
            {
                return (Person) node.getUserObject();
            }
            else
            {
                return null;
            }
        }
    
        private DefaultMutableTreeNode getSelectedPersonNode()
        {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) personTree.getLastSelectedPathComponent();
            if (node == null || !(node.getUserObject() instanceof Person))
            {
                return null;
            }
            else
            {
                return node;
            }
        }
    
        private DefaultTreeCellRenderer treeCellRenderer = new FocusableTreeCellRenderer() {
    
                                                             public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
                                                             {
                                                                 super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                                                                 DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                                                                 if (node.isRoot())
                                                                 {
                                                                     this.setIcon(getIconSource().getIcon("group.folder.icon"));
                                                                 }
                                                                 else
                                                                 {
                                                                     Object userObject = node.getUserObject();
                                                                     if (userObject instanceof Person)
                                                                     {
                                                                         Person o = (Person) userObject;
                                                                         this.setText(o.getFirstName() + " " + o.getLastName());
                                                                         this.setIcon(getIconSource().getIcon("person.bullet"));
                                                                     }
                                                                     //                                                                 else if (userObject instanceof Pet)
                                                                     //                                                                 {
                                                                     //                                                                     Pet p = (Pet) userObject;
                                                                     //                                                                     this.setText&#40;"<html>" + p.getName&#40;&#41; + " <i>&#40;" + p.getType&#40;&#41;.getName&#40;&#41; + "&#41;"&#41;;
                                                                     //                                                                     this.setIcon&#40;getIconSource&#40;&#41;.getIcon&#40;"pet.bullet"&#41;&#41;;
                                                                     //                                                                 &#125;
                                                                 &#125;
                                                                 return this;
                                                             &#125;
                                                         &#125;;
    
        private JPanel                  formPanel;
    
        public TreeCellRenderer getTreeCellRenderer&#40;&#41;
        &#123;
            return treeCellRenderer;
        &#125;
    
    &#125;
    Any Help is highly appreciated...

  2. #2
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default

    I don't have any experience with the date fields, so I can't address that.

    On the other questions, hopefully this will help:

    another question which constructor is correct? The one creating a new SwingBindingFactory, or getBindingFactory()???
    Either will work, but the latter (getBindingFactory) is more correct as it allows for the binding factory to be configured in the application-context.xml.

    how would i add the Command Buttons to the Bottom of the Property Panel?
    Maybe I'm mssing something more subtle, but adding command buttons is no different than adding any other swing component. You need to construct the JButton instances and then add them to the appropriate parent container. Maybe I didn't understand your question?

    HTH,
    Larry.

  3. #3
    Join Date
    Oct 2005
    Posts
    12

    Default

    Hi Larry,

    thanks for your reply.

    Quote Originally Posted by lstreepy
    Maybe I'm mssing something more subtle, but adding command buttons is no different than adding any other swing component. You need to construct the JButton instances and then add them to the appropriate parent container. Maybe I didn't understand your question?
    Well of course this is the way one would do it inside a normal swing app. But as far as I understand spring, spring comes with its own Action Event Framework.

    So what I was thinking of doing is to generate a DefaultFormFinish Action, which checks the FormModel to find the corresponding class, that has been used by the FormModelHelper, to create the FormModel, and then do a saveOrUpdate to the DB.

    The question was how can i wire the Action defined inside commands-context.xml to the Button? I have tried to look into the TitledPageApplicationDialog Class how it is done there, but could not quite figure out how the finish- and cancelCommands get there.

  4. #4
    Join Date
    Aug 2005
    Location
    Austin, TX
    Posts
    425

    Default

    If you just need to provide processing on the finish command when a dialog completes, you can simply override the "onFiinish" method in a subclass of an ApplicationDialog (like TitledPageApplicationDialog).

    If you want some other examples of creating action commands and placing buttons into a form, take a look at AbstractMasterForm and AbstractDetailForm.

    HTH,
    Larry.

  5. #5
    Join Date
    Oct 2005
    Posts
    12

    Default

    Hi Larry,

    Quote Originally Posted by lstreepy
    If you want some other examples of creating action commands and placing buttons into a form, take a look at AbstractMasterForm and AbstractDetailForm.
    I think I understand now how I can create my Command...

    Code:
        protected JComponent createButtonBar&#40;&#41; &#123;
            _commitCommand = getCommitCommand&#40;&#41;;
            _revertCommand = getRevertCommand&#40;&#41;;
            _cancelCommand = getCancelCommand&#40;&#41;;
    
            _formCommandGroup = CommandGroup.createCommandGroup&#40; null, new AbstractCommand&#91;&#93; &#123; _cancelCommand,
                    _revertCommand, _commitCommand &#125; &#41;;
            JComponent buttonBar = _formCommandGroup.createButtonBar&#40;&#41;;
            GuiStandardUtils.attachDialogBorder&#40; buttonBar &#41;;
            return buttonBar;
        &#125;
    I would implement my own method of getCommitCommandd, which checks if the command has already been configured, and if not creates it, and registers it with the global Command Configurer available through the ApplicationServices. sounds like a plan, doesn't it :wink:

  6. #6
    Join Date
    Oct 2005
    Posts
    12

    Default DateFormat?

    This still does not explain, how I can use different Dateformats per Form. Is there any advice?

  7. #7
    Join Date
    May 2005
    Posts
    394

    Default

    Benoit might have done something similar, check the comments on
    http://opensource.atlassian.com/proj...browse/RCP-204

  8. #8
    Join Date
    Oct 2005
    Posts
    12

    Default

    Hi Geoffry,

    thanks for your reply. This does set the binding globally though, or am I wrong?

    Here is my Problem:
    I have a form with a birth date field on which i only want to display the date dd.MM.yyyy then on another form, i configure a practice plan, which has 2 datetime fields one with the starttime (dd.MM.yy HH:mm) and one with the endtime (same format)...

    Is there a soltuion for not to select a binding strategy globally, but on a form level?

  9. #9
    Join Date
    Oct 2005
    Posts
    12

    Default

    Hi all,

    finally I have found the solution to my problem and the solution was easier, than of what I would have thought...

    Here is what I had to do. I had to create my Custom JCalendarBinding
    Code:
    import java.util.Date;
    
    import javax.swing.JComponent;
    
    import org.springframework.binding.form.FormModel;
    import org.springframework.richclient.form.binding.support.CustomBinding;
    
    import com.toedter.calendar.JDateChooser;
    
    
    public class JCalendarBinding extends CustomBinding
    &#123;
    
        private JDateChooser dateChooser;
        
        private Date theDate = null;
    
        private String dateFormat = "dd.MM.yyyy";
        
        public JCalendarBinding&#40;FormModel formModel, String formPropertyPath&#41;
        &#123;
            super&#40;formModel, formPropertyPath, Date.class&#41;;
            
            this.theDate = &#40;getValue&#40;&#41; == null&#41; ? new Date&#40;&#41; &#58; &#40;Date&#41;getValue&#40;&#41;;
            dateChooser = new JDateChooser&#40;&#41;;
            dateChooser.setDateFormatString&#40;getDateFormat&#40;&#41;&#41;;
            dateChooser.setDate&#40;this.theDate&#41;;
        &#125;
    
        public JCalendarBinding&#40;FormModel formModel, String formPropertyPath, String customFormat&#41;
        &#123;
            super&#40;formModel, formPropertyPath, Date.class&#41;;
            
            this.theDate = &#40;getValue&#40;&#41; == null&#41; ? new Date&#40;&#41; &#58; &#40;Date&#41;getValue&#40;&#41;;
            setDateFormat&#40;customFormat&#41;;
            
            dateChooser = new JDateChooser&#40;&#41;;
            dateChooser.setDateFormatString&#40;getDateFormat&#40;&#41;&#41;;
            dateChooser.setDate&#40;this.theDate&#41;;
        &#125;
    
        @Override
        protected void valueModelChanged&#40;Object newValue&#41;
        &#123;
            dateChooser.setDate&#40;&#40;Date&#41; newValue&#41;;
        &#125;
    
        @Override
        protected JComponent doBindControl&#40;&#41;
        &#123;
            return dateChooser;
        &#125;
    
        @Override
        protected void readOnlyChanged&#40;&#41;
        &#123;
            enabledChanged&#40;&#41;;
        &#125;
    
        @Override
        protected void enabledChanged&#40;&#41;
        &#123;
            dateChooser.setEnabled&#40;isEnabled&#40;&#41; && !isReadOnly&#40;&#41;&#41;;
        &#125;
    
        
        /**
         * @return Returns the dateFormat.
         */
        public String getDateFormat&#40;&#41;
        &#123;
            return dateFormat;
        &#125;
    
        
        /**
         * @param dateFormat The dateFormat to set.
         */
        public void setDateFormat&#40;String dateFormat&#41;
        &#123;
            this.dateFormat = dateFormat;
        &#125;
    
        
    &#125;
    And then it is simple to apply:

    Code:
    import javax.swing.JComponent;
    
    import org.springframework.binding.form.FormModel;
    import org.springframework.richclient.form.AbstractForm;
    import org.springframework.richclient.form.builder.TableFormBuilder;
    
    import de.byteaction.diamonds.custombinding.JCalendarBinding;
    
    public class PersonForm extends AbstractForm
    &#123;
    
        public final static String formName = "person.form";
    
        public PersonForm&#40;FormModel formModel&#41;
        &#123;
            super&#40;formModel, formName&#41;;
        &#125;
    
        protected JComponent createFormControl&#40;&#41;
        &#123;
            //        TableFormBuilder formBuilder = new TableFormBuilder&#40;new SwingBindingFactory&#40;getFormModel&#40;&#41;&#41;&#41;;
            TableFormBuilder formBuilder = new TableFormBuilder&#40;getBindingFactory&#40;&#41;&#41;;
            formBuilder.addSeparator&#40;"Name"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.add&#40;"firstName"&#41;;
            formBuilder.add&#40;"lastName"&#41;;
            formBuilder.row&#40;&#41;;
            //formBuilder.add&#40;"birthDate"&#41;;
            formBuilder.add&#40;new JCalendarBinding&#40;getFormModel&#40;&#41;, "birthDate", "E, dd. MMM. yyyy"&#41;&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.addSeparator&#40;"Adresse"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.add&#40;"street"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.add&#40;"zip"&#41;;
            formBuilder.add&#40;"city"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.addSeparator&#40;"Kontaktdaten"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.add&#40;"phone"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.add&#40;"phone2"&#41;;
            formBuilder.row&#40;&#41;;
            formBuilder.add&#40;"email"&#41;;
            return formBuilder.getForm&#40;&#41;;
        &#125;
    
    &#125;
    instead of referencing the birthDate directly, i put in a binder. Works like a Charm

    Keep up the good work you guys.

    Kind regards

    Buddy

  10. #10
    Join Date
    Aug 2005
    Location
    London (the English one!)
    Posts
    378

    Default

    Hi

    Sorry I did not notice this thread sooner.

    Here is what I have done, my problem is slightly different: I have two types of "Dates": simple Date and Timestamp. I was looking for a global and automated way to display either. As far as I am aware, does not make too much sense to use the NachoCalendar for a datetime/timestamp field.

    Furthermore, timestamps are readonly in my app.

    The way I have done it is simple, my domain objects are either Date or Timestamp and the binding can therefore be global.

    Dates bind to the RCP204:
    Code:
    <bean class="org.springframework.richclient.form.binding.swing.NachoCalendarDateFieldBinder">
      <property name="dateFormat">
        <value type="java.lang.String">dd-MMM-yyyy</value>
      </property>
    </bean>
    and the timestamps are going to readonly labels.

    regards

    Benoit

Similar Threads

  1. Replies: 17
    Last Post: Jan 2nd, 2007, 01:43 PM
  2. Replies: 5
    Last Post: Aug 31st, 2006, 02:55 AM
  3. Date format using JDBC Template
    By macmeureg in forum Data
    Replies: 5
    Last Post: Jun 29th, 2005, 10:18 AM
  4. Date Field Validation
    By sherihan in forum EJB
    Replies: 0
    Last Post: Feb 7th, 2005, 04:58 AM
  5. Date format for the whole context
    By peer2p in forum Web
    Replies: 2
    Last Post: Nov 5th, 2004, 03:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •