1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
33 *************************************************************************/
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
.task
.*;
43 /**************************************************************************/
44 /** helper class for validating a grid field before it is updated
46 <p>Actually, the mechanism for validating the field is not restricted to
47 grid control fields. Instead, it can be used for any bound controls.</p>
49 class GridFieldValidator
implements XUpdateListener
51 private XComponentContext m_xCtx
;
52 private XPropertySet m_xWatchedColumn
;
54 private boolean m_bWatching
;
56 /* ------------------------------------------------------------------ */
57 public GridFieldValidator( XComponentContext xCtx
, XPropertySet xWatchedGridColumn
)
61 m_xWatchedColumn
= xWatchedGridColumn
;
62 DocumentHelper
.getDocumentForComponent(xWatchedGridColumn
,
68 /* ------------------------------------------------------------------ */
69 public void enableColumnWatch( boolean bEnable
)
71 if ( bEnable
== m_bWatching
)
74 XUpdateBroadcaster xUpdate
= UnoRuntime
.queryInterface(
75 XUpdateBroadcaster
.class, m_xWatchedColumn
);
78 xUpdate
.addUpdateListener( this );
80 xUpdate
.removeUpdateListener( this );
82 m_bWatching
= bEnable
;
85 /* ------------------------------------------------------------------ */
86 /** shows a message that we can't do several things due to an implementation error
88 private void showInvalidValueMessage( )
92 // build the message we want to show
93 String sMessage
= "The column \"";
94 sMessage
+= FLTools
.getLabel( m_xWatchedColumn
);
95 sMessage
+= "\" is not allowed to contain empty strings.";
97 SQLContext aError
= new SQLContext(
98 "Invalid Value Entered",
102 new Any( new Type(), null ),
106 // instantiate an interaction handler who can handle SQLExceptions
107 XInteractionHandler xHandler
= UnoRuntime
.queryInterface(
108 XInteractionHandler
.class,
109 m_xCtx
.getServiceManager().createInstanceWithContext(
110 "com.sun.star.task.InteractionHandler", m_xCtx
) );
112 // create a new request and execute it
113 InteractionRequest aRequest
= new InteractionRequest( aError
);
114 xHandler
.handle( aRequest
);
116 catch( com
.sun
.star
.uno
.Exception e
)
118 System
.out
.println(e
);
123 /* ------------------------------------------------------------------ */
124 // XUpdateListener overridables
125 /* ------------------------------------------------------------------ */
126 public boolean approveUpdate( EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
128 boolean bApproved
= true;
131 // the control model which fired the event
132 XPropertySet xSourceProps
= UNO
.queryPropertySet( aEvent
.Source
);
134 String sNewText
= (String
)xSourceProps
.getPropertyValue( "Text" );
135 if ( 0 == sNewText
.length() )
137 // say that the value is invalid
138 showInvalidValueMessage( );
141 // reset the control value
142 // for this, we take the current value from the row set field the control
143 // is bound to, and forward it to the control model
144 XColumn xBoundColumn
= UNO
.queryColumn( xSourceProps
.getPropertyValue( "BoundField" ) );
145 if ( null != xBoundColumn
)
147 xSourceProps
.setPropertyValue( "Text", xBoundColumn
.getString() );
151 catch( com
.sun
.star
.uno
.Exception e
)
153 System
.out
.println(e
);
159 /* ------------------------------------------------------------------ */
160 public void updated( EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
164 /* ------------------------------------------------------------------ */
165 // XEventListener overridables
166 /* ------------------------------------------------------------------ */
167 public void disposing( EventObject aEvent
)