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 import com.sun.star.beans.PropertyChangeEvent;
24cdf0e10cSrcweir import com.sun.star.beans.XPropertyChangeListener;
25cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // __________ Imports __________
29cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir // base classes
32cdf0e10cSrcweir import com.sun.star.container.XIndexContainer;
33cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
34cdf0e10cSrcweir import com.sun.star.container.XNamed;
35cdf0e10cSrcweir import com.sun.star.form.FormComponentType;
36cdf0e10cSrcweir import com.sun.star.form.ListSourceType;
37cdf0e10cSrcweir import com.sun.star.form.XGridColumnFactory;
38cdf0e10cSrcweir import com.sun.star.form.XReset;
39cdf0e10cSrcweir import com.sun.star.form.XResetListener;
40cdf0e10cSrcweir import com.sun.star.form.runtime.FormFeature;
41cdf0e10cSrcweir import com.sun.star.lang.EventObject;
42cdf0e10cSrcweir import com.sun.star.lang.XComponent;
43cdf0e10cSrcweir import com.sun.star.sdb.CommandType;
44cdf0e10cSrcweir import com.sun.star.sdb.XColumnUpdate;
45cdf0e10cSrcweir import com.sun.star.sdbc.ResultSetConcurrency;
46cdf0e10cSrcweir import com.sun.star.sdbc.XConnection;
47cdf0e10cSrcweir import com.sun.star.sdbc.XDataSource;
48cdf0e10cSrcweir import com.sun.star.sdbc.XStatement;
49cdf0e10cSrcweir import com.sun.star.sdbcx.XColumnsSupplier;
50cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
51cdf0e10cSrcweir import com.sun.star.uno.XInterface;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /**************************************************************************/
54cdf0e10cSrcweir /** a class for enumerating a form component tree
55cdf0e10cSrcweir */
56cdf0e10cSrcweir class PrintComponentTree extends ComponentTreeTraversal
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	private String m_sPrefix;
59cdf0e10cSrcweir 
PrintComponentTree()60cdf0e10cSrcweir 	public PrintComponentTree()
61cdf0e10cSrcweir 	{
62cdf0e10cSrcweir 		m_sPrefix = new String();
63cdf0e10cSrcweir 	}
64cdf0e10cSrcweir 
handle( Object aFormComponent )65cdf0e10cSrcweir 	public void handle( Object aFormComponent ) throws com.sun.star.uno.Exception
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		// the name of the child
68cdf0e10cSrcweir 		XNamed xName = (XNamed)UnoRuntime.queryInterface( XNamed.class, aFormComponent );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 		// if it's a form control model, check it's type
71cdf0e10cSrcweir 		XPropertySet xProps = UNO.queryPropertySet( aFormComponent );
72cdf0e10cSrcweir 		String sTypeName = FLTools.classifyFormComponentType( xProps );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 		String sName;
75cdf0e10cSrcweir 		if ( null == xName )
76cdf0e10cSrcweir 			sName = "<unnamed>";
77cdf0e10cSrcweir 		else
78cdf0e10cSrcweir 			sName = xName.getName();
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 		// print the component's name
81cdf0e10cSrcweir 		if ( 0 != sTypeName.length() )
82cdf0e10cSrcweir 		{
83cdf0e10cSrcweir 			System.out.println( m_sPrefix + sName + " (" + sTypeName + ")" );
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 		else
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			System.out.println( m_sPrefix + sName );
88cdf0e10cSrcweir 		}
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 		// let the super class step down the tree
91cdf0e10cSrcweir 		m_sPrefix = m_sPrefix + " ";
92cdf0e10cSrcweir 		super.handle( aFormComponent );
93cdf0e10cSrcweir 		m_sPrefix = m_sPrefix.substring( 0, m_sPrefix.length() - 1 );
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir };
96cdf0e10cSrcweir 
97cdf0e10cSrcweir /**************************************************************************/
98cdf0e10cSrcweir /** a class revoking button models from a ButtonOperator instance
99cdf0e10cSrcweir */
100cdf0e10cSrcweir class RevokeButtons extends ComponentTreeTraversal
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	private ButtonOperator m_aOperator;
103cdf0e10cSrcweir 
RevokeButtons( ButtonOperator aOperator )104cdf0e10cSrcweir 	public RevokeButtons( ButtonOperator aOperator )
105cdf0e10cSrcweir 	{
106cdf0e10cSrcweir 		m_aOperator = aOperator;
107cdf0e10cSrcweir 	}
108cdf0e10cSrcweir 
handle( Object aFormComponent )109cdf0e10cSrcweir 	public void handle( Object aFormComponent ) throws com.sun.star.uno.Exception
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		// check if it's a button
112cdf0e10cSrcweir 		XPropertySet xProps = UNO.queryPropertySet( aFormComponent );
113cdf0e10cSrcweir 		XPropertySetInfo xPI = null;
114cdf0e10cSrcweir 		if ( null != xProps )
115cdf0e10cSrcweir 			xPI = xProps.getPropertySetInfo();
116cdf0e10cSrcweir 		if ( ( null != xPI ) && xPI.hasPropertyByName( "ClassId" ) )
117cdf0e10cSrcweir 		{
118cdf0e10cSrcweir 			Short nClassId = (Short)xProps.getPropertyValue( "ClassId" );
119cdf0e10cSrcweir 			if ( FormComponentType.COMMANDBUTTON == nClassId.shortValue() )
120cdf0e10cSrcweir 			{
121cdf0e10cSrcweir 				// yes, it is
122cdf0e10cSrcweir 				m_aOperator.revokeButton( xProps );
123cdf0e10cSrcweir 			}
124cdf0e10cSrcweir 		}
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 		// let the super class step down the tree (if possible)
127cdf0e10cSrcweir 		super.handle( aFormComponent );
128cdf0e10cSrcweir 	}
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir /**************************************************************************/
132cdf0e10cSrcweir public class DataAwareness extends DocumentBasedExample implements XPropertyChangeListener, XResetListener
133cdf0e10cSrcweir {
134cdf0e10cSrcweir 	/* ================================================================== */
135cdf0e10cSrcweir     private HsqlDatabase            m_database;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     private static final String     s_tableNameSalesmen = "SALESMEN";
138cdf0e10cSrcweir     private static final String     s_tableNameCustomers = "CUSTOMERS";
139cdf0e10cSrcweir     private static final String     s_tableNameSales = "SALES";
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	private XPropertySet			m_xMasterForm;
142cdf0e10cSrcweir 	private ButtonOperator			m_aOperator;
143cdf0e10cSrcweir 	private SalesFilter				m_aSalesFilter;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 	private KeyGenerator			m_aSalesmanKeyGenerator;
146cdf0e10cSrcweir 	private KeyGenerator			m_aSalesKeyGenerator;
147cdf0e10cSrcweir 	private ControlLock				m_aSalesmenLocker;
148cdf0e10cSrcweir 	private	ControlLock				m_aSalesLocker;
149cdf0e10cSrcweir 	private	GridFieldValidator		m_aSalesNameValidator;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	private boolean					m_bDefaultSalesDate;
152cdf0e10cSrcweir 	private boolean					m_bProtectKeyFields;
153cdf0e10cSrcweir 	private boolean					m_bAllowEmptySales;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
DataAwareness()156cdf0e10cSrcweir     public DataAwareness()
157cdf0e10cSrcweir 	{
158cdf0e10cSrcweir         super( DocumentType.WRITER );
159cdf0e10cSrcweir 		m_bDefaultSalesDate = false;
160cdf0e10cSrcweir 		m_bProtectKeyFields = false;
161cdf0e10cSrcweir 		m_bAllowEmptySales = false;
162cdf0e10cSrcweir 	}
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	/* ==================================================================
165cdf0e10cSrcweir 	   = form components
166cdf0e10cSrcweir 	   ================================================================== */
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
169cdf0e10cSrcweir 	/** enumerates and prints all the elements in the given container, together with the container itself
170cdf0e10cSrcweir 	*/
enumFormComponents( XNameAccess xContainer )171cdf0e10cSrcweir 	protected void enumFormComponents( XNameAccess xContainer ) throws java.lang.Exception
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 		String sObjectName;
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 		XNamed xNameAcc = (XNamed)UnoRuntime.queryInterface( XNamed.class, xContainer );
176cdf0e10cSrcweir 		if ( null == xNameAcc )
177cdf0e10cSrcweir 			sObjectName = new String( "<unnamed>" );
178cdf0e10cSrcweir 		else
179cdf0e10cSrcweir 			sObjectName = xNameAcc.getName();
180cdf0e10cSrcweir 		System.out.println( new String( "enumerating the container named \"" ) + sObjectName +
181cdf0e10cSrcweir 			new String( "\"\n" ) );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		PrintComponentTree aPrinter = new PrintComponentTree();
184cdf0e10cSrcweir 		aPrinter.handle( xContainer );
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
188cdf0e10cSrcweir 	/** enumerates and prints all form elements in the document
189cdf0e10cSrcweir 	*/
enumFormComponents( )190cdf0e10cSrcweir 	protected void enumFormComponents( ) throws java.lang.Exception
191cdf0e10cSrcweir 	{
192cdf0e10cSrcweir 		enumFormComponents( m_document.getFormComponentTreeRoot() );
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	/* ==================================================================
196cdf0e10cSrcweir 	   = UNO callbacks
197cdf0e10cSrcweir 	   ================================================================== */
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
200cdf0e10cSrcweir 	// XResetListener overridables
201cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
approveReset( EventObject aEvent )202cdf0e10cSrcweir     public boolean approveReset( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
203cdf0e10cSrcweir 	{
204cdf0e10cSrcweir 		// not interested in vetoing this
205cdf0e10cSrcweir 		return true;
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
resetted( EventObject aEvent )209cdf0e10cSrcweir     public void resetted( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
210cdf0e10cSrcweir 	{
211*a893be29SPedro Giffuni 		// check if this reset occurred becase we're on a new record
212cdf0e10cSrcweir 		XPropertySet xFormProps = UNO.queryPropertySet(  aEvent.Source );
213cdf0e10cSrcweir 		try
214cdf0e10cSrcweir 		{
215cdf0e10cSrcweir 			Boolean aIsNew = (Boolean)xFormProps.getPropertyValue( "IsNew" );
216cdf0e10cSrcweir 			if ( aIsNew.booleanValue() )
217cdf0e10cSrcweir 			{	// yepp
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 				if ( !m_bDefaultSalesDate )
220cdf0e10cSrcweir 				{	// we're interested to do all this only if the user told us to default the sales date
221cdf0e10cSrcweir 					// to "today"
222cdf0e10cSrcweir 					// As date fields do this defaulting automatically, the semantics is inverted here:
223cdf0e10cSrcweir 					// If we're told to default, we must do nothing, if we should not default, we must
224cdf0e10cSrcweir 					// reset the value which the date field set automatically.
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 					Integer aConcurrency = (Integer)xFormProps.getPropertyValue( "ResultSetConcurrency" );
227cdf0e10cSrcweir 					if ( ResultSetConcurrency.READ_ONLY != aConcurrency.intValue() )
228cdf0e10cSrcweir 					{
229cdf0e10cSrcweir 						// we're going to modify the record, though after that, to the user, it should look
230cdf0e10cSrcweir 						// like it has not been modified
231cdf0e10cSrcweir 						// So we need to ensure that we do not change the IsModified property with whatever we do
232cdf0e10cSrcweir 						Object aModifiedFlag = xFormProps.getPropertyValue( "IsModified" );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 						// get the columns of our master form
236cdf0e10cSrcweir 						XColumnsSupplier xSuppCols = (XColumnsSupplier)UnoRuntime.queryInterface(
237cdf0e10cSrcweir 							XColumnsSupplier.class, xFormProps );
238cdf0e10cSrcweir 						XNameAccess xCols = xSuppCols.getColumns();
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 						// and update the date column with a NULL value
241cdf0e10cSrcweir 						XColumnUpdate xDateColumn = (XColumnUpdate)UnoRuntime.queryInterface(
242cdf0e10cSrcweir 							XColumnUpdate.class, xCols.getByName( "SALEDATE" ) );
243cdf0e10cSrcweir 						xDateColumn.updateNull();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 						// then restore the flag
247cdf0e10cSrcweir 						xFormProps.setPropertyValue( "IsModified", aModifiedFlag );
248cdf0e10cSrcweir 					}
249cdf0e10cSrcweir 				}
250cdf0e10cSrcweir 			}
251cdf0e10cSrcweir 		}
252cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
253cdf0e10cSrcweir 		{
254cdf0e10cSrcweir 			System.out.println(e);
255cdf0e10cSrcweir 			e.printStackTrace();
256cdf0e10cSrcweir 		}
257cdf0e10cSrcweir 	}
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
260cdf0e10cSrcweir 	// XPropertyChangeListener overridables
261cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
propertyChange( PropertyChangeEvent aEvent )262cdf0e10cSrcweir     public void propertyChange( PropertyChangeEvent aEvent ) throws com.sun.star.uno.RuntimeException
263cdf0e10cSrcweir 	{
264cdf0e10cSrcweir 		try
265cdf0e10cSrcweir 		{
266cdf0e10cSrcweir 			// did it come from a radio button or checkbox?
267cdf0e10cSrcweir 			if ( aEvent.PropertyName.equals( "State" ) )
268cdf0e10cSrcweir 			{	// yep
269cdf0e10cSrcweir 				Short aNewState = (Short)aEvent.NewValue;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 				XPropertySet xModel = UNO.queryPropertySet( aEvent.Source );
272cdf0e10cSrcweir 				String sName = (String)xModel.getPropertyValue( "Name" );
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 				Short aClassId = (Short)xModel.getPropertyValue( "ClassId" );
275cdf0e10cSrcweir 				if ( FormComponentType.RADIOBUTTON == aClassId.shortValue() )
276cdf0e10cSrcweir 				{
277cdf0e10cSrcweir 					String sRefValue = (String)xModel.getPropertyValue( "RefValue" );
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 					short nNewValue = ((Short)aEvent.NewValue).shortValue();
280cdf0e10cSrcweir 					if ( sName.equals( "KeyGen" ) )
281cdf0e10cSrcweir 					{
282cdf0e10cSrcweir 						// it's one of the options for key generation
283cdf0e10cSrcweir 						if ( sRefValue.equals( "none" ) )
284cdf0e10cSrcweir 						{	// no automatic generation at all
285cdf0e10cSrcweir 							m_aSalesmanKeyGenerator.stopGenerator( );
286cdf0e10cSrcweir 							m_aSalesKeyGenerator.stopGenerator( );
287cdf0e10cSrcweir 						}
288cdf0e10cSrcweir 						else
289cdf0e10cSrcweir 						{
290cdf0e10cSrcweir 							boolean bGenerateOnReset = true;
291cdf0e10cSrcweir 							if ( sRefValue.equals( "update" ) )
292cdf0e10cSrcweir 							{	// generate on update
293cdf0e10cSrcweir 								bGenerateOnReset = ( 0 == nNewValue );
294cdf0e10cSrcweir 							}
295cdf0e10cSrcweir 							else if ( sRefValue.equals( "reset" ) )
296cdf0e10cSrcweir 							{	// generat on reset
297cdf0e10cSrcweir 								bGenerateOnReset = ( 0 != nNewValue );
298cdf0e10cSrcweir 							}
299cdf0e10cSrcweir 							m_aSalesmanKeyGenerator.activateKeyGenerator( bGenerateOnReset );
300cdf0e10cSrcweir 							m_aSalesKeyGenerator.activateKeyGenerator( bGenerateOnReset );
301cdf0e10cSrcweir 						}
302cdf0e10cSrcweir 					}
303cdf0e10cSrcweir 				}
304cdf0e10cSrcweir 				else if ( FormComponentType.CHECKBOX == aClassId.shortValue() )
305cdf0e10cSrcweir 				{
306cdf0e10cSrcweir 					boolean bEnabled = ( 0 != aNewState.shortValue() );
307cdf0e10cSrcweir 					if ( sName.equals( "defaultdate" ) )
308cdf0e10cSrcweir 					{
309cdf0e10cSrcweir 						m_bDefaultSalesDate = bEnabled;
310cdf0e10cSrcweir 					}
311cdf0e10cSrcweir 					else if ( sName.equals( "protectkeys" ) )
312cdf0e10cSrcweir 					{
313cdf0e10cSrcweir 						m_bProtectKeyFields = bEnabled;
314cdf0e10cSrcweir 						m_aSalesmenLocker.enableLock( m_bProtectKeyFields );
315cdf0e10cSrcweir 						m_aSalesLocker.enableLock( m_bProtectKeyFields );
316cdf0e10cSrcweir 					}
317cdf0e10cSrcweir 					else if ( sName.equals( "emptysales" ) )
318cdf0e10cSrcweir 					{
319cdf0e10cSrcweir 						m_bAllowEmptySales = bEnabled;
320cdf0e10cSrcweir 						m_aSalesNameValidator.enableColumnWatch( m_bAllowEmptySales );
321cdf0e10cSrcweir 					}
322cdf0e10cSrcweir 				}
323cdf0e10cSrcweir 			}
324cdf0e10cSrcweir 		}
325cdf0e10cSrcweir 		catch(com.sun.star.uno.Exception e)
326cdf0e10cSrcweir 		{
327cdf0e10cSrcweir 			System.out.println(e);
328cdf0e10cSrcweir 			e.printStackTrace();
329cdf0e10cSrcweir 		}
330cdf0e10cSrcweir 	}
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
333cdf0e10cSrcweir 	// XEventListener overridables
334cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
disposing( EventObject aEvent )335cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
336cdf0e10cSrcweir 	{
337cdf0e10cSrcweir         // simply disambiguate
338cdf0e10cSrcweir         super.disposing( aEvent );
339cdf0e10cSrcweir 	}
340cdf0e10cSrcweir 
341cdf0e10cSrcweir 	/* ==================================================================
342cdf0e10cSrcweir 	   = miscellaneous
343cdf0e10cSrcweir 	   ================================================================== */
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
346cdf0e10cSrcweir 	/** skips line feeds in the input stream
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 		@returns
349cdf0e10cSrcweir 			the first character which does not belong to a line feed
350cdf0e10cSrcweir 	*/
skipLineFeeds( java.io.InputStream aInput )351cdf0e10cSrcweir 	protected int skipLineFeeds( java.io.InputStream aInput ) throws java.io.IOException
352cdf0e10cSrcweir 	{
353cdf0e10cSrcweir 		// read characters, until we encounter something which is not a line feed character
354cdf0e10cSrcweir 		int nChar = aInput.read( );
355cdf0e10cSrcweir 		while ( ( 13 == nChar ) || ( 10 == nChar ) )
356cdf0e10cSrcweir 			nChar = aInput.read( );
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 		// now read everything which is behind this single character we are interested in
359cdf0e10cSrcweir 		while ( 0 < aInput.available() )
360cdf0e10cSrcweir 			aInput.read( );
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 		return nChar;
363cdf0e10cSrcweir 	}
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 	/* ==================================================================
366cdf0e10cSrcweir 	   = table handling
367cdf0e10cSrcweir 	   ================================================================== */
368cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
369cdf0e10cSrcweir 	/** checks if a given table exists.
370cdf0e10cSrcweir 
371cdf0e10cSrcweir 		<p>The check is made using a SELECT statement, so even if the connection
372cdf0e10cSrcweir 		is a n SDB-level connection, which may filter tables in it's table
373cdf0e10cSrcweir 		supplier, the result may be reliable ....</p>
374cdf0e10cSrcweir 	*/
existsInvisibleTable( XConnection xConn, String sTableName )375cdf0e10cSrcweir 	protected boolean existsInvisibleTable( XConnection xConn, String sTableName ) throws java.lang.Exception
376cdf0e10cSrcweir 	{
377cdf0e10cSrcweir 		String sStatement = "SELECT * FROM ";
378cdf0e10cSrcweir 		sStatement += sTableName;
379cdf0e10cSrcweir 		sStatement += " WHERE 0=1";
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 		boolean bSuccess = false;
382cdf0e10cSrcweir 		try
383cdf0e10cSrcweir 		{
384cdf0e10cSrcweir 			XStatement xStatement = xConn.createStatement();
385cdf0e10cSrcweir 			xStatement.execute( sStatement );
386cdf0e10cSrcweir 			// if we reached this point, the table probably exists
387cdf0e10cSrcweir 			bSuccess = true;
388cdf0e10cSrcweir 		}
389cdf0e10cSrcweir 		catch(com.sun.star.sdbc.SQLException e)
390cdf0e10cSrcweir 		{
391cdf0e10cSrcweir 		}
392cdf0e10cSrcweir 		return bSuccess;
393cdf0e10cSrcweir 	}
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
396cdf0e10cSrcweir 	/** add a specified table name to the table filter of the given data source.
397cdf0e10cSrcweir 	*/
makeTableVisible( XDataSource xDS, XConnection xConn, String sTableName )398cdf0e10cSrcweir 	protected void makeTableVisible( XDataSource xDS, XConnection xConn, String sTableName ) throws java.lang.Exception
399cdf0e10cSrcweir 	{
400cdf0e10cSrcweir 		// get the table filter
401cdf0e10cSrcweir 		XPropertySet xDSP = UNO.queryPropertySet( xDS );
402cdf0e10cSrcweir 		String[] aCurrentFilter = (String[])xDSP.getPropertyValue( "TableFilter" );
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 		// check if the table name is already part of it
405cdf0e10cSrcweir 		String sAllTables = "*";												// all tables
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 		for ( int i=0; i<aCurrentFilter.length; ++i )
408cdf0e10cSrcweir 		{
409cdf0e10cSrcweir 			String sCurrentTableFilter = aCurrentFilter[i];
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 			if ( sCurrentTableFilter.equals( sTableName ) )
412cdf0e10cSrcweir 				return;
413cdf0e10cSrcweir 			if ( sCurrentTableFilter.equals( sAllTables ) )
414cdf0e10cSrcweir 				return;
415cdf0e10cSrcweir 		}
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 		// if we are here, we have to add our table to the filter sequence
418cdf0e10cSrcweir 		String[] aNewFilter = new String[ aCurrentFilter.length + 1 ];
419cdf0e10cSrcweir 		// copy the existent filter entries
420cdf0e10cSrcweir 		for ( int i=0; i<aCurrentFilter.length; ++i )
421cdf0e10cSrcweir 			aNewFilter[i] = aCurrentFilter[i];
422cdf0e10cSrcweir 		// add our table
423cdf0e10cSrcweir 		aNewFilter[ aCurrentFilter.length ] = sTableName;
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 		xDSP.setPropertyValue( "TableFilter", aNewFilter );
426cdf0e10cSrcweir 	}
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
429cdf0e10cSrcweir 	/** executes the given statement on the given connection
430cdf0e10cSrcweir 	*/
implExecuteStatement( XConnection xConn, String sStatement )431cdf0e10cSrcweir 	protected boolean implExecuteStatement( XConnection xConn, String sStatement ) throws java.lang.Exception
432cdf0e10cSrcweir 	{
433cdf0e10cSrcweir 		try
434cdf0e10cSrcweir 		{
435cdf0e10cSrcweir 			XStatement xStatement = xConn.createStatement( );
436cdf0e10cSrcweir 			xStatement.execute( sStatement );
437cdf0e10cSrcweir 		}
438cdf0e10cSrcweir 		catch(com.sun.star.sdbc.SQLException e)
439cdf0e10cSrcweir 		{
440cdf0e10cSrcweir 			System.err.println( e );
441cdf0e10cSrcweir 			return false;
442cdf0e10cSrcweir 		}
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 		return true;
445cdf0e10cSrcweir 	}
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
448*a893be29SPedro Giffuni 	/** creates the table with the given name, using the given statement
449cdf0e10cSrcweir 	*/
implCreateTable( XConnection xConn, String sCreateStatement, String sTableName )450cdf0e10cSrcweir 	protected boolean implCreateTable( XConnection xConn, String sCreateStatement, String sTableName ) throws java.lang.Exception
451cdf0e10cSrcweir 	{
452cdf0e10cSrcweir 		if ( !implExecuteStatement( xConn, sCreateStatement ) )
453cdf0e10cSrcweir 		{
454cdf0e10cSrcweir 			System.out.println( "  could not create the table " + sTableName + "." );
455cdf0e10cSrcweir 			System.out.println( );
456cdf0e10cSrcweir 			return false;
457cdf0e10cSrcweir 		}
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 		return true;
460cdf0e10cSrcweir 	}
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
463cdf0e10cSrcweir 	/** creates the table SALESMEN
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 		@return
466cdf0e10cSrcweir 			<TRUE/> if and only if the creation succeeded
467cdf0e10cSrcweir 	*/
createTableSalesman( XConnection xConn )468cdf0e10cSrcweir 	protected boolean createTableSalesman( XConnection xConn ) throws java.lang.Exception
469cdf0e10cSrcweir 	{
470cdf0e10cSrcweir 		String sCreateStatement = "CREATE TABLE " + s_tableNameSalesmen + " ";
471cdf0e10cSrcweir 		sCreateStatement += "(SNR INTEGER NOT NULL, ";
472cdf0e10cSrcweir 		sCreateStatement += "FIRSTNAME VARCHAR(50), ";
473cdf0e10cSrcweir 		sCreateStatement += "LASTNAME VARCHAR(100), ";
474cdf0e10cSrcweir 		sCreateStatement += "STREET VARCHAR(50), ";
475cdf0e10cSrcweir 		sCreateStatement += "STATE VARCHAR(50), ";
476cdf0e10cSrcweir 		sCreateStatement += "ZIP INTEGER, ";
477cdf0e10cSrcweir 		sCreateStatement += "BIRTHDATE DATE, ";
478cdf0e10cSrcweir 		sCreateStatement += "PRIMARY KEY(SNR))";
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 		if ( implCreateTable( xConn, sCreateStatement, s_tableNameSalesmen) )
481cdf0e10cSrcweir 		{
482cdf0e10cSrcweir 			String sInsertionPrefix = "INSERT INTO " + s_tableNameSalesmen + " VALUES ";
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(1, 'Joseph', 'Smith', 'Bond Street', 'CA', 95460, '1946-07-02')" );
485cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(2, 'Frank', 'Jones', 'Lake silver', 'CA', 95460, '1963-12-24')" );
486cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(3, 'Jane', 'Esperansa', '23 Hollywood driver', 'CA', 95460, '1972-04-01')" );
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 			return true;
489cdf0e10cSrcweir 		}
490cdf0e10cSrcweir 		return false;
491cdf0e10cSrcweir 	}
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
494cdf0e10cSrcweir 	/** creates the table CUSTOMERS
495cdf0e10cSrcweir 
496cdf0e10cSrcweir 		@return
497cdf0e10cSrcweir 			<TRUE/> if and only if the creation succeeded
498cdf0e10cSrcweir 	*/
createTableCustomer( XConnection xConn )499cdf0e10cSrcweir 	protected boolean createTableCustomer( XConnection xConn ) throws java.lang.Exception
500cdf0e10cSrcweir 	{
501cdf0e10cSrcweir 		String sCreateStatement = "CREATE TABLE " + s_tableNameCustomers + " ";
502cdf0e10cSrcweir 		sCreateStatement += "(COS_NR INTEGER NOT NULL, ";
503cdf0e10cSrcweir 		sCreateStatement += "LASTNAME VARCHAR(100), ";
504cdf0e10cSrcweir 		sCreateStatement += "STREET VARCHAR(50), ";
505cdf0e10cSrcweir 		sCreateStatement += "CITY VARCHAR(50), ";
506cdf0e10cSrcweir 		sCreateStatement += "STATE VARCHAR(50), ";
507cdf0e10cSrcweir 		sCreateStatement += "ZIP INTEGER, ";
508cdf0e10cSrcweir 		sCreateStatement += "PRIMARY KEY(COS_NR))";
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 		if ( implCreateTable( xConn, sCreateStatement, s_tableNameCustomers ) )
511cdf0e10cSrcweir 		{
512cdf0e10cSrcweir 			String sInsertionPrefix = "INSERT INTO " + s_tableNameCustomers + " VALUES ";
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(100, 'Acme, Inc.', '99 Market Street', 'Groundsville', 'CA', 95199)" );
515cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(101, 'Superior BugSoft', '1 Party Place', 'Mendocino', 'CA', 95460)");
516cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(102, 'WeKnowAll, Inc.', '100 Coffee Lane', 'Meadows', 'CA', 93699)");
517cdf0e10cSrcweir 
518cdf0e10cSrcweir 			return true;
519cdf0e10cSrcweir 		}
520cdf0e10cSrcweir 		return false;
521cdf0e10cSrcweir 	}
522cdf0e10cSrcweir 
523cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
524cdf0e10cSrcweir 	/** creates the table SALES
525cdf0e10cSrcweir 
526cdf0e10cSrcweir 		@return
527cdf0e10cSrcweir 			<TRUE/> if and only if the creation succeeded
528cdf0e10cSrcweir 	*/
createTableSales( XConnection xConn )529cdf0e10cSrcweir 	protected boolean createTableSales( XConnection xConn ) throws java.lang.Exception
530cdf0e10cSrcweir 	{
531cdf0e10cSrcweir 		String sCreateStatement = "CREATE TABLE " + s_tableNameSales + " ";
532cdf0e10cSrcweir 		sCreateStatement += "(SALENR INTEGER NOT NULL, ";
533cdf0e10cSrcweir 		sCreateStatement += "COS_NR INTEGER NOT NULL, ";
534cdf0e10cSrcweir 		sCreateStatement += "SNR INTEGER NOT NULL, ";
535cdf0e10cSrcweir 		sCreateStatement += "NAME VARCHAR(50), ";
536cdf0e10cSrcweir 		sCreateStatement += "SALEDATE DATE, ";
537cdf0e10cSrcweir 		sCreateStatement += "PRICE DECIMAL(8,2), ";
538cdf0e10cSrcweir 		sCreateStatement += "PRIMARY KEY(SALENR))";
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 		if ( implCreateTable( xConn, sCreateStatement, s_tableNameSales ) )
541cdf0e10cSrcweir 		{
542cdf0e10cSrcweir 			String sInsertionPrefix = "INSERT INTO " + s_tableNameSales + " VALUES ";
543cdf0e10cSrcweir 
544cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(1, 100, 1, 'Fruits', '2005-02-12', 39.99)" );
545cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(2, 101, 3, 'Beef', '2005-10-18', 15.78)" );
546cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(3, 102, 3, 'Orange Juice', '2005-09-08', 25.63)" );
547cdf0e10cSrcweir 			implExecuteStatement( xConn, sInsertionPrefix + "(4, 101, 2, 'Oil', '2005-03-01', 12.30)" );
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 			return true;
550cdf0e10cSrcweir 		}
551cdf0e10cSrcweir 
552cdf0e10cSrcweir 		return false;
553cdf0e10cSrcweir 	}
554cdf0e10cSrcweir 
555cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
556cdf0e10cSrcweir 	/** ensures that the tables we need for our example exist
557cdf0e10cSrcweir 	*/
ensureTables()558cdf0e10cSrcweir 	protected void ensureTables() throws java.lang.Exception
559cdf0e10cSrcweir 	{
560cdf0e10cSrcweir 		// get the data source
561cdf0e10cSrcweir 		XDataSource xDS = m_database.getDataSource();
562cdf0e10cSrcweir 		XPropertySet xDSProps = UNO.queryPropertySet( xDS );
563cdf0e10cSrcweir 
564cdf0e10cSrcweir 		// connect to this data source
565cdf0e10cSrcweir 		XConnection xConn = xDS.getConnection( "", "" );
566cdf0e10cSrcweir 		XComponent xConnComp = UNO.queryComponent( xConn );
567cdf0e10cSrcweir 
568cdf0e10cSrcweir         createTableSalesman( xConn );
569cdf0e10cSrcweir         createTableCustomer( xConn );
570cdf0e10cSrcweir         createTableSales( xConn );
571cdf0e10cSrcweir 
572cdf0e10cSrcweir         // free the resources acquired by the connection
573cdf0e10cSrcweir 		xConnComp.dispose();
574cdf0e10cSrcweir 	}
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 	/* ==================================================================
577cdf0e10cSrcweir 	   = sample document handling
578cdf0e10cSrcweir 	   ================================================================== */
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
581cdf0e10cSrcweir 	/** creates the button used for demonstrating (amonst others) event handling
582cdf0e10cSrcweir 		@param nXPos
583cdf0e10cSrcweir 			x-position of the to be inserted shape
584cdf0e10cSrcweir 		@param nYPos
585cdf0e10cSrcweir 			y-position of the to be inserted shape
586cdf0e10cSrcweir 		@param nXSize
587cdf0e10cSrcweir 			width of the to be inserted shape
588cdf0e10cSrcweir 		@param sName
589cdf0e10cSrcweir 			the name of the model in the form component hierarchy
590cdf0e10cSrcweir 		@param sLabel
591cdf0e10cSrcweir 			the label of the button control
592cdf0e10cSrcweir 		@param sActionURL
593cdf0e10cSrcweir 			the URL of the action which should be triggered by the button
594cdf0e10cSrcweir 		@return
595cdf0e10cSrcweir 			the model of the newly created button
596cdf0e10cSrcweir 	*/
createButton( int nXPos, int nYPos, int nXSize, String sName, String sLabel, short _formFeature )597cdf0e10cSrcweir 	protected XPropertySet createButton( int nXPos, int nYPos, int nXSize, String sName, String sLabel, short _formFeature ) throws java.lang.Exception
598cdf0e10cSrcweir 	{
599cdf0e10cSrcweir 		XPropertySet xButton = m_formLayer.createControlAndShape( "CommandButton", nXPos, nYPos, nXSize, 6 );
600cdf0e10cSrcweir 		// the name for referring to it later:
601cdf0e10cSrcweir 		xButton.setPropertyValue( "Name", sName );
602cdf0e10cSrcweir 		// the label
603cdf0e10cSrcweir 		xButton.setPropertyValue( "Label", sLabel );
604cdf0e10cSrcweir 		// use the name as help text
605cdf0e10cSrcweir 		xButton.setPropertyValue( "HelpText", sName );
606cdf0e10cSrcweir 		// don't want buttons to be accessible by the "tab" key - this would be uncomfortable when traveling
607cdf0e10cSrcweir 		// with records with "tab"
608cdf0e10cSrcweir 		xButton.setPropertyValue( "Tabstop", new Boolean( false ) );
609cdf0e10cSrcweir         // similar, they should not steal the focus when clicked
610cdf0e10cSrcweir 		xButton.setPropertyValue( "FocusOnClick", new Boolean( false ) );
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 		m_aOperator.addButton( xButton, _formFeature );
613cdf0e10cSrcweir 
614cdf0e10cSrcweir 		return xButton;
615cdf0e10cSrcweir 	}
616cdf0e10cSrcweir 
617cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
618cdf0e10cSrcweir 	/** creates a column in a grid
619cdf0e10cSrcweir 		@param xGridModel
620cdf0e10cSrcweir 			specifies the model of the grid where the new column should be inserted
621cdf0e10cSrcweir 		@param sColumnService
622cdf0e10cSrcweir 			specifies the service name of the column to create (e.g. "NumericField")
623cdf0e10cSrcweir 		@param sDataField
624cdf0e10cSrcweir 			specifies the database field to which the column should be bound
625cdf0e10cSrcweir 		@param nWidth
626cdf0e10cSrcweir 			specifies the column width (in mm). If 0, no width is set.
627cdf0e10cSrcweir 		@return
628cdf0e10cSrcweir 			the newly created column
629cdf0e10cSrcweir 	*/
createGridColumn( Object aGridModel, String sColumnService, String sDataField, int nWidth )630cdf0e10cSrcweir 	XPropertySet createGridColumn( Object aGridModel, String sColumnService, String sDataField, int nWidth )
631cdf0e10cSrcweir 		throws com.sun.star.uno.Exception
632cdf0e10cSrcweir 	{
633cdf0e10cSrcweir 		// the container to insert columns into
634cdf0e10cSrcweir 		XIndexContainer xColumnContainer = UNO.queryIndexContainer( aGridModel );
635cdf0e10cSrcweir 		// the factory for creating column models
636cdf0e10cSrcweir 		XGridColumnFactory xColumnFactory = (XGridColumnFactory)UnoRuntime.queryInterface(
637cdf0e10cSrcweir 			XGridColumnFactory.class, aGridModel );
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 		// (let) create the new col
640cdf0e10cSrcweir 		XInterface xNewCol = (XInterface)xColumnFactory.createColumn( sColumnService );
641cdf0e10cSrcweir 		XPropertySet xColProps = UNO.queryPropertySet( xNewCol );
642cdf0e10cSrcweir 
643cdf0e10cSrcweir 		// some props
644cdf0e10cSrcweir 		// the field the column is bound to
645cdf0e10cSrcweir 		xColProps.setPropertyValue( "DataField", sDataField );
646cdf0e10cSrcweir 		// the "display name" of the column
647cdf0e10cSrcweir 		xColProps.setPropertyValue( "Label", sDataField );
648cdf0e10cSrcweir 		// the name of the column within it's parent
649cdf0e10cSrcweir 		xColProps.setPropertyValue( "Name", sDataField );
650cdf0e10cSrcweir 
651cdf0e10cSrcweir 		if ( nWidth > 0 )
652cdf0e10cSrcweir 			xColProps.setPropertyValue( "Width", new Integer( nWidth * 10 ) );
653cdf0e10cSrcweir 
654cdf0e10cSrcweir 		// insert
655cdf0e10cSrcweir 		xColumnContainer.insertByIndex( xColumnContainer.getCount(), xNewCol );
656cdf0e10cSrcweir 
657cdf0e10cSrcweir 		// outta here
658cdf0e10cSrcweir 		return xColProps;
659cdf0e10cSrcweir 	}
660cdf0e10cSrcweir 
661cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
662cdf0e10cSrcweir 	/** creates a column in a grid
663cdf0e10cSrcweir 	*/
createGridColumn( Object aGridModel, String sColumnService, String sDataField )664cdf0e10cSrcweir 	XPropertySet createGridColumn( Object aGridModel, String sColumnService, String sDataField )
665cdf0e10cSrcweir 		throws com.sun.star.uno.Exception
666cdf0e10cSrcweir 	{
667cdf0e10cSrcweir 		return createGridColumn( aGridModel, sColumnService, sDataField );
668cdf0e10cSrcweir 	}
669cdf0e10cSrcweir 
670cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
671cdf0e10cSrcweir 	/** creates our sample document
672cdf0e10cSrcweir 	*/
prepareDocument()673cdf0e10cSrcweir     protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception
674cdf0e10cSrcweir 	{
675cdf0e10cSrcweir         super.prepareDocument();
676cdf0e10cSrcweir 
677cdf0e10cSrcweir         m_database = new HsqlDatabase( m_xCtx );
678cdf0e10cSrcweir 
679cdf0e10cSrcweir         // ensure that we have the tables needed for our example
680cdf0e10cSrcweir         ensureTables();
681cdf0e10cSrcweir 
682cdf0e10cSrcweir         // --------------------------------------------------------------
683cdf0e10cSrcweir 		/* create some shapes */
684cdf0e10cSrcweir 		XPropertySet xSNRField =	m_formLayer.insertControlLine( "NumericField", "SNR", "", 3 );
685cdf0e10cSrcweir                                     m_formLayer.insertControlLine( "TextField", "FIRSTNAME", "", 11);
686cdf0e10cSrcweir                                     m_formLayer.insertControlLine( "TextField", "LASTNAME", "", 19 );
687cdf0e10cSrcweir                                     m_formLayer.insertControlLine( "TextField", "STREET", "", 27 );
688cdf0e10cSrcweir                                     m_formLayer.insertControlLine( "TextField", "STATE", "", 35 );
689cdf0e10cSrcweir         XPropertySet xZipField =    m_formLayer.insertControlLine( "NumericField", "ZIP", "", 43 );
690cdf0e10cSrcweir                                     m_formLayer.insertControlLine( "FormattedField", "BIRTHDATE", "", 51 );
691cdf0e10cSrcweir 
692cdf0e10cSrcweir 		// for the salesman number / zip code, we don't want to have decimal places:
693cdf0e10cSrcweir 		xSNRField.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) );
694cdf0e10cSrcweir 		xZipField.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) );
695cdf0e10cSrcweir 
696cdf0e10cSrcweir         // --------------------------------------------------------------
697cdf0e10cSrcweir 		/** need the form the control models belong to
698cdf0e10cSrcweir 			for this, we simply obtain the parent for any of the control models we have
699cdf0e10cSrcweir 
700cdf0e10cSrcweir 			Note that this involves knowledge about the implementation: If a control shape is
701cdf0e10cSrcweir 			inserted into a document, where the control model does not belong to the form component
702cdf0e10cSrcweir 			hierarchy, yet, it is automatically inserted into the first form, which is created
703cdf0e10cSrcweir 			if necessary.
704cdf0e10cSrcweir 		*/
705cdf0e10cSrcweir 		m_xMasterForm = FLTools.getParent( xZipField );
706cdf0e10cSrcweir 
707cdf0e10cSrcweir 		// set the data source signature at the form
708cdf0e10cSrcweir 		m_xMasterForm.setPropertyValue( "DataSourceName", m_database.getDocumentURL() );
709cdf0e10cSrcweir 		m_xMasterForm.setPropertyValue( "CommandType", new Integer( CommandType.TABLE ) );
710cdf0e10cSrcweir 		m_xMasterForm.setPropertyValue( "Command", "SALESMEN" );
711cdf0e10cSrcweir 
712cdf0e10cSrcweir         // --------------------------------------------------------------
713cdf0e10cSrcweir 		// insert the buttons
714cdf0e10cSrcweir 		// create our button operator, if necessary
715cdf0e10cSrcweir         m_aOperator = new ButtonOperator( m_xCtx, m_document, m_xMasterForm );
716cdf0e10cSrcweir 
717cdf0e10cSrcweir 		createButton( 2, 63, 8, "first", "<<", FormFeature.MoveToFirst );
718cdf0e10cSrcweir 		createButton( 12, 63, 8, "prev", "<", FormFeature.MoveToPrevious );
719cdf0e10cSrcweir 		createButton( 22, 63, 8, "next", ">", FormFeature.MoveToNext );
720cdf0e10cSrcweir 		createButton( 32, 63, 8, "last", ">>", FormFeature.MoveToLast );
721cdf0e10cSrcweir 		createButton( 42, 63, 8, "new", ">*", FormFeature.MoveToInsertRow );
722cdf0e10cSrcweir 		createButton( 58, 63, 13, "reload", "reload", FormFeature.ReloadForm );
723cdf0e10cSrcweir 
724cdf0e10cSrcweir         // --------------------------------------------------------------
725cdf0e10cSrcweir 		// create a sub for for the sales
726cdf0e10cSrcweir 
727cdf0e10cSrcweir 		// for this, first create a sub form and bind it to the SALES table
728cdf0e10cSrcweir 		XIndexContainer xSalesForm = m_document.createSubForm( m_xMasterForm, "Sales" );
729cdf0e10cSrcweir 		XPropertySet xSalesFormProps = UNO.queryPropertySet( xSalesForm );
730cdf0e10cSrcweir 
731cdf0e10cSrcweir 		xSalesFormProps.setPropertyValue( "DataSourceName", m_database.getDocumentURL() );
732cdf0e10cSrcweir 		xSalesFormProps.setPropertyValue( "CommandType", new Integer( CommandType.COMMAND ) );
733cdf0e10cSrcweir 
734cdf0e10cSrcweir 		String sCommand = new String( "SELECT * FROM " );
735cdf0e10cSrcweir 		sCommand += s_tableNameSales;
736cdf0e10cSrcweir 		sCommand += " WHERE " + s_tableNameSales + ".SNR = :salesmen";
737cdf0e10cSrcweir 		xSalesFormProps.setPropertyValue( "Command", sCommand );
738cdf0e10cSrcweir 
739cdf0e10cSrcweir 		// the master-details connection
740cdf0e10cSrcweir 		String[] aMasterFields = new String[] { "SNR" };		// the field in the master form
741cdf0e10cSrcweir 		String[] aDetailFields = new String[] { "salesmen" };	// the name in the detail form
742cdf0e10cSrcweir 		xSalesFormProps.setPropertyValue( "MasterFields", aMasterFields );
743cdf0e10cSrcweir 		xSalesFormProps.setPropertyValue( "DetailFields", aDetailFields );
744cdf0e10cSrcweir 
745cdf0e10cSrcweir 		// the create thr grid model
746cdf0e10cSrcweir         XPropertySet xSalesGridModel = m_formLayer.createControlAndShape( "GridControl", 2, 80, 162, 40, xSalesForm );
747cdf0e10cSrcweir 		xSalesGridModel.setPropertyValue( "Name", "SalesTable" );
748cdf0e10cSrcweir 		XPropertySet xKeyColumn		 =	createGridColumn( xSalesGridModel, "NumericField", "SALENR", 12 );
749cdf0e10cSrcweir 		XPropertySet xCustomerColumn =	createGridColumn( xSalesGridModel, "ListBox", "COS_NR", 40 );
750cdf0e10cSrcweir 		XPropertySet xSalesNameColumn =	createGridColumn( xSalesGridModel, "TextField", "NAME", 25 );
751cdf0e10cSrcweir 										createGridColumn( xSalesGridModel, "DateField", "SALEDATE", 24 );
752cdf0e10cSrcweir 										createGridColumn( xSalesGridModel, "CurrencyField", "PRICE", 16 );
753cdf0e10cSrcweir 
754cdf0e10cSrcweir 			// please note that a better solution for the SALEDATE field would have been to use
755cdf0e10cSrcweir 			// a FormattedField. But we want to demonstrate some effects with DateFields here ...
756cdf0e10cSrcweir 
757cdf0e10cSrcweir 		m_aSalesNameValidator = new GridFieldValidator( m_xCtx, xSalesNameColumn );
758cdf0e10cSrcweir 		m_aSalesNameValidator.enableColumnWatch( m_bAllowEmptySales );
759cdf0e10cSrcweir 
760cdf0e10cSrcweir 		xKeyColumn.setPropertyValue( "DecimalAccuracy", new Short( (short)0 ) );
761cdf0e10cSrcweir 
762cdf0e10cSrcweir 		// init the list box which is for choosing the customer a sale belongs to
763cdf0e10cSrcweir 		xCustomerColumn.setPropertyValue( "BoundColumn", new Short( (short)1 ) );
764cdf0e10cSrcweir 		xCustomerColumn.setPropertyValue( "Label", "Customer" );
765cdf0e10cSrcweir 		xCustomerColumn.setPropertyValue( "ListSourceType", ListSourceType.SQL );
766cdf0e10cSrcweir 
767cdf0e10cSrcweir 		String sListSource = "SELECT LASTNAME, COS_NR FROM ";
768cdf0e10cSrcweir 		sListSource += s_tableNameCustomers;
769cdf0e10cSrcweir 		String[] aListSource = new String[] { sListSource };
770cdf0e10cSrcweir 		xCustomerColumn.setPropertyValue( "ListSource", aListSource );
771cdf0e10cSrcweir 
772cdf0e10cSrcweir 		// We want to demonstrate how to reset fields to NULL, we do this with the SALEDATE field
773cdf0e10cSrcweir 		// above. For this, we add as reset listener to the form
774cdf0e10cSrcweir 		XReset xFormReset = UNO.queryReset( xSalesForm );
775cdf0e10cSrcweir 		xFormReset.addResetListener( this );
776cdf0e10cSrcweir 
777cdf0e10cSrcweir 
778cdf0e10cSrcweir         // --------------------------------------------------------------
779cdf0e10cSrcweir 		// the option for filtering the sales form
780cdf0e10cSrcweir 		XIndexContainer xSalesFilterForm = m_document.createSiblingForm( xSalesForm, "SalesFilter" );
781cdf0e10cSrcweir 		XPropertySet xSFFProps = UNO.queryPropertySet( xSalesFilterForm );
782cdf0e10cSrcweir         XPropertySet xLabel = m_formLayer.createControlAndShape( "FixedText", 2, 125, 35, 6, xSalesFilterForm );
783cdf0e10cSrcweir 		xLabel.setPropertyValue( "Label", "show only sales since" );
784cdf0e10cSrcweir 		xLabel.setPropertyValue( "Name", "FilterLabel" );
785cdf0e10cSrcweir 
786cdf0e10cSrcweir         XPropertySet xFilterSelection = m_formLayer.createControlAndShape( "ListBox", 40, 125, 59, 6, xSalesFilterForm );
787cdf0e10cSrcweir 		xFilterSelection.setPropertyValue( "Name", "FilterList" );
788cdf0e10cSrcweir 		xFilterSelection.setPropertyValue( "LabelControl", xLabel );
789cdf0e10cSrcweir         XPropertySet xManualFilter = m_formLayer.createControlAndShape( "DateField", 104, 125, 30, 6, xSalesFilterForm );
790cdf0e10cSrcweir 		xManualFilter.setPropertyValue( "Name", "ManualFilter" );
791cdf0e10cSrcweir         XPropertySet xApplyFilter = m_formLayer.createControlAndShape( "CommandButton", 139, 125, 25, 6, xSalesFilterForm );
792cdf0e10cSrcweir 		xApplyFilter.setPropertyValue( "Name", "ApplyFilter" );
793cdf0e10cSrcweir 		xApplyFilter.setPropertyValue( "DefaultButton", new Boolean( true ) );
794cdf0e10cSrcweir 		m_aSalesFilter = new SalesFilter( m_document, xSalesFormProps, xFilterSelection,
795cdf0e10cSrcweir 			xManualFilter, xApplyFilter );
796cdf0e10cSrcweir 
797cdf0e10cSrcweir 
798cdf0e10cSrcweir         // --------------------------------------------------------------
799cdf0e10cSrcweir 		// the options section
800cdf0e10cSrcweir 		// for this, we need a form which is a sibling of our master form (don't want to interfere
801cdf0e10cSrcweir 		// the controls which represent options only with the controls which are used for data access)
802cdf0e10cSrcweir 
803cdf0e10cSrcweir 		XIndexContainer xOptionsForm = m_document.createSiblingForm( m_xMasterForm, "Options" );
804cdf0e10cSrcweir 
805cdf0e10cSrcweir         xLabel = m_formLayer.createControlAndShape( "GroupBox", 98, 0, 66, 62, xOptionsForm );
806cdf0e10cSrcweir 		xLabel.setPropertyValue( "Name", "Options" );
807cdf0e10cSrcweir 		xLabel.setPropertyValue( "Label", "Options" );
808cdf0e10cSrcweir 
809cdf0e10cSrcweir 		// radio buttons which controls how we generate unique keys
810cdf0e10cSrcweir         xLabel = m_formLayer.createControlAndShape( "GroupBox", 103, 5, 56, 25, xOptionsForm );
811cdf0e10cSrcweir 		xLabel.setPropertyValue( "Label", "key generation" );
812cdf0e10cSrcweir 		xLabel.setPropertyValue( "Name", "KeyGeneration" );
813cdf0e10cSrcweir         XPropertySet xKeyGen = m_formLayer.createControlAndShape( "RadioButton", 106, 11, 50, 6, xOptionsForm );
814cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "Name", "KeyGen" );
815cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "Label", "no automatic generation" );
816cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "RefValue", "none" );
817cdf0e10cSrcweir 		xKeyGen.addPropertyChangeListener( "State", this );
818cdf0e10cSrcweir 
819cdf0e10cSrcweir         xKeyGen = m_formLayer.createControlAndShape( "RadioButton", 106, 17, 50, 6, xOptionsForm );
820cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "Name", "KeyGen" );
821cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "Label", "before inserting a record" );
822cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "RefValue", "update" );
823cdf0e10cSrcweir 		xKeyGen.addPropertyChangeListener( "State", this );
824cdf0e10cSrcweir 
825cdf0e10cSrcweir         xKeyGen = m_formLayer.createControlAndShape( "RadioButton", 106, 23, 50, 6, xOptionsForm );
826cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "Name", "KeyGen" );
827cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "Label", "when moving to a new record" );
828cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "RefValue", "reset" );
829cdf0e10cSrcweir 		xKeyGen.addPropertyChangeListener( "State", this );
830cdf0e10cSrcweir 
831cdf0e10cSrcweir 		// initialize listeners
832cdf0e10cSrcweir 		// master form - key generation
833cdf0e10cSrcweir 		m_aSalesmanKeyGenerator = new KeyGenerator( m_xMasterForm, "SNR", m_xCtx );
834cdf0e10cSrcweir 		m_aSalesmanKeyGenerator.activateKeyGenerator( true );
835cdf0e10cSrcweir 		// master form - control locking
836cdf0e10cSrcweir 		m_aSalesmenLocker = new ControlLock( m_xMasterForm, "SNR" );
837cdf0e10cSrcweir 		m_aSalesmenLocker.enableLock( m_bProtectKeyFields );
838cdf0e10cSrcweir 
839cdf0e10cSrcweir 		// details form - key generation
840cdf0e10cSrcweir 		m_aSalesKeyGenerator = new KeyGenerator( xSalesFormProps, "SALENR", m_xCtx );
841cdf0e10cSrcweir 		m_aSalesKeyGenerator.activateKeyGenerator( true );
842cdf0e10cSrcweir 
843cdf0e10cSrcweir 		// details form - control locking
844cdf0e10cSrcweir 		m_aSalesLocker = new ControlLock( xSalesFormProps, "SALENR" );
845cdf0e10cSrcweir 		m_aSalesLocker.enableLock( m_bProtectKeyFields );
846cdf0e10cSrcweir 
847*a893be29SPedro Giffuni 		// initially, we want to generate keys when moving to a new record
848cdf0e10cSrcweir 		xKeyGen.setPropertyValue( "DefaultState", new Short( (short)1 ) );
849cdf0e10cSrcweir 
850cdf0e10cSrcweir         // --------------------------------------------------------------
851cdf0e10cSrcweir 		// second options block
852cdf0e10cSrcweir         xLabel = m_formLayer.createControlAndShape( "GroupBox", 103, 33, 56, 25, xOptionsForm  );
853cdf0e10cSrcweir 		xLabel.setPropertyValue( "Name", "Misc" );
854cdf0e10cSrcweir 		xLabel.setPropertyValue( "Label", "Miscellaneous" );
855cdf0e10cSrcweir 
856cdf0e10cSrcweir         XPropertySet xCheck = m_formLayer.createControlAndShape( "CheckBox", 106, 39, 60, 6, xOptionsForm  );
857cdf0e10cSrcweir 		xCheck.setPropertyValue( "Name", "defaultdate" );
858cdf0e10cSrcweir 		xCheck.setPropertyValue( "Label", "default sales date to \"today\"" );
859cdf0e10cSrcweir         xCheck.setPropertyValue( "HelpText", "When checked, newly entered sales records are pre-filled with today's date, else left empty." );
860cdf0e10cSrcweir 		xCheck.addPropertyChangeListener( "State", this );
861cdf0e10cSrcweir 
862cdf0e10cSrcweir         xCheck = m_formLayer.createControlAndShape( "CheckBox", 106, 45, 60, 6, xOptionsForm  );
863cdf0e10cSrcweir 		xCheck.setPropertyValue( "Name", "protectkeys" );
864cdf0e10cSrcweir 		xCheck.setPropertyValue( "Label", "protect key fields from editing" );
865cdf0e10cSrcweir         xCheck.setPropertyValue( "HelpText", "When checked, you cannot modify the values in the table's key fields (SNR and SALENR)" );
866cdf0e10cSrcweir 		xCheck.addPropertyChangeListener( "State", this );
867cdf0e10cSrcweir 
868cdf0e10cSrcweir         xCheck = m_formLayer.createControlAndShape( "CheckBox", 106, 51, 60, 6, xOptionsForm  );
869cdf0e10cSrcweir 		xCheck.setPropertyValue( "Name", "emptysales" );
870cdf0e10cSrcweir 		xCheck.setPropertyValue( "Label", "check for empty sales names" );
871cdf0e10cSrcweir         xCheck.setPropertyValue( "HelpText", "When checked, you cannot enter empty values into the NAME column of the 'Sales' table." );
872cdf0e10cSrcweir 		xCheck.addPropertyChangeListener( "State", this );
873cdf0e10cSrcweir 
874cdf0e10cSrcweir         // dump the form component tree
875cdf0e10cSrcweir         enumFormComponents( );
876cdf0e10cSrcweir 	}
877cdf0e10cSrcweir 
878cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
onFormsAlive()879cdf0e10cSrcweir     protected void onFormsAlive()
880cdf0e10cSrcweir     {
881cdf0e10cSrcweir         m_aOperator.onFormsAlive();
882cdf0e10cSrcweir     }
883cdf0e10cSrcweir 
884cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
885cdf0e10cSrcweir 	/** performs any cleanup before exiting the program
886cdf0e10cSrcweir 	*/
cleanUp( )887cdf0e10cSrcweir 	protected void cleanUp( ) throws java.lang.Exception
888cdf0e10cSrcweir 	{
889cdf0e10cSrcweir 		// remove the listeners at the buttons
890cdf0e10cSrcweir 		RevokeButtons aRevoke = new RevokeButtons( m_aOperator );
891cdf0e10cSrcweir 		aRevoke.handle( m_document.getFormComponentTreeRoot( ) );
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 		// remove the key generator listeners from the form
894cdf0e10cSrcweir 		m_aSalesmanKeyGenerator.stopGenerator( );
895cdf0e10cSrcweir 		m_aSalesKeyGenerator.stopGenerator( );
896cdf0e10cSrcweir 
897cdf0e10cSrcweir 		// and the control lockers
898cdf0e10cSrcweir 		m_aSalesmenLocker.enableLock( false );
899cdf0e10cSrcweir 		m_aSalesLocker.enableLock( false );
900cdf0e10cSrcweir 
901cdf0e10cSrcweir 		// the validator for the grid column
902cdf0e10cSrcweir 		m_aSalesNameValidator.enableColumnWatch( false );
903cdf0e10cSrcweir 
904cdf0e10cSrcweir 		// remove our own reset listener from the form
905cdf0e10cSrcweir 		XNameAccess xMasterAsNames = (XNameAccess)UnoRuntime.queryInterface(
906cdf0e10cSrcweir 			XNameAccess.class, m_xMasterForm );
907cdf0e10cSrcweir 		XReset xFormReset = UNO.queryReset( xMasterAsNames.getByName( "Sales" ) );
908cdf0e10cSrcweir 		xFormReset.removeResetListener( this );
909cdf0e10cSrcweir 
910cdf0e10cSrcweir         super.cleanUp();
911cdf0e10cSrcweir 	}
912cdf0e10cSrcweir 
913cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
914cdf0e10cSrcweir 	/** class entry point
915cdf0e10cSrcweir 	*/
main(String argv[])916cdf0e10cSrcweir 	public static void main(String argv[]) throws java.lang.Exception
917cdf0e10cSrcweir 	{
918cdf0e10cSrcweir         DataAwareness aSample = new DataAwareness();
919cdf0e10cSrcweir 		aSample.run( argv );
920cdf0e10cSrcweir 	}
921cdf0e10cSrcweir }
922