Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RiverSelect.java @ 3786:4adc35aa655c
merged flys-artifacts/2.9.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:47 +0200 |
parents | 7b14d00bca8a |
children | 8e66293c5369 |
comparison
equal
deleted
inserted
replaced
3719:e82acd5c86f7 | 3786:4adc35aa655c |
---|---|
1 package de.intevation.flys.artifacts.states; | |
2 | |
3 import java.util.List; | |
4 | |
5 import org.apache.log4j.Logger; | |
6 | |
7 import org.w3c.dom.Element; | |
8 import org.w3c.dom.Node; | |
9 | |
10 import de.intevation.artifacts.Artifact; | |
11 import de.intevation.artifacts.CallContext; | |
12 | |
13 import de.intevation.artifacts.common.utils.XMLUtils; | |
14 | |
15 import de.intevation.artifactdatabase.ProtocolUtils; | |
16 import de.intevation.artifactdatabase.data.StateData; | |
17 | |
18 import de.intevation.flys.model.River; | |
19 | |
20 import de.intevation.flys.artifacts.FLYSArtifact; | |
21 import de.intevation.flys.artifacts.model.RiverFactory; | |
22 import de.intevation.flys.artifacts.resources.Resources; | |
23 | |
24 | |
25 /** | |
26 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
27 */ | |
28 public class RiverSelect extends DefaultState { | |
29 | |
30 /** The logger used in this class. */ | |
31 private static Logger logger = Logger.getLogger(RiverSelect.class); | |
32 | |
33 /** Error message that is thrown if no river was found based on a given | |
34 * name.*/ | |
35 public static final String ERROR_NO_SUCH_RIVER = | |
36 "error_feed_no_such_river"; | |
37 | |
38 /** Error message that is thrown if no river was found based on a given | |
39 * name.*/ | |
40 public static final String ERROR_NO_RIVER_SELECTED = | |
41 "error_feed_no_river_selected"; | |
42 | |
43 | |
44 /** | |
45 * The default constructor that initializes an empty State object. | |
46 */ | |
47 public RiverSelect() { | |
48 } | |
49 | |
50 | |
51 /** | |
52 * Initialize the state based on the state node in the configuration. | |
53 * | |
54 * @param config The state configuration node. | |
55 */ | |
56 public void setup(Node config) { | |
57 super.setup(config); | |
58 } | |
59 | |
60 | |
61 protected Element createData( | |
62 XMLUtils.ElementCreator cr, | |
63 Artifact artifact, | |
64 StateData data, | |
65 CallContext context) | |
66 { | |
67 Element select = ProtocolUtils.createArtNode( | |
68 cr, "select", | |
69 new String[] { "uiprovider" }, | |
70 new String[] { "select_with_map" }); | |
71 cr.addAttr(select, "name", data.getName(), true); | |
72 | |
73 Element label = ProtocolUtils.createArtNode( | |
74 cr, "label", null, null); | |
75 | |
76 Element choices = ProtocolUtils.createArtNode( | |
77 cr, "choices", null, null); | |
78 | |
79 select.appendChild(label); | |
80 | |
81 label.setTextContent(Resources.getMsg( | |
82 context.getMeta(), | |
83 getID(), | |
84 getID())); | |
85 | |
86 return select; | |
87 } | |
88 | |
89 | |
90 @Override | |
91 protected Element[] createItems( | |
92 XMLUtils.ElementCreator cr, | |
93 Artifact artifact, | |
94 String name, | |
95 CallContext context) | |
96 { | |
97 List<River> rivers = RiverFactory.getRivers(); | |
98 Element[] items = new Element[rivers.size()]; | |
99 | |
100 int idx = 0; | |
101 for (River river: rivers) { | |
102 items[idx++] = createRiverItem(cr, river); | |
103 } | |
104 | |
105 return items; | |
106 } | |
107 | |
108 | |
109 /** | |
110 * This method creates a node that represents a river item. This node | |
111 * contains the label and the value that describe the river. | |
112 * | |
113 * @param cr The ElementCreator. | |
114 * @param river The river. | |
115 * | |
116 * @return the element that contains the information about the river. | |
117 */ | |
118 protected Element createRiverItem(XMLUtils.ElementCreator cr, River river) { | |
119 Element item = ProtocolUtils.createArtNode(cr, "item", null, null); | |
120 Element label = ProtocolUtils.createArtNode(cr, "label", null, null); | |
121 Element value = ProtocolUtils.createArtNode(cr, "value", null, null); | |
122 | |
123 label.setTextContent(river.getName()); | |
124 value.setTextContent(river.getName()); | |
125 | |
126 item.appendChild(label); | |
127 item.appendChild(value); | |
128 | |
129 return item; | |
130 } | |
131 | |
132 | |
133 @Override | |
134 public boolean validate(Artifact artifact) | |
135 throws IllegalArgumentException | |
136 { | |
137 logger.debug("RiverSelect.validate"); | |
138 | |
139 FLYSArtifact flys = (FLYSArtifact) artifact; | |
140 | |
141 StateData dRiver = getData(flys, "river"); | |
142 | |
143 if (dRiver == null || dRiver.getValue() == null) { | |
144 throw new IllegalArgumentException(ERROR_NO_RIVER_SELECTED); | |
145 } | |
146 | |
147 River river = RiverFactory.getRiver((String) dRiver.getValue()); | |
148 | |
149 if (river == null) { | |
150 throw new IllegalArgumentException(ERROR_NO_SUCH_RIVER); | |
151 } | |
152 | |
153 return true; | |
154 } | |
155 | |
156 | |
157 @Override | |
158 protected String getUIProvider() { | |
159 return "river_panel"; | |
160 } | |
161 } | |
162 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |