Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / odk / examples / DevelopersGuide / Forms / DateValidator.java
blob646dfac797401b4adfa831316cdb4f488aa99cd7
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 public class DateValidator extends ControlValidator
24 /** Creates a new instance of NumericValidator */
25 public DateValidator( )
29 public String explainInvalid( Object Value )
31 try
33 if ( isVoid( Value ) )
34 return "empty input";
36 com.sun.star.util.Date dateValue = (com.sun.star.util.Date)Value;
37 if ( isDedicatedInvalidDate( dateValue ) )
38 return "this is no valid date";
40 if ( !isNextMonthsDate( dateValue ) )
41 return "date must denote a day in the current month";
43 catch( java.lang.Exception e )
45 return "oops. What did you enter for this to happen?";
47 return "";
50 public boolean isValid( Object Value )
52 try
54 if ( isVoid( Value ) )
55 return false;
57 com.sun.star.util.Date dateValue = (com.sun.star.util.Date)
58 com.sun.star.uno.AnyConverter.toObject(
59 com.sun.star.util.Date.class, Value);
60 if ( isDedicatedInvalidDate( dateValue ) )
61 return false;
63 if ( !isNextMonthsDate( dateValue ) )
64 return false;
65 return true;
67 catch( java.lang.Exception e )
69 e.printStackTrace( System.err );
71 return false;
74 private boolean isDedicatedInvalidDate( com.sun.star.util.Date dateValue )
76 return ( dateValue.Day == 0 ) && ( dateValue.Month == 0 ) && ( dateValue.Year == 0 );
79 private boolean isNextMonthsDate( com.sun.star.util.Date dateValue )
81 int overallMonth = dateValue.Year * 12 + dateValue.Month - 1;
83 int todaysMonth = new java.util.Date().getMonth();
84 int todaysYear = new java.util.Date().getYear() + 1900;
85 int todaysOverallMonth = todaysYear * 12 + todaysMonth;
87 return overallMonth == todaysOverallMonth;
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */