View Javadoc

1   /*
2    * @(#)$Id: DataBinding.java 742 2009-08-15 17:15:49Z kurmannu $
3    *
4    * Author   : Ueli Kurmann, igesture@uelikurmann.ch
5    *                                   
6    *                                   
7    * Purpose  : 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date       Who     Reason
14   *
15   * 23.03.2008 ukurmann  Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.tool.binding;
27  
28  import java.awt.event.FocusEvent;
29  import java.awt.event.FocusListener;
30  import java.beans.PropertyChangeEvent;
31  import java.beans.PropertyChangeListener;
32  import java.beans.PropertyVetoException;
33  import java.lang.reflect.InvocationTargetException;
34  
35  import javax.swing.JComponent;
36  import javax.swing.SwingUtilities;
37  
38  import org.apache.commons.beanutils.BeanUtils;
39  import org.ximtec.igesture.core.DataObject;
40  
41  public abstract class DataBinding<T extends JComponent> implements FocusListener, PropertyChangeListener {
42  
43    private DataObject dataObject;
44    private String property;
45  
46    /**
47     * Constructor
48     * 
49     * @param dataObject
50     *          the data object
51     * @param property
52     *          the name of the property
53     * @param key
54     *          the key used by the localization handler (can be null)
55     */
56    public DataBinding(DataObject dataObject, String property) {
57      this.dataObject = dataObject;
58      this.dataObject.addPropertyChangeListener(this);
59      this.property = property;
60    }
61  
62    /**
63     * This method provides functionality to update the view in case of model
64     * changes. This method is invoked on a propertyChange event.
65     */
66    protected abstract void updateView();
67  
68    /**
69     * This method provides functionality to update the model.
70     */
71    protected abstract void updateModel();
72  
73    /**
74     * Returns the JComponent which is involved in the binding process.
75     * 
76     * @return
77     */
78    public abstract T getComponent();
79  
80    /**
81     * Set a value in the dataobject
82     * 
83     * @param object
84     * @throws PropertyVetoException
85     */
86    protected void setValue(Object object) {
87      try {
88        BeanUtils.setProperty(dataObject, property, object);
89      } catch (SecurityException e) {
90      } catch (IllegalAccessException e) {
91      } catch (InvocationTargetException e) {
92  
93      }
94    }
95  
96    /**
97     * Read a value from the data object
98     * 
99     * @return
100    */
101   protected String getValue() {
102     try {
103       return BeanUtils.getProperty(dataObject, property);
104     } catch (IllegalAccessException e) {
105     } catch (InvocationTargetException e) {
106     } catch (NoSuchMethodException e) {
107     }
108     return null;
109   }
110 
111   /**
112    * Returns the data object
113    * 
114    * @return
115    */
116   public Object getObject() {
117     return dataObject;
118   }
119 
120   /**
121    * Returns the property name
122    * 
123    * @return
124    */
125   public String getProperty() {
126     return property;
127   }
128 
129   @Override
130   public void focusGained(FocusEvent arg0) {
131     // do nothing
132   }
133 
134   @Override
135   public void focusLost(FocusEvent arg0) {
136     updateModel();
137   }
138 
139   @Override
140   public final void propertyChange(PropertyChangeEvent evt) {
141     if (SwingUtilities.isEventDispatchThread()) {
142       updateView();
143     } else {
144       SwingUtilities.invokeLater(new Runnable() {
145 
146         @Override
147         public void run() {
148           updateView();
149         }
150       });
151     }
152   }
153 }