134dd1e25SAndrew Rist /**************************************************************
234dd1e25SAndrew Rist  *
334dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
434dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
534dd1e25SAndrew Rist  * distributed with this work for additional information
634dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
734dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
834dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
934dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
1034dd1e25SAndrew Rist  *
1134dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1234dd1e25SAndrew Rist  *
1334dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1434dd1e25SAndrew Rist  * software distributed under the License is distributed on an
1534dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1634dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
1734dd1e25SAndrew Rist  * specific language governing permissions and limitations
1834dd1e25SAndrew Rist  * under the License.
1934dd1e25SAndrew Rist  *
2034dd1e25SAndrew Rist  *************************************************************/
2134dd1e25SAndrew Rist 
2234dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.*;
25cdf0e10cSrcweir import com.sun.star.beans.*;
26cdf0e10cSrcweir import com.sun.star.form.*;
27cdf0e10cSrcweir import com.sun.star.lang.*;
28cdf0e10cSrcweir import com.sun.star.sdb.*;
29cdf0e10cSrcweir import com.sun.star.sdbc.*;
30cdf0e10cSrcweir import com.sun.star.sdbcx.*;
31cdf0e10cSrcweir import com.sun.star.container.*;
32cdf0e10cSrcweir import com.sun.star.awt.*;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /**************************************************************************/
35cdf0e10cSrcweir /** base class for helpers dealing with unique column values
36cdf0e10cSrcweir */
37cdf0e10cSrcweir class UniqueColumnValue
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
40cdf0e10cSrcweir 	/** extracts the name of the table a form is based on.
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 		<p>This method works for forms based directly on tables, and for forms based on statements, which
43cdf0e10cSrcweir 		themself are based on one table.<br/>
44cdf0e10cSrcweir 		Everything else (especially forms based on queries) is not yet implemented.</p>
45cdf0e10cSrcweir 	*/
extractTableName( XPropertySet xForm )46cdf0e10cSrcweir 	protected String extractTableName( XPropertySet xForm ) throws com.sun.star.uno.Exception
47cdf0e10cSrcweir 	{
48cdf0e10cSrcweir 		String sReturn;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 		Integer aCommandType = (Integer)xForm.getPropertyValue( "CommandType" );
51cdf0e10cSrcweir 		String sCommand = (String)xForm.getPropertyValue( "Command" );
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 		if ( CommandType.COMMAND == aCommandType.intValue() )
54cdf0e10cSrcweir 		{
55cdf0e10cSrcweir 			// get the connection from the form
56cdf0e10cSrcweir             XConnection xFormConn = (XConnection)UnoRuntime.queryInterface( XConnection.class,
57cdf0e10cSrcweir                 xForm.getPropertyValue( "ActiveConnection" ) );
58cdf0e10cSrcweir 			// and let it create a composer for us
59cdf0e10cSrcweir 			XSQLQueryComposerFactory xComposerFac =
60cdf0e10cSrcweir 				(XSQLQueryComposerFactory)UnoRuntime.queryInterface(
61cdf0e10cSrcweir 					XSQLQueryComposerFactory.class, xFormConn );
62cdf0e10cSrcweir 			XSQLQueryComposer xComposer = xComposerFac.createQueryComposer( );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 			// let this composer analyze the command
65cdf0e10cSrcweir 			xComposer.setQuery( sCommand );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 			// and ask it for the table(s)
68cdf0e10cSrcweir 			XTablesSupplier xSuppTables = (XTablesSupplier)UnoRuntime.queryInterface(
69cdf0e10cSrcweir 				XTablesSupplier.class, xComposer );
70cdf0e10cSrcweir 			XNameAccess xTables = xSuppTables.getTables();
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 			// simply take the first table name
73cdf0e10cSrcweir 			String[] aNames = xTables.getElementNames( );
74cdf0e10cSrcweir 			sCommand = aNames[0];
75cdf0e10cSrcweir 		}
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 		return sCommand;
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
81cdf0e10cSrcweir 	/** generates a statement which can be used to create a unique (in all conscience) value
82cdf0e10cSrcweir 		for the column given.
83cdf0e10cSrcweir 		<p>Currently, the implementation uses a very simple approach - it just determines the maximum of currently
84cdf0e10cSrcweir 		existing values in the column. If your concrete data source supports a more sophisticated approach of generating
85cdf0e10cSrcweir 		unique values, you probably want to adjust the <code>SELECT</code> statement below accordingly.</p>
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 		@returns
88cdf0e10cSrcweir 			a String which can be used as statement to retrieve a unique value for the given column.
89cdf0e10cSrcweir 			The result set resulting from such a execution contains the value in it's first column.
90cdf0e10cSrcweir 	*/
composeUniqueyKeyStatement( XPropertySet xForm, String sFieldName )91cdf0e10cSrcweir 	protected String composeUniqueyKeyStatement( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		String sStatement = new String( "SELECT MAX( " );
94cdf0e10cSrcweir 		sStatement += sFieldName;
95cdf0e10cSrcweir 		sStatement += new String( ") + 1 FROM " );
96cdf0e10cSrcweir 		// the table name is a property of the form
97cdf0e10cSrcweir 		sStatement += extractTableName( xForm );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 		// note that the implementation is imperfect (besides the problem that MAX is not a really good solution
100cdf0e10cSrcweir 		// for a database with more that one client):
101cdf0e10cSrcweir 		// It does not quote the field and the table name. This needs to be done if the database is intolerant
102cdf0e10cSrcweir 		// against such things - the XDatabaseMetaData, obtained from the connection, would be needed then
103cdf0e10cSrcweir 		// Unfortunately, there is no UNO service doing this - it would need to be implemented manually.
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 		return sStatement;
106cdf0e10cSrcweir 	}
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
109cdf0e10cSrcweir 	/** generates a unique (in all conscience) key into the column given
110cdf0e10cSrcweir 		@param xForm
111cdf0e10cSrcweir 			the form which contains the column in question
112cdf0e10cSrcweir 		@param sFieldName
113cdf0e10cSrcweir 			the name of the column
114cdf0e10cSrcweir 	*/
generatePrimaryKey( XPropertySet xForm, String sFieldName )115cdf0e10cSrcweir 	protected int generatePrimaryKey( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		// get the current connection of the form
118cdf0e10cSrcweir 		XConnection xConn = (XConnection)UnoRuntime.queryInterface(
119cdf0e10cSrcweir 			XConnection.class, xForm.getPropertyValue( "ActiveConnection" ) );
120cdf0e10cSrcweir 		// let it create a new statement
121cdf0e10cSrcweir 		XStatement xStatement = xConn.createStatement();
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 		// build the query string to determine a free value
124cdf0e10cSrcweir 		String sStatement = composeUniqueyKeyStatement( xForm, sFieldName );
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 		// execute the query
127cdf0e10cSrcweir 		XResultSet xResults = xStatement.executeQuery( sStatement );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 		// move the result set to the first record
130cdf0e10cSrcweir 		xResults.next( );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 		// get the value
133cdf0e10cSrcweir 		XRow xRow = (XRow)UnoRuntime.queryInterface( XRow.class, xResults );
134cdf0e10cSrcweir 		int nFreeValue = xRow.getInt( 1 );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		// dispose the temporary objects
137cdf0e10cSrcweir 		FLTools.disposeComponent( xStatement );
138cdf0e10cSrcweir 			// this should get rid of the result set, too
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 		return nFreeValue;
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
144cdf0e10cSrcweir 	/** inserts a unique (in all conscience) key into the column given
145cdf0e10cSrcweir 		@param xForm
146cdf0e10cSrcweir 			the form which contains the column in question
147cdf0e10cSrcweir 		@param sFieldName
148cdf0e10cSrcweir 			the name of the column
149cdf0e10cSrcweir 	*/
insertPrimaryKey( XPropertySet xForm, String sFieldName )150cdf0e10cSrcweir 	public void insertPrimaryKey( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
151cdf0e10cSrcweir 	{
152cdf0e10cSrcweir 		// check the privileges
153cdf0e10cSrcweir 		Integer aConcurrency = (Integer)xForm.getPropertyValue( "ResultSetConcurrency" );
154cdf0e10cSrcweir 		if ( ResultSetConcurrency.READ_ONLY != aConcurrency.intValue() )
155cdf0e10cSrcweir 		{
156cdf0e10cSrcweir 			// get the column object
157cdf0e10cSrcweir 			XColumnsSupplier xSuppCols = (XColumnsSupplier)UnoRuntime.queryInterface(
158cdf0e10cSrcweir 				XColumnsSupplier.class, xForm );
159cdf0e10cSrcweir 			XNameAccess xCols = xSuppCols.getColumns();
160cdf0e10cSrcweir 			XColumnUpdate xCol = (XColumnUpdate)UnoRuntime.queryInterface(
161cdf0e10cSrcweir 				XColumnUpdate.class, xCols.getByName( sFieldName ) );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 			xCol.updateInt( generatePrimaryKey( xForm, sFieldName ) );
164cdf0e10cSrcweir 		}
165cdf0e10cSrcweir 	}
166cdf0e10cSrcweir };
167cdf0e10cSrcweir 
168cdf0e10cSrcweir /**************************************************************************/
169cdf0e10cSrcweir /** base class for helpers dealing with unique column values
170cdf0e10cSrcweir */
171cdf0e10cSrcweir class KeyGeneratorForReset extends UniqueColumnValue implements XResetListener
172cdf0e10cSrcweir {
173cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
174cdf0e10cSrcweir 	private DocumentViewHelper	m_aView;
175cdf0e10cSrcweir 	private	String				m_sFieldName;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
178cdf0e10cSrcweir 	/** ctor
179cdf0e10cSrcweir 		@param aView
180cdf0e10cSrcweir 			the view which shall be used to focus controls
181cdf0e10cSrcweir 		@param sFieldName
182cdf0e10cSrcweir 			the name of the field for which keys should be generated
183cdf0e10cSrcweir 	*/
KeyGeneratorForReset( String sFieldName, DocumentViewHelper aView )184cdf0e10cSrcweir 	public KeyGeneratorForReset( String sFieldName, DocumentViewHelper aView )
185cdf0e10cSrcweir 	{
186cdf0e10cSrcweir 		m_sFieldName = sFieldName;
187cdf0e10cSrcweir 		m_aView = aView;
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
191cdf0e10cSrcweir 	/** sets the focus to the first control which is no fixed text, and not the
192cdf0e10cSrcweir 		one we're defaulting
193cdf0e10cSrcweir 	*/
defaultNewRecordFocus( XPropertySet xForm )194cdf0e10cSrcweir 	public void defaultNewRecordFocus( XPropertySet xForm ) throws com.sun.star.uno.Exception
195cdf0e10cSrcweir 	{
196cdf0e10cSrcweir 		XIndexAccess xFormAsContainer = (XIndexAccess)UnoRuntime.queryInterface(
197cdf0e10cSrcweir 			XIndexAccess.class, xForm );
198cdf0e10cSrcweir 		for ( int i = 0; i<xFormAsContainer.getCount(); ++i )
199cdf0e10cSrcweir 		{
200cdf0e10cSrcweir 			// the model
201cdf0e10cSrcweir 			XPropertySet xModel = UNO.queryPropertySet( xFormAsContainer.getByIndex( i ) );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 			// check if it's a valid leaf (no sub form or such)
204cdf0e10cSrcweir 			XPropertySetInfo xPSI = xModel.getPropertySetInfo( );
205cdf0e10cSrcweir 			if ( ( null == xPSI ) || !xPSI.hasPropertyByName( "ClassId" ) )
206cdf0e10cSrcweir 				continue;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 			// check if it's a fixed text
209cdf0e10cSrcweir 			Short nClassId = (Short)xModel.getPropertyValue( "ClassId" );
210cdf0e10cSrcweir 			if ( FormComponentType.FIXEDTEXT == nClassId.shortValue() )
211cdf0e10cSrcweir 				continue;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 			// check if it is bound to the field we are responsible for
214cdf0e10cSrcweir 			if ( !xPSI.hasPropertyByName( "DataField" ) )
215cdf0e10cSrcweir 				continue;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 			String sFieldDataSource = (String)xModel.getPropertyValue( "DataField" );
218cdf0e10cSrcweir 			if ( sFieldDataSource.equals( m_sFieldName ) )
219cdf0e10cSrcweir 				continue;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 			// both conditions do not apply
222cdf0e10cSrcweir 			// -> set the focus into the respective control
223cdf0e10cSrcweir 			XControlModel xCM = UNO.queryControlModel( xModel );
224cdf0e10cSrcweir 			m_aView.grabControlFocus( xCM);
225cdf0e10cSrcweir 			break;
226cdf0e10cSrcweir 		}
227cdf0e10cSrcweir 	}
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
230cdf0e10cSrcweir 	// XResetListener overridables
231cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
approveReset( com.sun.star.lang.EventObject rEvent )232cdf0e10cSrcweir     public boolean approveReset( com.sun.star.lang.EventObject rEvent ) throws com.sun.star.uno.RuntimeException
233cdf0e10cSrcweir 	{
234cdf0e10cSrcweir 		// not interested in vetoing this
235cdf0e10cSrcweir 		return true;
236cdf0e10cSrcweir 	}
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
resetted( com.sun.star.lang.EventObject aEvent )239cdf0e10cSrcweir     public void resetted( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
240cdf0e10cSrcweir 	{
241*a893be29SPedro Giffuni 		// check if this reset occurred becase we're on a new record
242cdf0e10cSrcweir 		XPropertySet xFormProps = UNO.queryPropertySet( aEvent.Source );
243cdf0e10cSrcweir 		try
244cdf0e10cSrcweir 		{
245cdf0e10cSrcweir 			Boolean aIsNew = (Boolean)xFormProps.getPropertyValue( "IsNew" );
246cdf0e10cSrcweir 			if ( aIsNew.booleanValue() )
247cdf0e10cSrcweir 			{	// yepp
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 				// we're going to modify the record, though after that, to the user, it should look
250cdf0e10cSrcweir 				// like it has not been modified
251cdf0e10cSrcweir 				// So we need to ensure that we do not change the IsModified property with whatever we do
252cdf0e10cSrcweir 				Object aModifiedFlag = xFormProps.getPropertyValue( "IsModified" );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 				// now set the value
255cdf0e10cSrcweir 				insertPrimaryKey( xFormProps, m_sFieldName );
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 				// then restore the flag
258cdf0e10cSrcweir 				xFormProps.setPropertyValue( "IsModified", aModifiedFlag );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 				// still one thing ... would be nice to have the focus in a control which is
261cdf0e10cSrcweir 				// the one which's value we just defaulted
262cdf0e10cSrcweir 				defaultNewRecordFocus( xFormProps );
263cdf0e10cSrcweir 			}
264cdf0e10cSrcweir 		}
265cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
266cdf0e10cSrcweir 		{
267cdf0e10cSrcweir 			System.out.println(e);
268cdf0e10cSrcweir 			e.printStackTrace();
269cdf0e10cSrcweir 		}
270cdf0e10cSrcweir 	}
271cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
272cdf0e10cSrcweir 	// XEventListener overridables
273cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
disposing( EventObject aEvent )274cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
275cdf0e10cSrcweir 	{
276cdf0e10cSrcweir 		// not interested in
277cdf0e10cSrcweir 	}
278cdf0e10cSrcweir };
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 
281cdf0e10cSrcweir /**************************************************************************/
282cdf0e10cSrcweir /** base class for helpers dealing with unique column values
283cdf0e10cSrcweir */
284cdf0e10cSrcweir class KeyGeneratorForUpdate extends UniqueColumnValue implements XRowSetApproveListener
285cdf0e10cSrcweir {
286cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
287cdf0e10cSrcweir 	private	String	m_sFieldName;
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
KeyGeneratorForUpdate( String sFieldName )290cdf0e10cSrcweir 	public KeyGeneratorForUpdate( String sFieldName )
291cdf0e10cSrcweir 	{
292cdf0e10cSrcweir 		m_sFieldName = sFieldName;
293cdf0e10cSrcweir 	}
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
296cdf0e10cSrcweir 	// XRowSetApproveListener overridables
297cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
approveCursorMove( com.sun.star.lang.EventObject aEvent )298cdf0e10cSrcweir     public boolean approveCursorMove( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir 		// not interested in vetoing moves
301cdf0e10cSrcweir 		return true;
302cdf0e10cSrcweir 	}
303cdf0e10cSrcweir 
304cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
approveRowChange( RowChangeEvent aEvent )305cdf0e10cSrcweir     public boolean approveRowChange( RowChangeEvent aEvent ) throws com.sun.star.uno.RuntimeException
306cdf0e10cSrcweir 	{
307cdf0e10cSrcweir 		if ( RowChangeAction.INSERT == aEvent.Action )
308cdf0e10cSrcweir 		{
309cdf0e10cSrcweir 			try
310cdf0e10cSrcweir 			{
311cdf0e10cSrcweir 				// the affected form
312cdf0e10cSrcweir 				XPropertySet xFormProps = UNO.queryPropertySet( aEvent.Source );
313cdf0e10cSrcweir 				// insert a new unique value
314cdf0e10cSrcweir 				insertPrimaryKey( xFormProps, m_sFieldName );
315cdf0e10cSrcweir 			}
316cdf0e10cSrcweir 			catch( com.sun.star.uno.Exception e )
317cdf0e10cSrcweir 			{
318cdf0e10cSrcweir 				System.out.println(e);
319cdf0e10cSrcweir 				e.printStackTrace();
320cdf0e10cSrcweir 			}
321cdf0e10cSrcweir 		}
322cdf0e10cSrcweir 		return true;
323cdf0e10cSrcweir 	}
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
approveRowSetChange( com.sun.star.lang.EventObject aEvent )326cdf0e10cSrcweir     public boolean approveRowSetChange( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
327cdf0e10cSrcweir 	{
328cdf0e10cSrcweir 		// not interested in vetoing executions of the row set
329cdf0e10cSrcweir 		return true;
330cdf0e10cSrcweir 	}
331cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
332cdf0e10cSrcweir 	// XEventListener overridables
333cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
disposing( EventObject aEvent )334cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
335cdf0e10cSrcweir 	{
336cdf0e10cSrcweir 		// not interested in
337cdf0e10cSrcweir 	}
338cdf0e10cSrcweir };
339cdf0e10cSrcweir 
340cdf0e10cSrcweir /**************************************************************************/
341cdf0e10cSrcweir /** allows to generate unique keys for a field of a Form
342cdf0e10cSrcweir */
343cdf0e10cSrcweir public class KeyGenerator
344cdf0e10cSrcweir {
345cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
346cdf0e10cSrcweir 	private	KeyGeneratorForReset	m_aResetKeyGenerator;
347cdf0e10cSrcweir 	private KeyGeneratorForUpdate	m_aUpdateKeyGenerator;
348cdf0e10cSrcweir 	private boolean					m_bResetListening;
349cdf0e10cSrcweir 	private boolean					m_bUpdateListening;
350cdf0e10cSrcweir 
351cdf0e10cSrcweir 	private DocumentHelper			m_aDocument;
352cdf0e10cSrcweir 	private	XPropertySet			m_xForm;
353cdf0e10cSrcweir 
354cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
355cdf0e10cSrcweir 	/** ctor
356cdf0e10cSrcweir 		@param xForm
357cdf0e10cSrcweir 			specified the form to operate on
358cdf0e10cSrcweir 		@param sFieldName
359cdf0e10cSrcweir 			specifies the field which's value should be manipulated
360cdf0e10cSrcweir 	*/
KeyGenerator( XPropertySet xForm, String sFieldName, XComponentContext xCtx )361cdf0e10cSrcweir 	public KeyGenerator( XPropertySet xForm, String sFieldName,
362cdf0e10cSrcweir                          XComponentContext xCtx )
363cdf0e10cSrcweir 	{
364cdf0e10cSrcweir 		m_xForm = xForm;
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 		DocumentHelper aDocument = DocumentHelper.getDocumentForComponent( xForm, xCtx );
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 		m_aResetKeyGenerator = new KeyGeneratorForReset( sFieldName, aDocument.getCurrentView() );
369cdf0e10cSrcweir 		m_aUpdateKeyGenerator = new KeyGeneratorForUpdate( sFieldName );
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 		m_bResetListening = m_bUpdateListening = false;
372cdf0e10cSrcweir 	}
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
375cdf0e10cSrcweir 	/** stops any actions on the form
376cdf0e10cSrcweir 	*/
stopGenerator( )377cdf0e10cSrcweir 	public void stopGenerator( )
378cdf0e10cSrcweir 	{
379cdf0e10cSrcweir 		XReset xFormReset = UNO.queryReset( m_xForm );
380cdf0e10cSrcweir 		xFormReset.removeResetListener( m_aResetKeyGenerator );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 		XRowSetApproveBroadcaster xFormBroadcaster = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
383cdf0e10cSrcweir 			XRowSetApproveBroadcaster.class, m_xForm );
384cdf0e10cSrcweir 		xFormBroadcaster.removeRowSetApproveListener( m_aUpdateKeyGenerator );
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 		m_bUpdateListening = m_bResetListening = false;
387cdf0e10cSrcweir 	}
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
390cdf0e10cSrcweir 	/** activates one of our two key generators
391cdf0e10cSrcweir 	*/
activateKeyGenerator( boolean bGenerateOnReset )392cdf0e10cSrcweir 	public void activateKeyGenerator( boolean bGenerateOnReset )
393cdf0e10cSrcweir 	{
394cdf0e10cSrcweir 		// for resets
395cdf0e10cSrcweir 		XReset xFormReset = UNO.queryReset( m_xForm );
396cdf0e10cSrcweir 		// for approving actions
397cdf0e10cSrcweir 		XRowSetApproveBroadcaster xFormBroadcaster = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
398cdf0e10cSrcweir 			XRowSetApproveBroadcaster.class, m_xForm );
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 		if ( bGenerateOnReset )
401cdf0e10cSrcweir 		{
402cdf0e10cSrcweir 			if ( !m_bResetListening )
403cdf0e10cSrcweir 				xFormReset.addResetListener( m_aResetKeyGenerator );
404cdf0e10cSrcweir 			if ( m_bUpdateListening )
405cdf0e10cSrcweir 				xFormBroadcaster.removeRowSetApproveListener( m_aUpdateKeyGenerator );
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 			m_bUpdateListening = false;
408cdf0e10cSrcweir 			m_bResetListening = true;
409cdf0e10cSrcweir 		}
410cdf0e10cSrcweir 		else
411cdf0e10cSrcweir 		{
412cdf0e10cSrcweir 			if ( m_bResetListening )
413cdf0e10cSrcweir 				xFormReset.removeResetListener( m_aResetKeyGenerator );
414cdf0e10cSrcweir 			if ( !m_bUpdateListening )
415cdf0e10cSrcweir 				xFormBroadcaster.addRowSetApproveListener( m_aUpdateKeyGenerator );
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 			m_bResetListening = false;
418cdf0e10cSrcweir 			m_bUpdateListening = true;
419cdf0e10cSrcweir 		}
420cdf0e10cSrcweir 	}
421cdf0e10cSrcweir };
422