Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef MANTID_API_ADSVALIDATOR_H_
#define MANTID_API_ADSVALIDATOR_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/TypedValidator.h"
#include "MantidKernel/StringTokenizer.h"
#include "MantidAPI/AnalysisDataService.h"
#include <vector>
namespace Mantid {
namespace API {
using namespace Mantid::Kernel;
/** ADSValidator : a validator that requires the value of a property to be
present in the ADS. The type must be std::string
Copyright © 2016 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class ADSValidator : public Kernel::TypedValidator<std::string> {
public:
/// Default constructor. Sets up an empty list of valid values.
ADSValidator(const bool allowMultiSelection = true,
const bool isOptional = false) :
TypedValidator<std::string>(), m_AllowMultiSelection(allowMultiSelection),
m_isOptional{ isOptional };
/// Clone the validator
IValidator_sptr clone() const override {
return boost::make_shared<ADSValidator>(*this);
}
bool isMultiSelectionAllowed() const
{
return m_AllowMultiSelection;
}
void setMultiSelectionAllowed(const bool isMultiSelectionAllowed) {
m_AllowMultiSelection = isMultiSelectionAllowed;
}
bool isOptional() const {
return m_isOptional;
}
void setOptional(const bool setOptional) {
m_isOptional = setOptional;
}
protected:
/** Checks if the string passed is in the ADS, or if all members are in the ADS
* @param value :: The value to test
* @return "" if the value is on the list, or "The workspace is not in the workspace list"
*/
std::string checkValidity(const std::string &value) const override {
if (!m_isOptional && isEmpty(value))
return "Select a value";
std::vector<std::string> ws_to_validate;
ws_to_validate.push_back(value);
if (m_AllowMultiSelection)
{
StringTokenizer tokenizer(value, ",|;\t",
StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_IGNORE_FINAL_EMPTY_TOKEN);
ws_to_validate = tokenizer.asVector();
}
auto& ads = AnalysisDataService::Instance();
std::ostringstream os;
for (std::string wsName : ws_to_validate) {
if (!ads.doesExist(wsName)) {
os << "The workspace \"" << value
<< "\" is not in the workspace list";
}
}
return os.str();
}
/**
* Is the value considered empty. Specialized string version to use empty
* @param value :: The value to check
* @return True if it is considered empty
*/
bool isEmpty(const std::string &value) const { return value.empty(); }
private:
/// if the validator should allow multiple selection
bool m_AllowMultiSelection;
/// if the validator should an empty selection
bool m_isOptional;
};
} // namespace API
} // namespace Mantid
#endif /* MANTID_API_ADSVALIDATOR_H_ */