6b16a05b3208262c3b62859f83b81ea67c60609a
[jalview.git] / src2 / fr / orsay / lri / varna / models / rna / ModeleBackboneElement.java
1 package fr.orsay.lri.varna.models.rna;
2
3 import java.awt.Color;
4 import java.io.Serializable;
5
6 import javax.xml.transform.sax.TransformerHandler;
7
8 import org.xml.sax.SAXException;
9 import org.xml.sax.helpers.AttributesImpl;
10
11 import fr.orsay.lri.varna.utils.XMLUtils;
12
13 public class ModeleBackboneElement implements Serializable{
14
15         /**
16          * 
17          */
18         private static final long serialVersionUID = -614968737102943216L;
19
20         public ModeleBackboneElement(int index, BackboneType t)
21         {
22                 _index=index;
23                 if (t==BackboneType.CUSTOM_COLOR)
24                 {
25                         throw new IllegalArgumentException("Error: Missing Color while constructing Backbone");
26                 }
27                 _type=t;
28         }
29         
30         public ModeleBackboneElement(int index, Color c)
31         {
32                 _index=index;
33                 _type=BackboneType.CUSTOM_COLOR;
34                 _color = c;
35         }
36         
37         public enum BackboneType{
38                 SOLID_TYPE ("solid"),
39                 DISCONTINUOUS_TYPE ("discontinuous"),
40                 MISSING_PART_TYPE ("missing"),
41                 CUSTOM_COLOR ("custom");
42                 
43                 private String label;
44                 
45                 BackboneType(String s)
46                 {
47                         label = s;
48                 }
49                 
50                 public String getLabel()
51                 {
52                         return label;
53                 }
54                 
55                 
56                 public static BackboneType getType(String lbl)
57                 {
58                         BackboneType[] vals = BackboneType.values();
59                         for(int i=0;i<vals.length;i++)
60                         {
61                                 if (vals[i].equals(lbl))
62                                         return vals[i];
63                         }
64                         return null;
65                 }
66                 
67         };
68         
69         private BackboneType _type;
70         private Color _color = null;
71         private int _index;
72
73         
74         public BackboneType getType()
75         {
76                 return _type;
77         }
78         
79         public int getIndex()
80         {
81                 return _index;
82         }
83         
84         public Color getColor()
85         {
86                 return _color;
87         }
88         
89         public static String XML_ELEMENT_NAME = "BackboneElement";
90         public static String XML_VAR_INDEX_NAME = "index";
91         public static String XML_VAR_TYPE_NAME = "type";
92         public static String XML_VAR_COLOR_NAME = "color";      
93         
94         public void toXML(TransformerHandler hd) throws SAXException
95         {
96                 AttributesImpl atts = new AttributesImpl();
97                 atts.addAttribute("","",XML_VAR_INDEX_NAME,"CDATA",""+_index);
98                 atts.addAttribute("","",XML_VAR_TYPE_NAME,"CDATA",""+_type.getLabel());
99                 if(_type==BackboneType.CUSTOM_COLOR){
100                         atts.addAttribute("","",XML_VAR_COLOR_NAME,"CDATA",""+XMLUtils.toHTMLNotation(_color)); 
101                 }
102                 hd.startElement("","",XML_ELEMENT_NAME,atts);
103                 hd.endElement("","",XML_ELEMENT_NAME);
104         }
105
106 }