JAL-1933 JAL-2432 percentile filter for select/hide columns by annotation in applet
[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 Checkbox percentThreshold = new Checkbox();
64
65   protected TextField thresholdValue = new TextField(20);
66
67   protected Frame frame;
68
69   protected Button ok = new Button();
70
71   protected Button cancel = new Button();
72
73   /**
74    * enabled if the user is dragging the slider - try to keep updates to a
75    * minimun
76    */
77   protected boolean sliderDragging = false;
78
79   public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
80   {
81     this.av = av;
82     this.ap = ap;
83   }
84
85   public AnnotationRowFilter()
86   {
87
88   }
89
90   protected int getSelectedThresholdItem(int indexValue)
91   {
92     int selectedThresholdItem = -1;
93     if (indexValue == 1)
94     {
95       selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
96     }
97     else if (indexValue == 2)
98     {
99       selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
100     }
101     return selectedThresholdItem;
102   }
103
104   public void modelChanged()
105   {
106     seqAssociated.setEnabled(enableSeqAss);
107   }
108
109   public void ok_actionPerformed(ActionEvent e)
110   {
111     updateView();
112     frame.setVisible(false);
113   }
114
115   public void cancel_actionPerformed(ActionEvent e)
116   {
117     reset();
118     ap.paintAlignment(true);
119     frame.setVisible(false);
120   }
121
122   public void thresholdCheck_actionPerformed(ActionEvent e)
123   {
124     updateView();
125   }
126
127   public void annotations_actionPerformed(ActionEvent e)
128   {
129     updateView();
130   }
131
132   public void threshold_actionPerformed(ActionEvent e)
133   {
134     updateView();
135   }
136
137   protected void setThresholdValueText()
138   {
139     adjusting = true;
140     if (percentThreshold.getState())
141     {
142       double scl = slider.getMaximum() - slider.getMinimum();
143       scl = (slider.getValue() - slider.getMinimum()) / scl;
144       thresholdValue.setText(100 * scl + "");
145     }
146     else
147     {
148       thresholdValue.setText((slider.getValue() / 1000f) + "");
149     }
150     thresholdValue.setCaretPosition(0);
151     adjusting = false;
152   }
153   
154   public void thresholdValue_actionPerformed(ActionEvent e)
155   {
156     try
157     {
158       float f = Float.parseFloat(thresholdValue.getText());
159       if (percentThreshold.getState())
160       {
161         slider.setValue(slider.getMinimum()
162                 + ((int) ((f / 100f) * (slider.getMaximum() - slider
163                         .getMinimum()))));
164       }
165       else
166       {
167         slider.setValue((int) (f * 1000));
168       }
169       updateView();
170     } catch (NumberFormatException ex)
171     {
172     }
173   }
174
175   protected void percentageValue_actionPerformed()
176   {
177     setThresholdValueText();
178   }
179
180   protected void populateThresholdComboBox(Choice threshold)
181   {
182     threshold.addItem(MessageManager
183             .getString("label.threshold_feature_no_threshold"));
184     threshold.addItem(MessageManager
185             .getString("label.threshold_feature_above_threshold"));
186     threshold.addItem(MessageManager
187             .getString("label.threshold_feature_below_threshold"));
188   }
189
190   public jalview.datamodel.AlignmentAnnotation getCurrentAnnotation()
191   {
192     return currentAnnotation;
193   }
194
195   public void setCurrentAnnotation(
196           jalview.datamodel.AlignmentAnnotation currentAnnotation)
197   {
198     this.currentAnnotation = currentAnnotation;
199   }
200
201   public abstract void valueChanged(boolean updateAllAnnotation);
202
203   public abstract void updateView();
204
205   public abstract void reset();
206 }