1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.uno
.*;
21 import com
.sun
.star
.beans
.*;
22 import com
.sun
.star
.form
.validation
.*;
24 public class SingleControlValidation
implements XFormComponentValidityListener
26 private DocumentHelper m_document
; /// our current test document
27 private FormLayer m_formLayer
; /// quick access to the form layer
29 private XPropertySet m_inputField
;
30 private XPropertySet m_inputLabel
;
31 private XPropertySet m_statusField
;
32 private XPropertySet m_explanationField
;
33 private XValidator m_validator
;
35 /* ------------------------------------------------------------------ */
36 public SingleControlValidation( DocumentHelper document
, int columnPos
, int rowPos
, String formComponentService
, XValidator validator
)
38 m_document
= document
;
39 m_validator
= validator
;
40 m_formLayer
= new FormLayer( m_document
);
41 createControls( columnPos
, rowPos
, formComponentService
, 1, 0 );
44 /* ------------------------------------------------------------------ */
45 public SingleControlValidation( DocumentHelper document
, int columnPos
, int rowPos
, String formComponentService
, XValidator validator
, int controlCount
, int controlHeight
)
47 m_document
= document
;
48 m_validator
= validator
;
49 m_formLayer
= new FormLayer( m_document
);
50 createControls( columnPos
, rowPos
, formComponentService
, controlCount
, controlHeight
);
53 /* ------------------------------------------------------------------ */
54 public XPropertySet
getInputField()
59 /* ------------------------------------------------------------------ */
60 public void setExplanatoryText( String text
)
64 m_inputLabel
.setPropertyValue( "Label", text
);
66 catch( com
.sun
.star
.uno
.Exception e
)
68 e
.printStackTrace( System
.err
);
72 /* ------------------------------------------------------------------ */
73 private void createControls( int columnPos
, int rowPos
, String formComponentService
, int controlCount
, int controlHeight
)
77 m_inputLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, rowPos
, 70, 12, null );
78 m_inputLabel
.setPropertyValue( "MultiLine", Boolean
.TRUE
);
80 com
.sun
.star
.awt
.FontDescriptor font
= (com
.sun
.star
.awt
.FontDescriptor
)m_inputLabel
.getPropertyValue( "FontDescriptor" );
81 font
.Weight
= com
.sun
.star
.awt
.FontWeight
.BOLD
;
82 m_inputLabel
.setPropertyValue( "FontDescriptor", font
);
84 if ( controlHeight
== 0 )
87 int controlPos
= rowPos
+ 12;
88 XPropertySet
[] controls
= new XPropertySet
[ controlCount
];
89 for ( int i
= 0; i
< controlCount
; ++i
, controlPos
+= controlHeight
)
91 controls
[ i
] = m_formLayer
.createControlAndShape( formComponentService
, columnPos
, controlPos
, 25, controlHeight
, null );
92 controls
[ i
].setPropertyValue( "Name", formComponentService
);
93 controls
[ i
].setPropertyValue( "Tag", String
.valueOf( i
) );
95 if ( controls
[ i
].getPropertySetInfo().hasPropertyByName( "Border" ) )
96 controls
[ i
].setPropertyValue( "Border", Short
.valueOf( (short)2 ) );
98 XValidatableFormComponent xComp
= UnoRuntime
.queryInterface( XValidatableFormComponent
.class,
100 xComp
.addFormComponentValidityListener( this );
102 m_inputField
= controls
[ 0 ];
106 XPropertySet xLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
107 xLabel
.setPropertyValue( "Label", "Status:" );
109 m_statusField
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
110 m_statusField
.setPropertyValue( "Label", "" );
114 xLabel
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
115 xLabel
.setPropertyValue( "Label", "Explanation for invalidity:" );
117 m_explanationField
= m_formLayer
.createControlAndShape( "FixedText", columnPos
, controlPos
, 70, 4, null );
118 m_explanationField
.setPropertyValue( "Label", "" );
120 XValidatable xValidatable
= UnoRuntime
.queryInterface( XValidatable
.class, m_inputField
);
121 xValidatable
.setValidator( m_validator
);
123 catch( java
.lang
.Exception e
)
125 e
.printStackTrace( System
.err
);
129 /* ------------------------------------------------------------------ */
130 /* XEventListener overridables */
131 /* ------------------------------------------------------------------ */
132 public void disposing( com
.sun
.star
.lang
.EventObject eventObject
)
137 /* ------------------------------------------------------------------ */
138 /* XFormComponentValidityListener overridables */
139 /* ------------------------------------------------------------------ */
140 public void componentValidityChanged( com
.sun
.star
.lang
.EventObject eventObject
)
144 if ( m_inputField
.equals( eventObject
.Source
) )
146 XValidatableFormComponent xComp
= UnoRuntime
.queryInterface( XValidatableFormComponent
.class,
147 eventObject
.Source
);
149 Object value
= xComp
.getCurrentValue();
151 // the current validity flag
152 boolean isValid
= xComp
.isValid();
154 m_statusField
.setPropertyValue("Label", isValid ?
"valid" : "invalid" );
155 m_statusField
.setPropertyValue( "TextColor", Integer
.valueOf( isValid ?
0x008000 : 0x800000 ) );
157 String validityMessage
= "";
159 validityMessage
= m_validator
.explainInvalid( value
);
160 m_explanationField
.setPropertyValue( "Label", validityMessage
);
163 catch( com
.sun
.star
.uno
.Exception e
)
165 e
.printStackTrace( System
.err
);
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */