Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / ControlLock.java
blobd518a5140a145a1ad08be91cfeefbebdb779afc7
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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 import com.sun.star.uno.*;
36 import com.sun.star.lang.*;
37 import com.sun.star.container.*;
38 import com.sun.star.beans.*;
39 import com.sun.star.form.*;
40 import com.sun.star.sdbc.*;
43 /**************************************************************************/
44 /** A helper class for recursively locking control models which are bound
45 to a specific field
48 class LockControlModels extends ComponentTreeTraversal
50 private String m_sDataField;
51 private Boolean m_aLockIt;
52 private int m_nLevel; // nesting level relative to the form we started with
54 /* ------------------------------------------------------------------ */
55 public LockControlModels( String sDataField, boolean bLockIt )
57 m_sDataField = sDataField;
58 m_aLockIt = Boolean.valueOf( bLockIt );
59 m_nLevel = 0;
62 /* ------------------------------------------------------------------ */
63 @Override
64 protected boolean shouldStepInto( XIndexContainer xContainer )
66 if ( !super.shouldStepInto( xContainer ) )
67 return false; // don't try to be more clever than our base class
69 XForm xForm = UnoRuntime.queryInterface( XForm.class, xContainer );
70 if ( ( null != xForm ) && ( m_nLevel > 1 ) )
71 // don't step into sub forms - we only handle the form we were originally
72 // applied to
73 return false;
75 return true;
78 /* ------------------------------------------------------------------ */
79 @Override
80 public void handle( Object aFormComponent ) throws com.sun.star.uno.Exception
82 // entering this nesting level
83 ++m_nLevel;
85 // check if the component has a DataField property
86 XPropertySet xCompProps = UNO.queryPropertySet( aFormComponent );
87 XPropertySetInfo xPSI = null;
88 if ( null != xCompProps )
89 xPSI = xCompProps.getPropertySetInfo();
91 if ( ( null != xPSI ) && xPSI.hasPropertyByName( "DataField" ) )
92 { // indeed it has ....
93 String sDataField = (String)xCompProps.getPropertyValue( "DataField" );
94 if ( sDataField.equals( m_sDataField ) )
95 { // we found a control model which is bound to what we're looking for
96 xCompProps.setPropertyValue( "ReadOnly", m_aLockIt );
100 // allow the super class to step down, if possible
101 super.handle( aFormComponent );
103 // leaving this nesting level
104 --m_nLevel;
108 /**************************************************************************/
109 /** a class which automatically handles control locking.
110 <p>The class has to be bound to a form. Upon every movement of the form,
111 all controls which are bound to a (to be specified) field are locked
112 on existing and unlocked on new records.</p>
114 class ControlLock implements XRowSetListener
116 private XPropertySet m_xForm;
117 private String m_sDataField;
118 private boolean m_bLockingEnabled;
119 private boolean m_bPreviousRoundLock;
121 /* ------------------------------------------------------------------ */
122 ControlLock( XPropertySet xForm, String sBoundDataField )
124 m_xForm = xForm;
125 m_sDataField = sBoundDataField;
126 m_bLockingEnabled = false;
127 m_bPreviousRoundLock = false;
130 /* ------------------------------------------------------------------ */
131 /** updates the locks on the affected controls
133 private void updateLocks( )
137 // first determine if we need to lock
138 Boolean aIsNewRecord = (Boolean)m_xForm.getPropertyValue( "IsNew" );
140 boolean bNeedLock = m_bLockingEnabled && !aIsNewRecord.booleanValue();
142 if ( m_bPreviousRoundLock != bNeedLock )
144 LockControlModels aLocker = new LockControlModels( m_sDataField, bNeedLock );
145 aLocker.handle( m_xForm );
146 m_bPreviousRoundLock = bNeedLock;
149 // please note that we choose the expensive way here: We always loop through
150 // _all_ control models belonging to the form. This clearly slows down the
151 // whole process.
152 // A better solution would be to cache the affected control models. Then we
153 // could either rely on the fact that the model hierarchy is static, or we
154 // could add ourself as container listener to the form.
156 catch(com.sun.star.uno.Exception e)
158 System.out.println(e);
159 e.printStackTrace();
163 /* ------------------------------------------------------------------ */
164 /** enables the locking in general
165 <p>If the control models are really locked depends on the current
166 record of the form: on the insert row, controls are never locked.</p>
168 public void enableLock( boolean bLock )
170 // remember this new setting
171 m_bLockingEnabled = bLock;
173 // add or remove ourself as listener to get notified of cursor moves
174 XRowSet xRowSet = UnoRuntime.queryInterface(
175 XRowSet.class, m_xForm );
176 if ( m_bLockingEnabled )
178 xRowSet.addRowSetListener( this );
180 else
182 xRowSet.removeRowSetListener( this );
185 // update the locks
186 updateLocks();
189 /* ==================================================================
190 = UNO callbacks
191 ================================================================== */
193 /* ------------------------------------------------------------------ */
194 // XResetListener overridables
195 /* ------------------------------------------------------------------ */
196 public void cursorMoved( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
198 updateLocks( );
201 /* ------------------------------------------------------------------ */
202 public void rowChanged( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
204 // not interested in
207 /* ------------------------------------------------------------------ */
208 public void rowSetChanged( EventObject aEvent ) throws com.sun.star.uno.RuntimeException
210 // not interested in
213 /* ------------------------------------------------------------------ */
214 // XEventListener overridables
215 /* ------------------------------------------------------------------ */
216 public void disposing( EventObject aEvent )
218 // not interested in