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