1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 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. 20 * 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. 32 * 33 *************************************************************************/ 34 // java base stuff 35 import com.sun.star.awt.ActionEvent; 36 import com.sun.star.awt.XActionListener; 37 import com.sun.star.awt.XButton; 38 import com.sun.star.beans.XPropertySet; 39 import com.sun.star.form.runtime.FormOperations; 40 import com.sun.star.form.runtime.XFeatureInvalidation; 41 import com.sun.star.form.runtime.XFormOperations; 42 import com.sun.star.lang.EventObject; 43 import com.sun.star.uno.UnoRuntime; 44 import com.sun.star.uno.XComponentContext; 45 import java.util.Vector; 46 47 48 /**************************************************************************/ 49 /** a helper class for operating the buttons 50 */ 51 public class ButtonOperator implements XActionListener, XFeatureInvalidation 52 { 53 private XComponentContext m_componentContext; 54 private DocumentHelper m_aDocument; 55 private XPropertySet m_form; 56 private XFormOperations m_formOperations; 57 58 private Vector m_aButtons; 59 60 /* ------------------------------------------------------------------ */ 61 /** ctor 62 */ 63 public ButtonOperator( XComponentContext xCtx, DocumentHelper aDocument, XPropertySet _form ) 64 { 65 m_componentContext = xCtx; 66 m_aDocument = aDocument; 67 m_form = _form; 68 m_aButtons = new Vector(); 69 } 70 71 /* ------------------------------------------------------------------ */ 72 private short getAssociatedFormFeature( XPropertySet _buttonModel ) 73 { 74 short formFeature = -1; 75 try 76 { 77 formFeature = Short.valueOf( (String)_buttonModel.getPropertyValue( "Tag" ) ); 78 } 79 catch( com.sun.star.uno.Exception e ) 80 { 81 } 82 return formFeature; 83 } 84 85 /* ------------------------------------------------------------------ */ 86 /** get's the button which we operate and which is responsible for a given URL 87 */ 88 private XPropertySet getButton( short _formFeature ) 89 { 90 for ( int i=0; i < m_aButtons.size(); ++i ) 91 { 92 XPropertySet button = (XPropertySet)m_aButtons.elementAt( i ); 93 if ( _formFeature == getAssociatedFormFeature( button ) ) 94 return button; 95 } 96 return null; 97 } 98 99 /* ------------------------------------------------------------------ */ 100 /** announces a button which the operator should be responsible for 101 */ 102 private int getButtonIndex( XPropertySet xButton ) 103 { 104 int nPos = -1; 105 for ( int i=0; ( i < m_aButtons.size() ) && ( -1 == nPos ); ++i ) 106 { 107 if ( xButton.equals( m_aButtons.elementAt( i ) ) ) 108 nPos = i; 109 } 110 return nPos; 111 } 112 113 /* ------------------------------------------------------------------ */ 114 /** announces a button which the operator should be responsible for 115 */ 116 public void addButton( XPropertySet _buttonModel, short _formFeature ) throws java.lang.Exception 117 { 118 // the current view to the document 119 DocumentViewHelper aCurrentView = m_aDocument.getCurrentView(); 120 121 // add a listener so we get noticed if the user presses the button 122 XButton xButtonControl = (XButton)UnoRuntime.queryInterface( XButton.class, 123 aCurrentView.getFormControl( _buttonModel ) ); 124 xButtonControl.addActionListener( this ); 125 126 _buttonModel.setPropertyValue( "Tag", String.valueOf( _formFeature ) ); 127 128 // remember the button 129 m_aButtons.add( _buttonModel ); 130 } 131 132 /* ------------------------------------------------------------------ */ 133 public void revokeButton( XPropertySet xButtonModel ) 134 { 135 int nPos = getButtonIndex( xButtonModel ); 136 if ( -1 < nPos ) 137 { 138 m_aButtons.remove( nPos ); 139 } 140 } 141 142 /* ================================================================== 143 = XActionListener 144 ================================================================== */ 145 /* ------------------------------------------------------------------ */ 146 /* called when a button has been pressed 147 */ 148 public void actionPerformed( ActionEvent aEvent ) throws com.sun.star.uno.RuntimeException 149 { 150 // get the model's name 151 XPropertySet buttonModel = (XPropertySet)FLTools.getModel( aEvent.Source, XPropertySet.class ); 152 try 153 { 154 short formFeature = getAssociatedFormFeature( buttonModel ); 155 if ( formFeature != -1 ) 156 m_formOperations.execute( formFeature ); 157 } 158 catch( final com.sun.star.uno.Exception e ) 159 { 160 } 161 } 162 163 /* ------------------------------------------------------------------ */ 164 /* (to be) called when the form layer has been switched to alive mode 165 * @todo 166 * register as listener somewhere ... 167 */ 168 public void onFormsAlive() 169 { 170 try 171 { 172 m_formOperations = FormOperations.createWithFormController( 173 m_componentContext, m_aDocument.getCurrentView().getFormController( m_form ) ); 174 m_formOperations.setFeatureInvalidation( this ); 175 invalidateAllFeatures(); 176 } 177 catch( final com.sun.star.uno.Exception e ) 178 { 179 } 180 } 181 182 /* ================================================================== 183 = XEventListener 184 ================================================================== */ 185 public void disposing( EventObject aEvent ) 186 { 187 // not interested in 188 } 189 190 /* ================================================================== 191 = XFeatureInvalidation 192 ================================================================== */ 193 private void updateButtonState( XPropertySet _buttonModel, short _formFeature ) 194 { 195 try 196 { 197 _buttonModel.setPropertyValue( "Enabled", m_formOperations.isEnabled( _formFeature ) ); 198 } 199 catch( com.sun.star.uno.Exception e ) 200 { 201 } 202 } 203 204 public void invalidateFeatures( short[] _features ) throws com.sun.star.uno.RuntimeException 205 { 206 for ( int i=0; i<_features.length; ++i ) 207 { 208 XPropertySet buttonModel = getButton( _features[i] ); 209 if ( buttonModel != null ) 210 updateButtonState( buttonModel, _features[i] ); 211 } 212 } 213 214 public void invalidateAllFeatures() throws com.sun.star.uno.RuntimeException 215 { 216 for ( int i=0; i < m_aButtons.size(); ++i ) 217 { 218 XPropertySet buttonModel = (XPropertySet)m_aButtons.elementAt( i ); 219 updateButtonState( buttonModel, getAssociatedFormFeature( buttonModel ) ); 220 } 221 } 222 }; 223 224