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 24 package integration.forms; 25 26 import com.sun.star.uno.*; 27 import com.sun.star.util.*; 28 import com.sun.star.lang.*; 29 import com.sun.star.accessibility.*; 30 import com.sun.star.container.*; 31 import com.sun.star.beans.*; 32 import com.sun.star.form.binding.*; 33 import com.sun.star.form.validation.*; 34 35 import integration.forms.DocumentHelper; 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 /* ------------------------------------------------------------------ */ SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator )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 /* ------------------------------------------------------------------ */ SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator, int controlCount, int controlHeight )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 /* ------------------------------------------------------------------ */ getInputField()72 public XPropertySet getInputField() 73 { 74 return m_inputField; 75 } 76 77 /* ------------------------------------------------------------------ */ setExplanatoryText( String text )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 /* ------------------------------------------------------------------ */ createControls( int columnPos, int rowPos, String formComponentService, int controlCount, int controlHeight )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 /* ------------------------------------------------------------------ */ disposing( com.sun.star.lang.EventObject eventObject )150 public void disposing( com.sun.star.lang.EventObject eventObject ) 151 { 152 // not interested in 153 } 154 155 /* ------------------------------------------------------------------ */ 156 /* XFormComponentValidityListener overridables */ 157 /* ------------------------------------------------------------------ */ componentValidityChanged( com.sun.star.lang.EventObject eventObject )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