e58099d4590b93fdab82f0e5a10aeefd6a7c966d
[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       Vector allGroups = ap.getAlignment().getGroups();
57       SequenceGroup sg;
58       for (int g = 0; g < allGroups.size(); g++)
59       {
60         sg = (SequenceGroup) allGroups.get(g);
61         if (sg.cs != null)
62         {
63           oldgroupColours.put(sg, sg.cs);
64         }
65       }
66     }
67     this.av = av;
68     this.ap = ap;
69
70     if (oldcs instanceof RNAHelicesColour)
71     {
72       RNAHelicesColour rhc = (RNAHelicesColour) oldcs;
73
74     }
75
76     adjusting = true;
77     Vector list = new Vector();
78     int index = 1;
79     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
80     {
81       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
82       if (!list.contains(label))
83         list.addElement(label);
84       else
85         list.addElement(label + "_" + (index++));
86     }
87
88     adjusting = false;
89
90     changeColour();
91
92   }
93
94   void changeColour()
95   {
96     // Check if combobox is still adjusting
97     if (adjusting)
98     {
99       return;
100     }
101
102     currentAnnotation = av.getAlignment().getAlignmentAnnotation()[0];// annotations.getSelectedIndex()];
103
104     RNAHelicesColour rhc = null;
105
106     rhc = new RNAHelicesColour(currentAnnotation);
107    
108     av.setGlobalColourScheme(rhc);
109
110     if (av.getAlignment().getGroups() != null)
111     {
112       Vector allGroups = ap.getAlignment().getGroups();
113       SequenceGroup sg;
114       for (int g = 0; g < allGroups.size(); g++)
115       {
116         sg = (SequenceGroup) allGroups.get(g);
117
118         if (sg.cs == null)
119         {
120           continue;
121         }
122
123         sg.cs = new RNAHelicesColour(currentAnnotation);
124
125       }
126     }
127
128     ap.paintAlignment(false);
129   }
130
131   void reset()
132   {
133     av.setGlobalColourScheme(oldcs);
134     if (av.getAlignment().getGroups() != null)
135     {
136       Vector allGroups = ap.getAlignment().getGroups();
137       SequenceGroup sg;
138       for (int g = 0; g < allGroups.size(); g++)
139       {
140         sg = (SequenceGroup) allGroups.get(g);
141         sg.cs = (ColourSchemeI) oldgroupColours.get(sg);
142       }
143     }
144   }
145
146   public void annotations_actionPerformed(ActionEvent e)
147   {
148     changeColour();
149   }
150
151 }