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