Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / ButtonOperator.java
blob55e6429b90f3f2951b0c6ccc52e8ca6eeab8f9d8
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 *************************************************************************/
34 // java base stuff
35 import java.util.ArrayList;
37 import com.sun.star.awt.ActionEvent;
38 import com.sun.star.awt.XActionListener;
39 import com.sun.star.awt.XButton;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.form.runtime.FormOperations;
42 import com.sun.star.form.runtime.XFeatureInvalidation;
43 import com.sun.star.form.runtime.XFormOperations;
44 import com.sun.star.lang.EventObject;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XComponentContext;
49 /**************************************************************************/
50 /** a helper class for operating the buttons
52 public class ButtonOperator implements XActionListener, XFeatureInvalidation
54 private XComponentContext m_componentContext;
55 private DocumentHelper m_aDocument;
56 private XPropertySet m_form;
57 private XFormOperations m_formOperations;
59 private ArrayList<XPropertySet> m_aButtons;
61 /* ------------------------------------------------------------------ */
62 /** ctor
64 public ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form )
66 m_componentContext = xCtx;
67 m_aDocument = aDocument;
68 m_form = _form;
69 m_aButtons = new ArrayList<XPropertySet>();
72 /* ------------------------------------------------------------------ */
73 private short getAssociatedFormFeature( XPropertySet _buttonModel )
75 short formFeature = -1;
76 try
78 formFeature = Short.valueOf( (String)_buttonModel.getPropertyValue( "Tag" ) );
80 catch( com.sun.star.uno.Exception e )
83 return formFeature;
86 /* ------------------------------------------------------------------ */
87 /** get's the button which we operate and which is responsible for a given URL
89 private XPropertySet getButton( short _formFeature )
91 for ( int i=0; i < m_aButtons.size(); ++i )
93 XPropertySet button = m_aButtons.get( i );
94 if ( _formFeature == getAssociatedFormFeature( button ) )
95 return button;
97 return null;
100 /* ------------------------------------------------------------------ */
101 /** announces a button which the operator should be responsible for
103 private int getButtonIndex( XPropertySet xButton )
105 int nPos = -1;
106 for ( int i=0; ( i < m_aButtons.size() ) && ( -1 == nPos ); ++i )
108 if ( xButton.equals( m_aButtons.get( i ) ) )
109 nPos = i;
111 return nPos;
114 /* ------------------------------------------------------------------ */
115 /** announces a button which the operator should be responsible for
117 public void addButton( XPropertySet _buttonModel, short _formFeature ) throws java.lang.Exception
119 // the current view to the document
120 DocumentViewHelper aCurrentView = m_aDocument.getCurrentView();
122 // add a listener so we get noticed if the user presses the button
123 XButton xButtonControl = UnoRuntime.queryInterface( XButton.class,
124 aCurrentView.getFormControl( _buttonModel ) );
125 xButtonControl.addActionListener( this );
127 _buttonModel.setPropertyValue( "Tag", String.valueOf( _formFeature ) );
129 // remember the button
130 m_aButtons.add( _buttonModel );
133 /* ------------------------------------------------------------------ */
134 public void revokeButton( XPropertySet xButtonModel )
136 int nPos = getButtonIndex( xButtonModel );
137 if ( -1 < nPos )
139 m_aButtons.remove( nPos );
143 /* ==================================================================
144 = XActionListener
145 ================================================================== */
146 /* ------------------------------------------------------------------ */
147 /* called when a button has been pressed
149 public void actionPerformed( ActionEvent aEvent ) throws com.sun.star.uno.RuntimeException
151 // get the model's name
152 XPropertySet buttonModel = FLTools.getModel( aEvent.Source, XPropertySet.class );
155 short formFeature = getAssociatedFormFeature( buttonModel );
156 if ( formFeature != -1 )
157 m_formOperations.execute( formFeature );
159 catch( final com.sun.star.uno.Exception e )
164 /* ------------------------------------------------------------------ */
165 /* (to be) called when the form layer has been switched to alive mode
166 * @todo
167 * register as listener somewhere ...
169 public void onFormsAlive()
171 m_formOperations = FormOperations.createWithFormController(
172 m_componentContext, m_aDocument.getCurrentView().getFormController( m_form ) );
173 m_formOperations.setFeatureInvalidation( this );
174 invalidateAllFeatures();
177 /* ==================================================================
178 = XEventListener
179 ================================================================== */
180 public void disposing( EventObject aEvent )
182 // not interested in
185 /* ==================================================================
186 = XFeatureInvalidation
187 ================================================================== */
188 private void updateButtonState( XPropertySet _buttonModel, short _formFeature )
192 _buttonModel.setPropertyValue( "Enabled", m_formOperations.isEnabled( _formFeature ) );
194 catch( com.sun.star.uno.Exception e )
199 public void invalidateFeatures( short[] _features ) throws com.sun.star.uno.RuntimeException
201 for ( int i=0; i<_features.length; ++i )
203 XPropertySet buttonModel = getButton( _features[i] );
204 if ( buttonModel != null )
205 updateButtonState( buttonModel, _features[i] );
209 public void invalidateAllFeatures() throws com.sun.star.uno.RuntimeException
211 for ( XPropertySet buttonModel : m_aButtons )
213 updateButtonState( buttonModel, getAssociatedFormFeature( buttonModel ) );