001    package edu.nrao.sss.webapp.faces.conf;
002    
003    import edu.nrao.sss.util.JaxbUtility;
004    
005    import javax.xml.bind.JAXBException;
006    import javax.xml.bind.annotation.XmlAttribute;
007    import javax.xml.bind.annotation.XmlElement;
008    import javax.xml.bind.annotation.XmlRootElement;
009    import javax.xml.stream.XMLStreamException;
010    
011    import java.io.FileNotFoundException;
012    import java.io.Reader;
013    
014    import java.util.List;
015    import java.util.ArrayList;
016    
017    @XmlRootElement(name="selector-configuration")
018    public class SelectorConfiguration
019    {
020            private List<RegionMap> regionMaps;
021    
022            /** creates a new empty SelectorConfiguration */
023            public SelectorConfiguration()
024            {
025                    this.regionMaps = new ArrayList<RegionMap>();
026            }
027    
028            /**
029             * @return our list of available RegionMaps. This should never return null.
030             */
031            @XmlElement(name="region-map")
032            public List<RegionMap> getRegionMaps()
033            {
034                    return this.regionMaps;
035            }
036    
037            /**
038             * set our list of available RegionMaps. if <code>maps</code> is null, an
039             * empty List is saved instead.
040             */
041            public void setRegionMaps(List<RegionMap> maps)
042            {
043                    if (maps == null)
044                            this.regionMaps = new ArrayList<RegionMap>();
045    
046                    else
047                            this.regionMaps = maps;
048            }
049    
050            /**
051             * @return RegionMap with matching <code>name</code>. null if none found.
052             */
053            public RegionMap getRegionMap(String name)
054            {
055                    if (name != null)
056                    {
057                            for (RegionMap m : getRegionMaps())
058                            {
059                                    if (name.equals(m.getName()))
060                                            return m;
061                            }
062                    }
063    
064                    //else
065                    return null;
066            }
067    
068            /**
069             * creates a SelectorConfiguration from the xml file specified with
070             * <code>xmlFile</code>.
071             */
072            public static SelectorConfiguration fromXml(String xmlFile) throws JAXBException, XMLStreamException, FileNotFoundException
073            {
074                    return JaxbUtility.getSharedInstance().xmlFileToObject(xmlFile, SelectorConfiguration.class);
075            }
076    
077            /**
078             * Creates a new SelectorConfiguration based on the XML data read from {@code reader}.
079             * 
080             * @param reader the source of the XML data.
081             *               If this value is <i>null</i>, <i>null</i> is returned.
082             *               
083             * @return a new SelectorConfiguration based on the XML data read from {@code reader}.
084             * 
085             * @throws XMLStreamException if the XML is not well-formed,
086             *           or for some other "unexpected processing conditions".
087             *           
088             * @throws JAXBException if anything else goes wrong during the
089             *           transformation.
090             */
091            public static SelectorConfiguration fromXml(Reader reader)
092                    throws JAXBException, XMLStreamException
093            {
094                    return JaxbUtility.getSharedInstance().readObjectAsXmlFrom(reader, SelectorConfiguration.class, null);
095            }
096    
097            /**
098             * @return an xml representation of this configuration.
099             */
100            public String toXml() throws JAXBException
101            {
102                    return JaxbUtility.getSharedInstance().objectToXmlString(this);
103            }
104    
105            /**
106             * quick xml to/from test
107             */
108            public static void main(String[] a)
109            {
110                    try
111                    {
112                            if (a.length < 1)
113                            {
114                                    SelectorConfiguration conf = new SelectorConfiguration();
115    
116                                    Region r = new Region();
117                                    r.setX(5);
118                                    r.setY(5);
119                                    r.setWidth(15);
120                                    r.setHeight(15);
121    
122                                    List<Region> regs = new ArrayList<Region>();
123                                    regs.add(r);
124    
125                                    r = r.clone();
126                                    r.setX(290);
127                                    regs.add(r);
128    
129                                    RegionMap m = new RegionMap();
130                                    m.setRegions(regs);
131    
132                                    List<RegionMap> maps = new ArrayList<RegionMap>();
133                                    maps.add(m);
134    
135                                    conf.setRegionMaps(maps);
136    
137                                    System.out.println(conf.toXml());
138                            }
139    
140                            else
141                            {
142                                    SelectorConfiguration c = SelectorConfiguration.fromXml(a[0]);
143                                    System.out.println(c);
144                            }
145                    }
146    
147                    catch (Exception e)
148                    {
149                            e.printStackTrace();
150                            System.exit(1);
151                    }
152            }
153    
154    
155            public static class RegionMap
156            {
157                    private String name;
158                    private List<Region> regions;
159    
160                    public RegionMap()
161                    {
162                            this.name = "";
163                            this.regions = new ArrayList<Region>();
164                    }
165    
166                    /**
167                     * @return the name of this RegionMap. This should never return null.
168                     */
169                    @XmlAttribute(required=true)
170                    public String getName()
171                    {
172                            return this.name;
173                    }
174    
175                    /**
176                     * Sets the name of this RegionMap. If <code>n</code> is null, the name
177                     * is set to the empty string.
178                     */
179                    public void setName(String n)
180                    {
181                            if (n == null)
182                                    this.name = "";
183    
184                            else
185                                    this.name = n;
186                    }
187    
188                    /**
189                     * @return The Regions held in this RegionMap. This should never return
190                     * null.
191                     */
192                    @XmlElement(name="region")
193                    public List<Region> getRegions()
194                    {
195                            return this.regions;
196                    }
197    
198                    /**
199                     * sets the Regions held in this RegionMap. If the list is null, the
200                     * regions list is set to an empty List.
201                     */
202                    public void setRegions(List<Region> list)
203                    {
204                            if (list == null)
205                                    this.regions = new ArrayList<Region>();
206    
207                            else
208                                    this.regions = list;
209                    }
210    
211                    /** adds r to our region list */
212                    public void addRegion(Region r)
213                    {
214                            this.regions.add(r);
215                    }
216    
217                    /**
218                     * Removes r from the list.
219                     * @return true if an element was actually removed.
220                     */
221                    public boolean removeRegion(Region r)
222                    {
223                            return this.regions.remove(r);
224                    }
225    
226                    /**
227                     * Removes the region at <code>index</code> from the list and returns it.
228                     */
229                    public Region removeRegion(int index)
230                    {
231                            return this.regions.remove(index);
232                    }
233            }
234    
235            public static class Region implements Cloneable
236            {
237                    private int x,y,w,h;
238                    private String name;
239    
240                    public Region()
241                    {
242                            x = y = w = h = 0;
243                            name = "";
244                    }
245    
246                    @XmlAttribute(required=true)
247                    public String getName()
248                    {
249                            return this.name;
250                    }
251    
252                    public void setName(String n)
253                    {
254                            this.name = n;
255                    }
256    
257                    @XmlAttribute(required=true)
258                    public int getX()
259                    {
260                            return this.x;
261                    }
262    
263                    public void setX(int x)
264                    {
265                            this.x = x;
266                    }
267    
268                    @XmlAttribute(required=true)
269                    public int getY()
270                    {
271                            return this.y;
272                    }
273    
274                    public void setY(int y)
275                    {
276                            this.y = y;
277                    }
278    
279                    @XmlAttribute(required=true)
280                    public int getWidth()
281                    {
282                            return this.w;
283                    }
284    
285                    public void setWidth(int w)
286                    {
287                            this.w = w;
288                    }
289    
290                    @XmlAttribute(required=true)
291                    public int getHeight()
292                    {
293                            return this.h;
294                    }
295    
296                    public void setHeight(int h)
297                    {
298                            this.h = h;
299                    }
300    
301                    public String toString()
302                    {
303                            return "(" + x + ", " + y + ") " + w + " x " + h;
304                    }
305    
306                    public Region clone()
307                    {
308                            try
309                            {
310                                    return (Region)super.clone();
311                            }
312                            catch(CloneNotSupportedException e)
313                            {
314                                    throw new RuntimeException(e);
315                            }
316                    }
317            }
318    }