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