merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Forms / ButtonOperator.java
blob07aea599b24dd8606c633d11c1fc454ef18db5d5
1 /*************************************************************************
3 * $RCSfile: ButtonOperator.java,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:27:59 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
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
17 * are met:
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 *************************************************************************/
40 package org.openoffice.sdk.forms;
42 // java base stuff
43 import com.sun.star.awt.ActionEvent;
44 import com.sun.star.awt.XActionListener;
45 import com.sun.star.awt.XButton;
46 import com.sun.star.beans.XPropertySet;
47 import com.sun.star.form.runtime.FormOperations;
48 import com.sun.star.form.runtime.XFeatureInvalidation;
49 import com.sun.star.form.runtime.XFormOperations;
50 import com.sun.star.lang.EventObject;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XComponentContext;
53 import java.util.Vector;
56 /**************************************************************************/
57 /** a helper class for operating the buttons
59 public class ButtonOperator implements XActionListener, XFeatureInvalidation
61 private XComponentContext m_componentContext;
62 private DocumentHelper m_aDocument;
63 private XPropertySet m_form;
64 private XFormOperations m_formOperations;
66 private Vector m_aButtons;
68 /* ------------------------------------------------------------------ */
69 /** ctor
71 public ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form )
73 m_componentContext = xCtx;
74 m_aDocument = aDocument;
75 m_form = _form;
76 m_aButtons = new Vector();
79 /* ------------------------------------------------------------------ */
80 private short getAssociatedFormFeature( XPropertySet _buttonModel )
82 short formFeature = -1;
83 try
85 formFeature = Short.valueOf( (String)_buttonModel.getPropertyValue( "Tag" ) );
87 catch( com.sun.star.uno.Exception e )
90 return formFeature;
93 /* ------------------------------------------------------------------ */
94 /** get's the button which we operate and which is responsible for a given URL
96 private XPropertySet getButton( short _formFeature )
98 for ( int i=0; i < m_aButtons.size(); ++i )
100 XPropertySet button = (XPropertySet)m_aButtons.elementAt( i );
101 if ( _formFeature == getAssociatedFormFeature( button ) )
102 return button;
104 return null;
107 /* ------------------------------------------------------------------ */
108 /** announces a button which the operator should be responsible for
110 private int getButtonIndex( XPropertySet xButton )
112 int nPos = -1;
113 for ( int i=0; ( i < m_aButtons.size() ) && ( -1 == nPos ); ++i )
115 if ( xButton.equals( m_aButtons.elementAt( i ) ) )
116 nPos = i;
118 return nPos;
121 /* ------------------------------------------------------------------ */
122 /** announces a button which the operator should be responsible for
124 public void addButton( XPropertySet _buttonModel, short _formFeature ) throws java.lang.Exception
126 // the current view to the document
127 DocumentViewHelper aCurrentView = m_aDocument.getCurrentView();
129 // add a listener so we get noticed if the user presses the button
130 XButton xButtonControl = (XButton)UnoRuntime.queryInterface( XButton.class,
131 aCurrentView.getFormControl( _buttonModel ) );
132 xButtonControl.addActionListener( this );
134 _buttonModel.setPropertyValue( "Tag", String.valueOf( _formFeature ) );
136 // remember the button
137 m_aButtons.add( _buttonModel );
140 /* ------------------------------------------------------------------ */
141 public void revokeButton( XPropertySet xButtonModel )
143 int nPos = getButtonIndex( xButtonModel );
144 if ( -1 < nPos )
146 m_aButtons.remove( nPos );
150 /* ==================================================================
151 = XActionListener
152 ================================================================== */
153 /* ------------------------------------------------------------------ */
154 /* called when a button has been pressed
156 public void actionPerformed( ActionEvent aEvent ) throws com.sun.star.uno.RuntimeException
158 // get the model's name
159 XPropertySet buttonModel = (XPropertySet)FLTools.getModel( aEvent.Source, XPropertySet.class );
162 short formFeature = getAssociatedFormFeature( buttonModel );
163 if ( formFeature != -1 )
164 m_formOperations.execute( formFeature );
166 catch( final com.sun.star.uno.Exception e )
171 /* ------------------------------------------------------------------ */
172 /* (to be) called when the form layer has been switched to alive mode
173 * @todo
174 * register as listener somewhere ...
176 public void onFormsAlive()
180 m_formOperations = FormOperations.createWithFormController(
181 m_componentContext, m_aDocument.getCurrentView().getFormController( m_form ) );
182 m_formOperations.setFeatureInvalidation( this );
183 invalidateAllFeatures();
185 catch( final com.sun.star.uno.Exception e )
190 /* ==================================================================
191 = XEventListener
192 ================================================================== */
193 public void disposing( EventObject aEvent )
195 // not interested in
198 /* ==================================================================
199 = XFeatureInvalidation
200 ================================================================== */
201 private void updateButtonState( XPropertySet _buttonModel, short _formFeature )
205 _buttonModel.setPropertyValue( "Enabled", m_formOperations.isEnabled( _formFeature ) );
207 catch( com.sun.star.uno.Exception e )
212 public void invalidateFeatures( short[] _features ) throws com.sun.star.uno.RuntimeException
214 for ( int i=0; i<_features.length; ++i )
216 XPropertySet buttonModel = getButton( _features[i] );
217 if ( buttonModel != null )
218 updateButtonState( buttonModel, _features[i] );
222 public void invalidateAllFeatures() throws com.sun.star.uno.RuntimeException
224 for ( int i=0; i < m_aButtons.size(); ++i )
226 XPropertySet buttonModel = (XPropertySet)m_aButtons.elementAt( i );
227 updateButtonState( buttonModel, getAssociatedFormFeature( buttonModel ) );