JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / models / rna / ModelBaseStyle.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10
11  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.models.rna;
19
20 import java.awt.Color;
21 import java.awt.Font;
22 import java.io.Serializable;
23 import java.util.ArrayList;
24
25 import javax.xml.transform.sax.TransformerHandler;
26
27 import org.xml.sax.SAXException;
28 import org.xml.sax.helpers.AttributesImpl;
29
30
31 import fr.orsay.lri.varna.exceptions.ExceptionModeleStyleBaseSyntaxError;
32 import fr.orsay.lri.varna.exceptions.ExceptionParameterError;
33 import fr.orsay.lri.varna.models.VARNAConfig;
34 import fr.orsay.lri.varna.utils.XMLUtils;
35
36 /**
37  * The display Style of a rna base with the base name font, the ouline,
38  * innerline, number and name color
39  * 
40  * @author darty
41  * 
42  */
43 public class ModelBaseStyle implements Cloneable, Serializable {
44
45         /**
46          * 
47          */
48         private static final long serialVersionUID = -4331494086323517208L;
49
50         private Color _base_outline_color, _base_inner_color, _base_number_color,
51                         _base_name_color;
52
53         private boolean _selected;
54         
55         
56         public static String XML_ELEMENT_NAME = "basestyle";
57         public static String XML_VAR_OUTLINE_NAME = "outline";
58         public static String XML_VAR_INNER_NAME = "inner";
59         public static String XML_VAR_NUMBER_NAME = "num";
60         public static String XML_VAR_NAME_NAME = "name";
61
62         public void toXML(TransformerHandler hd) throws SAXException
63         {
64                 AttributesImpl atts = new AttributesImpl();
65                 atts.addAttribute("","",XML_VAR_OUTLINE_NAME,"CDATA",""+XMLUtils.toHTMLNotation(_base_outline_color));
66                 atts.addAttribute("","",XML_VAR_INNER_NAME,"CDATA",""+XMLUtils.toHTMLNotation(_base_inner_color));
67                 atts.addAttribute("","",XML_VAR_NUMBER_NAME,"CDATA",""+XMLUtils.toHTMLNotation(_base_number_color));
68                 atts.addAttribute("","",XML_VAR_NAME_NAME,"CDATA",""+XMLUtils.toHTMLNotation(_base_name_color));
69                 
70                 hd.startElement("","",XML_ELEMENT_NAME,atts);
71                 hd.endElement("","",XML_ELEMENT_NAME);
72         }       
73         
74         public ModelBaseStyle clone()
75         {
76                 ModelBaseStyle result = new ModelBaseStyle();
77                 result._base_inner_color  = this._base_inner_color;
78                 result._base_name_color  = this._base_name_color;
79                 result._base_number_color  = this._base_number_color;
80                 result._base_outline_color  = this._base_outline_color;
81                 result._selected  = this._selected;
82                 return result;
83         }
84         
85         
86         
87         /**
88          * Creates a new base style with default colors and font
89          * 
90          * @see VARNAConfig#BASE_OUTLINE_COLOR_DEFAULT
91          * @see VARNAConfig#BASE_INNER_COLOR_DEFAULT
92          * @see VARNAConfig#BASE_NUMBER_COLOR_DEFAULT
93          * @see VARNAConfig#BASE_NAME_COLOR_DEFAULT
94          * 
95          */
96         public ModelBaseStyle() {
97                 _base_outline_color = VARNAConfig.BASE_OUTLINE_COLOR_DEFAULT;
98                 _base_inner_color = VARNAConfig.BASE_INNER_COLOR_DEFAULT;
99                 _base_number_color = VARNAConfig.BASE_NUMBER_COLOR_DEFAULT;
100                 _base_name_color = VARNAConfig.BASE_NAME_COLOR_DEFAULT;
101                 _selected = false;
102         }
103
104         /**
105          * Creates a new base style with custom colors and custom font
106          * 
107          * @param outline
108          *            The out line color of the base
109          * @param inner
110          *            The inner line color of the base
111          * @param number
112          *            The number color of the base
113          * @param name
114          *            The name color of the base
115          * @param font
116          *            The name font of the base
117          */
118         public ModelBaseStyle(Color outline, Color inner, Color number,
119                         Color name, Font font) {
120                 _base_outline_color = outline;
121                 _base_inner_color = inner;
122                 _base_number_color = number;
123                 _base_name_color = name;
124         }
125
126         public ModelBaseStyle(String parameterValue)
127                         throws ExceptionModeleStyleBaseSyntaxError, ExceptionParameterError {
128                 this();
129                 assignParameters(parameterValue);
130         }
131
132         public ModelBaseStyle(ModelBaseStyle msb) {
133                 _base_outline_color = msb.getBaseOutlineColor();
134                 _base_inner_color = msb.getBaseInnerColor();
135                 _base_number_color = msb.getBaseNumberColor();
136                 _base_name_color = msb.getBaseNameColor();
137         }
138
139         public Color getBaseOutlineColor() {
140                 return _base_outline_color;
141         }
142
143         public void setBaseOutlineColor(Color _base_outline_color) {
144                 this._base_outline_color = _base_outline_color;
145         }
146
147         public Color getBaseInnerColor() {
148                 return _base_inner_color;
149         }
150
151         public void setBaseInnerColor(Color _base_inner_color) {
152                 this._base_inner_color = _base_inner_color;
153         }
154
155         public Color getBaseNumberColor() {
156                 return _base_number_color;
157         }
158
159         public void setBaseNumberColor(Color _base_numbers_color) {
160                 this._base_number_color = _base_numbers_color;
161         }
162
163         public Color getBaseNameColor() {
164                 return _base_name_color;
165         }
166
167         public void setBaseNameColor(Color _base_name_color) {
168                 this._base_name_color = _base_name_color;
169         }
170
171         public static final String PARAM_INNER_COLOR = "fill";
172         public static final String PARAM_OUTLINE_COLOR = "outline";
173         public static final String PARAM_TEXT_COLOR = "label";
174         public static final String PARAM_NUMBER_COLOR = "number";
175
176         public static Color getSafeColor(String col) {
177                 Color result;
178                 try {
179                         result = Color.decode(col);
180
181                 } catch (NumberFormatException e) {
182
183                         result = Color.getColor(col, Color.green);
184                 }
185                 return result;
186         }
187
188         public void assignParameters(String parametersValue)
189                         throws ExceptionModeleStyleBaseSyntaxError, ExceptionParameterError {
190                 if (parametersValue.equals(""))
191                         return;
192
193                 String[] parametersL = parametersValue.split(",");
194
195                 ArrayList<String> namesArray = new ArrayList<String>();
196                 ArrayList<String> valuesArray = new ArrayList<String>();
197                 String[] param;
198                 for (int i = 0; i < parametersL.length; i++) {
199                         param = parametersL[i].split("=");
200                         if (param.length != 2)
201                                 throw new ExceptionModeleStyleBaseSyntaxError(
202                                                 "Bad parameter: '" + param[0] + "' ...");
203                         namesArray.add(param[0].replace(" ", ""));
204                         valuesArray.add(param[1].replace(" ", ""));
205
206                 }
207
208                 for (int i = 0; i < namesArray.size(); i++) {
209                         if (namesArray.get(i).toLowerCase().equals(PARAM_INNER_COLOR)) {
210                                 try {
211                                         setBaseInnerColor(getSafeColor(valuesArray.get(i)));
212                                 } catch (NumberFormatException e) {
213                                         throw new ExceptionParameterError(e.getMessage(),
214                                                         "Bad inner color Syntax:" + valuesArray.get(i));
215                                 }
216                         } else if (namesArray.get(i).toLowerCase().equals(PARAM_TEXT_COLOR)) {
217                                 try {
218                                         setBaseNameColor(getSafeColor(valuesArray.get(i)));
219                                 } catch (NumberFormatException e) {
220                                         throw new ExceptionParameterError(e.getMessage(),
221                                                         "Bad name color Syntax:" + valuesArray.get(i));
222                                 }
223                         } else if (namesArray.get(i).toLowerCase().equals(
224                                         PARAM_NUMBER_COLOR)) {
225                                 try {
226                                         setBaseNumberColor(getSafeColor(valuesArray.get(i)));
227                                 } catch (NumberFormatException e) {
228                                         throw new ExceptionParameterError(e.getMessage(),
229                                                         "Bad numbers color Syntax:" + valuesArray.get(i));
230                                 }
231                         } else if (namesArray.get(i).toLowerCase().equals(
232                                         PARAM_OUTLINE_COLOR)) {
233                                 try {
234                                         setBaseOutlineColor(getSafeColor(valuesArray.get(i)));
235                                 } catch (NumberFormatException e) {
236                                         throw new ExceptionParameterError(e.getMessage(),
237                                                         "Bad outline color Syntax:" + valuesArray.get(i));
238                                 }
239                         } else
240                                 throw new ExceptionModeleStyleBaseSyntaxError(
241                                                 "Unknown parameter:" + namesArray.get(i));
242                 }
243         }
244
245         /**
246          * Find the font style integer from a string. Return <code>null</code> if
247          * the font style is unknown.
248          * 
249          * @param s
250          *            The <code>string</code> to decode
251          * @return The font style integer as <code>Font.PLAIN<code>.
252          */
253         public static Integer StyleToInteger(String s) {
254                 Integer style;
255                 if (s.toLowerCase().equals("italic"))
256                         style = Font.ITALIC;
257                 else if (s.toLowerCase().equals("bold"))
258                         style = Font.BOLD;
259                 else if (s.toLowerCase().equals("plain"))
260                         style = Font.PLAIN;
261                 else
262                         style = null;
263                 return style;
264         }
265
266 }