allows for null Jmol viewer
[jalview.git] / src2 / fr / orsay / lri / varna / models / BaseList.java
1 package fr.orsay.lri.varna.models;
2
3 import java.awt.Color;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashSet;
7
8 import fr.orsay.lri.varna.models.rna.ModeleBase;
9
10 public class BaseList {
11         private HashSet<ModeleBase> _bases = new HashSet<ModeleBase>(); 
12         private String _caption;
13
14         public BaseList( BaseList b)
15         {
16                 _caption = b._caption;
17                 _bases = new HashSet<ModeleBase>(b._bases);
18         }
19
20         
21         public BaseList( String caption)
22         {
23                 _caption = caption;
24         }
25         
26         
27         public BaseList( String caption, ModeleBase mb)
28         {
29                 this(caption);
30                 addBase(mb);
31         }
32
33         public boolean contains(ModeleBase mb)
34         {
35                 return _bases.contains(mb);
36         }
37
38         
39         public String getCaption()
40         {
41                 return _caption;
42         }
43         
44         public void addBase(ModeleBase b)
45         {
46                 _bases.add(b);
47         }
48
49         public void removeBase(ModeleBase b)
50         {
51                 _bases.remove(b);
52         }
53
54         
55         public void addBases(Collection<? extends ModeleBase> mbs)
56         {
57                 _bases.addAll(mbs);
58         }
59
60         public ArrayList<ModeleBase> getBases()
61         {
62                 return new ArrayList<ModeleBase>(_bases);
63         }
64
65         public void clear()
66         {
67                 _bases.clear();
68         }
69
70         public static Color getAverageColor(ArrayList<Color> cols)
71         {
72                 int r=0,g=0,b=0;
73                 for (Color c : cols)
74                 {
75                         r += c.getRed();
76                         g += c.getGreen();
77                         b += c.getBlue();
78                 }
79                 if (cols.size()>0)
80                 { 
81                         r /= cols.size();
82                         g /= cols.size();
83                         b /= cols.size();
84                 }
85                 return new Color(r,g,b);
86         }
87         
88         public Color getAverageOutlineColor()
89         {
90                 ArrayList<Color> cols = new ArrayList<Color>(); 
91                 for (ModeleBase mb : _bases)
92                 {  cols.add(mb.getStyleBase().getBaseOutlineColor()); }
93                 return getAverageColor(cols);
94         }
95
96         public Color getAverageNameColor()
97         {
98                 ArrayList<Color> cols = new ArrayList<Color>(); 
99                 for (ModeleBase mb : _bases)
100                 {  cols.add(mb.getStyleBase().getBaseNameColor()); }
101                 return getAverageColor(cols);
102         }
103
104         public Color getAverageNumberColor()
105         {
106                 ArrayList<Color> cols = new ArrayList<Color>(); 
107                 for (ModeleBase mb : _bases)
108                 {  cols.add(mb.getStyleBase().getBaseNumberColor()); }
109                 return getAverageColor(cols);
110         }
111
112         public Color getAverageInnerColor()
113         {
114                 ArrayList<Color> cols = new ArrayList<Color>(); 
115                 for (ModeleBase mb : _bases)
116                 {  cols.add(mb.getStyleBase().getBaseInnerColor()); }
117                 return getAverageColor(cols);
118         }
119
120         public String getNumbers()
121         {
122                 String result = ""; 
123                 boolean first = true;
124                 for (ModeleBase mb:_bases)
125                 {  
126                         if (!first)
127                         { result += ","; }
128                         else
129                         { first = false; }
130                         result += "" + mb.getBaseNumber(); 
131                 }
132                 result += "";
133                 return result;
134         }
135
136         public String getContents()
137         {
138                 String result = ""; 
139                 boolean first = true;
140                 for (ModeleBase mb:_bases)
141                 {  
142                         if (!first)
143                         { result += ","; }
144                         else
145                         { first = false; }
146                         result += "" + mb.getContent(); 
147                 }
148                 result += "";
149                 return result;
150         }
151
152         public ArrayList<Integer> getIndices()
153         {
154                 ArrayList<Integer> indices = new ArrayList<Integer>();
155                 for (ModeleBase mb : _bases)
156                 {
157                         indices.add(mb.getIndex());
158                 }
159                 return indices;
160         }
161         
162         /**
163          * Returns, in a new BaseList, the intersection of the current BaseList and of the argument.
164          * @param mb The base list to be used for the intersection
165          * @return The intersection of the current base list and the argument.
166          */
167         
168         public BaseList retainAll(BaseList mb)
169         {
170                 HashSet<ModeleBase> cp = new HashSet<ModeleBase>();
171                 cp.addAll(_bases);
172                 cp.retainAll(mb._bases);
173                 BaseList result = new BaseList("TmpIntersection");
174                 result.addBases(cp);
175                 return result;
176         }
177
178         /**
179          * Returns, in a new BaseList, the list consisting of the current BaseList minus the list passed as argument.
180          * @param mb The base list to be subtracted from the current one
181          * @return The current base list minus the list passed as argument.
182          */
183         
184         public BaseList removeAll(BaseList mb)
185         {
186                 HashSet<ModeleBase> cp = new HashSet<ModeleBase>();
187                 cp.addAll(_bases);
188                 cp.removeAll(mb._bases);
189                 BaseList result = new BaseList("TmpMinus");
190                 result.addBases(cp);
191                 return result;
192         }       
193         
194         public int size()
195         {
196                 return _bases.size();
197         }
198
199         
200 }