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