2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com
.sun
.star
.uno
.*;
20 import com
.sun
.star
.beans
.*;
21 import com
.sun
.star
.form
.validation
.*;
23 public class SingleControlValidation
implements XFormComponentValidityListener
25 private DocumentHelper m_document
; /// our current test document
26 private FormLayer m_formLayer
; /// quick access to the form layer
28 private XPropertySet m_inputField
;
29 private XPropertySet m_inputLabel
;
30 private XPropertySet m_statusField
;
31 private XPropertySet m_explanationField
;
32 private XValidator m_validator
;
34 /* ------------------------------------------------------------------ */
35 public SingleControlValidation( DocumentHelper document
, int columnPos
, int rowPos
, String formComponentService
, XValidator validator
)
37 m_document
= document
;
38 m_validator
= validator
;
39 m_formLayer
= new FormLayer( m_document
);
40 createControls( columnPos
, rowPos
, formComponentService
, 1, 0 );
43 /* ------------------------------------------------------------------ */
44 public SingleControlValidation( DocumentHelper document
, int columnPos
, int rowPos
, String formComponentService
, XValidator validator
, int controlCount
, int controlHeight
)
46 m_document
= document
;
47 m_validator
= validator
;
48 m_formLayer
= new FormLayer( m_document
);
49 createControls( columnPos
, rowPos
, formComponentService
, controlCount
, controlHeight
);
52 /* ------------------------------------------------------------------ */
53 public XPropertySet
getInputField()
58 /* ------------------------------------------------------------------ */
59 public void setExplanatoryText( String text
)
63 m_inputLabel
.setPropertyValue( "Label", text
);
65 catch( com
.sun
.star
.uno
.Exception e
)
67 e
.printStackTrace( System
.err
);
71 /* ------------------------------------------------------------------ */
72 private void createControls( int columnPos
, int rowPos
, String formComponentService
, int controlCount
, int controlHeight
)
76 m_inputLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, rowPos
, 70, 12, null );
77 m_inputLabel
.setPropertyValue( "MultiLine", Boolean
.TRUE
);
79 com
.sun
.star
.awt
.FontDescriptor font
= (com
.sun
.star
.awt
.FontDescriptor
)m_inputLabel
.getPropertyValue( "FontDescriptor" );
80 font
.Weight
= com
.sun
.star
.awt
.FontWeight
.BOLD
;
81 m_inputLabel
.setPropertyValue( "FontDescriptor", font
);
83 if ( controlHeight
== 0 )
86 int controlPos
= rowPos
+ 12;
87 XPropertySet
[] controls
= new XPropertySet
[ controlCount
];
88 for ( int i
= 0; i
< controlCount
; ++i
, controlPos
+= controlHeight
)
90 controls
[ i
] = m_formLayer
.createControlAndShape( formComponentService
, columnPos
, controlPos
, 25, controlHeight
, null );
91 controls
[ i
].setPropertyValue( "Name", formComponentService
);
92 controls
[ i
].setPropertyValue( "Tag", String
.valueOf( i
) );
94 if ( controls
[ i
].getPropertySetInfo().hasPropertyByName( "Border" ) )
95 controls
[ i
].setPropertyValue( "Border", Short
.valueOf( (short)2 ) );
97 XValidatableFormComponent xComp
= UnoRuntime
.queryInterface( XValidatableFormComponent
.class,
99 xComp
.addFormComponentValidityListener( this );
101 m_inputField
= controls
[ 0 ];
105 XPropertySet xLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
106 xLabel
.setPropertyValue( "Label", "Status:" );
108 m_statusField
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
109 m_statusField
.setPropertyValue( "Label", "" );
113 xLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
114 xLabel
.setPropertyValue( "Label", "Explanation for invalidity:" );
116 m_explanationField
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
117 m_explanationField
.setPropertyValue( "Label", "" );
119 XValidatable xValidatable
= UnoRuntime
.queryInterface( XValidatable
.class, m_inputField
);
120 xValidatable
.setValidator( m_validator
);
122 catch( java
.lang
.Exception e
)
124 e
.printStackTrace( System
.err
);
128 /* ------------------------------------------------------------------ */
129 /* XEventListener overridables */
130 /* ------------------------------------------------------------------ */
131 public void disposing( com
.sun
.star
.lang
.EventObject eventObject
)
136 /* ------------------------------------------------------------------ */
137 /* XFormComponentValidityListener overridables */
138 /* ------------------------------------------------------------------ */
139 public void componentValidityChanged( com
.sun
.star
.lang
.EventObject eventObject
)
143 if ( m_inputField
.equals( eventObject
.Source
) )
145 XValidatableFormComponent xComp
= UnoRuntime
.queryInterface( XValidatableFormComponent
.class,
146 eventObject
.Source
);
148 Object value
= xComp
.getCurrentValue();
150 // the current validity flag
151 boolean isValid
= xComp
.isValid();
153 m_statusField
.setPropertyValue("Label", isValid ?
"valid" : "invalid" );
154 m_statusField
.setPropertyValue( "TextColor", Integer
.valueOf( isValid ?
0x008000 : 0x800000 ) );
156 String validityMessage
= "";
158 validityMessage
= m_validator
.explainInvalid( value
);
159 m_explanationField
.setPropertyValue( "Label", validityMessage
);
162 catch( com
.sun
.star
.uno
.Exception e
)
164 e
.printStackTrace( System
.err
);