merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Forms / GridFieldValidator.java
blobe003e8bb3d1628eb8691c67ccfd4e68a2ff78ecf
1 /*************************************************************************
3 * $RCSfile: GridFieldValidator.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:30:37 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 /**************************************************************************/
42 import com.sun.star.uno.*;
43 import com.sun.star.beans.*;
44 import com.sun.star.form.*;
45 import com.sun.star.lang.*;
46 import com.sun.star.sdb.*;
47 import com.sun.star.sdbc.*;
48 import com.sun.star.sdbcx.*;
49 import com.sun.star.container.*;
50 import com.sun.star.awt.*;
51 import com.sun.star.task.*;
53 /**************************************************************************/
54 /** helper class for validating a grid field before it is updated
56 <p>Actually, the mechanism for validating the field is not restricted to
57 grid control fields. Instead, it can be used for any bound controls.</p>
59 class GridFieldValidator implements XUpdateListener
61 private DocumentHelper m_aDocument;
62 private XComponentContext m_xCtx;
63 private XPropertySet m_xWatchedColumn;
65 private boolean m_bWatching;
67 /* ------------------------------------------------------------------ */
68 public GridFieldValidator( XComponentContext xCtx, XPropertySet xWatchedGridColumn )
70 // remember
71 m_xCtx = xCtx;
72 m_xWatchedColumn = xWatchedGridColumn;
73 m_aDocument = DocumentHelper.getDocumentForComponent(xWatchedGridColumn,
74 xCtx);
76 m_bWatching = false;
79 /* ------------------------------------------------------------------ */
80 public void enableColumnWatch( boolean bEnable )
82 if ( bEnable == m_bWatching )
83 return;
85 XUpdateBroadcaster xUpdate = (XUpdateBroadcaster)UnoRuntime.queryInterface(
86 XUpdateBroadcaster.class, m_xWatchedColumn );
88 if ( bEnable )
89 xUpdate.addUpdateListener( this );
90 else
91 xUpdate.removeUpdateListener( this );
93 m_bWatching = bEnable;
96 /* ------------------------------------------------------------------ */
97 /** shows a message that we can't do several things due to an implementation error
99 private void showInvalidValueMessage( )
103 // build the message we want to show
104 String sMessage = "The column \"";
105 sMessage += FLTools.getLabel( m_xWatchedColumn );
106 sMessage += "\" is not allowed to contain empty strings.";
108 SQLContext aError = new SQLContext(
109 new String( "Invalid Value Entered" ),
110 null,
111 new String( "S1000" ),
113 new Any( new Type(), null ),
114 sMessage
117 // instantiate an interaction handler who can handle SQLExceptions
118 XInteractionHandler xHandler = (XInteractionHandler)UnoRuntime.queryInterface(
119 XInteractionHandler.class,
120 m_xCtx.getServiceManager().createInstanceWithContext(
121 "com.sun.star.sdb.InteractionHandler", m_xCtx ) );
123 // create a new request and execute it
124 InteractionRequest aRequest = new InteractionRequest( aError );
125 xHandler.handle( aRequest );
127 catch( com.sun.star.uno.Exception e )
129 System.out.println(e);
130 e.printStackTrace();
134 /* ------------------------------------------------------------------ */
135 // XUpdateListener overridables
136 /* ------------------------------------------------------------------ */
137 public boolean approveUpdate( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
139 boolean bApproved = true;
142 // the control model which fired the event
143 XPropertySet xSourceProps = UNO.queryPropertySet( aEvent.Source );
145 String sNewText = (String)xSourceProps.getPropertyValue( "Text" );
146 if ( 0 == sNewText.length() )
148 // say that the value is invalid
149 showInvalidValueMessage( );
150 bApproved = false;
152 // reset the control value
153 // for this, we take the current value from the row set field the control
154 // is bound to, and forward it to the control model
155 XColumn xBoundColumn = UNO.queryColumn( xSourceProps.getPropertyValue( "BoundField" ) );
156 if ( null != xBoundColumn )
158 xSourceProps.setPropertyValue( "Text", xBoundColumn.getString() );
162 catch( com.sun.star.uno.Exception e )
164 System.out.println(e);
165 e.printStackTrace();
167 return bApproved;
170 /* ------------------------------------------------------------------ */
171 public void updated( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
175 /* ------------------------------------------------------------------ */
176 // XEventListener overridables
177 /* ------------------------------------------------------------------ */
178 public void disposing( EventObject aEvent )
180 // not interested in