1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir // java base stuff
24cdf0e10cSrcweir import com.sun.star.awt.ActionEvent;
25cdf0e10cSrcweir import com.sun.star.awt.XActionListener;
26cdf0e10cSrcweir import com.sun.star.awt.XButton;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.form.runtime.FormOperations;
29cdf0e10cSrcweir import com.sun.star.form.runtime.XFeatureInvalidation;
30cdf0e10cSrcweir import com.sun.star.form.runtime.XFormOperations;
31cdf0e10cSrcweir import com.sun.star.lang.EventObject;
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
34cdf0e10cSrcweir import java.util.Vector;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /**************************************************************************/
38cdf0e10cSrcweir /** a helper class for operating the buttons
39cdf0e10cSrcweir */
40cdf0e10cSrcweir public class ButtonOperator implements XActionListener, XFeatureInvalidation
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 	private XComponentContext	m_componentContext;
43cdf0e10cSrcweir 	private DocumentHelper		m_aDocument;
44cdf0e10cSrcweir     private XPropertySet        m_form;
45cdf0e10cSrcweir     private XFormOperations     m_formOperations;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	private Vector				m_aButtons;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
50cdf0e10cSrcweir 	/** ctor
51cdf0e10cSrcweir 	*/
ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form )52cdf0e10cSrcweir 	public ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form )
53cdf0e10cSrcweir 	{
54cdf0e10cSrcweir 		m_componentContext = xCtx;
55cdf0e10cSrcweir 		m_aDocument = aDocument;
56cdf0e10cSrcweir         m_form = _form;
57cdf0e10cSrcweir 		m_aButtons = new Vector();
58cdf0e10cSrcweir 	}
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
getAssociatedFormFeature( XPropertySet _buttonModel )61cdf0e10cSrcweir     private short getAssociatedFormFeature( XPropertySet _buttonModel )
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		short formFeature = -1;
64cdf0e10cSrcweir 		try
65cdf0e10cSrcweir 		{
66cdf0e10cSrcweir             formFeature = Short.valueOf( (String)_buttonModel.getPropertyValue( "Tag" ) );
67cdf0e10cSrcweir 		}
68cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
69cdf0e10cSrcweir 		{
70cdf0e10cSrcweir 		}
71cdf0e10cSrcweir 		return formFeature;
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
75cdf0e10cSrcweir 	/** get's the button which we operate and which is responsible for a given URL
76cdf0e10cSrcweir 	*/
getButton( short _formFeature )77cdf0e10cSrcweir 	private XPropertySet getButton( short _formFeature )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		for ( int i=0; i < m_aButtons.size(); ++i )
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir             XPropertySet button = (XPropertySet)m_aButtons.elementAt( i );
82cdf0e10cSrcweir 			if ( _formFeature == getAssociatedFormFeature( button ) )
83cdf0e10cSrcweir 				return button;
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 		return null;
86cdf0e10cSrcweir 	}
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
89cdf0e10cSrcweir 	/** announces a button which the operator should be responsible for
90cdf0e10cSrcweir 	*/
getButtonIndex( XPropertySet xButton )91cdf0e10cSrcweir 	private int getButtonIndex( XPropertySet xButton )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		int nPos = -1;
94cdf0e10cSrcweir 		for ( int i=0; ( i < m_aButtons.size() ) && ( -1 == nPos ); ++i )
95cdf0e10cSrcweir 		{
96cdf0e10cSrcweir 			if ( xButton.equals( m_aButtons.elementAt( i ) ) )
97cdf0e10cSrcweir 				nPos = i;
98cdf0e10cSrcweir 		}
99cdf0e10cSrcweir 		return nPos;
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
103cdf0e10cSrcweir 	/** announces a button which the operator should be responsible for
104cdf0e10cSrcweir 	*/
addButton( XPropertySet _buttonModel, short _formFeature )105cdf0e10cSrcweir 	public void addButton( XPropertySet _buttonModel, short _formFeature  ) throws java.lang.Exception
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		// the current view to the document
108cdf0e10cSrcweir 		DocumentViewHelper aCurrentView = m_aDocument.getCurrentView();
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 		// add a listener so we get noticed if the user presses the button
111cdf0e10cSrcweir 		XButton xButtonControl = (XButton)UnoRuntime.queryInterface( XButton.class,
112cdf0e10cSrcweir 			aCurrentView.getFormControl( _buttonModel ) );
113cdf0e10cSrcweir 		xButtonControl.addActionListener( this );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         _buttonModel.setPropertyValue( "Tag", String.valueOf( _formFeature ) );
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		// remember the button
118cdf0e10cSrcweir 		m_aButtons.add( _buttonModel );
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
revokeButton( XPropertySet xButtonModel )122cdf0e10cSrcweir 	public void revokeButton( XPropertySet xButtonModel )
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		int nPos = getButtonIndex( xButtonModel );
125cdf0e10cSrcweir 		if ( -1 < nPos )
126cdf0e10cSrcweir 		{
127cdf0e10cSrcweir 			m_aButtons.remove( nPos );
128cdf0e10cSrcweir 		}
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	/* ==================================================================
132cdf0e10cSrcweir 	   = XActionListener
133cdf0e10cSrcweir 	   ================================================================== */
134cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
135cdf0e10cSrcweir 	/* called when a button has been pressed
136cdf0e10cSrcweir 	*/
actionPerformed( ActionEvent aEvent )137cdf0e10cSrcweir     public void actionPerformed( ActionEvent aEvent ) throws com.sun.star.uno.RuntimeException
138cdf0e10cSrcweir 	{
139cdf0e10cSrcweir 		// get the model's name
140cdf0e10cSrcweir         XPropertySet buttonModel = (XPropertySet)FLTools.getModel( aEvent.Source, XPropertySet.class );
141cdf0e10cSrcweir         try
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             short formFeature = getAssociatedFormFeature( buttonModel );
144cdf0e10cSrcweir             if ( formFeature != -1 )
145cdf0e10cSrcweir                 m_formOperations.execute( formFeature );
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir         catch( final com.sun.star.uno.Exception e )
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
153cdf0e10cSrcweir 	/* (to be) called when the form layer has been switched to alive mode
154cdf0e10cSrcweir      * @todo
155cdf0e10cSrcweir      *  register as listener somewhere ...
156cdf0e10cSrcweir 	*/
onFormsAlive()157cdf0e10cSrcweir     public void onFormsAlive()
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         try
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             m_formOperations = FormOperations.createWithFormController(
162cdf0e10cSrcweir                 m_componentContext, m_aDocument.getCurrentView().getFormController( m_form ) );
163cdf0e10cSrcweir             m_formOperations.setFeatureInvalidation( this );
164cdf0e10cSrcweir             invalidateAllFeatures();
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         catch( final com.sun.star.uno.Exception e )
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	/* ==================================================================
172cdf0e10cSrcweir 	   = XEventListener
173cdf0e10cSrcweir 	   ================================================================== */
disposing( EventObject aEvent )174cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
175cdf0e10cSrcweir 	{
176cdf0e10cSrcweir 		// not interested in
177cdf0e10cSrcweir 	}
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	/* ==================================================================
180cdf0e10cSrcweir 	   = XFeatureInvalidation
181cdf0e10cSrcweir 	   ================================================================== */
updateButtonState( XPropertySet _buttonModel, short _formFeature )182cdf0e10cSrcweir     private void updateButtonState( XPropertySet _buttonModel, short _formFeature )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         try
185cdf0e10cSrcweir         {
186cdf0e10cSrcweir             _buttonModel.setPropertyValue( "Enabled", m_formOperations.isEnabled( _formFeature ) );
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e )
189cdf0e10cSrcweir         {
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
invalidateFeatures( short[] _features )193cdf0e10cSrcweir     public void invalidateFeatures( short[] _features ) throws com.sun.star.uno.RuntimeException
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         for ( int i=0; i<_features.length; ++i )
196cdf0e10cSrcweir         {
197cdf0e10cSrcweir             XPropertySet buttonModel = getButton( _features[i] );
198cdf0e10cSrcweir             if ( buttonModel != null )
199cdf0e10cSrcweir                 updateButtonState( buttonModel, _features[i] );
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir 
invalidateAllFeatures()203cdf0e10cSrcweir     public void invalidateAllFeatures() throws com.sun.star.uno.RuntimeException
204cdf0e10cSrcweir     {
205cdf0e10cSrcweir 		for ( int i=0; i < m_aButtons.size(); ++i )
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir             XPropertySet buttonModel = (XPropertySet)m_aButtons.elementAt( i );
208cdf0e10cSrcweir             updateButtonState( buttonModel, getAssociatedFormFeature( buttonModel ) );
209cdf0e10cSrcweir 		}
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir };
212cdf0e10cSrcweir 
213