JAL-1114 - refactor methods handling Vectors and Hashtables to Lists and Maps, and...
[jalview.git] / src / jalview / schemes / RNAHelicesColourChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * 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  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.schemes;
19
20 import java.util.*;
21 import java.awt.event.*;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27
28 /**
29  * Helps generate the colors for RNA secondary structure. Future: add option to
30  * change colors based on covariation.
31  * 
32  * @author Lauren Michelle Lui
33  * 
34  */
35 public class RNAHelicesColourChooser
36 {
37
38   AlignViewportI av;
39
40   AlignmentViewPanel ap;
41
42   ColourSchemeI oldcs;
43
44   Hashtable oldgroupColours;
45
46   jalview.datamodel.AlignmentAnnotation currentAnnotation;
47
48   boolean adjusting = false;
49
50   public RNAHelicesColourChooser(AlignViewportI av, final AlignmentViewPanel ap)
51   {
52     oldcs = av.getGlobalColourScheme();
53     if (av.getAlignment().getGroups() != null)
54     {
55       oldgroupColours = new Hashtable();
56       for (SequenceGroup sg:ap.getAlignment().getGroups())
57       {
58         if (sg.cs != null)
59         {
60           oldgroupColours.put(sg, sg.cs);
61         }
62       }
63     }
64     this.av = av;
65     this.ap = ap;
66
67     if (oldcs instanceof RNAHelicesColour)
68     {
69       RNAHelicesColour rhc = (RNAHelicesColour) oldcs;
70
71     }
72
73     adjusting = true;
74     Vector list = new Vector();
75     int index = 1;
76     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
77     {
78       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
79       if (!list.contains(label))
80         list.addElement(label);
81       else
82         list.addElement(label + "_" + (index++));
83     }
84
85     adjusting = false;
86
87     changeColour();
88
89   }
90
91   void changeColour()
92   {
93     // Check if combobox is still adjusting
94     if (adjusting)
95     {
96       return;
97     }
98
99     currentAnnotation = av.getAlignment().getAlignmentAnnotation()[0];// annotations.getSelectedIndex()];
100
101     RNAHelicesColour rhc = null;
102
103     rhc = new RNAHelicesColour(currentAnnotation);
104    
105     av.setGlobalColourScheme(rhc);
106
107     if (av.getAlignment().getGroups() != null)
108     {
109       for (SequenceGroup sg:ap.getAlignment().getGroups())
110       {
111         if (sg.cs == null)
112         {
113           continue;
114         }
115
116         sg.cs = new RNAHelicesColour(currentAnnotation);
117
118       }
119     }
120
121     ap.paintAlignment(false);
122   }
123
124   void reset()
125   {
126     av.setGlobalColourScheme(oldcs);
127     if (av.getAlignment().getGroups() != null)
128     {
129       for (SequenceGroup sg:ap.getAlignment().getGroups())
130       {
131         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
132       }
133     }
134   }
135
136   public void annotations_actionPerformed(ActionEvent e)
137   {
138     changeColour();
139   }
140
141 }