Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / ButtonOperator.java
blob890ea7251e70aca478a5cd28518274de9a099c45
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
35 // java base stuff
36 import java.util.ArrayList;
38 import com.sun.star.awt.ActionEvent;
39 import com.sun.star.awt.XActionListener;
40 import com.sun.star.awt.XButton;
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.form.runtime.FormOperations;
43 import com.sun.star.form.runtime.XFeatureInvalidation;
44 import com.sun.star.form.runtime.XFormOperations;
45 import com.sun.star.lang.EventObject;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XComponentContext;
50 /**************************************************************************/
51 /** a helper class for operating the buttons
53 public class ButtonOperator implements XActionListener, XFeatureInvalidation
55 private XComponentContext m_componentContext;
56 private DocumentHelper m_aDocument;
57 private XPropertySet m_form;
58 private XFormOperations m_formOperations;
60 private ArrayList<XPropertySet> m_aButtons;
62 /* ------------------------------------------------------------------ */
63 /** ctor
65 public ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form )
67 m_componentContext = xCtx;
68 m_aDocument = aDocument;
69 m_form = _form;
70 m_aButtons = new ArrayList<XPropertySet>();
73 /* ------------------------------------------------------------------ */
74 private short getAssociatedFormFeature( XPropertySet _buttonModel )
76 short formFeature = -1;
77 try
79 formFeature = Short.valueOf( (String)_buttonModel.getPropertyValue( "Tag" ) );
81 catch( com.sun.star.uno.Exception e )
84 return formFeature;
87 /* ------------------------------------------------------------------ */
88 /** gets the button which we operate and which is responsible for a given URL
90 private XPropertySet getButton( short _formFeature )
92 for ( int i=0; i < m_aButtons.size(); ++i )
94 XPropertySet button = m_aButtons.get( i );
95 if ( _formFeature == getAssociatedFormFeature( button ) )
96 return button;
98 return null;
101 /* ------------------------------------------------------------------ */
102 /** announces a button which the operator should be responsible for
104 private int getButtonIndex( XPropertySet xButton )
106 int nPos = -1;
107 for ( int i=0; ( i < m_aButtons.size() ) && ( -1 == nPos ); ++i )
109 if ( xButton.equals( m_aButtons.get( i ) ) )
110 nPos = i;
112 return nPos;
115 /* ------------------------------------------------------------------ */
116 /** announces a button which the operator should be responsible for
118 public void addButton( XPropertySet _buttonModel, short _formFeature ) throws java.lang.Exception
120 // the current view to the document
121 DocumentViewHelper aCurrentView = m_aDocument.getCurrentView();
123 // add a listener so we get noticed if the user presses the button
124 XButton xButtonControl = UnoRuntime.queryInterface( XButton.class,
125 aCurrentView.getFormControl( _buttonModel ) );
126 xButtonControl.addActionListener( this );
128 _buttonModel.setPropertyValue( "Tag", String.valueOf( _formFeature ) );
130 // remember the button
131 m_aButtons.add( _buttonModel );
134 /* ------------------------------------------------------------------ */
135 public void revokeButton( XPropertySet xButtonModel )
137 int nPos = getButtonIndex( xButtonModel );
138 if ( -1 < nPos )
140 m_aButtons.remove( nPos );
144 /* ==================================================================
145 = XActionListener
146 ================================================================== */
147 /* ------------------------------------------------------------------ */
148 /* called when a button has been pressed
150 public void actionPerformed( ActionEvent aEvent ) throws com.sun.star.uno.RuntimeException
152 // get the model's name
153 XPropertySet buttonModel = FLTools.getModel( aEvent.Source, XPropertySet.class );
156 short formFeature = getAssociatedFormFeature( buttonModel );
157 if ( formFeature != -1 )
158 m_formOperations.execute( formFeature );
160 catch( final com.sun.star.uno.Exception e )
165 /* ------------------------------------------------------------------ */
166 /* (to be) called when the form layer has been switched to alive mode
167 * @todo
168 * register as listener somewhere ...
170 public void onFormsAlive()
172 m_formOperations = FormOperations.createWithFormController(
173 m_componentContext, m_aDocument.getCurrentView().getFormController( m_form ) );
174 m_formOperations.setFeatureInvalidation( this );
175 invalidateAllFeatures();
178 /* ==================================================================
179 = XEventListener
180 ================================================================== */
181 public void disposing( EventObject aEvent )
183 // not interested in
186 /* ==================================================================
187 = XFeatureInvalidation
188 ================================================================== */
189 private void updateButtonState( XPropertySet _buttonModel, short _formFeature )
193 _buttonModel.setPropertyValue( "Enabled", m_formOperations.isEnabled( _formFeature ) );
195 catch( com.sun.star.uno.Exception e )
200 public void invalidateFeatures( short[] _features ) throws com.sun.star.uno.RuntimeException
202 for ( int i=0; i<_features.length; ++i )
204 XPropertySet buttonModel = getButton( _features[i] );
205 if ( buttonModel != null )
206 updateButtonState( buttonModel, _features[i] );
210 public void invalidateAllFeatures() throws com.sun.star.uno.RuntimeException
212 for ( XPropertySet buttonModel : m_aButtons )
214 updateButtonState( buttonModel, getAssociatedFormFeature( buttonModel ) );
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */