1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /**************************************************************************/
25cdf0e10cSrcweir import com.sun.star.uno.*;
26cdf0e10cSrcweir import com.sun.star.beans.*;
27cdf0e10cSrcweir import com.sun.star.form.*;
28cdf0e10cSrcweir import com.sun.star.lang.*;
29cdf0e10cSrcweir import com.sun.star.sdb.*;
30cdf0e10cSrcweir import com.sun.star.sdbc.*;
31cdf0e10cSrcweir import com.sun.star.sdbcx.*;
32cdf0e10cSrcweir import com.sun.star.container.*;
33cdf0e10cSrcweir import com.sun.star.awt.*;
34cdf0e10cSrcweir import com.sun.star.task.*;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /**************************************************************************/
37cdf0e10cSrcweir /** helper class for validating a grid field before it is updated
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 	<p>Actually, the mechanism for validating the field is not restricted to
40cdf0e10cSrcweir 	grid control fields. Instead, it can be used for any bound controls.</p>
41cdf0e10cSrcweir */
42cdf0e10cSrcweir class GridFieldValidator implements XUpdateListener
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	private DocumentHelper		m_aDocument;
45cdf0e10cSrcweir 	private	XComponentContext	m_xCtx;
46cdf0e10cSrcweir 	private XPropertySet		m_xWatchedColumn;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	private	boolean			m_bWatching;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
GridFieldValidator( XComponentContext xCtx, XPropertySet xWatchedGridColumn )51cdf0e10cSrcweir 	public GridFieldValidator( XComponentContext xCtx, XPropertySet xWatchedGridColumn )
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		// remember
54cdf0e10cSrcweir 		m_xCtx = xCtx;
55cdf0e10cSrcweir 		m_xWatchedColumn = xWatchedGridColumn;
56cdf0e10cSrcweir 		m_aDocument = DocumentHelper.getDocumentForComponent(xWatchedGridColumn,
57cdf0e10cSrcweir                                                              xCtx);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 		m_bWatching = false;
60cdf0e10cSrcweir 	}
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
enableColumnWatch( boolean bEnable )63cdf0e10cSrcweir 	public void enableColumnWatch( boolean bEnable )
64cdf0e10cSrcweir 	{
65cdf0e10cSrcweir 		if ( bEnable == m_bWatching )
66cdf0e10cSrcweir 			return;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 		XUpdateBroadcaster xUpdate = (XUpdateBroadcaster)UnoRuntime.queryInterface(
69cdf0e10cSrcweir 			XUpdateBroadcaster.class, m_xWatchedColumn );
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 		if ( bEnable )
72cdf0e10cSrcweir 			xUpdate.addUpdateListener( this );
73cdf0e10cSrcweir 		else
74cdf0e10cSrcweir 			xUpdate.removeUpdateListener( this );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 		m_bWatching = bEnable;
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
80cdf0e10cSrcweir 	/** shows a message that we can't do several things due to an implementation error
81cdf0e10cSrcweir 	*/
showInvalidValueMessage( )82cdf0e10cSrcweir 	private void showInvalidValueMessage( )
83cdf0e10cSrcweir 	{
84cdf0e10cSrcweir 		try
85cdf0e10cSrcweir 		{
86cdf0e10cSrcweir 			// build the message we want to show
87cdf0e10cSrcweir 			String sMessage = "The column \"";
88cdf0e10cSrcweir 			sMessage += FLTools.getLabel( m_xWatchedColumn );
89cdf0e10cSrcweir 			sMessage += "\" is not allowed to contain empty strings.";
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 			SQLContext aError = new SQLContext(
92cdf0e10cSrcweir 				new String( "Invalid Value Entered" ),
93cdf0e10cSrcweir 				null,
94cdf0e10cSrcweir 				new String( "S1000" ),
95cdf0e10cSrcweir 				0,
96cdf0e10cSrcweir 				new Any( new Type(), null ),
97cdf0e10cSrcweir 				sMessage
98cdf0e10cSrcweir 			);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 			// instantiate an interaction handler who can handle SQLExceptions
101cdf0e10cSrcweir 			XInteractionHandler xHandler = (XInteractionHandler)UnoRuntime.queryInterface(
102cdf0e10cSrcweir 				XInteractionHandler.class,
103cdf0e10cSrcweir                 m_xCtx.getServiceManager().createInstanceWithContext(
104cdf0e10cSrcweir                     "com.sun.star.task.InteractionHandler", m_xCtx ) );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 			// create a new request and execute it
107cdf0e10cSrcweir 			InteractionRequest aRequest = new InteractionRequest( aError );
108cdf0e10cSrcweir 			xHandler.handle( aRequest );
109cdf0e10cSrcweir 		}
110cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			System.out.println(e);
113cdf0e10cSrcweir 			e.printStackTrace();
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
118cdf0e10cSrcweir 	// XUpdateListener overridables
119cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
approveUpdate( EventObject aEvent )120cdf0e10cSrcweir     public boolean approveUpdate( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
121cdf0e10cSrcweir 	{
122cdf0e10cSrcweir 		boolean bApproved = true;
123cdf0e10cSrcweir 		try
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			// the control model which fired the event
126cdf0e10cSrcweir 			XPropertySet xSourceProps = UNO.queryPropertySet( aEvent.Source );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 			String sNewText = (String)xSourceProps.getPropertyValue( "Text" );
129cdf0e10cSrcweir 			if ( 0 == sNewText.length() )
130cdf0e10cSrcweir 			{
131cdf0e10cSrcweir 				// say that the value is invalid
132cdf0e10cSrcweir 				showInvalidValueMessage( );
133cdf0e10cSrcweir 				bApproved = false;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 				// reset the control value
136cdf0e10cSrcweir 				// for this, we take the current value from the row set field the control
137cdf0e10cSrcweir 				// is bound to, and forward it to the control model
138cdf0e10cSrcweir 				XColumn xBoundColumn = UNO.queryColumn( xSourceProps.getPropertyValue( "BoundField" ) );
139cdf0e10cSrcweir 				if ( null != xBoundColumn )
140cdf0e10cSrcweir 				{
141cdf0e10cSrcweir 					xSourceProps.setPropertyValue( "Text", xBoundColumn.getString() );
142cdf0e10cSrcweir 				}
143cdf0e10cSrcweir 			}
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
146cdf0e10cSrcweir 		{
147cdf0e10cSrcweir 			System.out.println(e);
148cdf0e10cSrcweir 			e.printStackTrace();
149cdf0e10cSrcweir 		}
150cdf0e10cSrcweir 		return bApproved;
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
updated( EventObject aEvent )154cdf0e10cSrcweir     public void updated( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir 	}
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
159cdf0e10cSrcweir 	// XEventListener overridables
160cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
disposing( EventObject aEvent )161cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		// not interested in
164cdf0e10cSrcweir 	}
165cdf0e10cSrcweir };
166