comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/LocationSelect.java @ 9584:1a7cfeb1ff89

Punkt 10.8 WQ-Vorschau
author gernotbelger
date Wed, 09 Jan 2019 17:13:25 +0100
parents e4606eae8ea5
children
comparison
equal deleted inserted replaced
9583:2a67d05a1af0 9584:1a7cfeb1ff89
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.artifacts.states; 9 package org.dive4elements.river.artifacts.states;
10 10
11 import gnu.trove.TDoubleArrayList;
12
13 import org.apache.log4j.Logger; 11 import org.apache.log4j.Logger;
14 12 import org.dive4elements.artifactdatabase.data.StateData;
13 import org.dive4elements.artifacts.Artifact;
14 import org.dive4elements.artifacts.CallContext;
15 import org.dive4elements.artifacts.common.utils.XMLUtils;
16 import org.dive4elements.river.artifacts.D4EArtifact;
15 import org.w3c.dom.Element; 17 import org.w3c.dom.Element;
16 18
17 import org.dive4elements.artifacts.Artifact; 19 import gnu.trove.TDoubleArrayList;
18 import org.dive4elements.artifacts.CallContext;
19
20 import org.dive4elements.artifacts.common.utils.XMLUtils;
21
22 import org.dive4elements.artifactdatabase.data.StateData;
23
24 import org.dive4elements.river.artifacts.D4EArtifact;
25
26 20
27 /** 21 /**
28 * This state is used to realize the input of multiple locations as string. 22 * This state is used to realize the input of multiple locations as string.
29 * 23 *
30 * The string should be a whitespace separated list of double values where each 24 * The string should be a whitespace separated list of double values where each
32 * 26 *
33 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
34 */ 28 */
35 public class LocationSelect extends LocationDistanceSelect { 29 public class LocationSelect extends LocationDistanceSelect {
36 30
37 /** The log used in this class.*/ 31 private static final long serialVersionUID = 1L;
32 /** The log used in this class. */
38 private static Logger log = Logger.getLogger(LocationSelect.class); 33 private static Logger log = Logger.getLogger(LocationSelect.class);
39
40 34
41 public LocationSelect() { 35 public LocationSelect() {
42 } 36 }
43
44 37
45 /** UI Provider (which input method should the client provide to user. */ 38 /** UI Provider (which input method should the client provide to user. */
46 @Override 39 @Override
47 protected String getUIProvider() { 40 protected String getUIProvider() {
48 return "location_panel"; 41 return "location_panel";
49 } 42 }
50 43
51
52 @Override 44 @Override
53 protected Element[] createItems( 45 protected Element[] createItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) {
54 XMLUtils.ElementCreator cr, 46 final double[] minmax = getMinMax(artifact);
55 Artifact artifact,
56 String name,
57 CallContext context)
58 {
59 double[] minmax = getMinMax(artifact);
60 47
61 double minVal = Double.MIN_VALUE; 48 double minVal = Double.MIN_VALUE;
62 double maxVal = Double.MAX_VALUE; 49 double maxVal = Double.MAX_VALUE;
63 50
64 if (minmax != null) { 51 if (minmax != null) {
65 minVal = minmax[0]; 52 minVal = minmax[0];
66 maxVal = minmax[1]; 53 maxVal = minmax[1];
67 } 54 } else {
68 else {
69 log.warn("Could not read min/max distance values!"); 55 log.warn("Could not read min/max distance values!");
70 } 56 }
71 57
72 if (name.equals(LOCATIONS)) { 58 if (name.equals(LOCATIONS)) {
73 Element min = createItem( 59 final Element min = createItem(cr, new String[] { "min", new Double(minVal).toString() });
74 cr,
75 new String[] {"min", new Double(minVal).toString()});
76 60
77 Element max = createItem( 61 final Element max = createItem(cr, new String[] { "max", new Double(maxVal).toString() });
78 cr,
79 new String[] {"max", new Double(maxVal).toString()});
80 62
81 return new Element[] { min, max }; 63 return new Element[] { min, max };
82 } 64 }
83 65
84 return null; 66 return null;
85 } 67 }
86 68
87
88 /** Validates data from artifact. */ 69 /** Validates data from artifact. */
89 @Override 70 @Override
90 public boolean validate(Artifact artifact) 71 public boolean validate(final Artifact artifact) throws IllegalArgumentException {
91 throws IllegalArgumentException
92 {
93 log.debug("LocationSelect.validate"); 72 log.debug("LocationSelect.validate");
94 73
95 D4EArtifact flys = (D4EArtifact) artifact; 74 final D4EArtifact flys = (D4EArtifact) artifact;
96 StateData data = getData(flys, LOCATIONS); 75 final StateData data = getData(flys, LOCATIONS);
97 76
98 String locationStr = data != null 77 final String locationStr = data != null ? (String) data.getValue() : null;
99 ? (String) data.getValue()
100 : null;
101 78
102 if (locationStr == null || locationStr.length() == 0) { 79 if (locationStr == null || locationStr.length() == 0) {
103 log.error("No locations given."); 80 log.error("No locations given.");
104 throw new IllegalArgumentException("error_empty_state"); 81 throw new IllegalArgumentException("error_empty_state");
105 } 82 }
106 83
107 double[] minmax = getMinMax(artifact); 84 final double[] minmax = getMinMax(artifact);
108 double[] mm = extractLocations(locationStr); 85 final double[] mm = extractLocations(locationStr);
109 86
110 log.debug("Inserted min location: " + mm[0]); 87 log.debug("Inserted min location: " + mm[0]);
111 log.debug("Inserted max location: " + mm[mm.length-1]); 88 log.debug("Inserted max location: " + mm[mm.length - 1]);
112 89
113 return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length-1], 0d); 90 return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length - 1], 0d);
114 } 91 }
115
116 92
117 /** 93 /**
118 * This method takes a string that consist of whitespace separated double 94 * This method takes a string that consist of whitespace separated double
119 * values and returns the double values as array. 95 * values and returns the double values as array.
120 * 96 *
121 * @param locationStr The locations inserted in this state. 97 * @param locationStr
98 * The locations inserted in this state.
122 * 99 *
123 * @return the locations as array. 100 * @return the locations as array.
124 */ 101 */
125 protected double[] extractLocations(String locationStr) { 102 protected double[] extractLocations(final String locationStr) {
126 String[] tmp = locationStr.split(" "); 103 final String[] tmp = locationStr.split(" ");
127 TDoubleArrayList locations = new TDoubleArrayList(); 104 final TDoubleArrayList locations = new TDoubleArrayList();
128 105
129 for (String l: tmp) { 106 for (final String l : tmp) {
130 try { 107 try {
131 locations.add(Double.parseDouble(l)); 108 locations.add(Double.parseDouble(l));
132 } 109 }
133 catch (NumberFormatException nfe) { 110 catch (final NumberFormatException nfe) {
134 log.warn(nfe, nfe); 111 log.warn(nfe, nfe);
135 } 112 }
136 } 113 }
137 114
138 locations.sort(); 115 locations.sort();

http://dive4elements.wald.intevation.org