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