1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SingleControlValidation.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 import com
.sun
.star
.uno
.*;
32 import com
.sun
.star
.util
.*;
33 import com
.sun
.star
.lang
.*;
34 import com
.sun
.star
.accessibility
.*;
35 import com
.sun
.star
.container
.*;
36 import com
.sun
.star
.beans
.*;
37 import com
.sun
.star
.form
.binding
.*;
38 import com
.sun
.star
.form
.validation
.*;
42 * @author fs@openoffice.org
44 public class SingleControlValidation
implements XFormComponentValidityListener
46 private DocumentHelper m_document
; /// our current test document
47 private FormLayer m_formLayer
; /// quick access to the form layer
48 private XMultiServiceFactory m_orb
; /// our service factory
50 private XPropertySet m_inputField
;
51 private XPropertySet m_inputLabel
;
52 private XPropertySet m_statusField
;
53 private XPropertySet m_explanationField
;
54 private XValidator m_validator
;
56 /* ------------------------------------------------------------------ */
57 public SingleControlValidation( DocumentHelper document
, int columnPos
, int rowPos
, String formComponentService
, XValidator validator
)
59 m_document
= document
;
60 m_validator
= validator
;
61 m_formLayer
= new FormLayer( m_document
);
62 createControls( columnPos
, rowPos
, formComponentService
, 1, 0 );
65 /* ------------------------------------------------------------------ */
66 public SingleControlValidation( DocumentHelper document
, int columnPos
, int rowPos
, String formComponentService
, XValidator validator
, int controlCount
, int controlHeight
)
68 m_document
= document
;
69 m_validator
= validator
;
70 m_formLayer
= new FormLayer( m_document
);
71 createControls( columnPos
, rowPos
, formComponentService
, controlCount
, controlHeight
);
74 /* ------------------------------------------------------------------ */
75 public XPropertySet
getInputField()
80 /* ------------------------------------------------------------------ */
81 public void setExplanatoryText( String text
)
85 m_inputLabel
.setPropertyValue( "Label", text
);
87 catch( com
.sun
.star
.uno
.Exception e
)
89 e
.printStackTrace( System
.err
);
93 /* ------------------------------------------------------------------ */
94 private void createControls( int columnPos
, int rowPos
, String formComponentService
, int controlCount
, int controlHeight
)
98 m_inputLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, rowPos
, 70, 12, null );
99 m_inputLabel
.setPropertyValue( "MultiLine", new Boolean( true ) );
101 com
.sun
.star
.awt
.FontDescriptor font
= (com
.sun
.star
.awt
.FontDescriptor
)m_inputLabel
.getPropertyValue( "FontDescriptor" );
102 font
.Weight
= com
.sun
.star
.awt
.FontWeight
.BOLD
;
103 m_inputLabel
.setPropertyValue( "FontDescriptor", font
);
105 if ( controlHeight
== 0 )
108 int controlPos
= rowPos
+ 12;
109 XPropertySet
[] controls
= new XPropertySet
[ controlCount
];
110 for ( int i
= 0; i
< controlCount
; ++i
, controlPos
+= controlHeight
)
112 controls
[ i
] = m_formLayer
.createControlAndShape( formComponentService
, columnPos
, controlPos
, 25, controlHeight
, null );
113 controls
[ i
].setPropertyValue( "Name", formComponentService
);
114 controls
[ i
].setPropertyValue( "Tag", String
.valueOf( i
) );
116 if ( controls
[ i
].getPropertySetInfo().hasPropertyByName( "Border" ) )
117 controls
[ i
].setPropertyValue( "Border", new Short( (short)2 ) );
119 XValidatableFormComponent xComp
= (XValidatableFormComponent
)UnoRuntime
.queryInterface( XValidatableFormComponent
.class,
121 xComp
.addFormComponentValidityListener( this );
123 m_inputField
= controls
[ 0 ];
125 // ----------------------------------
127 XPropertySet xLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
128 xLabel
.setPropertyValue( "Label", new String( "Status:" ) );
130 m_statusField
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
131 m_statusField
.setPropertyValue( "Label", new String( "" ) );
133 // ----------------------------------
135 xLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
136 xLabel
.setPropertyValue( "Label", new String( "Explanation for invalidity:" ) );
138 m_explanationField
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
139 m_explanationField
.setPropertyValue( "Label", new String( "" ) );
141 XValidatable xValidatable
= (XValidatable
)UnoRuntime
.queryInterface( XValidatable
.class, m_inputField
);
142 xValidatable
.setValidator( m_validator
);
144 catch( java
.lang
.Exception e
)
146 e
.printStackTrace( System
.out
);
150 /* ------------------------------------------------------------------ */
151 /* XEventListener overridables */
152 /* ------------------------------------------------------------------ */
153 public void disposing( com
.sun
.star
.lang
.EventObject eventObject
)
158 /* ------------------------------------------------------------------ */
159 /* XFormComponentValidityListener overridables */
160 /* ------------------------------------------------------------------ */
161 public void componentValidityChanged( com
.sun
.star
.lang
.EventObject eventObject
)
165 if ( m_inputField
.equals( eventObject
.Source
) )
167 XValidatableFormComponent xComp
= (XValidatableFormComponent
)UnoRuntime
.queryInterface( XValidatableFormComponent
.class,
168 eventObject
.Source
);
170 Object value
= xComp
.getCurrentValue();
172 // the current validity flag
173 boolean isValid
= xComp
.isValid();
175 m_statusField
.setPropertyValue("Label", isValid ?
"valid" : "invalid" );
176 m_statusField
.setPropertyValue( "TextColor", new Integer( isValid ?
0x008000 : 0x800000 ) );
178 String validityMessage
= "";
180 validityMessage
= m_validator
.explainInvalid( value
);
181 m_explanationField
.setPropertyValue( "Label", validityMessage
);
184 catch( com
.sun
.star
.uno
.Exception e
)
186 e
.printStackTrace( System
.out
);