1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TableCellTextBinding.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 import com
.sun
.star
.uno
.Type
;
31 import com
.sun
.star
.uno
.UnoRuntime
;
32 import com
.sun
.star
.table
.XCell
;
33 import com
.sun
.star
.util
.XModifyListener
;
34 import com
.sun
.star
.beans
.XPropertySet
;
35 import com
.sun
.star
.text
.XTextRange
;
36 import com
.sun
.star
.form
.binding
.IncompatibleTypesException
;
38 /** a value binding to be connected to a form control
40 This binding synchronizes the text contained in a table cell (which you must
41 pass upon construction) to the text in an XBindableValue.
43 Well, in real it does not synchronize both directions. The ValueBinding
44 service has not much room for own activity: It allows notification of changes
45 in the own value, and it allows external instances to set the current value.
47 Note that we implement this binding as a separate thread, which is (more or
48 less permanently) polling for a new text at the cell. This is unfortunate, but
49 sadly the Writer table cells do not support actively notifying changes in their
50 content to other interested parties.
52 public class TableCellTextBinding
53 extends java
.lang
.Thread
54 implements com
.sun
.star
.form
.binding
.XValueBinding
,
55 com
.sun
.star
.util
.XModifyBroadcaster
57 private XTextRange m_cellText
;
58 private Object m_writeSignal
;
59 private String m_newCellText
;
60 private String m_lastKnownCellText
;
61 private boolean m_haveNewCellText
;
62 private java
.util
.List m_listeners
;
64 /** Creates a new instance of TableCellTextBinding */
65 public TableCellTextBinding( XCell cell
)
67 m_cellText
= (XTextRange
)UnoRuntime
.queryInterface( XTextRange
.class, cell
);
69 m_newCellText
= new String();
70 m_listeners
= new java
.util
.LinkedList();
75 /** retrieves the list of data types which this binding can exchange
77 public com
.sun
.star
.uno
.Type
[] getSupportedValueTypes()
81 // well, only strings here ...
86 catch( java
.lang
.Exception e
)
89 return new Type
[] { };
92 /** retrieves the current value
94 public Object
getValue(com
.sun
.star
.uno
.Type type
) throws com
.sun
.star
.form
.binding
.IncompatibleTypesException
96 if ( !type
.equals( getStringType() ) )
97 throw new com
.sun
.star
.form
.binding
.IncompatibleTypesException();
99 return m_cellText
.getString();
104 public void setValue(Object obj
) throws com
.sun
.star
.form
.binding
.IncompatibleTypesException
111 catch( java
.lang
.ClassCastException e
)
113 throw new com
.sun
.star
.form
.binding
.IncompatibleTypesException();
115 // remember the new text
116 synchronized( m_newCellText
)
118 m_newCellText
= text
;
119 m_haveNewCellText
= true;
121 // and wake up the thread which is waiting for it
122 synchronized( m_writeSignal
)
124 m_writeSignal
.notify();
128 /** determines whether a given value type is supported
130 public boolean supportsType(com
.sun
.star
.uno
.Type type
)
132 return type
.equals( getStringType() );
135 /** retrieves the UNO type for the string class
137 private static final Type
getStringType()
139 return new com
.sun
.star
.uno
.Type( String
.class );
148 m_writeSignal
= new Object();
152 synchronized( m_writeSignal
)
154 m_writeSignal
.wait( 200 );
157 // if there's new text in the control, propagate it to the cell
158 synchronized ( m_newCellText
)
160 if ( m_haveNewCellText
)
162 m_cellText
.setString( m_newCellText
);
163 m_lastKnownCellText
= m_newCellText
;
165 m_haveNewCellText
= false;
168 // if there's new text in the cell, propagate it to the control
169 String currentCellText
= m_cellText
.getString();
170 if ( !currentCellText
.equals( m_lastKnownCellText
) )
172 m_lastKnownCellText
= currentCellText
;
173 // notify the modification
174 synchronized( m_listeners
)
176 com
.sun
.star
.lang
.EventObject eventSource
= new com
.sun
.star
.lang
.EventObject( this );
178 java
.util
.Iterator loop
= m_listeners
.iterator();
179 while ( loop
.hasNext() )
181 ((XModifyListener
)loop
.next()).modified( eventSource
);
187 catch( java
.lang
.Exception e
)
189 e
.printStackTrace(System
.err
);
193 public void addModifyListener(com
.sun
.star
.util
.XModifyListener xModifyListener
)
195 synchronized( m_listeners
)
197 m_listeners
.add( xModifyListener
);
201 public void removeModifyListener(com
.sun
.star
.util
.XModifyListener xModifyListener
)
203 synchronized( m_listeners
)
205 m_listeners
.remove( xModifyListener
);
209 public void disposing(com
.sun
.star
.lang
.EventObject eventObject
)