3 import jalview.bin.Cache;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.Annotation;
6 import jalview.datamodel.ColumnSelection;
7 import jalview.datamodel.GraphLine;
8 import jalview.schemes.AnnotationColourGradient;
9 import jalview.schemes.ColourSchemeI;
10 import jalview.util.MessageManager;
12 import java.awt.BorderLayout;
13 import java.awt.Color;
14 import java.awt.Dimension;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.event.MouseAdapter;
18 import java.awt.event.MouseEvent;
19 import java.util.Hashtable;
20 import java.util.Vector;
22 import javax.swing.JButton;
23 import javax.swing.JCheckBox;
24 import javax.swing.JComboBox;
25 import javax.swing.JInternalFrame;
26 import javax.swing.JLayeredPane;
27 import javax.swing.JPanel;
28 import javax.swing.JSlider;
29 import javax.swing.JTextField;
30 import javax.swing.event.ChangeEvent;
31 import javax.swing.event.ChangeListener;
33 import net.miginfocom.swing.MigLayout;
35 public class AnnotationColumnSelection extends JPanel
45 Hashtable oldgroupColours;
47 private JComboBox annotations;
51 JPanel minColour = new JPanel();
53 JPanel maxColour = new JPanel();
55 JButton defColours = new JButton();
57 JButton ok = new JButton();
59 JButton cancel = new JButton();
61 JPanel jPanel1 = new JPanel();
63 JPanel jPanel2 = new JPanel();
65 BorderLayout borderLayout1 = new BorderLayout();
67 private JComboBox threshold = new JComboBox();
69 JSlider slider = new JSlider();
71 JTextField thresholdValue = new JTextField(20);
73 JCheckBox currentColours = new JCheckBox();
75 JCheckBox thresholdIsMin = new JCheckBox();
77 JCheckBox seqAssociated = new JCheckBox();
79 private jalview.datamodel.AlignmentAnnotation currentAnnotation;
81 boolean adjusting = false;
84 * enabled if the user is dragging the slider - try to keep updates to a
87 boolean sliderDragging = false;
89 public AnnotationColumnSelection(AlignViewport av, final AlignmentPanel ap)
94 frame = new JInternalFrame();
95 frame.setContentPane(this);
96 frame.setLayer(JLayeredPane.PALETTE_LAYER);
97 Desktop.addInternalFrame(frame, "Select By Annotation", 520, 215);
99 slider.addChangeListener(new ChangeListener()
102 public void stateChanged(ChangeEvent evt)
106 thresholdValue.setText((slider.getValue() / 1000f) + "");
107 valueChanged(!sliderDragging);
111 slider.addMouseListener(new MouseAdapter()
114 public void mousePressed(MouseEvent e)
116 sliderDragging = true;
117 super.mousePressed(e);
121 public void mouseDragged(MouseEvent e)
123 sliderDragging = true;
124 super.mouseDragged(e);
128 public void mouseReleased(MouseEvent evt)
132 sliderDragging = false;
135 ap.paintAlignment(true);
139 if (av.getAlignment().getAlignmentAnnotation() == null)
144 // Always get default shading from preferences.
149 setAnnotations(new JComboBox(
150 getAnnotationItems(seqAssociated.isSelected())));
152 threshold.addItem(MessageManager
153 .getString("label.threshold_feature_no_thereshold"));
154 threshold.addItem(MessageManager
155 .getString("label.threshold_feature_above_thereshold"));
156 threshold.addItem(MessageManager
157 .getString("label.threshold_feature_below_thereshold"));
159 if (av.getCurrentAnnotationColumnSelectionState() != null)
161 annotations.setSelectedIndex(av
162 .getCurrentAnnotationColumnSelectionState().getAnnotations()
163 .getSelectedIndex());
164 threshold.setSelectedIndex(av
165 .getCurrentAnnotationColumnSelectionState().getThreshold()
166 .getSelectedIndex());
167 System.out.println("selected annotation : "
168 + av.getCurrentAnnotationColumnSelectionState()
169 .getAnnotations().getSelectedIndex());
170 System.out.println("selected threshold : "
171 + av.getCurrentAnnotationColumnSelectionState()
172 .getThreshold().getSelectedIndex());
178 } catch (Exception ex)
184 changeColumnSelection();
190 private Vector<String> getAnnotationItems(boolean isSeqAssociated)
192 Vector<String> list = new Vector<String>();
194 int[] anmap = new int[av.getAlignment().getAlignmentAnnotation().length];
195 boolean enableSeqAss = false;
196 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
198 if (av.getAlignment().getAlignmentAnnotation()[i].sequenceRef == null)
209 String label = av.getAlignment().getAlignmentAnnotation()[i].label;
210 if (!list.contains(label))
212 anmap[list.size()] = i;
218 if (!isSeqAssociated)
220 anmap[list.size()] = i;
221 list.add(label + "_" + (index++));
225 seqAssociated.setEnabled(enableSeqAss);
226 this.annmap = new int[list.size()];
227 System.arraycopy(anmap, 0, this.annmap, 0, this.annmap.length);
231 private void setDefaultMinMax()
233 minColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MIN",
235 maxColour.setBackground(Cache.getDefaultColour("ANNOTATIONCOLOUR_MAX",
239 public AnnotationColumnSelection()
244 } catch (Exception ex)
246 ex.printStackTrace();
250 private void jbInit() throws Exception
253 ok.setText(MessageManager.getString("action.ok"));
254 ok.addActionListener(new ActionListener()
257 public void actionPerformed(ActionEvent e)
259 ok_actionPerformed(e);
262 cancel.setOpaque(false);
263 cancel.setText(MessageManager.getString("action.cancel"));
264 cancel.addActionListener(new ActionListener()
267 public void actionPerformed(ActionEvent e)
269 cancel_actionPerformed(e);
273 getAnnotations().addActionListener(new ActionListener()
276 public void actionPerformed(ActionEvent e)
278 annotations_actionPerformed(e);
281 getThreshold().addActionListener(new ActionListener()
284 public void actionPerformed(ActionEvent e)
286 threshold_actionPerformed(e);
289 thresholdValue.addActionListener(new ActionListener()
292 public void actionPerformed(ActionEvent e)
294 thresholdValue_actionPerformed(e);
297 slider.setPaintLabels(false);
298 slider.setPaintTicks(true);
299 slider.setBackground(Color.white);
300 slider.setEnabled(false);
301 slider.setOpaque(false);
302 slider.setPreferredSize(new Dimension(100, 32));
303 thresholdValue.setEnabled(false);
304 thresholdValue.setColumns(7);
305 thresholdIsMin.setBackground(Color.white);
306 thresholdIsMin.setFont(JvSwingUtils.getLabelFont());
307 thresholdIsMin.setText(MessageManager
308 .getString("label.threshold_minmax"));
309 thresholdIsMin.addActionListener(new ActionListener()
312 public void actionPerformed(ActionEvent actionEvent)
314 thresholdIsMin_actionPerformed(actionEvent);
317 seqAssociated.setBackground(Color.white);
318 seqAssociated.setFont(JvSwingUtils.getLabelFont());
319 seqAssociated.setText(MessageManager
320 .getString("label.per_sequence_only"));
321 seqAssociated.addActionListener(new ActionListener()
325 public void actionPerformed(ActionEvent arg0)
327 seqAssociated_actionPerformed(arg0);
331 this.setLayout(borderLayout1);
332 jPanel2.setLayout(new MigLayout("", "[left][center][right]", "[][][]"));
333 jPanel1.setBackground(Color.white);
334 jPanel2.setBackground(Color.white);
338 jPanel2.add(getAnnotations(), "grow, wrap");
339 jPanel2.add(seqAssociated, "wrap");
340 jPanel2.add(getThreshold(), "grow, wrap");
341 jPanel2.add(thresholdIsMin, "wrap");
342 jPanel2.add(slider, "grow");
343 jPanel2.add(thresholdValue, "grow");
344 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
345 this.add(jPanel2, java.awt.BorderLayout.CENTER);
349 protected void seqAssociated_actionPerformed(ActionEvent arg0)
352 String cursel = (String) getAnnotations().getSelectedItem();
353 boolean isvalid = false, isseqs = seqAssociated.isSelected();
354 this.getAnnotations().removeAllItems();
355 for (String anitem : getAnnotationItems(seqAssociated.isSelected()))
357 if (anitem.equals(cursel) || (isseqs && cursel.startsWith(anitem)))
362 this.getAnnotations().addItem(anitem);
367 this.getAnnotations().setSelectedItem(cursel);
371 if (getAnnotations().getItemCount() > 0)
373 getAnnotations().setSelectedIndex(0);
379 void changeColumnSelection()
381 // Check if combobox is still adjusting
387 setCurrentAnnotation(av.getAlignment().getAlignmentAnnotation()[annmap[getAnnotations()
388 .getSelectedIndex()]]);
390 int aboveThreshold = -1;
391 if (getThreshold().getSelectedIndex() == 1)
393 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
395 else if (getThreshold().getSelectedIndex() == 2)
397 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
400 slider.setEnabled(true);
401 thresholdValue.setEnabled(true);
402 thresholdIsMin.setEnabled(true);
404 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
406 slider.setEnabled(false);
407 thresholdValue.setEnabled(false);
408 thresholdValue.setText("");
409 thresholdIsMin.setEnabled(false);
411 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD
412 && getCurrentAnnotation().threshold == null)
414 getCurrentAnnotation()
415 .setThreshold(new jalview.datamodel.GraphLine(
416 (getCurrentAnnotation().graphMax - getCurrentAnnotation().graphMin) / 2f,
417 "Threshold", Color.black));
420 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
423 float range = getCurrentAnnotation().graphMax * 1000
424 - getCurrentAnnotation().graphMin * 1000;
426 slider.setMinimum((int) (getCurrentAnnotation().graphMin * 1000));
427 slider.setMaximum((int) (getCurrentAnnotation().graphMax * 1000));
428 slider.setValue((int) (getCurrentAnnotation().threshold.value * 1000));
429 thresholdValue.setText(getCurrentAnnotation().threshold.value + "");
430 slider.setMajorTickSpacing((int) (range / 10f));
431 slider.setEnabled(true);
432 thresholdValue.setEnabled(true);
436 markColumnsContaining(getCurrentAnnotation(), aboveThreshold);
437 av.setCurrentAnnotationColumnSelectionState(this);
438 ap.alignmentChanged();
439 // ensure all associated views (overviews, structures, etc) are notified of
441 ap.paintAlignment(true);
444 public boolean markColumnsContaining(
445 AlignmentAnnotation currentAnnotation, int thresholdComparisonType)
449 if (currentAnnotation != null)
451 Annotation[] annotations = currentAnnotation.annotations;
452 ColumnSelection cs = av.getColumnSelection();
454 if (thresholdComparisonType == AnnotationColourGradient.NO_THRESHOLD)
459 if (annotations[count] != null)
461 if (currentAnnotation.label.equals("Secondary Structure")
462 && annotations[count].secondaryStructure != ' ')
464 cs.addElement(count);
466 else if (currentAnnotation.label
467 .equals("Iron Sulphur Contacts"))
469 cs.addElement(count);
471 else if (annotations[count].value != 0.0)
473 cs.addElement(count);
478 } while (count < annotations.length);
485 if (annotations[count] != null)
487 if (thresholdComparisonType == AnnotationColourGradient.ABOVE_THRESHOLD)
489 if (annotations[count].value > currentAnnotation.threshold.value)
491 cs.addElement(count);
494 else if (thresholdComparisonType == AnnotationColourGradient.BELOW_THRESHOLD)
496 if (annotations[count].value < currentAnnotation.threshold.value)
498 cs.addElement(count);
504 } while (count < annotations.length);
509 } catch (Exception e)
516 public void ok_actionPerformed(ActionEvent e)
518 changeColumnSelection();
521 frame.setClosed(true);
522 } catch (Exception ex)
527 public void cancel_actionPerformed(ActionEvent e)
530 // ensure all original colouring is propagated to listeners.
531 ap.paintAlignment(true);
534 frame.setClosed(true);
535 } catch (Exception ex)
542 av.getColumnSelection().clear();
545 public void thresholdCheck_actionPerformed(ActionEvent e)
547 changeColumnSelection();
550 public void annotations_actionPerformed(ActionEvent e)
552 changeColumnSelection();
555 public void threshold_actionPerformed(ActionEvent e)
557 changeColumnSelection();
560 public void thresholdValue_actionPerformed(ActionEvent e)
564 float f = Float.parseFloat(thresholdValue.getText());
565 slider.setValue((int) (f * 1000));
566 changeColumnSelection();
567 } catch (NumberFormatException ex)
572 public void valueChanged(boolean updateAllAnnotation)
574 getCurrentAnnotation().threshold.value = slider.getValue() / 1000f;
575 changeColumnSelection();
576 // propagateSeqAssociatedThreshold(updateAllAnnotation);
577 ap.paintAlignment(false);
580 private void propagateSeqAssociatedThreshold(boolean allAnnotation)
582 if (getCurrentAnnotation().sequenceRef == null
583 || getCurrentAnnotation().threshold == null)
589 float thr = getCurrentAnnotation().threshold.value;
590 for (int i = 0; i < av.getAlignment().getAlignmentAnnotation().length; i++)
592 AlignmentAnnotation aa = av.getAlignment().getAlignmentAnnotation()[i];
593 if (aa.label.equals(getCurrentAnnotation().label)
594 && (getCurrentAnnotation().getCalcId() == null ? aa
595 .getCalcId() == null : getCurrentAnnotation()
597 .equals(aa.getCalcId())))
599 if (aa.threshold == null)
601 aa.threshold = new GraphLine(getCurrentAnnotation().threshold);
605 aa.threshold.value = thr;
611 public void currentColours_actionPerformed(ActionEvent e)
613 if (currentColours.isSelected())
618 maxColour.setEnabled(!currentColours.isSelected());
619 minColour.setEnabled(!currentColours.isSelected());
621 changeColumnSelection();
624 public void thresholdIsMin_actionPerformed(ActionEvent actionEvent)
626 changeColumnSelection();
629 public jalview.datamodel.AlignmentAnnotation getCurrentAnnotation()
631 return currentAnnotation;
634 public void setCurrentAnnotation(
635 jalview.datamodel.AlignmentAnnotation currentAnnotation)
637 this.currentAnnotation = currentAnnotation;
640 public JComboBox getThreshold()
645 public void setThreshold(JComboBox threshold)
647 this.threshold = threshold;
650 public JComboBox getAnnotations()
655 public void setAnnotations(JComboBox annotations)
657 this.annotations = annotations;