merge from 2_4_Release branch
[jalview.git] / src / jalview / binding / JalviewUserColours.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 JalviewUserColours.
30  * 
31  * @version $Revision$ $Date$
32  */
33 public class JalviewUserColours implements java.io.Serializable
34 {
35
36   // --------------------------/
37   // - Class/Member Variables -/
38   // --------------------------/
39
40   /**
41    * Field _schemeName.
42    */
43   private java.lang.String _schemeName;
44
45   /**
46    * Field _colourList.
47    */
48   private java.util.Vector _colourList;
49
50   // ----------------/
51   // - Constructors -/
52   // ----------------/
53
54   public JalviewUserColours()
55   {
56     super();
57     this._colourList = new java.util.Vector();
58   }
59
60   // -----------/
61   // - Methods -/
62   // -----------/
63
64   /**
65    * 
66    * 
67    * @param vColour
68    * @throws java.lang.IndexOutOfBoundsException
69    *                 if the index given is outside the bounds of the collection
70    */
71   public void addColour(final Colour vColour)
72           throws java.lang.IndexOutOfBoundsException
73   {
74     this._colourList.addElement(vColour);
75   }
76
77   /**
78    * 
79    * 
80    * @param index
81    * @param vColour
82    * @throws java.lang.IndexOutOfBoundsException
83    *                 if the index given is outside the bounds of the collection
84    */
85   public void addColour(final int index, final Colour vColour)
86           throws java.lang.IndexOutOfBoundsException
87   {
88     this._colourList.add(index, vColour);
89   }
90
91   /**
92    * Method enumerateColour.
93    * 
94    * @return an Enumeration over all Colour elements
95    */
96   public java.util.Enumeration enumerateColour()
97   {
98     return this._colourList.elements();
99   }
100
101   /**
102    * Method getColour.
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 Colour at the given index
108    */
109   public Colour getColour(final int index)
110           throws java.lang.IndexOutOfBoundsException
111   {
112     // check bounds for index
113     if (index < 0 || index >= this._colourList.size())
114     {
115       throw new IndexOutOfBoundsException("getColour: Index value '"
116               + index + "' not in range [0.."
117               + (this._colourList.size() - 1) + "]");
118     }
119
120     return (Colour) _colourList.get(index);
121   }
122
123   /**
124    * Method getColour.Returns the contents of the collection in an Array.
125    * <p>
126    * Note: Just in case the collection contents are changing in another thread,
127    * we pass a 0-length Array of the correct type into the API call. This way we
128    * <i>know</i> that the Array returned is of exactly the correct length.
129    * 
130    * @return this collection as an Array
131    */
132   public Colour[] getColour()
133   {
134     Colour[] array = new Colour[0];
135     return (Colour[]) this._colourList.toArray(array);
136   }
137
138   /**
139    * Method getColourCount.
140    * 
141    * @return the size of this collection
142    */
143   public int getColourCount()
144   {
145     return this._colourList.size();
146   }
147
148   /**
149    * Returns the value of field 'schemeName'.
150    * 
151    * @return the value of field 'SchemeName'.
152    */
153   public java.lang.String getSchemeName()
154   {
155     return this._schemeName;
156   }
157
158   /**
159    * Method isValid.
160    * 
161    * @return true if this object is valid according to the schema
162    */
163   public boolean isValid()
164   {
165     try
166     {
167       validate();
168     } catch (org.exolab.castor.xml.ValidationException vex)
169     {
170       return false;
171     }
172     return true;
173   }
174
175   /**
176    * 
177    * 
178    * @param out
179    * @throws org.exolab.castor.xml.MarshalException
180    *                 if object is null or if any SAXException is thrown during
181    *                 marshaling
182    * @throws org.exolab.castor.xml.ValidationException
183    *                 if this object is an invalid instance according to the
184    *                 schema
185    */
186   public void marshal(final java.io.Writer out)
187           throws org.exolab.castor.xml.MarshalException,
188           org.exolab.castor.xml.ValidationException
189   {
190     Marshaller.marshal(this, out);
191   }
192
193   /**
194    * 
195    * 
196    * @param handler
197    * @throws java.io.IOException
198    *                 if an IOException occurs during marshaling
199    * @throws org.exolab.castor.xml.ValidationException
200    *                 if this object is an invalid instance according to the
201    *                 schema
202    * @throws org.exolab.castor.xml.MarshalException
203    *                 if object is null or if any SAXException is thrown during
204    *                 marshaling
205    */
206   public void marshal(final org.xml.sax.ContentHandler handler)
207           throws java.io.IOException,
208           org.exolab.castor.xml.MarshalException,
209           org.exolab.castor.xml.ValidationException
210   {
211     Marshaller.marshal(this, handler);
212   }
213
214   /**
215    */
216   public void removeAllColour()
217   {
218     this._colourList.clear();
219   }
220
221   /**
222    * Method removeColour.
223    * 
224    * @param vColour
225    * @return true if the object was removed from the collection.
226    */
227   public boolean removeColour(final Colour vColour)
228   {
229     boolean removed = _colourList.remove(vColour);
230     return removed;
231   }
232
233   /**
234    * Method removeColourAt.
235    * 
236    * @param index
237    * @return the element removed from the collection
238    */
239   public Colour removeColourAt(final int index)
240   {
241     java.lang.Object obj = this._colourList.remove(index);
242     return (Colour) obj;
243   }
244
245   /**
246    * 
247    * 
248    * @param index
249    * @param vColour
250    * @throws java.lang.IndexOutOfBoundsException
251    *                 if the index given is outside the bounds of the collection
252    */
253   public void setColour(final int index, final Colour vColour)
254           throws java.lang.IndexOutOfBoundsException
255   {
256     // check bounds for index
257     if (index < 0 || index >= this._colourList.size())
258     {
259       throw new IndexOutOfBoundsException("setColour: Index value '"
260               + index + "' not in range [0.."
261               + (this._colourList.size() - 1) + "]");
262     }
263
264     this._colourList.set(index, vColour);
265   }
266
267   /**
268    * 
269    * 
270    * @param vColourArray
271    */
272   public void setColour(final Colour[] vColourArray)
273   {
274     // -- copy array
275     _colourList.clear();
276
277     for (int i = 0; i < vColourArray.length; i++)
278     {
279       this._colourList.add(vColourArray[i]);
280     }
281   }
282
283   /**
284    * Sets the value of field 'schemeName'.
285    * 
286    * @param schemeName
287    *                the value of field 'schemeName'.
288    */
289   public void setSchemeName(final java.lang.String schemeName)
290   {
291     this._schemeName = schemeName;
292   }
293
294   /**
295    * Method unmarshal.
296    * 
297    * @param reader
298    * @throws org.exolab.castor.xml.MarshalException
299    *                 if object is null or if any SAXException is thrown during
300    *                 marshaling
301    * @throws org.exolab.castor.xml.ValidationException
302    *                 if this object is an invalid instance according to the
303    *                 schema
304    * @return the unmarshaled jalview.binding.JalviewUserColours
305    */
306   public static jalview.binding.JalviewUserColours unmarshal(
307           final java.io.Reader reader)
308           throws org.exolab.castor.xml.MarshalException,
309           org.exolab.castor.xml.ValidationException
310   {
311     return (jalview.binding.JalviewUserColours) Unmarshaller.unmarshal(
312             jalview.binding.JalviewUserColours.class, reader);
313   }
314
315   /**
316    * 
317    * 
318    * @throws org.exolab.castor.xml.ValidationException
319    *                 if this object is an invalid instance according to the
320    *                 schema
321    */
322   public void validate() throws org.exolab.castor.xml.ValidationException
323   {
324     org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
325     validator.validate(this);
326   }
327
328 }