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