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