1*ae15d43aSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ae15d43aSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ae15d43aSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ae15d43aSAndrew Rist * distributed with this work for additional information 6*ae15d43aSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ae15d43aSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ae15d43aSAndrew Rist * "License"); you may not use this file except in compliance 9*ae15d43aSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*ae15d43aSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*ae15d43aSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ae15d43aSAndrew Rist * software distributed under the License is distributed on an 15*ae15d43aSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ae15d43aSAndrew Rist * KIND, either express or implied. See the License for the 17*ae15d43aSAndrew Rist * specific language governing permissions and limitations 18*ae15d43aSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*ae15d43aSAndrew Rist *************************************************************/ 21*ae15d43aSAndrew Rist 22*ae15d43aSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import com.sun.star.uno.*; 25cdf0e10cSrcweir import com.sun.star.util.*; 26cdf0e10cSrcweir import com.sun.star.lang.*; 27cdf0e10cSrcweir import com.sun.star.accessibility.*; 28cdf0e10cSrcweir import com.sun.star.container.*; 29cdf0e10cSrcweir import com.sun.star.beans.*; 30cdf0e10cSrcweir import com.sun.star.form.binding.*; 31cdf0e10cSrcweir import com.sun.star.form.validation.*; 32cdf0e10cSrcweir 33cdf0e10cSrcweir /** 34cdf0e10cSrcweir * 35cdf0e10cSrcweir * @author fs@openoffice.org 36cdf0e10cSrcweir */ 37cdf0e10cSrcweir public class SingleControlValidation implements XFormComponentValidityListener 38cdf0e10cSrcweir { 39cdf0e10cSrcweir private DocumentHelper m_document; /// our current test document 40cdf0e10cSrcweir private FormLayer m_formLayer; /// quick access to the form layer 41cdf0e10cSrcweir private XMultiServiceFactory m_orb; /// our service factory 42cdf0e10cSrcweir 43cdf0e10cSrcweir private XPropertySet m_inputField; 44cdf0e10cSrcweir private XPropertySet m_inputLabel; 45cdf0e10cSrcweir private XPropertySet m_statusField; 46cdf0e10cSrcweir private XPropertySet m_explanationField; 47cdf0e10cSrcweir private XValidator m_validator; 48cdf0e10cSrcweir 49cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 50cdf0e10cSrcweir public SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator ) 51cdf0e10cSrcweir { 52cdf0e10cSrcweir m_document = document; 53cdf0e10cSrcweir m_validator = validator; 54cdf0e10cSrcweir m_formLayer = new FormLayer( m_document ); 55cdf0e10cSrcweir createControls( columnPos, rowPos, formComponentService, 1, 0 ); 56cdf0e10cSrcweir } 57cdf0e10cSrcweir 58cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 59cdf0e10cSrcweir public SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator, int controlCount, int controlHeight ) 60cdf0e10cSrcweir { 61cdf0e10cSrcweir m_document = document; 62cdf0e10cSrcweir m_validator = validator; 63cdf0e10cSrcweir m_formLayer = new FormLayer( m_document ); 64cdf0e10cSrcweir createControls( columnPos, rowPos, formComponentService, controlCount, controlHeight ); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 68cdf0e10cSrcweir public XPropertySet getInputField() 69cdf0e10cSrcweir { 70cdf0e10cSrcweir return m_inputField; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir 73cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 74cdf0e10cSrcweir public void setExplanatoryText( String text ) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir try 77cdf0e10cSrcweir { 78cdf0e10cSrcweir m_inputLabel.setPropertyValue( "Label", text ); 79cdf0e10cSrcweir } 80cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir e.printStackTrace( System.err ); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir } 85cdf0e10cSrcweir 86cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 87cdf0e10cSrcweir private void createControls( int columnPos, int rowPos, String formComponentService, int controlCount, int controlHeight ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir try 90cdf0e10cSrcweir { 91cdf0e10cSrcweir m_inputLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, rowPos, 70, 12, null ); 92cdf0e10cSrcweir m_inputLabel.setPropertyValue( "MultiLine", new Boolean( true ) ); 93cdf0e10cSrcweir 94cdf0e10cSrcweir com.sun.star.awt.FontDescriptor font = (com.sun.star.awt.FontDescriptor)m_inputLabel.getPropertyValue( "FontDescriptor" ); 95cdf0e10cSrcweir font.Weight = com.sun.star.awt.FontWeight.BOLD; 96cdf0e10cSrcweir m_inputLabel.setPropertyValue( "FontDescriptor", font ); 97cdf0e10cSrcweir 98cdf0e10cSrcweir if ( controlHeight == 0 ) 99cdf0e10cSrcweir controlHeight = 6; 100cdf0e10cSrcweir 101cdf0e10cSrcweir int controlPos = rowPos + 12; 102cdf0e10cSrcweir XPropertySet[] controls = new XPropertySet[ controlCount ]; 103cdf0e10cSrcweir for ( int i = 0; i < controlCount; ++i, controlPos += controlHeight ) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir controls[ i ] = m_formLayer.createControlAndShape( formComponentService, columnPos, controlPos, 25, controlHeight, null ); 106cdf0e10cSrcweir controls[ i ].setPropertyValue( "Name", formComponentService ); 107cdf0e10cSrcweir controls[ i ].setPropertyValue( "Tag", String.valueOf( i ) ); 108cdf0e10cSrcweir 109cdf0e10cSrcweir if ( controls[ i ].getPropertySetInfo().hasPropertyByName( "Border" ) ) 110cdf0e10cSrcweir controls[ i ].setPropertyValue( "Border", new Short( (short)2 ) ); 111cdf0e10cSrcweir 112cdf0e10cSrcweir XValidatableFormComponent xComp = (XValidatableFormComponent)UnoRuntime.queryInterface( XValidatableFormComponent.class, 113cdf0e10cSrcweir controls[ i ] ); 114cdf0e10cSrcweir xComp.addFormComponentValidityListener( this ); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir m_inputField = controls[ 0 ]; 117cdf0e10cSrcweir 118cdf0e10cSrcweir // ---------------------------------- 119cdf0e10cSrcweir controlPos += 4; 120cdf0e10cSrcweir XPropertySet xLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 121cdf0e10cSrcweir xLabel.setPropertyValue( "Label", new String( "Status:" ) ); 122cdf0e10cSrcweir controlPos += 4; 123cdf0e10cSrcweir m_statusField = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 124cdf0e10cSrcweir m_statusField.setPropertyValue( "Label", new String( "" ) ); 125cdf0e10cSrcweir 126cdf0e10cSrcweir // ---------------------------------- 127cdf0e10cSrcweir controlPos += 6; 128cdf0e10cSrcweir xLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 129cdf0e10cSrcweir xLabel.setPropertyValue( "Label", new String( "Explanation for invalidity:" ) ); 130cdf0e10cSrcweir controlPos += 4; 131cdf0e10cSrcweir m_explanationField = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 132cdf0e10cSrcweir m_explanationField.setPropertyValue( "Label", new String( "" ) ); 133cdf0e10cSrcweir 134cdf0e10cSrcweir XValidatable xValidatable = (XValidatable)UnoRuntime.queryInterface( XValidatable.class, m_inputField ); 135cdf0e10cSrcweir xValidatable.setValidator( m_validator ); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir catch( java.lang.Exception e ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir e.printStackTrace( System.out ); 140cdf0e10cSrcweir } 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 144cdf0e10cSrcweir /* XEventListener overridables */ 145cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 146cdf0e10cSrcweir public void disposing( com.sun.star.lang.EventObject eventObject ) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir // not interested in 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 152cdf0e10cSrcweir /* XFormComponentValidityListener overridables */ 153cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 154cdf0e10cSrcweir public void componentValidityChanged( com.sun.star.lang.EventObject eventObject ) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir try 157cdf0e10cSrcweir { 158cdf0e10cSrcweir if ( m_inputField.equals( eventObject.Source ) ) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir XValidatableFormComponent xComp = (XValidatableFormComponent)UnoRuntime.queryInterface( XValidatableFormComponent.class, 161cdf0e10cSrcweir eventObject.Source ); 162cdf0e10cSrcweir // the current value 163cdf0e10cSrcweir Object value = xComp.getCurrentValue(); 164cdf0e10cSrcweir 165cdf0e10cSrcweir // the current validity flag 166cdf0e10cSrcweir boolean isValid = xComp.isValid(); 167cdf0e10cSrcweir 168cdf0e10cSrcweir m_statusField.setPropertyValue("Label", isValid ? "valid" : "invalid" ); 169cdf0e10cSrcweir m_statusField.setPropertyValue( "TextColor", new Integer( isValid ? 0x008000 : 0x800000 ) ); 170cdf0e10cSrcweir 171cdf0e10cSrcweir String validityMessage = ""; 172cdf0e10cSrcweir if ( !isValid ) 173cdf0e10cSrcweir validityMessage = m_validator.explainInvalid( value ); 174cdf0e10cSrcweir m_explanationField.setPropertyValue( "Label", validityMessage ); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir } 177cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir e.printStackTrace( System.out ); 180cdf0e10cSrcweir } 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir } 184