1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir import com.sun.star.uno.*; 29*cdf0e10cSrcweir import com.sun.star.util.*; 30*cdf0e10cSrcweir import com.sun.star.lang.*; 31*cdf0e10cSrcweir import com.sun.star.accessibility.*; 32*cdf0e10cSrcweir import com.sun.star.container.*; 33*cdf0e10cSrcweir import com.sun.star.beans.*; 34*cdf0e10cSrcweir import com.sun.star.form.binding.*; 35*cdf0e10cSrcweir import com.sun.star.form.validation.*; 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir /** 38*cdf0e10cSrcweir * 39*cdf0e10cSrcweir * @author fs@openoffice.org 40*cdf0e10cSrcweir */ 41*cdf0e10cSrcweir public class SingleControlValidation implements XFormComponentValidityListener 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir private DocumentHelper m_document; /// our current test document 44*cdf0e10cSrcweir private FormLayer m_formLayer; /// quick access to the form layer 45*cdf0e10cSrcweir private XMultiServiceFactory m_orb; /// our service factory 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir private XPropertySet m_inputField; 48*cdf0e10cSrcweir private XPropertySet m_inputLabel; 49*cdf0e10cSrcweir private XPropertySet m_statusField; 50*cdf0e10cSrcweir private XPropertySet m_explanationField; 51*cdf0e10cSrcweir private XValidator m_validator; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 54*cdf0e10cSrcweir public SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator ) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir m_document = document; 57*cdf0e10cSrcweir m_validator = validator; 58*cdf0e10cSrcweir m_formLayer = new FormLayer( m_document ); 59*cdf0e10cSrcweir createControls( columnPos, rowPos, formComponentService, 1, 0 ); 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 63*cdf0e10cSrcweir public SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator, int controlCount, int controlHeight ) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir m_document = document; 66*cdf0e10cSrcweir m_validator = validator; 67*cdf0e10cSrcweir m_formLayer = new FormLayer( m_document ); 68*cdf0e10cSrcweir createControls( columnPos, rowPos, formComponentService, controlCount, controlHeight ); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 72*cdf0e10cSrcweir public XPropertySet getInputField() 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir return m_inputField; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 78*cdf0e10cSrcweir public void setExplanatoryText( String text ) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir try 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir m_inputLabel.setPropertyValue( "Label", text ); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir e.printStackTrace( System.err ); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 91*cdf0e10cSrcweir private void createControls( int columnPos, int rowPos, String formComponentService, int controlCount, int controlHeight ) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir try 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir m_inputLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, rowPos, 70, 12, null ); 96*cdf0e10cSrcweir m_inputLabel.setPropertyValue( "MultiLine", new Boolean( true ) ); 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir com.sun.star.awt.FontDescriptor font = (com.sun.star.awt.FontDescriptor)m_inputLabel.getPropertyValue( "FontDescriptor" ); 99*cdf0e10cSrcweir font.Weight = com.sun.star.awt.FontWeight.BOLD; 100*cdf0e10cSrcweir m_inputLabel.setPropertyValue( "FontDescriptor", font ); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir if ( controlHeight == 0 ) 103*cdf0e10cSrcweir controlHeight = 6; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir int controlPos = rowPos + 12; 106*cdf0e10cSrcweir XPropertySet[] controls = new XPropertySet[ controlCount ]; 107*cdf0e10cSrcweir for ( int i = 0; i < controlCount; ++i, controlPos += controlHeight ) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir controls[ i ] = m_formLayer.createControlAndShape( formComponentService, columnPos, controlPos, 25, controlHeight, null ); 110*cdf0e10cSrcweir controls[ i ].setPropertyValue( "Name", formComponentService ); 111*cdf0e10cSrcweir controls[ i ].setPropertyValue( "Tag", String.valueOf( i ) ); 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir if ( controls[ i ].getPropertySetInfo().hasPropertyByName( "Border" ) ) 114*cdf0e10cSrcweir controls[ i ].setPropertyValue( "Border", new Short( (short)2 ) ); 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir XValidatableFormComponent xComp = (XValidatableFormComponent)UnoRuntime.queryInterface( XValidatableFormComponent.class, 117*cdf0e10cSrcweir controls[ i ] ); 118*cdf0e10cSrcweir xComp.addFormComponentValidityListener( this ); 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir m_inputField = controls[ 0 ]; 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir // ---------------------------------- 123*cdf0e10cSrcweir controlPos += 4; 124*cdf0e10cSrcweir XPropertySet xLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 125*cdf0e10cSrcweir xLabel.setPropertyValue( "Label", new String( "Status:" ) ); 126*cdf0e10cSrcweir controlPos += 4; 127*cdf0e10cSrcweir m_statusField = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 128*cdf0e10cSrcweir m_statusField.setPropertyValue( "Label", new String( "" ) ); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir // ---------------------------------- 131*cdf0e10cSrcweir controlPos += 6; 132*cdf0e10cSrcweir xLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 133*cdf0e10cSrcweir xLabel.setPropertyValue( "Label", new String( "Explanation for invalidity:" ) ); 134*cdf0e10cSrcweir controlPos += 4; 135*cdf0e10cSrcweir m_explanationField = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null ); 136*cdf0e10cSrcweir m_explanationField.setPropertyValue( "Label", new String( "" ) ); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir XValidatable xValidatable = (XValidatable)UnoRuntime.queryInterface( XValidatable.class, m_inputField ); 139*cdf0e10cSrcweir xValidatable.setValidator( m_validator ); 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir catch( java.lang.Exception e ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir e.printStackTrace( System.out ); 144*cdf0e10cSrcweir } 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 148*cdf0e10cSrcweir /* XEventListener overridables */ 149*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 150*cdf0e10cSrcweir public void disposing( com.sun.star.lang.EventObject eventObject ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir // not interested in 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 156*cdf0e10cSrcweir /* XFormComponentValidityListener overridables */ 157*cdf0e10cSrcweir /* ------------------------------------------------------------------ */ 158*cdf0e10cSrcweir public void componentValidityChanged( com.sun.star.lang.EventObject eventObject ) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir try 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir if ( m_inputField.equals( eventObject.Source ) ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir XValidatableFormComponent xComp = (XValidatableFormComponent)UnoRuntime.queryInterface( XValidatableFormComponent.class, 165*cdf0e10cSrcweir eventObject.Source ); 166*cdf0e10cSrcweir // the current value 167*cdf0e10cSrcweir Object value = xComp.getCurrentValue(); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir // the current validity flag 170*cdf0e10cSrcweir boolean isValid = xComp.isValid(); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir m_statusField.setPropertyValue("Label", isValid ? "valid" : "invalid" ); 173*cdf0e10cSrcweir m_statusField.setPropertyValue( "TextColor", new Integer( isValid ? 0x008000 : 0x800000 ) ); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir String validityMessage = ""; 176*cdf0e10cSrcweir if ( !isValid ) 177*cdf0e10cSrcweir validityMessage = m_validator.explainInvalid( value ); 178*cdf0e10cSrcweir m_explanationField.setPropertyValue( "Label", validityMessage ); 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir catch( com.sun.star.uno.Exception e ) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir e.printStackTrace( System.out ); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir } 188