comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/LocationDistanceSelect.java @ 9175:34dc0163ad2d

DistancePanel Berechnungsstrecke->Darstellungsbereich Refactoring
author gernotbelger
date Mon, 25 Jun 2018 17:58:11 +0200
parents 07df414d5d88
children
comparison
equal deleted inserted replaced
9174:06bb3e94a927 9175:34dc0163ad2d
7 */ 7 */
8 8
9 package org.dive4elements.river.artifacts.states; 9 package org.dive4elements.river.artifacts.states;
10 10
11 import org.apache.log4j.Logger; 11 import org.apache.log4j.Logger;
12 12 import org.dive4elements.artifactdatabase.data.StateData;
13 import org.dive4elements.artifacts.Artifact; 13 import org.dive4elements.artifacts.Artifact;
14 14 import org.dive4elements.artifacts.CallContext;
15 import org.dive4elements.artifactdatabase.data.StateData;
16
17 import org.dive4elements.river.artifacts.D4EArtifact; 15 import org.dive4elements.river.artifacts.D4EArtifact;
18 import org.dive4elements.river.artifacts.WINFOArtifact; 16 import org.dive4elements.river.artifacts.WINFOArtifact;
19
20 import org.dive4elements.river.artifacts.access.RangeAccess; 17 import org.dive4elements.river.artifacts.access.RangeAccess;
21 18 import org.dive4elements.river.artifacts.resources.Resources;
22 19
23 /** 20 /**
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
25 */ 22 */
26 public class LocationDistanceSelect 23 public class LocationDistanceSelect extends ComputationRangeState {
27 extends ComputationRangeState
28 {
29 24
30 /** The log used in this class. */ 25 /** The log used in this class. */
31 private static Logger log = Logger.getLogger(LocationDistanceSelect.class); 26 private static Logger log = Logger.getLogger(LocationDistanceSelect.class);
32 27
33 /** The name of the 'mode' field. */ 28 /** The name of the 'mode' field. */
34 public static final String MODE = "ld_mode"; 29 public static final String MODE = "ld_mode";
35 30
36 /** The name of the 'locations' field. */ 31 /** The name of the 'locations' field. */
37 public static final String LOCATIONS = "ld_locations"; 32 public static final String LOCATIONS = "ld_locations";
38 33
39
40 /** 34 /**
41 * The default constructor that initializes an empty State object. 35 * The default constructor that initializes an empty State object.
42 */ 36 */
43 public LocationDistanceSelect() { 37 public LocationDistanceSelect() {
44 } 38 }
45 39
46
47 @Override 40 @Override
48 protected String getUIProvider() { 41 protected String getUIProvider() {
49 return "location_distance_panel"; 42 return "location_distance_panel";
50 } 43 }
51 44
52
53 /** Validates the range (or location). */ 45 /** Validates the range (or location). */
54 @Override 46 @Override
55 public boolean validate(Artifact artifact) 47 public boolean validate(final Artifact artifact) throws IllegalArgumentException {
56 throws IllegalArgumentException
57 {
58 log.debug("LocationDistanceSelect.validate"); 48 log.debug("LocationDistanceSelect.validate");
59 49
60 D4EArtifact flys = (D4EArtifact)artifact; 50 final D4EArtifact flys = (D4EArtifact) artifact;
61 StateData mode = getData(flys, MODE); 51 final StateData mode = getData(flys, MODE);
62 String mValue = mode != null ? (String)mode.getValue() : null; 52 final String mValue = mode != null ? (String) mode.getValue() : null;
63 if (mValue != null) { 53 if (mValue != null) {
64 if (mValue.equals("distance")) { 54 if (mValue.equals("distance")) {
65 return super.validate(flys); 55 return super.validate(flys);
66 } 56 } else {
67 else {
68 return validateLocations(flys); 57 return validateLocations(flys);
69 } 58 }
70 } 59 }
71 return false; 60 return false;
72 } 61 }
73 62
74
75 /** Validate selected locations. */ 63 /** Validate selected locations. */
76 protected boolean validateLocations(D4EArtifact flys) 64 protected boolean validateLocations(final D4EArtifact flys) throws IllegalArgumentException {
77 throws IllegalArgumentException 65 final StateData dValues = getData(flys, LOCATIONS);
78 { 66 final String values = dValues != null ? (String) dValues.getValue() : null;
79 StateData dValues = getData(flys, LOCATIONS);
80 String values = dValues != null ? (String)dValues.getValue() : null;
81 67
82 if (values == null || values.length() == 0) { 68 if (values == null || values.length() == 0) {
83 throw new IllegalArgumentException("error_empty_state"); 69 throw new IllegalArgumentException("error_empty_state");
84 } 70 }
85 71
86 double[] absMinMax = getMinMax(flys); 72 final double[] absMinMax = getMinMax(flys);
87 double[] relMinMax = getMinMaxFromString(values); 73 final double[] relMinMax = getMinMaxFromString(values);
88 74
89 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) { 75 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) {
90 throw new IllegalArgumentException("error_feed_from_out_of_range"); 76 throw new IllegalArgumentException("error_feed_from_out_of_range");
91 } 77 }
92 78
95 } 81 }
96 82
97 return true; 83 return true;
98 } 84 }
99 85
100
101 /** 86 /**
102 * Extracts the min/max values from String <i>s</i>. An 87 * Extracts the min/max values from String <i>s</i>. An
103 * IllegalArgumentException is thrown if there is a value that throws a 88 * IllegalArgumentException is thrown if there is a value that throws a
104 * NumberFormatException. 89 * NumberFormatException.
105 * 90 *
106 * @param s String that contains whitespace separated double values. 91 * @param s
92 * String that contains whitespace separated double values.
107 * 93 *
108 * @return a 2dmin array [min,max]. 94 * @return a 2dmin array [min,max].
109 */ 95 */
110 public static double[] getMinMaxFromString(String s) 96 public static double[] getMinMaxFromString(final String s) throws IllegalArgumentException {
111 throws IllegalArgumentException 97 final String[] values = s.split(" ");
112 {
113 String[] values = s.split(" ");
114 98
115 double[] minmax = new double[] { 99 final double[] minmax = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
116 Double.MAX_VALUE,
117 -Double.MAX_VALUE };
118 100
119 for (String v: values) { 101 for (final String v : values) {
120 try { 102 try {
121 double value = Double.valueOf(v); 103 final double value = Double.valueOf(v);
122 104
123 minmax[0] = minmax[0] < value ? minmax[0] : value; 105 minmax[0] = minmax[0] < value ? minmax[0] : value;
124 minmax[1] = minmax[1] > value ? minmax[1] : value; 106 minmax[1] = minmax[1] > value ? minmax[1] : value;
125 } 107 }
126 catch (NumberFormatException nfe) { 108 catch (final NumberFormatException nfe) {
127 throw new IllegalArgumentException( 109 throw new IllegalArgumentException("error_invalid_double_value");
128 "error_invalid_double_value");
129 } 110 }
130 } 111 }
131 112
132 return minmax; 113 return minmax;
133 } 114 }
134 115
135 116 public static double[] getLocations(final WINFOArtifact flys) {
136 public static double[] getLocations(WINFOArtifact flys) { 117 final RangeAccess ra = new RangeAccess(flys);
137 RangeAccess ra = new RangeAccess(flys);
138 return ra.getLocations(); 118 return ra.getLocations();
139 } 119 }
120
121 @Override
122 protected String getTitle(final CallContext context) {
123 // REMARK: that is how it should be: return Resources.getMsg(context.getMeta(), getID());
124 return Resources.getMsg(context.getMeta(), "state.title.location_distance_state");
125 }
126
140 } 127 }
141 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 128 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org