1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.uno.*;
36*cdf0e10cSrcweir import com.sun.star.beans.*;
37*cdf0e10cSrcweir import com.sun.star.form.*;
38*cdf0e10cSrcweir import com.sun.star.lang.*;
39*cdf0e10cSrcweir import com.sun.star.sdb.*;
40*cdf0e10cSrcweir import com.sun.star.sdbc.*;
41*cdf0e10cSrcweir import com.sun.star.sdbcx.*;
42*cdf0e10cSrcweir import com.sun.star.container.*;
43*cdf0e10cSrcweir import com.sun.star.awt.*;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /**************************************************************************/
46*cdf0e10cSrcweir /** base class for helpers dealing with unique column values
47*cdf0e10cSrcweir */
48*cdf0e10cSrcweir class UniqueColumnValue
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
51*cdf0e10cSrcweir 	/** extracts the name of the table a form is based on.
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 		<p>This method works for forms based directly on tables, and for forms based on statements, which
54*cdf0e10cSrcweir 		themself are based on one table.<br/>
55*cdf0e10cSrcweir 		Everything else (especially forms based on queries) is not yet implemented.</p>
56*cdf0e10cSrcweir 	*/
57*cdf0e10cSrcweir 	protected String extractTableName( XPropertySet xForm ) throws com.sun.star.uno.Exception
58*cdf0e10cSrcweir 	{
59*cdf0e10cSrcweir 		String sReturn;
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 		Integer aCommandType = (Integer)xForm.getPropertyValue( "CommandType" );
62*cdf0e10cSrcweir 		String sCommand = (String)xForm.getPropertyValue( "Command" );
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 		if ( CommandType.COMMAND == aCommandType.intValue() )
65*cdf0e10cSrcweir 		{
66*cdf0e10cSrcweir 			// get the connection from the form
67*cdf0e10cSrcweir             XConnection xFormConn = (XConnection)UnoRuntime.queryInterface( XConnection.class,
68*cdf0e10cSrcweir                 xForm.getPropertyValue( "ActiveConnection" ) );
69*cdf0e10cSrcweir 			// and let it create a composer for us
70*cdf0e10cSrcweir 			XSQLQueryComposerFactory xComposerFac =
71*cdf0e10cSrcweir 				(XSQLQueryComposerFactory)UnoRuntime.queryInterface(
72*cdf0e10cSrcweir 					XSQLQueryComposerFactory.class, xFormConn );
73*cdf0e10cSrcweir 			XSQLQueryComposer xComposer = xComposerFac.createQueryComposer( );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 			// let this composer analyze the command
76*cdf0e10cSrcweir 			xComposer.setQuery( sCommand );
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 			// and ask it for the table(s)
79*cdf0e10cSrcweir 			XTablesSupplier xSuppTables = (XTablesSupplier)UnoRuntime.queryInterface(
80*cdf0e10cSrcweir 				XTablesSupplier.class, xComposer );
81*cdf0e10cSrcweir 			XNameAccess xTables = xSuppTables.getTables();
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 			// simply take the first table name
84*cdf0e10cSrcweir 			String[] aNames = xTables.getElementNames( );
85*cdf0e10cSrcweir 			sCommand = aNames[0];
86*cdf0e10cSrcweir 		}
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 		return sCommand;
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
92*cdf0e10cSrcweir 	/** generates a statement which can be used to create a unique (in all conscience) value
93*cdf0e10cSrcweir 		for the column given.
94*cdf0e10cSrcweir 		<p>Currently, the implementation uses a very simple approach - it just determines the maximum of currently
95*cdf0e10cSrcweir 		existing values in the column. If your concrete data source supports a more sophisticated approach of generating
96*cdf0e10cSrcweir 		unique values, you probably want to adjust the <code>SELECT</code> statement below accordingly.</p>
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 		@returns
99*cdf0e10cSrcweir 			a String which can be used as statement to retrieve a unique value for the given column.
100*cdf0e10cSrcweir 			The result set resulting from such a execution contains the value in it's first column.
101*cdf0e10cSrcweir 	*/
102*cdf0e10cSrcweir 	protected String composeUniqueyKeyStatement( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 		String sStatement = new String( "SELECT MAX( " );
105*cdf0e10cSrcweir 		sStatement += sFieldName;
106*cdf0e10cSrcweir 		sStatement += new String( ") + 1 FROM " );
107*cdf0e10cSrcweir 		// the table name is a property of the form
108*cdf0e10cSrcweir 		sStatement += extractTableName( xForm );
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 		// note that the implementation is imperfect (besides the problem that MAX is not a really good solution
111*cdf0e10cSrcweir 		// for a database with more that one client):
112*cdf0e10cSrcweir 		// It does not quote the field and the table name. This needs to be done if the database is intolerant
113*cdf0e10cSrcweir 		// against such things - the XDatabaseMetaData, obtained from the connection, would be needed then
114*cdf0e10cSrcweir 		// Unfortunately, there is no UNO service doing this - it would need to be implemented manually.
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 		return sStatement;
117*cdf0e10cSrcweir 	}
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
120*cdf0e10cSrcweir 	/** generates a unique (in all conscience) key into the column given
121*cdf0e10cSrcweir 		@param xForm
122*cdf0e10cSrcweir 			the form which contains the column in question
123*cdf0e10cSrcweir 		@param sFieldName
124*cdf0e10cSrcweir 			the name of the column
125*cdf0e10cSrcweir 	*/
126*cdf0e10cSrcweir 	protected int generatePrimaryKey( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
127*cdf0e10cSrcweir 	{
128*cdf0e10cSrcweir 		// get the current connection of the form
129*cdf0e10cSrcweir 		XConnection xConn = (XConnection)UnoRuntime.queryInterface(
130*cdf0e10cSrcweir 			XConnection.class, xForm.getPropertyValue( "ActiveConnection" ) );
131*cdf0e10cSrcweir 		// let it create a new statement
132*cdf0e10cSrcweir 		XStatement xStatement = xConn.createStatement();
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		// build the query string to determine a free value
135*cdf0e10cSrcweir 		String sStatement = composeUniqueyKeyStatement( xForm, sFieldName );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		// execute the query
138*cdf0e10cSrcweir 		XResultSet xResults = xStatement.executeQuery( sStatement );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 		// move the result set to the first record
141*cdf0e10cSrcweir 		xResults.next( );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 		// get the value
144*cdf0e10cSrcweir 		XRow xRow = (XRow)UnoRuntime.queryInterface( XRow.class, xResults );
145*cdf0e10cSrcweir 		int nFreeValue = xRow.getInt( 1 );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 		// dispose the temporary objects
148*cdf0e10cSrcweir 		FLTools.disposeComponent( xStatement );
149*cdf0e10cSrcweir 			// this should get rid of the result set, too
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		return nFreeValue;
152*cdf0e10cSrcweir 	}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
155*cdf0e10cSrcweir 	/** inserts a unique (in all conscience) key into the column given
156*cdf0e10cSrcweir 		@param xForm
157*cdf0e10cSrcweir 			the form which contains the column in question
158*cdf0e10cSrcweir 		@param sFieldName
159*cdf0e10cSrcweir 			the name of the column
160*cdf0e10cSrcweir 	*/
161*cdf0e10cSrcweir 	public void insertPrimaryKey( XPropertySet xForm, String sFieldName ) throws com.sun.star.uno.Exception
162*cdf0e10cSrcweir 	{
163*cdf0e10cSrcweir 		// check the privileges
164*cdf0e10cSrcweir 		Integer aConcurrency = (Integer)xForm.getPropertyValue( "ResultSetConcurrency" );
165*cdf0e10cSrcweir 		if ( ResultSetConcurrency.READ_ONLY != aConcurrency.intValue() )
166*cdf0e10cSrcweir 		{
167*cdf0e10cSrcweir 			// get the column object
168*cdf0e10cSrcweir 			XColumnsSupplier xSuppCols = (XColumnsSupplier)UnoRuntime.queryInterface(
169*cdf0e10cSrcweir 				XColumnsSupplier.class, xForm );
170*cdf0e10cSrcweir 			XNameAccess xCols = xSuppCols.getColumns();
171*cdf0e10cSrcweir 			XColumnUpdate xCol = (XColumnUpdate)UnoRuntime.queryInterface(
172*cdf0e10cSrcweir 				XColumnUpdate.class, xCols.getByName( sFieldName ) );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 			xCol.updateInt( generatePrimaryKey( xForm, sFieldName ) );
175*cdf0e10cSrcweir 		}
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir };
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir /**************************************************************************/
180*cdf0e10cSrcweir /** base class for helpers dealing with unique column values
181*cdf0e10cSrcweir */
182*cdf0e10cSrcweir class KeyGeneratorForReset extends UniqueColumnValue implements XResetListener
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
185*cdf0e10cSrcweir 	private DocumentViewHelper	m_aView;
186*cdf0e10cSrcweir 	private	String				m_sFieldName;
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
189*cdf0e10cSrcweir 	/** ctor
190*cdf0e10cSrcweir 		@param aView
191*cdf0e10cSrcweir 			the view which shall be used to focus controls
192*cdf0e10cSrcweir 		@param sFieldName
193*cdf0e10cSrcweir 			the name of the field for which keys should be generated
194*cdf0e10cSrcweir 	*/
195*cdf0e10cSrcweir 	public KeyGeneratorForReset( String sFieldName, DocumentViewHelper aView )
196*cdf0e10cSrcweir 	{
197*cdf0e10cSrcweir 		m_sFieldName = sFieldName;
198*cdf0e10cSrcweir 		m_aView = aView;
199*cdf0e10cSrcweir 	}
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
202*cdf0e10cSrcweir 	/** sets the focus to the first control which is no fixed text, and not the
203*cdf0e10cSrcweir 		one we're defaulting
204*cdf0e10cSrcweir 	*/
205*cdf0e10cSrcweir 	public void defaultNewRecordFocus( XPropertySet xForm ) throws com.sun.star.uno.Exception
206*cdf0e10cSrcweir 	{
207*cdf0e10cSrcweir 		XIndexAccess xFormAsContainer = (XIndexAccess)UnoRuntime.queryInterface(
208*cdf0e10cSrcweir 			XIndexAccess.class, xForm );
209*cdf0e10cSrcweir 		for ( int i = 0; i<xFormAsContainer.getCount(); ++i )
210*cdf0e10cSrcweir 		{
211*cdf0e10cSrcweir 			// the model
212*cdf0e10cSrcweir 			XPropertySet xModel = UNO.queryPropertySet( xFormAsContainer.getByIndex( i ) );
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 			// check if it's a valid leaf (no sub form or such)
215*cdf0e10cSrcweir 			XPropertySetInfo xPSI = xModel.getPropertySetInfo( );
216*cdf0e10cSrcweir 			if ( ( null == xPSI ) || !xPSI.hasPropertyByName( "ClassId" ) )
217*cdf0e10cSrcweir 				continue;
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 			// check if it's a fixed text
220*cdf0e10cSrcweir 			Short nClassId = (Short)xModel.getPropertyValue( "ClassId" );
221*cdf0e10cSrcweir 			if ( FormComponentType.FIXEDTEXT == nClassId.shortValue() )
222*cdf0e10cSrcweir 				continue;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 			// check if it is bound to the field we are responsible for
225*cdf0e10cSrcweir 			if ( !xPSI.hasPropertyByName( "DataField" ) )
226*cdf0e10cSrcweir 				continue;
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 			String sFieldDataSource = (String)xModel.getPropertyValue( "DataField" );
229*cdf0e10cSrcweir 			if ( sFieldDataSource.equals( m_sFieldName ) )
230*cdf0e10cSrcweir 				continue;
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 			// both conditions do not apply
233*cdf0e10cSrcweir 			// -> set the focus into the respective control
234*cdf0e10cSrcweir 			XControlModel xCM = UNO.queryControlModel( xModel );
235*cdf0e10cSrcweir 			m_aView.grabControlFocus( xCM);
236*cdf0e10cSrcweir 			break;
237*cdf0e10cSrcweir 		}
238*cdf0e10cSrcweir 	}
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
241*cdf0e10cSrcweir 	// XResetListener overridables
242*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
243*cdf0e10cSrcweir     public boolean approveReset( com.sun.star.lang.EventObject rEvent ) throws com.sun.star.uno.RuntimeException
244*cdf0e10cSrcweir 	{
245*cdf0e10cSrcweir 		// not interested in vetoing this
246*cdf0e10cSrcweir 		return true;
247*cdf0e10cSrcweir 	}
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
250*cdf0e10cSrcweir     public void resetted( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		// check if this reset occured becase we're on a new record
253*cdf0e10cSrcweir 		XPropertySet xFormProps = UNO.queryPropertySet( aEvent.Source );
254*cdf0e10cSrcweir 		try
255*cdf0e10cSrcweir 		{
256*cdf0e10cSrcweir 			Boolean aIsNew = (Boolean)xFormProps.getPropertyValue( "IsNew" );
257*cdf0e10cSrcweir 			if ( aIsNew.booleanValue() )
258*cdf0e10cSrcweir 			{	// yepp
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 				// we're going to modify the record, though after that, to the user, it should look
261*cdf0e10cSrcweir 				// like it has not been modified
262*cdf0e10cSrcweir 				// So we need to ensure that we do not change the IsModified property with whatever we do
263*cdf0e10cSrcweir 				Object aModifiedFlag = xFormProps.getPropertyValue( "IsModified" );
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 				// now set the value
266*cdf0e10cSrcweir 				insertPrimaryKey( xFormProps, m_sFieldName );
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 				// then restore the flag
269*cdf0e10cSrcweir 				xFormProps.setPropertyValue( "IsModified", aModifiedFlag );
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 				// still one thing ... would be nice to have the focus in a control which is
272*cdf0e10cSrcweir 				// the one which's value we just defaulted
273*cdf0e10cSrcweir 				defaultNewRecordFocus( xFormProps );
274*cdf0e10cSrcweir 			}
275*cdf0e10cSrcweir 		}
276*cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
277*cdf0e10cSrcweir 		{
278*cdf0e10cSrcweir 			System.out.println(e);
279*cdf0e10cSrcweir 			e.printStackTrace();
280*cdf0e10cSrcweir 		}
281*cdf0e10cSrcweir 	}
282*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
283*cdf0e10cSrcweir 	// XEventListener overridables
284*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
285*cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
286*cdf0e10cSrcweir 	{
287*cdf0e10cSrcweir 		// not interested in
288*cdf0e10cSrcweir 	}
289*cdf0e10cSrcweir };
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir /**************************************************************************/
293*cdf0e10cSrcweir /** base class for helpers dealing with unique column values
294*cdf0e10cSrcweir */
295*cdf0e10cSrcweir class KeyGeneratorForUpdate extends UniqueColumnValue implements XRowSetApproveListener
296*cdf0e10cSrcweir {
297*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
298*cdf0e10cSrcweir 	private	String	m_sFieldName;
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
301*cdf0e10cSrcweir 	public KeyGeneratorForUpdate( String sFieldName )
302*cdf0e10cSrcweir 	{
303*cdf0e10cSrcweir 		m_sFieldName = sFieldName;
304*cdf0e10cSrcweir 	}
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
307*cdf0e10cSrcweir 	// XRowSetApproveListener overridables
308*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
309*cdf0e10cSrcweir     public boolean approveCursorMove( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
310*cdf0e10cSrcweir 	{
311*cdf0e10cSrcweir 		// not interested in vetoing moves
312*cdf0e10cSrcweir 		return true;
313*cdf0e10cSrcweir 	}
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
316*cdf0e10cSrcweir     public boolean approveRowChange( RowChangeEvent aEvent ) throws com.sun.star.uno.RuntimeException
317*cdf0e10cSrcweir 	{
318*cdf0e10cSrcweir 		if ( RowChangeAction.INSERT == aEvent.Action )
319*cdf0e10cSrcweir 		{
320*cdf0e10cSrcweir 			try
321*cdf0e10cSrcweir 			{
322*cdf0e10cSrcweir 				// the affected form
323*cdf0e10cSrcweir 				XPropertySet xFormProps = UNO.queryPropertySet( aEvent.Source );
324*cdf0e10cSrcweir 				// insert a new unique value
325*cdf0e10cSrcweir 				insertPrimaryKey( xFormProps, m_sFieldName );
326*cdf0e10cSrcweir 			}
327*cdf0e10cSrcweir 			catch( com.sun.star.uno.Exception e )
328*cdf0e10cSrcweir 			{
329*cdf0e10cSrcweir 				System.out.println(e);
330*cdf0e10cSrcweir 				e.printStackTrace();
331*cdf0e10cSrcweir 			}
332*cdf0e10cSrcweir 		}
333*cdf0e10cSrcweir 		return true;
334*cdf0e10cSrcweir 	}
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
337*cdf0e10cSrcweir     public boolean approveRowSetChange( com.sun.star.lang.EventObject aEvent ) throws com.sun.star.uno.RuntimeException
338*cdf0e10cSrcweir 	{
339*cdf0e10cSrcweir 		// not interested in vetoing executions of the row set
340*cdf0e10cSrcweir 		return true;
341*cdf0e10cSrcweir 	}
342*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
343*cdf0e10cSrcweir 	// XEventListener overridables
344*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
345*cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
346*cdf0e10cSrcweir 	{
347*cdf0e10cSrcweir 		// not interested in
348*cdf0e10cSrcweir 	}
349*cdf0e10cSrcweir };
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir /**************************************************************************/
352*cdf0e10cSrcweir /** allows to generate unique keys for a field of a Form
353*cdf0e10cSrcweir */
354*cdf0e10cSrcweir public class KeyGenerator
355*cdf0e10cSrcweir {
356*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
357*cdf0e10cSrcweir 	private	KeyGeneratorForReset	m_aResetKeyGenerator;
358*cdf0e10cSrcweir 	private KeyGeneratorForUpdate	m_aUpdateKeyGenerator;
359*cdf0e10cSrcweir 	private boolean					m_bResetListening;
360*cdf0e10cSrcweir 	private boolean					m_bUpdateListening;
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 	private DocumentHelper			m_aDocument;
363*cdf0e10cSrcweir 	private	XPropertySet			m_xForm;
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
366*cdf0e10cSrcweir 	/** ctor
367*cdf0e10cSrcweir 		@param xForm
368*cdf0e10cSrcweir 			specified the form to operate on
369*cdf0e10cSrcweir 		@param sFieldName
370*cdf0e10cSrcweir 			specifies the field which's value should be manipulated
371*cdf0e10cSrcweir 	*/
372*cdf0e10cSrcweir 	public KeyGenerator( XPropertySet xForm, String sFieldName,
373*cdf0e10cSrcweir                          XComponentContext xCtx )
374*cdf0e10cSrcweir 	{
375*cdf0e10cSrcweir 		m_xForm = xForm;
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 		DocumentHelper aDocument = DocumentHelper.getDocumentForComponent( xForm, xCtx );
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 		m_aResetKeyGenerator = new KeyGeneratorForReset( sFieldName, aDocument.getCurrentView() );
380*cdf0e10cSrcweir 		m_aUpdateKeyGenerator = new KeyGeneratorForUpdate( sFieldName );
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir 		m_bResetListening = m_bUpdateListening = false;
383*cdf0e10cSrcweir 	}
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
386*cdf0e10cSrcweir 	/** stops any actions on the form
387*cdf0e10cSrcweir 	*/
388*cdf0e10cSrcweir 	public void stopGenerator( )
389*cdf0e10cSrcweir 	{
390*cdf0e10cSrcweir 		XReset xFormReset = UNO.queryReset( m_xForm );
391*cdf0e10cSrcweir 		xFormReset.removeResetListener( m_aResetKeyGenerator );
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir 		XRowSetApproveBroadcaster xFormBroadcaster = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
394*cdf0e10cSrcweir 			XRowSetApproveBroadcaster.class, m_xForm );
395*cdf0e10cSrcweir 		xFormBroadcaster.removeRowSetApproveListener( m_aUpdateKeyGenerator );
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir 		m_bUpdateListening = m_bResetListening = false;
398*cdf0e10cSrcweir 	}
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
401*cdf0e10cSrcweir 	/** activates one of our two key generators
402*cdf0e10cSrcweir 	*/
403*cdf0e10cSrcweir 	public void activateKeyGenerator( boolean bGenerateOnReset )
404*cdf0e10cSrcweir 	{
405*cdf0e10cSrcweir 		// for resets
406*cdf0e10cSrcweir 		XReset xFormReset = UNO.queryReset( m_xForm );
407*cdf0e10cSrcweir 		// for approving actions
408*cdf0e10cSrcweir 		XRowSetApproveBroadcaster xFormBroadcaster = (XRowSetApproveBroadcaster)UnoRuntime.queryInterface(
409*cdf0e10cSrcweir 			XRowSetApproveBroadcaster.class, m_xForm );
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir 		if ( bGenerateOnReset )
412*cdf0e10cSrcweir 		{
413*cdf0e10cSrcweir 			if ( !m_bResetListening )
414*cdf0e10cSrcweir 				xFormReset.addResetListener( m_aResetKeyGenerator );
415*cdf0e10cSrcweir 			if ( m_bUpdateListening )
416*cdf0e10cSrcweir 				xFormBroadcaster.removeRowSetApproveListener( m_aUpdateKeyGenerator );
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir 			m_bUpdateListening = false;
419*cdf0e10cSrcweir 			m_bResetListening = true;
420*cdf0e10cSrcweir 		}
421*cdf0e10cSrcweir 		else
422*cdf0e10cSrcweir 		{
423*cdf0e10cSrcweir 			if ( m_bResetListening )
424*cdf0e10cSrcweir 				xFormReset.removeResetListener( m_aResetKeyGenerator );
425*cdf0e10cSrcweir 			if ( !m_bUpdateListening )
426*cdf0e10cSrcweir 				xFormBroadcaster.addRowSetApproveListener( m_aUpdateKeyGenerator );
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir 			m_bResetListening = false;
429*cdf0e10cSrcweir 			m_bUpdateListening = true;
430*cdf0e10cSrcweir 		}
431*cdf0e10cSrcweir 	}
432*cdf0e10cSrcweir };
433