1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 |
|
---|
23 | using System;
|
---|
24 | using System.IO;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.SciLabParameterOptimization {
|
---|
31 | [Item("PathValue", "Represents a path.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class PathValue : StringValue {
|
---|
34 | [Storable]
|
---|
35 | private bool checkExistence;
|
---|
36 | public bool CheckExistence {
|
---|
37 | get { return checkExistence; }
|
---|
38 | set {
|
---|
39 | if (ReadOnly) throw new NotSupportedException("CheckExistence cannot be set. PathValue is read-only.");
|
---|
40 | if (value != checkExistence) {
|
---|
41 | checkExistence = value;
|
---|
42 | OnCheckExistenceChanged();
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override string Value {
|
---|
48 | get { return base.Value; }
|
---|
49 | set {
|
---|
50 | if (value == null) value = string.Empty;
|
---|
51 | if (checkExistence && !string.IsNullOrEmpty(value) && !File.Exists(value) && !Directory.Exists(value))
|
---|
52 | throw new ArgumentException(string.Format("The given file {0} does not exists.", value));
|
---|
53 | if (value[value.Length - 1] == Path.DirectorySeparatorChar)
|
---|
54 | value = value.TrimEnd(Path.DirectorySeparatorChar);
|
---|
55 | if (value[value.Length - 1] == Path.AltDirectorySeparatorChar)
|
---|
56 | value = value.TrimEnd(Path.AltDirectorySeparatorChar);
|
---|
57 | base.Value = value;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | protected PathValue(bool deserializing) : base(deserializing) { }
|
---|
63 | protected PathValue(PathValue original, Cloner cloner)
|
---|
64 | : base(original, cloner) {
|
---|
65 | checkExistence = original.checkExistence;
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected PathValue()
|
---|
69 | : base(string.Empty) {
|
---|
70 | checkExistence = true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public abstract bool Exists();
|
---|
74 |
|
---|
75 | protected override bool Validate(string value, out string errorMessage) {
|
---|
76 | if (CheckExistence && !File.Exists(value) && !Directory.Exists(value)) {
|
---|
77 | errorMessage = string.Format("The given path {0} does not exist.", value);
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 | return base.Validate(value, out errorMessage);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public event EventHandler CheckExistenceChanged;
|
---|
84 | protected virtual void OnCheckExistenceChanged() {
|
---|
85 | var handler = CheckExistenceChanged;
|
---|
86 | if (handler != null)
|
---|
87 | handler(this, EventArgs.Empty);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|