Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / DateValidator.java
blob05803e7a6704f1a44cc5d19438b72710d4339afa
1 /*
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 .
20 public class DateValidator extends ControlValidator
23 /** Creates a new instance of NumericValidator */
24 public DateValidator( )
28 public String explainInvalid( Object Value )
30 try
32 if ( isVoid( Value ) )
33 return "empty input";
35 com.sun.star.util.Date dateValue = (com.sun.star.util.Date)Value;
36 if ( isDedicatedInvalidDate( dateValue ) )
37 return "this is no valid date";
39 if ( !isNextMonthsDate( dateValue ) )
40 return "date must denote a day in the current month";
42 catch( java.lang.Exception e )
44 return "oops. What did you enter for this to happen?";
46 return "";
49 public boolean isValid( Object Value )
51 try
53 if ( isVoid( Value ) )
54 return false;
56 com.sun.star.util.Date dateValue = (com.sun.star.util.Date)
57 com.sun.star.uno.AnyConverter.toObject(
58 com.sun.star.util.Date.class, Value);
59 if ( isDedicatedInvalidDate( dateValue ) )
60 return false;
62 if ( !isNextMonthsDate( dateValue ) )
63 return false;
64 return true;
66 catch( java.lang.Exception e )
68 e.printStackTrace( System.err );
70 return false;
73 private boolean isDedicatedInvalidDate( com.sun.star.util.Date dateValue )
75 return ( dateValue.Day == 0 ) && ( dateValue.Month == 0 ) && ( dateValue.Year == 0 );
78 private boolean isNextMonthsDate( com.sun.star.util.Date dateValue )
80 int overallMonth = dateValue.Year * 12 + dateValue.Month - 1;
82 int todaysMonth = new java.util.Date().getMonth();
83 int todaysYear = new java.util.Date().getYear() + 1900;
84 int todaysOverallMonth = todaysYear * 12 + todaysMonth;
86 return overallMonth == todaysOverallMonth;