2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 import com
.sun
.star
.uno
.Type
;
19 import com
.sun
.star
.uno
.UnoRuntime
;
20 import com
.sun
.star
.table
.XCell
;
21 import com
.sun
.star
.util
.XModifyListener
;
22 import com
.sun
.star
.text
.XTextRange
;
24 /** a value binding to be connected to a form control
26 This binding synchronizes the text contained in a table cell (which you must
27 pass upon construction) to the text in an XBindableValue.
29 Well, in real it does not synchronize both directions. The ValueBinding
30 service has not much room for own activity: It allows notification of changes
31 in the own value, and it allows external instances to set the current value.
33 Note that we implement this binding as a separate thread, which is (more or
34 less permanently) polling for a new text at the cell. This is unfortunate, but
35 sadly the Writer table cells do not support actively notifying changes in their
36 content to other interested parties.
38 public class TableCellTextBinding
39 extends java
.lang
.Thread
40 implements com
.sun
.star
.form
.binding
.XValueBinding
,
41 com
.sun
.star
.util
.XModifyBroadcaster
43 private XTextRange m_cellText
;
44 private Object m_writeSignal
;
45 private String m_newCellText
;
46 private String m_lastKnownCellText
;
47 private boolean m_haveNewCellText
;
48 private java
.util
.List
<XModifyListener
> m_listeners
;
50 /** Creates a new instance of TableCellTextBinding */
51 public TableCellTextBinding( XCell cell
)
53 m_cellText
= UnoRuntime
.queryInterface( XTextRange
.class, cell
);
56 m_listeners
= new java
.util
.LinkedList
<XModifyListener
>();
61 /** retrieves the list of data types which this binding can exchange
63 public com
.sun
.star
.uno
.Type
[] getSupportedValueTypes()
67 // well, only strings here ...
72 catch( java
.lang
.Exception e
)
75 return new Type
[] { };
78 /** retrieves the current value
80 public Object
getValue(com
.sun
.star
.uno
.Type type
) throws com
.sun
.star
.form
.binding
.IncompatibleTypesException
82 if ( !type
.equals( getStringType() ) )
83 throw new com
.sun
.star
.form
.binding
.IncompatibleTypesException();
85 return m_cellText
.getString();
90 public void setValue(Object obj
) throws com
.sun
.star
.form
.binding
.IncompatibleTypesException
97 catch( java
.lang
.ClassCastException e
)
99 throw new com
.sun
.star
.form
.binding
.IncompatibleTypesException();
101 // remember the new text
102 synchronized( m_newCellText
)
104 m_newCellText
= text
;
105 m_haveNewCellText
= true;
107 // and wake up the thread which is waiting for it
108 synchronized( m_writeSignal
)
110 m_writeSignal
.notify();
114 /** determines whether a given value type is supported
116 public boolean supportsType(com
.sun
.star
.uno
.Type type
)
118 return type
.equals( getStringType() );
121 /** retrieves the UNO type for the string class
123 private static final Type
getStringType()
125 return new com
.sun
.star
.uno
.Type( String
.class );
135 m_writeSignal
= new Object();
139 synchronized( m_writeSignal
)
141 m_writeSignal
.wait( 200 );
144 // if there's new text in the control, propagate it to the cell
145 synchronized ( m_newCellText
)
147 if ( m_haveNewCellText
)
149 m_cellText
.setString( m_newCellText
);
150 m_lastKnownCellText
= m_newCellText
;
152 m_haveNewCellText
= false;
155 // if there's new text in the cell, propagate it to the control
156 String currentCellText
= m_cellText
.getString();
157 if ( !currentCellText
.equals( m_lastKnownCellText
) )
159 m_lastKnownCellText
= currentCellText
;
160 // notify the modification
161 synchronized( m_listeners
)
163 com
.sun
.star
.lang
.EventObject eventSource
= new com
.sun
.star
.lang
.EventObject( this );
165 java
.util
.Iterator
<XModifyListener
> loop
= m_listeners
.iterator();
166 while ( loop
.hasNext() )
168 loop
.next().modified( eventSource
);
174 catch( java
.lang
.Exception e
)
176 e
.printStackTrace(System
.err
);
180 public void addModifyListener(com
.sun
.star
.util
.XModifyListener xModifyListener
)
182 synchronized( m_listeners
)
184 m_listeners
.add( xModifyListener
);
188 public void removeModifyListener(com
.sun
.star
.util
.XModifyListener xModifyListener
)
190 synchronized( m_listeners
)
192 m_listeners
.remove( xModifyListener
);