Refactored AnnotationColumnChoser and AnnotationColorChooser to apply the DRY principle
[jalview.git] / src / jalview / gui / AnnotationColumnChooser.java
1 package jalview.gui;
2
3 import jalview.schemes.AnnotationColourGradient;
4 import jalview.util.MessageManager;
5
6 import java.awt.BorderLayout;
7 import java.awt.Color;
8 import java.awt.Dimension;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11
12 import javax.swing.JButton;
13 import javax.swing.JComboBox;
14 import javax.swing.JInternalFrame;
15 import javax.swing.JLayeredPane;
16 import javax.swing.JPanel;
17
18 import net.miginfocom.swing.MigLayout;
19
20 @SuppressWarnings("serial")
21 public class AnnotationColumnChooser extends AnnotationRowFilter
22 {
23
24   private JComboBox<String> annotations;
25
26   JButton ok = new JButton();
27
28   JButton cancel = new JButton();
29
30   JPanel jPanel1 = new JPanel();
31
32   JPanel jPanel2 = new JPanel();
33
34   BorderLayout borderLayout1 = new BorderLayout();
35
36   private JComboBox<String> threshold = new JComboBox<String>();
37
38
39   public AnnotationColumnChooser(AlignViewport av, final AlignmentPanel ap)
40   {
41     super(av, ap);
42     frame = new JInternalFrame();
43     frame.setContentPane(this);
44     frame.setLayer(JLayeredPane.PALETTE_LAYER);
45     Desktop.addInternalFrame(frame, "Select By Annotation", 520, 215);
46
47     addSliderChangeListener();
48     addSliderMouseListeners();
49
50     if (av.getAlignment().getAlignmentAnnotation() == null)
51     {
52       return;
53     }
54
55     adjusting = true;
56
57     setAnnotations(new JComboBox<String>(
58             getAnnotationItems(seqAssociated.isSelected())));
59     populateThresholdComboBox(threshold);
60
61     if (av.getCurrentAnnotationColumnSelectionState() != null)
62     {
63       annotations.setSelectedIndex(av
64               .getCurrentAnnotationColumnSelectionState().getAnnotations()
65               .getSelectedIndex());
66       threshold.setSelectedIndex(av
67               .getCurrentAnnotationColumnSelectionState().getThreshold()
68               .getSelectedIndex());
69     }
70
71     try
72     {
73       jbInit();
74     } catch (Exception ex)
75     {
76     }
77     adjusting = false;
78
79     updateView();
80     frame.invalidate();
81     frame.pack();
82   }
83
84
85   public AnnotationColumnChooser()
86   {
87     try
88     {
89       jbInit();
90     } catch (Exception ex)
91     {
92       ex.printStackTrace();
93     }
94   }
95
96   private void jbInit() throws Exception
97   {
98     ok.setOpaque(false);
99     ok.setText(MessageManager.getString("action.ok"));
100     ok.addActionListener(new ActionListener()
101     {
102       @Override
103       public void actionPerformed(ActionEvent e)
104       {
105         ok_actionPerformed(e);
106       }
107     });
108     cancel.setOpaque(false);
109     cancel.setText(MessageManager.getString("action.cancel"));
110     cancel.addActionListener(new ActionListener()
111     {
112       @Override
113       public void actionPerformed(ActionEvent e)
114       {
115         cancel_actionPerformed(e);
116       }
117     });
118
119     getAnnotations().addActionListener(new ActionListener()
120     {
121       @Override
122       public void actionPerformed(ActionEvent e)
123       {
124         annotations_actionPerformed(e);
125       }
126     });
127     getThreshold().addActionListener(new ActionListener()
128     {
129       @Override
130       public void actionPerformed(ActionEvent e)
131       {
132         threshold_actionPerformed(e);
133       }
134     });
135     thresholdValue.addActionListener(new ActionListener()
136     {
137       @Override
138       public void actionPerformed(ActionEvent e)
139       {
140         thresholdValue_actionPerformed(e);
141       }
142     });
143     slider.setPaintLabels(false);
144     slider.setPaintTicks(true);
145     slider.setBackground(Color.white);
146     slider.setEnabled(false);
147     slider.setOpaque(false);
148     slider.setPreferredSize(new Dimension(100, 32));
149     thresholdValue.setEnabled(false);
150     thresholdValue.setColumns(7);
151     thresholdIsMin.setBackground(Color.white);
152     thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
153     thresholdIsMin.setText(MessageManager
154             .getString("label.threshold_minmax"));
155     thresholdIsMin.addActionListener(new ActionListener()
156     {
157       @Override
158       public void actionPerformed(ActionEvent actionEvent)
159       {
160         thresholdIsMin_actionPerformed(actionEvent);
161       }
162     });
163     seqAssociated.setBackground(Color.white);
164     seqAssociated.setFont(JvSwingUtils.getLabelFont());
165     seqAssociated.setText(MessageManager
166             .getString("label.per_sequence_only"));
167     seqAssociated.addActionListener(new ActionListener()
168     {
169
170       @Override
171       public void actionPerformed(ActionEvent arg0)
172       {
173         seqAssociated_actionPerformed(arg0, annotations, seqAssociated);
174       }
175     });
176
177     this.setLayout(borderLayout1);
178     jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
179     jPanel1.setBackground(Color.white);
180     jPanel2.setBackground(Color.white);
181
182     jPanel1.add(ok);
183     jPanel1.add(cancel);
184     jPanel2.add(getAnnotations(), "grow, wrap");
185     jPanel2.add(seqAssociated, "wrap");
186     jPanel2.add(getThreshold(), "grow, wrap");
187     jPanel2.add(thresholdIsMin, "wrap");
188     jPanel2.add(slider, "grow");
189     jPanel2.add(thresholdValue, "grow");
190     this.add(jPanel1, java.awt.BorderLayout.SOUTH);
191     this.add(jPanel2, java.awt.BorderLayout.CENTER);
192     this.validate();
193   }
194
195
196   public void reset()
197   {
198     av.getColumnSelection().clear();
199   }
200
201   public void valueChanged(boolean updateAllAnnotation)
202   {
203     getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
204     updateView();
205     propagateSeqAssociatedThreshold(updateAllAnnotation,
206             getCurrentAnnotation());
207     ap.paintAlignment(false);
208   }
209
210   public JComboBox<String> getThreshold()
211   {
212     return threshold;
213   }
214
215   public void setThreshold(JComboBox<String> threshold)
216   {
217     this.threshold = threshold;
218   }
219
220   public JComboBox<String> getAnnotations()
221   {
222     return annotations;
223   }
224
225   public void setAnnotations(JComboBox<String> annotations)
226   {
227     this.annotations = annotations;
228   }
229
230   @Override
231   public void updateView()
232   {
233     changeColumnSelection();
234   }
235   void changeColumnSelection()
236   {
237     // Check if combobox is still adjusting
238     if (adjusting)
239     {
240       return;
241     }
242
243     setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
244             .getSelectedIndex()]]);
245
246
247     int selectedThresholdItem = getSelectedThresholdItem(getThreshold()
248             .getSelectedIndex());
249
250     slider.setEnabled(true);
251     thresholdValue.setEnabled(true);
252     thresholdIsMin.setEnabled(true);
253
254     if (selectedThresholdItem == AnnotationColourGradient.NO_THRESHOLD)
255     {
256       slider.setEnabled(false);
257       thresholdValue.setEnabled(false);
258       thresholdValue.setText("");
259       thresholdIsMin.setEnabled(false);
260     }
261     else if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD
262             && getCurrentAnnotation().threshold == null)
263     {
264       getCurrentAnnotation()
265               .setThreshold(new jalview.datamodel.GraphLine(
266                               (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
267                       "Threshold", Color.black));
268     }
269
270     if (selectedThresholdItem != AnnotationColourGradient.NO_THRESHOLD)
271     {
272       adjusting = true;
273       float range = getCurrentAnnotation().graphMax * 1000
274               - getCurrentAnnotation().graphMin * 1000;
275
276       slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
277       slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
278       slider.setValue((int) (getCurrentAnnotation().threshold.value * 1000));
279       thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
280       slider.setMajorTickSpacing((int) (range / 10f));
281       slider.setEnabled(true);
282       thresholdValue.setEnabled(true);
283       adjusting = false;
284     }
285
286     markColumnsContaining(getCurrentAnnotation(), selectedThresholdItem);
287     av.setCurrentAnnotationColumnSelectionState(this);
288     ap.alignmentChanged();
289     ap.paintAlignment(true);
290   }
291 }