2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28 import java.awt.Rectangle;
30 public class AnnotationColourChooser
31 extends Panel implements ActionListener,
32 AdjustmentListener, ItemListener, MouseListener
38 Hashtable oldgroupColours;
39 jalview.datamodel.AlignmentAnnotation currentAnnotation;
40 boolean adjusting = false;
42 public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)
51 oldcs = av.getGlobalColourScheme();
52 if (av.alignment.getGroups() != null)
54 oldgroupColours = new Hashtable();
55 Vector allGroups = ap.av.alignment.getGroups();
57 for (int g = 0; g < allGroups.size(); g++)
59 sg = (SequenceGroup) allGroups.elementAt(g);
62 oldgroupColours.put(sg, sg.cs);
64 oldgroupColours.put(sg, "null");
71 slider.addAdjustmentListener(this);
72 slider.addMouseListener(this);
74 if (av.alignment.getAlignmentAnnotation() == null)
79 if (oldcs instanceof AnnotationColourGradient)
81 AnnotationColourGradient acg = (AnnotationColourGradient) oldcs;
82 minColour.setBackground(acg.getMinColour());
83 maxColour.setBackground(acg.getMaxColour());
87 minColour.setBackground(Color.orange);
88 maxColour.setBackground(Color.red);
93 Vector list = new Vector();
95 for (int i = 0; i < av.alignment.getAlignmentAnnotation().length; i++)
97 String label = av.alignment.getAlignmentAnnotation()[i].label;
98 if (!list.contains(label))
99 list.addElement(label);
101 list.addElement(label+"_"+(index++));
104 for (int i = 0; i < list.size(); i++)
106 annotations.addItem(list.elementAt(i).toString());
109 threshold.addItem("No Threshold");
110 threshold.addItem("Above Threshold");
111 threshold.addItem("Below Threshold");
119 jalview.bin.JalviewLite.addFrame(frame, "Colour by Annotation", 480, 145);
123 public AnnotationColourChooser()
131 ex.printStackTrace();
135 private void jbInit()
138 minColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
139 minColour.setLabel("Min Colour");
140 minColour.addActionListener(this);
142 maxColour.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
143 maxColour.setLabel("Max Colour");
144 maxColour.addActionListener(this);
146 thresholdIsMin.addItemListener(this);
148 ok.addActionListener(this);
150 cancel.setLabel("Cancel");
151 cancel.addActionListener(this);
153 this.setLayout(borderLayout1);
154 jPanel2.setLayout(flowLayout1);
155 annotations.addItemListener(this);
157 jPanel1.setBackground(Color.white);
158 jPanel2.setBackground(Color.white);
159 threshold.addItemListener(this);
160 jPanel3.setLayout(null);
161 thresholdValue.addActionListener(this);
163 slider.setBackground(Color.white);
164 slider.setEnabled(false);
165 slider.setBounds(new Rectangle(153, 3, 93, 21));
166 thresholdValue.setEnabled(false);
167 thresholdValue.setBounds(new Rectangle(248, 2, 79, 22));
168 thresholdValue.setColumns(5);
169 jPanel3.setBackground(Color.white);
170 currentColours.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
171 currentColours.setLabel("Use Original Colours");
172 currentColours.addItemListener(this);
174 threshold.setBounds(new Rectangle(11, 3, 139, 22));
175 thresholdIsMin.setBackground(Color.white);
176 thresholdIsMin.setLabel("Threshold is min/max");
177 thresholdIsMin.setBounds(new Rectangle(328, 3, 135, 23));
180 jPanel2.add(annotations);
181 jPanel2.add(currentColours);
182 jPanel2.add(minColour);
183 jPanel2.add(maxColour);
184 jPanel3.add(threshold);
186 jPanel3.add(thresholdValue);
187 jPanel3.add(thresholdIsMin);
188 this.add(jPanel2, java.awt.BorderLayout.NORTH);
189 this.add(jPanel3, java.awt.BorderLayout.CENTER);
190 this.add(jPanel1, java.awt.BorderLayout.SOUTH);
193 Choice annotations = new Choice();
194 Button minColour = new Button();
195 Button maxColour = new Button();
196 Button ok = new Button();
197 Button cancel = new Button();
198 Panel jPanel1 = new Panel();
199 Panel jPanel2 = new Panel();
200 Choice threshold = new Choice();
201 FlowLayout flowLayout1 = new FlowLayout();
202 Panel jPanel3 = new Panel();
203 Scrollbar slider = new Scrollbar(Scrollbar.HORIZONTAL);
204 TextField thresholdValue = new TextField(20);
205 Checkbox currentColours = new Checkbox();
206 BorderLayout borderLayout1 = new BorderLayout();
207 Checkbox thresholdIsMin = new Checkbox();
209 public void actionPerformed(ActionEvent evt)
211 if (evt.getSource() == thresholdValue)
215 float f = new Float(thresholdValue.getText()).floatValue();
216 slider.setValue( (int) (f * 1000));
217 adjustmentValueChanged(null);
219 catch (NumberFormatException ex)
222 else if (evt.getSource() == minColour)
224 minColour_actionPerformed(null);
226 else if (evt.getSource() == maxColour)
228 maxColour_actionPerformed(null);
231 else if (evt.getSource() == ok)
234 frame.setVisible(false);
236 else if (evt.getSource() == cancel)
239 ap.paintAlignment(true);
240 frame.setVisible(false);
249 public void itemStateChanged(ItemEvent evt)
251 if (evt.getSource() == currentColours)
253 if (currentColours.getState())
258 maxColour.setEnabled(!currentColours.getState());
259 minColour.setEnabled(!currentColours.getState());
266 public void adjustmentValueChanged(AdjustmentEvent evt)
270 thresholdValue.setText( ( (float) slider.getValue() / 1000f) + "");
271 if (currentColours.getState()
272 && ! (av.getGlobalColourScheme() instanceof AnnotationColourGradient))
277 currentAnnotation.threshold.value = (float) slider.getValue() / 1000f;
278 ap.paintAlignment(false);
282 public void minColour_actionPerformed(Color newCol)
286 minColour.setBackground(newCol);
292 new UserDefinedColours(this, "Min Colour",
293 minColour.getBackground());
298 public void maxColour_actionPerformed(Color newCol)
302 maxColour.setBackground(newCol);
308 new UserDefinedColours(this, "Max Colour",
309 maxColour.getBackground());
315 // Check if combobox is still adjusting
322 currentAnnotation = av.alignment.getAlignmentAnnotation()
323 [annotations.getSelectedIndex()];
325 int aboveThreshold = -1;
326 if (threshold.getSelectedItem().equals("Above Threshold"))
328 aboveThreshold = AnnotationColourGradient.ABOVE_THRESHOLD;
330 else if (threshold.getSelectedItem().equals("Below Threshold"))
332 aboveThreshold = AnnotationColourGradient.BELOW_THRESHOLD;
335 slider.setEnabled(true);
336 thresholdValue.setEnabled(true);
338 if (aboveThreshold == AnnotationColourGradient.NO_THRESHOLD)
340 slider.setEnabled(false);
341 thresholdValue.setEnabled(false);
342 thresholdValue.setText("");
344 else if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD &&
345 currentAnnotation.threshold == null)
347 currentAnnotation.setThreshold(new jalview.datamodel.GraphLine
348 ( (currentAnnotation.graphMax -
349 currentAnnotation.graphMin) / 2f,
354 if (aboveThreshold != AnnotationColourGradient.NO_THRESHOLD)
358 slider.setMinimum( (int) (currentAnnotation.graphMin * 1000));
359 slider.setMaximum( (int) (currentAnnotation.graphMax * 1000));
360 slider.setValue( (int) (currentAnnotation.threshold.value * 1000));
361 thresholdValue.setText(currentAnnotation.threshold.value + "");
362 slider.setEnabled(true);
363 thresholdValue.setEnabled(true);
367 AnnotationColourGradient acg = null;
368 if (currentColours.getState())
370 acg = new AnnotationColourGradient(
372 av.getGlobalColourScheme(), aboveThreshold);
377 new AnnotationColourGradient(
379 minColour.getBackground(),
380 maxColour.getBackground(),
384 if (currentAnnotation.graphMin == 0f && currentAnnotation.graphMax == 0f)
386 acg.predefinedColours = true;
389 acg.thresholdIsMinMax = thresholdIsMin.getState();
391 av.setGlobalColourScheme(acg);
393 if (av.alignment.getGroups() != null)
395 Vector allGroups = ap.av.alignment.getGroups();
397 for (int g = 0; g < allGroups.size(); g++)
399 sg = (SequenceGroup) allGroups.elementAt(g);
406 if (currentColours.getState())
408 sg.cs = new AnnotationColourGradient(
410 sg.cs, aboveThreshold);
414 sg.cs = new AnnotationColourGradient(
416 minColour.getBackground(),
417 maxColour.getBackground(),
424 ap.paintAlignment(false);
429 av.setGlobalColourScheme(oldcs);
430 if (av.alignment.getGroups() != null)
432 Vector allGroups = ap.av.alignment.getGroups();
434 for (int g = 0; g < allGroups.size(); g++)
436 sg = (SequenceGroup) allGroups.elementAt(g);
437 Object cs = oldgroupColours.get(sg);
438 if (cs instanceof ColourSchemeI)
440 sg.cs = (ColourSchemeI) cs;
442 // probably the "null" string we set it to if it was null originally.
447 ap.paintAlignment(true);
451 public void mouseClicked(MouseEvent evt){}
452 public void mousePressed(MouseEvent evt){}
453 public void mouseReleased(MouseEvent evt){ ap.paintAlignment(true);}
454 public void mouseEntered(MouseEvent evt){}
455 public void mouseExited(MouseEvent evt){}