1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 import com.sun.star.uno.Type;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.table.XCell;
30 import com.sun.star.util.XModifyListener;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.text.XTextRange;
33 import com.sun.star.form.binding.IncompatibleTypesException;
34 
35 /** a value binding to be connected to a form control
36 
37     This binding synchronizes the text contained in a table cell (which you must
38     pass upon construction) to the text in an XBindableValue.
39 
40     Well, in real it does not synchronize both directions. The ValueBinding
41     service has not much room for own activity: It allows notification of changes
42     in the own value, and it allows external instances to set the current value.
43 
44     Note that we implement this binding as a separate thread, which is (more or
45     less permanently) polling for a new text at the cell. This is unfortunate, but
46     sadly the Writer table cells do not support actively notifying changes in their
47     content to other interested parties.
48 */
49 public class TableCellTextBinding
50                 extends     java.lang.Thread
51                 implements  com.sun.star.form.binding.XValueBinding,
52                             com.sun.star.util.XModifyBroadcaster
53 {
54     private XTextRange  m_cellText;
55     private Object      m_writeSignal;
56     private String      m_newCellText;
57     private String      m_lastKnownCellText;
58     private boolean     m_haveNewCellText;
59     private java.util.List  m_listeners;
60 
61     /** Creates a new instance of TableCellTextBinding */
62     public TableCellTextBinding( XCell cell )
63     {
64         m_cellText = (XTextRange)UnoRuntime.queryInterface( XTextRange.class, cell );
65 
66         m_newCellText = new String();
67         m_listeners = new java.util.LinkedList();
68 
69         start();
70     }
71 
72     /** retrieves the list of data types which this binding can exchange
73     */
74     public com.sun.star.uno.Type[] getSupportedValueTypes()
75     {
76         try
77         {
78             // well, only strings here ...
79             return new Type[] {
80                 getStringType()
81             };
82         }
83         catch( java.lang.Exception e )
84         {
85         }
86         return new Type[] { };
87     }
88 
89     /** retrieves the current value
90     */
91     public Object getValue(com.sun.star.uno.Type type) throws com.sun.star.form.binding.IncompatibleTypesException
92     {
93         if ( !type.equals( getStringType() ) )
94             throw new com.sun.star.form.binding.IncompatibleTypesException();
95 
96         return m_cellText.getString();
97     }
98 
99     /** sets a new value
100     */
101     public void setValue(Object obj) throws com.sun.star.form.binding.IncompatibleTypesException
102     {
103         String text;
104         try
105         {
106             text = (String)obj;
107         }
108         catch( java.lang.ClassCastException e )
109         {
110             throw new com.sun.star.form.binding.IncompatibleTypesException();
111         }
112         // remember the new text
113         synchronized( m_newCellText )
114         {
115             m_newCellText = text;
116             m_haveNewCellText = true;
117         }
118         // and wake up the thread which is waiting for it
119         synchronized( m_writeSignal )
120         {
121             m_writeSignal.notify();
122         }
123     }
124 
125     /** determines whether a given value type is supported
126     */
127     public boolean supportsType(com.sun.star.uno.Type type)
128     {
129         return type.equals( getStringType() );
130     }
131 
132     /** retrieves the UNO type for the string class
133     */
134     private static final Type getStringType()
135     {
136         return new com.sun.star.uno.Type( String.class );
137     }
138 
139     /** runs the thread
140     */
141     public void run()
142     {
143         try
144         {
145             m_writeSignal = new Object();
146             while ( true )
147             {
148                 // go sleep a while
149                 synchronized( m_writeSignal )
150                 {
151                     m_writeSignal.wait( 200 );
152                 }
153 
154                 // if there's new text in the control, propagate it to the cell
155                 synchronized ( m_newCellText )
156                 {
157                     if ( m_haveNewCellText )
158                     {
159                         m_cellText.setString( m_newCellText );
160                         m_lastKnownCellText = m_newCellText;
161                     }
162                     m_haveNewCellText = false;
163                 }
164 
165                 // if there's new text in the cell, propagate it to the control
166                 String currentCellText = m_cellText.getString();
167                 if ( !currentCellText.equals( m_lastKnownCellText ) )
168                 {
169                     m_lastKnownCellText = currentCellText;
170                     // notify the modification
171                     synchronized( m_listeners )
172                     {
173                         com.sun.star.lang.EventObject eventSource = new com.sun.star.lang.EventObject( this );
174 
175                         java.util.Iterator loop = m_listeners.iterator();
176                         while ( loop.hasNext() )
177                         {
178                             ((XModifyListener)loop.next()).modified( eventSource );
179                         }
180                     }
181                 }
182             }
183         }
184         catch( java.lang.Exception e )
185         {
186             e.printStackTrace(System.err);
187         }
188     }
189 
190     public void addModifyListener(com.sun.star.util.XModifyListener xModifyListener)
191     {
192         synchronized( m_listeners )
193         {
194             m_listeners.add( xModifyListener );
195         }
196     }
197 
198     public void removeModifyListener(com.sun.star.util.XModifyListener xModifyListener)
199     {
200         synchronized( m_listeners )
201         {
202             m_listeners.remove( xModifyListener );
203         }
204     }
205 
206     public void disposing(com.sun.star.lang.EventObject eventObject)
207     {
208         // not interested in
209     }
210 }
211