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