Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.core / src / main / java / es / unex / sextante / parameters / ParameterTableField.java @ 315

History | View | Annotate | Download (6.89 KB)

1
package es.unex.sextante.parameters;
2

    
3
import java.awt.geom.Point2D;
4
import java.io.IOException;
5
import java.util.ArrayList;
6

    
7
import org.kxml2.io.KXmlParser;
8
import org.kxml2.io.KXmlSerializer;
9
import org.xmlpull.v1.XmlPullParser;
10
import org.xmlpull.v1.XmlPullParserException;
11

    
12
import es.unex.sextante.additionalInfo.AdditionalInfo;
13
import es.unex.sextante.additionalInfo.AdditionalInfoTableField;
14
import es.unex.sextante.dataObjects.I3DRasterLayer;
15
import es.unex.sextante.dataObjects.IRasterLayer;
16
import es.unex.sextante.dataObjects.ITable;
17
import es.unex.sextante.dataObjects.IVectorLayer;
18
import es.unex.sextante.exceptions.NullParameterAdditionalInfoException;
19
import es.unex.sextante.exceptions.NullParameterValueException;
20
import es.unex.sextante.exceptions.WrongParameterTypeException;
21

    
22
/**
23
 * A parameter representing a field in a table or in the attributes table of a vector layer
24
 * 
25
 * @author volaya
26
 * 
27
 */
28
public class ParameterTableField
29
         extends
