b6c3640b3e00c2150a51f1d97e86daffee3d1c51
[jalview.git] / src / jalview / binding / FeatureSettings.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.binding;
20
21 // ---------------------------------/
22 // - Imported classes and packages -/
23 // ---------------------------------/
24
25 import org.exolab.castor.xml.Marshaller;
26 import org.exolab.castor.xml.Unmarshaller;
27
28 /**
29  * Class FeatureSettings.
30  * 
31  * @version $Revision$ $Date$
32  */
33 public class FeatureSettings implements java.io.Serializable
34 {
35
36   // --------------------------/
37   // - Class/Member Variables -/
38   // --------------------------/
39
40   /**
41    * Field _settingList.
42    */
43   private java.util.Vector _settingList;
44
45   // ----------------/
46   // - Constructors -/
47   // ----------------/
48
49   public FeatureSettings()
50   {
51     super();
52     this._settingList = new java.util.Vector();
53   }
54
55   // -----------/
56   // - Methods -/
57   // -----------/
58
59   /**
60    * 
61    * 
62    * @param vSetting
63    * @throws java.lang.IndexOutOfBoundsException
64    *                 if the index given is outside the bounds of the collection
65    */
66   public void addSetting(final jalview.binding.Setting vSetting)
67           throws java.lang.IndexOutOfBoundsException
68   {
69     this._settingList.addElement(vSetting);
70   }
71
72   /**
73    * 
74    * 
75    * @param index
76    * @param vSetting
77    * @throws java.lang.IndexOutOfBoundsException
78    *                 if the index given is outside the bounds of the collection
79    */
80   public void addSetting(final int index,
81           final jalview.binding.Setting vSetting)
82           throws java.lang.IndexOutOfBoundsException
83   {
84     this._settingList.add(index, vSetting);
85   }
86
87   /**
88    * Method enumerateSetting.
89    * 
90    * @return an Enumeration over all jalview.binding.Setting elements
91    */
92   public java.util.Enumeration enumerateSetting()
93   {
94     return this._settingList.elements();
95   }
96
97   /**
98    * Method getSetting.
99    * 
100    * @param index
101    * @throws java.lang.IndexOutOfBoundsException
102    *                 if the index given is outside the bounds of the collection
103    * @return the value of the jalview.binding.Setting at the given index
104    */
105   public jalview.binding.Setting getSetting(final int index)
106           throws java.lang.IndexOutOfBoundsException
107   {
108     // check bounds for index
109     if (index < 0 || index >= this._settingList.size())
110     {
111       throw new IndexOutOfBoundsException("getSetting: Index value '"
112               + index + "' not in range [0.."
113               + (this._settingList.size() - 1) + "]");
114     }
115
116     return (jalview.binding.Setting) _settingList.get(index);
117   }
118
119   /**
120    * Method getSetting.Returns the contents of the collection in an Array.
121    * <p>
122    * Note: Just in case the collection contents are changing in another thread,
123    * we pass a 0-length Array of the correct type into the API call. This way we
124    * <i>know</i> that the Array returned is of exactly the correct length.
125    * 
126    * @return this collection as an Array
127    */
128   public jalview.binding.Setting[] getSetting()
129   {
130     jalview.binding.Setting[] array = new jalview.binding.Setting[0];
131     return (jalview.binding.Setting[]) this._settingList.toArray(array);
132   }
133
134   /**
135    * Method getSettingCount.
136    * 
137    * @return the size of this collection
138    */
139   public int getSettingCount()
140   {
141     return this._settingList.size();
142   }
143
144   /**
145    * Method isValid.
146    * 
147    * @return true if this object is valid according to the schema
148    */
149   public boolean isValid()
150   {
151     try
152     {
153       validate();
154     } catch (org.exolab.castor.xml.ValidationException vex)
155     {
156       return false;
157     }
158     return true;
159   }
160
161   /**
162    * 
163    * 
164    * @param out
165    * @throws org.exolab.castor.xml.MarshalException
166    *                 if object is null or if any SAXException is thrown during
167    *                 marshaling
168    * @throws org.exolab.castor.xml.ValidationException
169    *                 if this object is an invalid instance according to the
170    *                 schema
171    */
172   public void marshal(final java.io.Writer out)
173           throws org.exolab.castor.xml.MarshalException,
174           org.exolab.castor.xml.ValidationException
175   {
176     Marshaller.marshal(this, out);
177   }
178
179   /**
180    * 
181    * 
182    * @param handler
183    * @throws java.io.IOException
184    *                 if an IOException occurs during marshaling
185    * @throws org.exolab.castor.xml.ValidationException
186    *                 if this object is an invalid instance according to the
187    *                 schema
188    * @throws org.exolab.castor.xml.MarshalException
189    *                 if object is null or if any SAXException is thrown during
190    *                 marshaling
191    */
192   public void marshal(final org.xml.sax.ContentHandler handler)
193           throws java.io.IOException,
194           org.exolab.castor.xml.MarshalException,
195           org.exolab.castor.xml.ValidationException
196   {
197     Marshaller.marshal(this, handler);
198   }
199
200   /**
201    */
202   public void removeAllSetting()
203   {
204     this._settingList.clear();
205   }
206
207   /**
208    * Method removeSetting.
209    * 
210    * @param vSetting
211    * @return true if the object was removed from the collection.
212    */
213   public boolean removeSetting(final jalview.binding.Setting vSetting)
214   {
215     boolean removed = _settingList.remove(vSetting);
216     return removed;
217   }
218
219   /**
220    * Method removeSettingAt.
221    * 
222    * @param index
223    * @return the element removed from the collection
224    */
225   public jalview.binding.Setting removeSettingAt(final int index)
226   {
227     java.lang.Object obj = this._settingList.remove(index);
228     return (jalview.binding.Setting) obj;
229   }
230
231   /**
232    * 
233    * 
234    * @param index
235    * @param vSetting
236    * @throws java.lang.IndexOutOfBoundsException
237    *                 if the index given is outside the bounds of the collection
238    */
239   public void setSetting(final int index,
240           final jalview.binding.Setting vSetting)
241           throws java.lang.IndexOutOfBoundsException
242   {
243     // check bounds for index
244     if (index < 0 || index >= this._settingList.size())
245     {
246       throw new IndexOutOfBoundsException("setSetting: Index value '"
247               + index + "' not in range [0.."
248               + (this._settingList.size() - 1) + "]");
249     }
250
251     this._settingList.set(index, vSetting);
252   }
253
254   /**
255    * 
256    * 
257    * @param vSettingArray
258    */
259   public void setSetting(final jalview.binding.Setting[] vSettingArray)
260   {
261     // -- copy array
262     _settingList.clear();
263
264     for (int i = 0; i < vSettingArray.length; i++)
265     {
266       this._settingList.add(vSettingArray[i]);
267     }
268   }
269
270   /**
271    * Method unmarshal.
272    * 
273    * @param reader
274    * @throws org.exolab.castor.xml.MarshalException
275    *                 if object is null or if any SAXException is thrown during
276    *                 marshaling
277    * @throws org.exolab.castor.xml.ValidationException
278    *                 if this object is an invalid instance according to the
279    *                 schema
280    * @return the unmarshaled jalview.binding.FeatureSettings
281    */
282   public static jalview.binding.FeatureSettings unmarshal(
283           final java.io.Reader reader)
284           throws org.exolab.castor.xml.MarshalException,
285           org.exolab.castor.xml.ValidationException
286   {
287     return (jalview.binding.FeatureSettings) Unmarshaller.unmarshal(
288             jalview.binding.FeatureSettings.class, reader);
289   }
290
291   /**
292    * 
293    * 
294    * @throws org.exolab.castor.xml.ValidationException
295    *                 if this object is an invalid instance according to the
296    *                 schema
297    */
298   public void validate() throws org.exolab.castor.xml.ValidationException
299   {
300     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
301     validator.validate(this);
302   }
303
304 }