Merge branch 'releases/Release_2_11_3_Branch'
[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
35 @SuppressWarnings("serial")
36 public abstract class AnnotationRowFilter extends Panel
37 {
38   protected AlignViewport av;
39
40   protected AlignmentPanel ap;
41
42   protected int[] annmap;
43
44   protected boolean enableSeqAss = false;
45
46   private jalview.datamodel.AlignmentAnnotation currentAnnotation;
47
48   protected boolean adjusting = false;
49
50   protected Checkbox currentColours = new Checkbox();
51
52   protected Panel minColour = new Panel();
53
54   protected Panel maxColour = new Panel();
55
56   protected Checkbox seqAssociated = new Checkbox();
57
58   protected Checkbox thresholdIsMin = new Checkbox();
59
60   protected Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
61
62   protected Checkbox percentThreshold = new Checkbox();
63
64   protected TextField thresholdValue = new TextField(20);
65
66   protected Frame frame;
67
68   protected Button ok = new Button();
69
70   protected Button cancel = new Button();
71
72   /**
73    * enabled if the user is dragging the slider - try to keep updates to a
74    * minimun
75    */
76   protected boolean sliderDragging = false;
77
78   public AnnotationRowFilter(AlignViewport av, final AlignmentPanel ap)
79   {
80     this.av = av;
81     this.ap = ap;
82   }
83
84   public AnnotationRowFilter()
85   {
86
87   }
88
89   protected int getSelectedThresholdItem(int indexValue)
90   {
91     int selectedThresholdItem = -1;
92     if (indexValue == 1)
93     {
94       selectedThresholdItem = AnnotationColourGradient.ABOVE_THRESHOLD;
95     }
96     else if (indexValue == 2)
97     {
98       selectedThresholdItem = AnnotationColourGradient.BELOW_THRESHOLD;
99     }
100     return selectedThresholdItem;
101   }
102
103   public void modelChanged()
104   {
105     seqAssociated.setEnabled(enableSeqAss);
106   }
107
108   public void ok_actionPerformed(ActionEvent e)
109   {
110     updateView();
111     frame.setVisible(false);
112   }
113
114   public void cancel_actionPerformed(ActionEvent e)
115   {
116     reset();
117     ap.paintAlignment(true, true);
118     frame.setVisible(false);
119   }
120
121   public void thresholdCheck_actionPerformed(ActionEvent e)
122   {
123     updateView();
124   }
125
126   public void annotations_actionPerformed(ActionEvent e)
127   {
128     updateView();
129   }
130
131   public void threshold_actionPerformed(ActionEvent e)
132   {
133     updateView();
134   }
135
136   /**
137    * update the text field from the threshold slider. preserves state of
138    * 'adjusting' so safe to call in init.
139    */
140   protected void setThresholdValueText()
141   {
142     boolean oldadj = adjusting;
143     adjusting = true;
144     if (percentThreshold.getState())
145     {
146       double scl = slider.getMaximum() - slider.getMinimum();
147       scl = (slider.getValue() - slider.getMinimum()) / scl;
148       thresholdValue.setText(100f * scl + "");
149     }
150     else
151     {
152       thresholdValue.setText((slider.getValue() / 1000f) + "");
153     }
154     thresholdValue.setCaretPosition(0);
155     adjusting = oldadj;
156   }
157
158   public void thresholdValue_actionPerformed(ActionEvent e)
159   {
160     try
161     {
162       float f = Float.parseFloat(thresholdValue.getText());
163       if (percentThreshold.getState())
164       {
165         int pos = slider.getMinimum()
166                 + (int) ((slider.getMaximum() - slider.getMinimum()) * f
167                         / 100f);
168         slider.setValue(pos);
169       }
170       else
171       {
172         slider.setValue((int) (f * 1000));
173       }
174       valueChanged(false);
175     } catch (NumberFormatException ex)
176     {
177     }
178   }
179
180   protected void percentageValue_actionPerformed()
181   {
182     setThresholdValueText();
183   }
184
185   protected void populateThresholdComboBox(Choice threshold)
186   {
187     threshold.addItem(MessageManager
188             .getString("label.threshold_feature_no_threshold"));
189     threshold.addItem(MessageManager
190             .getString("label.threshold_feature_above_threshold"));
191     threshold.addItem(MessageManager
192             .getString("label.threshold_feature_below_threshold"));
193   }
194
195   public jalview.datamodel.AlignmentAnnotation getCurrentAnnotation()
196   {
197     return currentAnnotation;
198   }
199
200   public void setCurrentAnnotation(
201           jalview.datamodel.AlignmentAnnotation currentAnnotation)
202   {
203     this.currentAnnotation = currentAnnotation;
204   }
205
206   public abstract void valueChanged(boolean updateAllAnnotation);
207
208   public abstract void updateView();
209
210   public abstract void reset();
211 }