30
            Parameter {
31

    
32
   private static final String PARENT    = "parent";
33
   private static final String MANDATORY = "mandatory";
34
    private String fieldName;
35

    
36

    
37
   @Override
38
   public String getParameterTypeName() {
39

    
40
      return "Table Field";
41

    
42
   }
43

    
44

    
45
   @Override
46
   public IRasterLayer getParameterValueAsRasterLayer() throws WrongParameterTypeException {
47

    
48
      throw new WrongParameterTypeException();
49

    
50
   }
51

    
52

    
53
   @Override
54
   public I3DRasterLayer getParameterValueAs3DRasterLayer() throws WrongParameterTypeException {
55

    
56
      throw new WrongParameterTypeException();
57

    
58
   }
59

    
60

    
61
   @Override
62
   public ITable getParameterValueAsTable() throws WrongParameterTypeException {
63

    
64
      throw new WrongParameterTypeException();
65

    
66
   }
67

    
68

    
69
   @Override
70
   public IVectorLayer getParameterValueAsVectorLayer() throws WrongParameterTypeException {
71

    
72
      throw new WrongParameterTypeException();
73

    
74
   }
75

    
76

    
77
   public ArrayList getParameterValueAsMultipleRasterLayer() throws WrongParameterTypeException {
78

    
79
      throw new WrongParameterTypeException();
80

    
81
   }
82

    
83

    
84
   public ArrayList getParameterValueAsMultipleVectorLayer() throws WrongParameterTypeException {
85

    
86
      throw new WrongParameterTypeException();
87

    
88
   }
89

    
90

    
91
   public ArrayList getParameterValueAsMultipleTable() throws WrongParameterTypeException {
92

    
93
      throw new WrongParameterTypeException();
94

    
95
   }
96

    
97

    
98
   @Override
99
   public int getParameterValueAsInt() throws WrongParameterTypeException, NullParameterValueException {
100

    
101
      if (m_ParameterValue != null) {
102
         return ((Integer) m_ParameterValue).intValue();
103
      }
104
      throw new NullParameterValueException();
105

    
106
   }
107

    
108

    
109
   @Override
110
   public double getParameterValueAsDouble() throws WrongParameterTypeException {
111

    
112
      throw new WrongParameterTypeException();
113

    
114
   }
115

    
116

    
117
   @Override
118
   public boolean getParameterValueAsBoolean() throws WrongParameterTypeException {
119

    
120
      throw new WrongParameterTypeException();
121

    
122
   }
123

    
124

    
125
   @Override
126
   public String getParameterValueAsString() throws WrongParameterTypeException, NullParameterValueException {
127

    
128
      if (fieldName != null) {
129
         return fieldName;
130
      }
131

    
132
      return null;
133

    
134
   }
135

    
136

    
137
   @Override
138
   public Point2D getParameterValueAsPoint() throws WrongParameterTypeException {
139

    
140
      throw new WrongParameterTypeException();
141

    
142
   }
143

    
144

    
145
   @Override
146
   public boolean setParameterAdditionalInfo(final AdditionalInfo additionalInfo) {
147

    
148
      if (additionalInfo instanceof AdditionalInfoTableField) {
149
         m_ParameterAdditionalInfo = additionalInfo;
150
         return true;
151
      }
152
      else {
153
         return false;
154
      }
155

    
156
   }
157

    
158

    
159
   @Override
160
   public boolean setParameterValue(final Object value) {
161

    
162
      if (value instanceof Integer) {
163
         m_ParameterValue = value;
164
         return true;
165
      }
166
      if (value instanceof String) {
167
          this.fieldName = (String) value;
168
      }
169
      return false;
170
   }
171

    
172

    
173
   public boolean setParameterValue(final int iValue) {
174

    
175
      final Integer Value = new Integer(iValue);
176
      m_ParameterValue = Value;
177
      return true;
178

    
179
   }
180

    
181

    
182
   @Override
183
   public Class getParameterClass() {
184

    
185
      return Integer.class;
186

    
187
   }
188

    
189

    
190
   @Override
191
   protected void serializeAttributes(final KXmlSerializer serializer) throws NullParameterAdditionalInfoException, IOException {
192

    
193
      final AdditionalInfoTableField aitf = (AdditionalInfoTableField) m_ParameterAdditionalInfo;
194
      if (aitf != null) {
195
         serializer.text("\n");
196
         serializer.text("\t\t\t");
197
         serializer.startTag(null, ATTRIBUTE);
198
         serializer.attribute(null, NAME, PARENT);
199
         serializer.attribute(null, VALUE, aitf.getParentParameterName());
200
         serializer.endTag(null, ATTRIBUTE);
201
         serializer.text("\n");
202
         serializer.text("\t\t\t");
203
         serializer.startTag(null, ATTRIBUTE);
204
         serializer.attribute(null, NAME, MANDATORY);
205
         serializer.attribute(null, VALUE, Boolean.valueOf(aitf.getIsMandatory()).toString());
206
         serializer.endTag(null, ATTRIBUTE);
207
      }
208
      else {
209
         throw new NullParameterAdditionalInfoException();
210
      }
211

    
212
   }
213

    
214

    
215
   public static Parameter deserialize(final KXmlParser parser) throws XmlPullParserException, IOException {
216

    
217
      String sParent = null;
218
      boolean bMandatory = true;
219

    
220
      int tag = parser.nextTag();
221

    
222
      boolean bOver = false;
223
      while (!bOver) {
224
         switch (tag) {
225
            case XmlPullParser.START_TAG:
226
               if (parser.getName().compareTo(ATTRIBUTE) == 0) {
227
                  final String sName = parser.getAttributeValue("", NAME);
228
                  if (sName.compareTo(PARENT) == 0) {
229
                     sParent = parser.getAttributeValue("", VALUE);
230
                  }
231
                  else if (sName.compareTo(PARENT) == 0) {
232
                     final String sMandatory = parser.getAttributeValue("", VALUE);
233
                     bMandatory = Boolean.parseBoolean(sMandatory);
234
                  }
235
               }
236
               break;
237
            case XmlPullParser.END_TAG:
238
               if (parser.getName().compareTo(INPUT) == 0) {
239
                  bOver = true;
240
               }
241
               break;
242
            case XmlPullParser.TEXT:
243
               break;
244
         }
245

    
246
         if (!bOver) {
247
            tag = parser.next();
248
         }
249

    
250
      }
251

    
252
      final ParameterTableField param = new ParameterTableField();
253
      final AdditionalInfoTableField ai = new AdditionalInfoTableField(sParent, bMandatory);
254
      param.setParameterAdditionalInfo(ai);
255

    
256
      return param;
257

    
258
   }
259

    
260

    
261
   @Override
262
   public String getCommandLineParameter() {
263

    
264
      final Integer i = (Integer) m_ParameterValue;
265

    
266
      return "\"" + i.toString() + "\"";
267

    
268
   }
269

    
270

    
271
   @Override
272
   public boolean isParameterValueCorrect() {
273

    
274
      return m_ParameterValue != null;
275

    
276
   }
277

    
278
}