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