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 import com.sun.star.uno.*;
25 import com.sun.star.util.*;
26 import com.sun.star.lang.*;
27 import com.sun.star.accessibility.*;
28 import com.sun.star.container.*;
29 import com.sun.star.beans.*;
30 import com.sun.star.form.binding.*;
31 import com.sun.star.form.validation.*;
32 
33 /**
34  *
35  * @author  fs@openoffice.org
36  */
37 public class SingleControlValidation implements XFormComponentValidityListener
38 {
39     private DocumentHelper          m_document;         /// our current test document
40     private FormLayer               m_formLayer;        /// quick access to the form layer
41     private XMultiServiceFactory    m_orb;              /// our service factory
42 
43     private XPropertySet            m_inputField;
44     private XPropertySet            m_inputLabel;
45     private XPropertySet            m_statusField;
46     private XPropertySet            m_explanationField;
47     private XValidator              m_validator;
48 
49     /* ------------------------------------------------------------------ */
SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator )50     public SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator )
51     {
52         m_document = document;
53         m_validator = validator;
54         m_formLayer = new FormLayer( m_document );
55         createControls( columnPos, rowPos, formComponentService, 1, 0 );
56     }
57 
58     /* ------------------------------------------------------------------ */
SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator, int controlCount, int controlHeight )59     public SingleControlValidation( DocumentHelper document, int columnPos, int rowPos, String formComponentService, XValidator validator, int controlCount, int controlHeight )
60     {
61         m_document = document;
62         m_validator = validator;
63         m_formLayer = new FormLayer( m_document );
64         createControls( columnPos, rowPos, formComponentService, controlCount, controlHeight );
65     }
66 
67     /* ------------------------------------------------------------------ */
getInputField()68     public XPropertySet getInputField()
69     {
70         return m_inputField;
71     }
72 
73     /* ------------------------------------------------------------------ */
setExplanatoryText( String text )74     public void setExplanatoryText( String text )
75     {
76         try
77         {
78             m_inputLabel.setPropertyValue( "Label", text );
79         }
80         catch( com.sun.star.uno.Exception e )
81         {
82             e.printStackTrace( System.err );
83         }
84     }
85 
86     /* ------------------------------------------------------------------ */
createControls( int columnPos, int rowPos, String formComponentService, int controlCount, int controlHeight )87     private void createControls( int columnPos, int rowPos, String formComponentService, int controlCount, int controlHeight )
88     {
89         try
90         {
91             m_inputLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, rowPos, 70, 12, null );
92             m_inputLabel.setPropertyValue( "MultiLine", new Boolean( true ) );
93 
94             com.sun.star.awt.FontDescriptor font = (com.sun.star.awt.FontDescriptor)m_inputLabel.getPropertyValue( "FontDescriptor" );
95             font.Weight = com.sun.star.awt.FontWeight.BOLD;
96             m_inputLabel.setPropertyValue( "FontDescriptor", font );
97 
98             if ( controlHeight == 0 )
99                 controlHeight = 6;
100 
101             int controlPos = rowPos + 12;
102             XPropertySet[] controls = new XPropertySet[ controlCount ];
103             for ( int i = 0; i < controlCount; ++i, controlPos += controlHeight )
104             {
105                 controls[ i ] = m_formLayer.createControlAndShape( formComponentService, columnPos, controlPos, 25, controlHeight, null );
106                 controls[ i ].setPropertyValue( "Name", formComponentService );
107                 controls[ i ].setPropertyValue( "Tag", String.valueOf( i ) );
108 
109                 if ( controls[ i ].getPropertySetInfo().hasPropertyByName( "Border" ) )
110                     controls[ i ].setPropertyValue( "Border", new Short( (short)2 ) );
111 
112                 XValidatableFormComponent xComp = (XValidatableFormComponent)UnoRuntime.queryInterface( XValidatableFormComponent.class,
113                     controls[ i ] );
114                 xComp.addFormComponentValidityListener( this );
115             }
116             m_inputField = controls[ 0 ];
117 
118             // ----------------------------------
119             controlPos += 4;
120             XPropertySet xLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null );
121             xLabel.setPropertyValue( "Label", new String( "Status:" ) );
122             controlPos += 4;
123             m_statusField = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null );
124             m_statusField.setPropertyValue( "Label", new String( "" ) );
125 
126             // ----------------------------------
127             controlPos += 6;
128             xLabel = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null );
129             xLabel.setPropertyValue( "Label", new String( "Explanation for invalidity:" ) );
130             controlPos += 4;
131             m_explanationField = m_formLayer.createControlAndShape( "FixedText", columnPos, controlPos, 70, 4, null );
132             m_explanationField.setPropertyValue( "Label", new String( "" ) );
133 
134             XValidatable xValidatable = (XValidatable)UnoRuntime.queryInterface( XValidatable.class, m_inputField );
135             xValidatable.setValidator( m_validator );
136         }
137         catch( java.lang.Exception e  )
138         {
139             e.printStackTrace( System.out );
140         }
141     }
142 
143     /* ------------------------------------------------------------------ */
144     /* XEventListener overridables                                        */
145     /* ------------------------------------------------------------------ */
disposing( com.sun.star.lang.EventObject eventObject )146     public void disposing( com.sun.star.lang.EventObject eventObject )
147     {
148         // not interested in
149     }
150 
151     /* ------------------------------------------------------------------ */
152     /* XFormComponentValidityListener overridables                        */
153     /* ------------------------------------------------------------------ */
componentValidityChanged( com.sun.star.lang.EventObject eventObject )154     public void componentValidityChanged( com.sun.star.lang.EventObject eventObject )
155     {
156         try
157         {
158             if ( m_inputField.equals( eventObject.Source ) )
159             {
160                 XValidatableFormComponent xComp = (XValidatableFormComponent)UnoRuntime.queryInterface( XValidatableFormComponent.class,
161                     eventObject.Source );
162                 // the current value
163                 Object value = xComp.getCurrentValue();
164 
165                 // the current validity flag
166                 boolean isValid = xComp.isValid();
167 
168                 m_statusField.setPropertyValue("Label", isValid ? "valid" : "invalid" );
169                 m_statusField.setPropertyValue( "TextColor", new Integer( isValid ? 0x008000 : 0x800000 ) );
170 
171                 String validityMessage = "";
172                 if ( !isValid )
173                     validityMessage = m_validator.explainInvalid( value );
174                 m_explanationField.setPropertyValue( "Label", validityMessage );
175             }
176         }
177         catch( com.sun.star.uno.Exception e )
178         {
179             e.printStackTrace( System.out );
180         }
181     }
182 
183 }
184