cd00fcf37d927613a543e2d063c2024ad7223f80
[jalview.git] / src / jalview / appletgui / AnnotationRowFilter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.schemes.AnnotationColourGradient;
24 import jalview.util.MessageManager;
25
26 import java.awt.Button;
27 import java.awt.Checkbox;
28 import java.awt.Choice;
29 import java.awt.Frame;
30 import java.awt.Panel;
31 import java.awt.Scrollbar;
32 import java.awt.TextField;
33 import java.awt.event.ActionEvent;
34 import java.util.Vector;
35
36 @SuppressWarnings("serial")
37 public abstract class AnnotationRowFilter extends Panel
38 {
39   protected AlignViewport av;
40
41   protected AlignmentPanel ap;
42
43   protected int[] annmap;
44
45   protected boolean enableSeqAss = false;
46
47   private jalview.datamodel.AlignmentAnnotation currentAnnotation;
48
49   protected boolean adjusting = false;
50
51   protected Checkbox currentColours = new Checkbox();
52
53   protected Panel minColour = new Panel();
54
55   protected Panel maxColour = new Panel();
56
57   protected Checkbox seqAssociated = new Checkbox();
58
59   protected Checkbox thresholdIsMin = new Checkbox();
60
61   protected Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
62
63   protected TextField thresholdValue = new TextField(20);
64
65   protected Frame frame;
66
67   protected Button ok = new Button();
68
69   protected Button cancel = new Button();
70
71   /**
72    * enabled if the user is dragging the slider - try to keep updates to a
73    * minimun
74    */
75   protected boolean sliderDragging = false;
76
77   public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
78   {
79     this.av = av;
80     this.ap = ap;
81   }
82
83   public AnnotationRowFilter()
84   {
85
86   }
87
88   public Vector getAnnotationItems(boolean isSeqAssociated)
89   {
90     Vector list = new Vector();
91     int index = 1;
92     int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
93     for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
94     {
95       if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
96       {
97         if (isSeqAssociated)
98         {
99           continue;
100         }
101       }
102       else
103       {
104         enableSeqAss = true;
105       }
106       String label = av.getAlignment().getAlignmentAnnotation()[i].label;
107       // add associated sequence ID if available
108       if (!isSeqAssociated
109               && av.getAlignment().getAlignmentAnnotation()[i].sequenceRef != null)
110       {
111         label = label
112                 + "_"
113                 + av.getAlignment().getAlignmentAnnotation()[i].sequenceRef
114                         .getName();
115       }
116       // make label unique
117       if (!list.contains(label))
118       {
119         anmap[list.size()] = i;
120         list.add(label);
121
122       }
123       else
124       {
125         if (!isSeqAssociated)
126         {
127           anmap[list.size()] = i;
128           list.add(label + "_" + (index++));
129         }
130       }
131     }
132     this.annmap = new int[list.size()];
133     System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
134     return list;
135   }
136
137   protected int getSelectedThresholdItem(int indexValue)
138   {
139     int selectedThresholdItem = -1;
140     if (indexValue == 1)
141     {
142       selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
143     }
144     else if (indexValue == 2)
145     {
146       selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
147     }
148     return selectedThresholdItem;
149   }
150
151   public void modelChanged()
152   {
153     seqAssociated.setEnabled(enableSeqAss);
154   }
155
156   public void ok_actionPerformed(ActionEvent e)
157   {
158     updateView();
159     frame.setVisible(false);
160   }
161
162   public void cancel_actionPerformed(ActionEvent e)
163   {
164     reset();
165     ap.paintAlignment(true);
166     frame.setVisible(false);
167   }
168
169   public void thresholdCheck_actionPerformed(ActionEvent e)
170   {
171     updateView();
172   }
173
174   public void annotations_actionPerformed(ActionEvent e)
175   {
176     updateView();
177   }
178
179   public void threshold_actionPerformed(ActionEvent e)
180   {
181     updateView();
182   }
183
184   public void thresholdValue_actionPerformed(ActionEvent e)
185   {
186     try
187     {
188       float f = Float.parseFloat(thresholdValue.getText());
189       slider.setValue((int) (f * 1000));
190       updateView();
191     } catch (NumberFormatException ex)
192     {
193     }
194   }
195
196   protected void populateThresholdComboBox(Choice threshold)
197   {
198     threshold.addItem(MessageManager
199             .getString("label.threshold_feature_no_threshold"));
200     threshold.addItem(MessageManager
201             .getString("label.threshold_feature_above_threshold"));
202     threshold.addItem(MessageManager
203             .getString("label.threshold_feature_below_threshold"));
204   }
205
206   public jalview.datamodel.AlignmentAnnotation getCurrentAnnotation()
207   {
208     return currentAnnotation;
209   }
210
211   public void setCurrentAnnotation(
212           jalview.datamodel.AlignmentAnnotation currentAnnotation)
213   {
214     this.currentAnnotation = currentAnnotation;
215   }
216
217   public abstract void valueChanged(boolean updateAllAnnotation);
218
219   public abstract void updateView();
220
221   public abstract void reset();
222 }