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