1 /*************************************************************************
3 * $RCSfile: ControlLock.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:28:13 $
9 * The Contents of this file are made available subject to the terms of
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
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 import com
.sun
.star
.uno
.*;
42 import com
.sun
.star
.lang
.*;
43 import com
.sun
.star
.container
.*;
44 import com
.sun
.star
.beans
.*;
45 import com
.sun
.star
.form
.*;
46 import com
.sun
.star
.util
.*;
47 import com
.sun
.star
.sdbc
.*;
50 /**************************************************************************/
51 /** A helper class for recursively locking control models which are bound
55 class LockControlModels
extends ComponentTreeTraversal
57 private String m_sDataField
;
58 private Boolean m_aLockIt
;
59 private int m_nLevel
; // nesting level relative to the form we started with
61 /* ------------------------------------------------------------------ */
62 public LockControlModels( String sDataField
, boolean bLockIt
)
64 m_sDataField
= sDataField
;
65 m_aLockIt
= new Boolean( bLockIt
);
69 /* ------------------------------------------------------------------ */
70 protected boolean shouldStepInto( XIndexContainer xContainer
) throws com
.sun
.star
.uno
.Exception
72 if ( !super.shouldStepInto( xContainer
) )
73 return false; // don't try to be more clever than our base class
75 XForm xForm
= (XForm
)UnoRuntime
.queryInterface( XForm
.class, xContainer
);
76 if ( ( null != xForm
) && ( m_nLevel
> 1 ) )
77 // don't step into sub forms - we only handle the form we were originally
84 /* ------------------------------------------------------------------ */
85 public void handle( Object aFormComponent
) throws com
.sun
.star
.uno
.Exception
87 // entering this nesting level
90 // check if the component has a DataField property
91 XPropertySet xCompProps
= UNO
.queryPropertySet( aFormComponent
);
92 XPropertySetInfo xPSI
= null;
93 if ( null != xCompProps
)
94 xPSI
= xCompProps
.getPropertySetInfo();
96 if ( ( null != xPSI
) && xPSI
.hasPropertyByName( "DataField" ) )
97 { // indeed it has ....
98 String sDataField
= (String
)xCompProps
.getPropertyValue( "DataField" );
99 if ( sDataField
.equals( m_sDataField
) )
100 { // we found a control model which is bound to what we're looking for
101 xCompProps
.setPropertyValue( "ReadOnly", m_aLockIt
);
105 // allow the super class to step down, if possible
106 super.handle( aFormComponent
);
108 // leaving this nesting level
113 /**************************************************************************/
114 /** a class which automatically handles control locking.
115 <p>The class has to be bound to a form. Upon every movement of the form,
116 all controls which are bound to a (to be specified) field are locked
117 on existing and unlocked on new records.</p>
119 class ControlLock
implements XRowSetListener
121 private XPropertySet m_xForm
;
122 private String m_sDataField
;
123 private boolean m_bLockingEnabled
;
124 private boolean m_bPreviousRoundLock
;
126 /* ------------------------------------------------------------------ */
127 ControlLock( XPropertySet xForm
, String sBoundDataField
)
130 m_sDataField
= sBoundDataField
;
131 m_bLockingEnabled
= false;
132 m_bPreviousRoundLock
= false;
135 /* ------------------------------------------------------------------ */
136 /** updates the locks on the affected controls
138 protected void updateLocks( )
142 // first determine if we need to lock
143 Boolean aIsNewRecord
= (Boolean
)m_xForm
.getPropertyValue( "IsNew" );
145 boolean bNeedLock
= m_bLockingEnabled
&& !aIsNewRecord
.booleanValue();
147 if ( m_bPreviousRoundLock
!= bNeedLock
)
149 LockControlModels aLocker
= new LockControlModels( m_sDataField
, bNeedLock
);
150 aLocker
.handle( m_xForm
);
151 m_bPreviousRoundLock
= bNeedLock
;
154 // please note that we choose the expensive way here: We always loop through
155 // _all_ control models belonging to the form. This clearly slows down the
157 // A better solution would be to cache the affected control models. Then we
158 // could either rely on the fact that the model hierarchy is static, or we
159 // could add ourself as container listener to the form.
161 catch(com
.sun
.star
.uno
.Exception e
)
163 System
.out
.println(e
);
168 /* ------------------------------------------------------------------ */
169 /** enables the locking in general
170 <p>If the control models are really locked depends on the current
171 record of the form: on the insert row, controls are never locked.</p>
173 public void enableLock( boolean bLock
)
175 // remember this new setting
176 m_bLockingEnabled
= bLock
;
178 // add or remove ourself as listener to get notified of cursor moves
179 XRowSet xRowSet
= (XRowSet
)UnoRuntime
.queryInterface(
180 XRowSet
.class, m_xForm
);
181 if ( m_bLockingEnabled
)
183 xRowSet
.addRowSetListener( this );
187 xRowSet
.removeRowSetListener( this );
194 /* ==================================================================
196 ================================================================== */
198 /* ------------------------------------------------------------------ */
199 // XResetListener overridables
200 /* ------------------------------------------------------------------ */
201 public void cursorMoved( EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
206 /* ------------------------------------------------------------------ */
207 public void rowChanged( EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
212 /* ------------------------------------------------------------------ */
213 public void rowSetChanged( EventObject aEvent
) throws com
.sun
.star
.uno
.RuntimeException
218 /* ------------------------------------------------------------------ */
219 // XEventListener overridables
220 /* ------------------------------------------------------------------ */
221 public void disposing( EventObject aEvent
)