1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir // java base stuff
35*cdf0e10cSrcweir import com.sun.star.awt.ActionEvent;
36*cdf0e10cSrcweir import com.sun.star.awt.XActionListener;
37*cdf0e10cSrcweir import com.sun.star.awt.XButton;
38*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
39*cdf0e10cSrcweir import com.sun.star.form.runtime.FormOperations;
40*cdf0e10cSrcweir import com.sun.star.form.runtime.XFeatureInvalidation;
41*cdf0e10cSrcweir import com.sun.star.form.runtime.XFormOperations;
42*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
45*cdf0e10cSrcweir import java.util.Vector;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /**************************************************************************/
49*cdf0e10cSrcweir /** a helper class for operating the buttons
50*cdf0e10cSrcweir */
51*cdf0e10cSrcweir public class ButtonOperator implements XActionListener, XFeatureInvalidation
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	private XComponentContext	m_componentContext;
54*cdf0e10cSrcweir 	private DocumentHelper		m_aDocument;
55*cdf0e10cSrcweir     private XPropertySet        m_form;
56*cdf0e10cSrcweir     private XFormOperations     m_formOperations;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	private Vector				m_aButtons;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
61*cdf0e10cSrcweir 	/** ctor
62*cdf0e10cSrcweir 	*/
63*cdf0e10cSrcweir 	public ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form )
64*cdf0e10cSrcweir 	{
65*cdf0e10cSrcweir 		m_componentContext = xCtx;
66*cdf0e10cSrcweir 		m_aDocument = aDocument;
67*cdf0e10cSrcweir         m_form = _form;
68*cdf0e10cSrcweir 		m_aButtons = new Vector();
69*cdf0e10cSrcweir 	}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
72*cdf0e10cSrcweir     private short getAssociatedFormFeature( XPropertySet _buttonModel )
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		short formFeature = -1;
75*cdf0e10cSrcweir 		try
76*cdf0e10cSrcweir 		{
77*cdf0e10cSrcweir             formFeature = Short.valueOf( (String)_buttonModel.getPropertyValue( "Tag" ) );
78*cdf0e10cSrcweir 		}
79*cdf0e10cSrcweir 		catch( com.sun.star.uno.Exception e )
80*cdf0e10cSrcweir 		{
81*cdf0e10cSrcweir 		}
82*cdf0e10cSrcweir 		return formFeature;
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
86*cdf0e10cSrcweir 	/** get's the button which we operate and which is responsible for a given URL
87*cdf0e10cSrcweir 	*/
88*cdf0e10cSrcweir 	private XPropertySet getButton( short _formFeature )
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		for ( int i=0; i < m_aButtons.size(); ++i )
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir             XPropertySet button = (XPropertySet)m_aButtons.elementAt( i );
93*cdf0e10cSrcweir 			if ( _formFeature == getAssociatedFormFeature( button ) )
94*cdf0e10cSrcweir 				return button;
95*cdf0e10cSrcweir 		}
96*cdf0e10cSrcweir 		return null;
97*cdf0e10cSrcweir 	}
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
100*cdf0e10cSrcweir 	/** announces a button which the operator should be responsible for
101*cdf0e10cSrcweir 	*/
102*cdf0e10cSrcweir 	private int getButtonIndex( XPropertySet xButton )
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 		int nPos = -1;
105*cdf0e10cSrcweir 		for ( int i=0; ( i < m_aButtons.size() ) && ( -1 == nPos ); ++i )
106*cdf0e10cSrcweir 		{
107*cdf0e10cSrcweir 			if ( xButton.equals( m_aButtons.elementAt( i ) ) )
108*cdf0e10cSrcweir 				nPos = i;
109*cdf0e10cSrcweir 		}
110*cdf0e10cSrcweir 		return nPos;
111*cdf0e10cSrcweir 	}
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
114*cdf0e10cSrcweir 	/** announces a button which the operator should be responsible for
115*cdf0e10cSrcweir 	*/
116*cdf0e10cSrcweir 	public void addButton( XPropertySet _buttonModel, short _formFeature  ) throws java.lang.Exception
117*cdf0e10cSrcweir 	{
118*cdf0e10cSrcweir 		// the current view to the document
119*cdf0e10cSrcweir 		DocumentViewHelper aCurrentView = m_aDocument.getCurrentView();
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		// add a listener so we get noticed if the user presses the button
122*cdf0e10cSrcweir 		XButton xButtonControl = (XButton)UnoRuntime.queryInterface( XButton.class,
123*cdf0e10cSrcweir 			aCurrentView.getFormControl( _buttonModel ) );
124*cdf0e10cSrcweir 		xButtonControl.addActionListener( this );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         _buttonModel.setPropertyValue( "Tag", String.valueOf( _formFeature ) );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 		// remember the button
129*cdf0e10cSrcweir 		m_aButtons.add( _buttonModel );
130*cdf0e10cSrcweir 	}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
133*cdf0e10cSrcweir 	public void revokeButton( XPropertySet xButtonModel )
134*cdf0e10cSrcweir 	{
135*cdf0e10cSrcweir 		int nPos = getButtonIndex( xButtonModel );
136*cdf0e10cSrcweir 		if ( -1 < nPos )
137*cdf0e10cSrcweir 		{
138*cdf0e10cSrcweir 			m_aButtons.remove( nPos );
139*cdf0e10cSrcweir 		}
140*cdf0e10cSrcweir 	}
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	/* ==================================================================
143*cdf0e10cSrcweir 	   = XActionListener
144*cdf0e10cSrcweir 	   ================================================================== */
145*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
146*cdf0e10cSrcweir 	/* called when a button has been pressed
147*cdf0e10cSrcweir 	*/
148*cdf0e10cSrcweir     public void actionPerformed( ActionEvent aEvent ) throws com.sun.star.uno.RuntimeException
149*cdf0e10cSrcweir 	{
150*cdf0e10cSrcweir 		// get the model's name
151*cdf0e10cSrcweir         XPropertySet buttonModel = (XPropertySet)FLTools.getModel( aEvent.Source, XPropertySet.class );
152*cdf0e10cSrcweir         try
153*cdf0e10cSrcweir         {
154*cdf0e10cSrcweir             short formFeature = getAssociatedFormFeature( buttonModel );
155*cdf0e10cSrcweir             if ( formFeature != -1 )
156*cdf0e10cSrcweir                 m_formOperations.execute( formFeature );
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir         catch( final com.sun.star.uno.Exception e )
159*cdf0e10cSrcweir         {
160*cdf0e10cSrcweir         }
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	/* ------------------------------------------------------------------ */
164*cdf0e10cSrcweir 	/* (to be) called when the form layer has been switched to alive mode
165*cdf0e10cSrcweir      * @todo
166*cdf0e10cSrcweir      *  register as listener somewhere ...
167*cdf0e10cSrcweir 	*/
168*cdf0e10cSrcweir     public void onFormsAlive()
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         try
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir             m_formOperations = FormOperations.createWithFormController(
173*cdf0e10cSrcweir                 m_componentContext, m_aDocument.getCurrentView().getFormController( m_form ) );
174*cdf0e10cSrcweir             m_formOperations.setFeatureInvalidation( this );
175*cdf0e10cSrcweir             invalidateAllFeatures();
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir         catch( final com.sun.star.uno.Exception e )
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	/* ==================================================================
183*cdf0e10cSrcweir 	   = XEventListener
184*cdf0e10cSrcweir 	   ================================================================== */
185*cdf0e10cSrcweir 	public void disposing( EventObject aEvent )
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		// not interested in
188*cdf0e10cSrcweir 	}
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	/* ==================================================================
191*cdf0e10cSrcweir 	   = XFeatureInvalidation
192*cdf0e10cSrcweir 	   ================================================================== */
193*cdf0e10cSrcweir     private void updateButtonState( XPropertySet _buttonModel, short _formFeature )
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         try
196*cdf0e10cSrcweir         {
197*cdf0e10cSrcweir             _buttonModel.setPropertyValue( "Enabled", m_formOperations.isEnabled( _formFeature ) );
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e )
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     public void invalidateFeatures( short[] _features ) throws com.sun.star.uno.RuntimeException
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         for ( int i=0; i<_features.length; ++i )
207*cdf0e10cSrcweir         {
208*cdf0e10cSrcweir             XPropertySet buttonModel = getButton( _features[i] );
209*cdf0e10cSrcweir             if ( buttonModel != null )
210*cdf0e10cSrcweir                 updateButtonState( buttonModel, _features[i] );
211*cdf0e10cSrcweir         }
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     public void invalidateAllFeatures() throws com.sun.star.uno.RuntimeException
215*cdf0e10cSrcweir     {
216*cdf0e10cSrcweir 		for ( int i=0; i < m_aButtons.size(); ++i )
217*cdf0e10cSrcweir 		{
218*cdf0e10cSrcweir             XPropertySet buttonModel = (XPropertySet)m_aButtons.elementAt( i );
219*cdf0e10cSrcweir             updateButtonState( buttonModel, getAssociatedFormFeature( buttonModel ) );
220*cdf0e10cSrcweir 		}
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir };
223*cdf0e10cSrcweir 
224