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