JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / models / annotations / ChemProbAnnotation.java
1 package fr.orsay.lri.varna.models.annotations;
2
3 import java.awt.Color;
4 import java.awt.geom.Point2D;
5 import java.io.Serializable;
6
7 import javax.xml.transform.sax.TransformerHandler;
8
9 import org.xml.sax.SAXException;
10 import org.xml.sax.helpers.AttributesImpl;
11
12 import fr.orsay.lri.varna.models.rna.ModeleBase;
13 import fr.orsay.lri.varna.utils.XMLUtils;
14
15
16 public class ChemProbAnnotation implements Serializable {
17
18         public static final String HEADER_TEXT = "ChemProbAnnotation";
19
20         /**
21          * 
22          */
23         private static final long serialVersionUID = 5833315460145031242L;
24
25
26         public enum ChemProbAnnotationType 
27         {
28                 TRIANGLE,
29                 ARROW,
30                 PIN,
31                 DOT;
32         };
33         
34         public static double DEFAULT_INTENSITY = 1.0;
35         public static ChemProbAnnotationType DEFAULT_TYPE = ChemProbAnnotationType.ARROW;
36         public static Color DEFAULT_COLOR = Color.blue.darker();
37
38         private ModeleBase _mbfst;
39         private ModeleBase _mbsnd;
40         private Color _color;
41         private double _intensity;
42         private ChemProbAnnotationType _type;
43         private boolean _outward;
44         
45         public static String XML_ELEMENT_NAME = "ChemProbAnnotation";
46         public static String XML_VAR_INDEX5_NAME = "Index5";
47         public static String XML_VAR_INDEX3_NAME = "Index3";
48         public static String XML_VAR_COLOR_NAME = "Color";
49         public static String XML_VAR_INTENSITY_NAME = "Intensity";
50         public static String XML_VAR_TYPE_NAME = "Type";
51         public static String XML_VAR_OUTWARD_NAME = "Outward";
52
53         public void toXML(TransformerHandler hd) throws SAXException
54         {
55                 AttributesImpl atts = new AttributesImpl();
56                 atts.addAttribute("","",XML_VAR_INDEX5_NAME,"CDATA",""+_mbfst.getIndex());
57                 atts.addAttribute("","",XML_VAR_INDEX3_NAME,"CDATA",""+_mbsnd.getIndex());
58                 atts.addAttribute("","",XML_VAR_COLOR_NAME,"CDATA",XMLUtils.toHTMLNotation(_color));
59                 atts.addAttribute("","",XML_VAR_INTENSITY_NAME,"CDATA",""+_intensity);
60                 atts.addAttribute("","",XML_VAR_TYPE_NAME,"CDATA",""+_type);
61                 atts.addAttribute("","",XML_VAR_OUTWARD_NAME,"CDATA",""+_outward);
62                 hd.startElement("","",XML_ELEMENT_NAME,atts);
63                 hd.endElement("","",XML_ELEMENT_NAME);
64         }
65
66         
67         public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, String styleDesc) {
68                 this(mbfst,mbsnd);
69                 applyStyle(styleDesc);
70         }
71
72         public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd) {
73                 this(mbfst,mbsnd,ChemProbAnnotation.DEFAULT_TYPE,ChemProbAnnotation.DEFAULT_INTENSITY);
74         }
75
76         public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, double intensity) {
77                 this(mbfst,mbsnd,ChemProbAnnotation.DEFAULT_TYPE,intensity);
78         }
79
80         public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, ChemProbAnnotationType type) {
81                 this(mbfst,mbsnd,type,ChemProbAnnotation.DEFAULT_INTENSITY);
82         }
83         
84         public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, ChemProbAnnotationType type, double intensity) {
85                 this(mbfst,mbsnd, type, intensity, DEFAULT_COLOR, true);
86         }
87
88         public ChemProbAnnotation(ModeleBase mbfst, ModeleBase mbsnd, ChemProbAnnotationType type, double intensity, Color color, boolean out) {
89                 if (mbfst.getIndex()>mbsnd.getIndex())
90                 {
91                         ModeleBase tmp = mbsnd;
92                         mbsnd = mbfst;
93                         mbfst = tmp;
94                 }
95                 _mbfst = mbfst;
96                 _mbsnd = mbsnd;
97                 _type = type;
98                 _intensity = intensity;
99                 _color = color;
100                 _outward = out;
101         }
102
103         public boolean isOut()
104         {
105                 return _outward; 
106         }
107         
108         public void setOut(boolean b)
109         {
110                 _outward = b;
111         }
112
113         public Color getColor()
114         {
115                 return _color; 
116         }
117
118         public double getIntensity()
119         {
120                 return _intensity; 
121         }
122         
123         public ChemProbAnnotationType getType()
124         {
125                 return _type; 
126         }
127         
128         public void setColor(Color c){
129                 _color = c;
130         }
131
132         public void setIntensity(double d){
133                 _intensity = d;
134         }
135         
136         public Point2D.Double getAnchorPosition()
137         {
138                 Point2D.Double result = new Point2D.Double(
139                                 (_mbfst.getCoords().x+_mbsnd.getCoords().x)/2.0,
140                                 (_mbfst.getCoords().y+_mbsnd.getCoords().y)/2.0);
141                 return result;
142         }
143         
144         public Point2D.Double getDirVector()
145         {
146                 Point2D.Double norm = getNormalVector();
147                 Point2D.Double result = new Point2D.Double(-norm.y,norm.x);
148                 Point2D.Double anchor = getAnchorPosition();
149                 Point2D.Double center = new Point2D.Double(
150                                 (_mbfst.getCenter().x+_mbsnd.getCenter().x)/2.0,
151                                 (_mbfst.getCenter().y+_mbsnd.getCenter().y)/2.0);
152                 Point2D.Double vradius = new Point2D.Double(
153                                 (center.x-anchor.x)/2.0,
154                                 (center.y-anchor.y)/2.0);
155                 if (_outward)
156                 {
157                   if (result.x*vradius.x+result.y*vradius.y>0)
158                   {
159                         return new Point2D.Double(-result.x,-result.y);
160                   }
161                 }
162                 else
163                 {
164                           if (result.x*vradius.x+result.y*vradius.y<0)
165                           {
166                                 return new Point2D.Double(-result.x,-result.y);
167                           }                     
168                 }
169                 return result;          
170         }
171         public Point2D.Double getNormalVector()
172         {
173                 Point2D.Double tmp;
174                 if (_mbfst==_mbsnd)
175                 {
176                         tmp = new Point2D.Double(
177                                         (-(_mbsnd.getCenter().y-_mbsnd.getCoords().y)),
178                                         ((_mbsnd.getCenter().x-_mbsnd.getCoords().x)));                 
179                 }
180                 else
181                 {
182                         tmp = new Point2D.Double(
183                                 (_mbsnd.getCoords().x-_mbfst.getCoords().x)/2.0,
184                                 (_mbsnd.getCoords().y-_mbfst.getCoords().y)/2.0);
185                 }
186                 
187                 double norm = tmp.distance(0, 0);
188                 Point2D.Double result = new Point2D.Double(tmp.x/norm,tmp.y/norm);
189                 return result;                          
190         }
191         
192         public static ChemProbAnnotationType annotTypeFromString(String value)
193         {
194                 if (value.toLowerCase().equals("arrow"))
195                 {return ChemProbAnnotationType.ARROW;}
196                 else if (value.toLowerCase().equals("triangle"))
197                 {return ChemProbAnnotationType.TRIANGLE;}
198                 else if (value.toLowerCase().equals("pin"))
199                 {return ChemProbAnnotationType.PIN;}
200                 else if (value.toLowerCase().equals("dot"))
201                 {return ChemProbAnnotationType.DOT;}
202                 else
203                 {return ChemProbAnnotationType.ARROW;}
204         }
205         
206         
207         public void applyStyle(String styleDesc)
208         {
209                 String[] chemProbs = styleDesc.split(",");
210                 for (int i = 0; i < chemProbs.length; i++) {
211                         String thisStyle = chemProbs[i];
212                         String[] data = thisStyle.split("=");
213                         if (data.length==2)
214                         {
215                                 String name = data[0];
216                                 String value = data[1];
217                                 if (name.toLowerCase().equals("color"))
218                                 {
219                                         Color c = Color.decode(value);
220                                         if (c==null)
221                                         { c = _color; }
222                                         setColor(c);
223                                 }
224                                 else if (name.toLowerCase().equals("intensity"))
225                                 {
226                                         _intensity = Double.parseDouble(value);
227                                 }
228                                 else if (name.toLowerCase().equals("dir"))
229                                 {
230                                         _outward = value.toLowerCase().equals("out");
231                                 }
232                                 else if (name.toLowerCase().equals("glyph"))
233                                 {
234                                         _type= annotTypeFromString(value);
235                                 }
236                         }
237                 }
238         }
239         
240         public void setType(ChemProbAnnotationType s)
241         {
242                 _type = s;
243         }
244
245         public ChemProbAnnotation clone()
246         {
247                 ChemProbAnnotation result = new ChemProbAnnotation(this._mbfst,this._mbsnd);
248                 result._intensity = _intensity;
249                 result._type = _type;
250                 result._color= _color;
251                 result._outward = _outward;
252                 return result;
253         }
254         
255         public String toString()
256         {
257                 return "Chem. prob. "+this._type+" Base#"+this._mbfst.getBaseNumber()+"-"+this._mbsnd.getBaseNumber();
258         }
259         
260 }