View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.editors;
20  
21  import java.beans.PropertyEditorSupport;
22  
23  import org.promotego.beans.BeanSupport;
24  import org.promotego.dao.interfaces.Dao;
25  
26  /***
27   * A parameterized property editor that maps BeanSupport<T> objects to and from
28   * their id's as mapped by the provided Dao.  Used by controllers to map object
29   * ID's posted by the browser to the proper object types.
30   * 
31   * @author alf
32   *
33   * @param <T>
34   */
35  public class BeanIdEditor<T extends BeanSupport<T>> extends PropertyEditorSupport
36  {
37      private T m_tee;
38      private Dao<T> m_dao;
39      
40      public BeanIdEditor(Dao<T> addressDao)
41      {
42          m_dao = addressDao;
43      }
44  
45      public String getAsText()
46      {
47          if (m_tee != null)
48          {
49              return m_tee.getId().toString();
50          }
51          else
52          {
53              return null;
54          }
55      }
56  
57      public Object getValue()
58      {
59          return m_tee;
60      }
61  
62      public void setAsText(String text) throws IllegalArgumentException
63      {
64          m_tee = m_dao.getById(Long.parseLong(text));
65      }
66  
67      public void setValue(Object value)
68      {
69          if (value == null)
70          {
71              // Do nothing
72              return;
73          }
74          
75          @SuppressWarnings("unchecked")
76          T address = (T)value;
77  
78          assert address.getId() != null : "Id may not be null";
79          m_tee = address;
80      }
81  }