Null sequence check
[jalview.git] / src / jalview / gui / AnnotationColourChooser.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package jalview.gui;\r
20 \r
21 import javax.swing.*;\r
22 import java.awt.event.ActionListener;\r
23 import java.awt.event.ActionEvent;\r
24 import java.awt.BorderLayout;\r
25 import java.awt.*;\r
26 \r
27 import jalview.schemes.*;\r
28 \r
29 public class AnnotationColourChooser\r
30     extends JPanel\r
31 {\r
32   JInternalFrame frame;\r
33   AlignViewport av;\r
34   AlignmentPanel ap;\r
35   ColourSchemeI oldcs;\r
36 \r
37   public AnnotationColourChooser(AlignViewport av, AlignmentPanel ap)\r
38   {\r
39     oldcs = av.getGlobalColourScheme();\r
40     this.av = av;\r
41     this.ap = ap;\r
42     frame = new JInternalFrame();\r
43     frame.setContentPane(this);\r
44     frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
45     Desktop.addInternalFrame(frame, "Colour by Annotation", 480, 110);\r
46 \r
47     try{\r
48       jbInit();\r
49     }catch(Exception ex){}\r
50 \r
51     if(av.alignment.getAlignmentAnnotation()==null)\r
52       return;\r
53 \r
54     for(int i=0; i<av.alignment.getAlignmentAnnotation().length; i++)\r
55     {\r
56       annotations.addItem(av.alignment.getAlignmentAnnotation()[i].label);\r
57     }\r
58 \r
59   //  if(av.getGlobalColourScheme() instanceof AnnotationColourGradient)\r
60     {\r
61     //  minColour.\r
62     }\r
63   //  else\r
64     {\r
65       minColour.setBackground(Color.orange);\r
66       maxColour.setBackground(Color.red);\r
67     }\r
68   }\r
69 \r
70   public AnnotationColourChooser()\r
71   {\r
72     try\r
73     {\r
74       jbInit();\r
75     }\r
76     catch (Exception ex)\r
77     {\r
78       ex.printStackTrace();\r
79     }\r
80   }\r
81 \r
82   private void jbInit()\r
83       throws Exception\r
84   {\r
85     minColour.setBounds(new Rectangle(145, 5, 85, 25));\r
86     minColour.setToolTipText("");\r
87     minColour.setMargin(new Insets(2, 2, 2, 2));\r
88     minColour.setText("Min Colour");\r
89     minColour.addActionListener(new ActionListener()\r
90     {\r
91       public void actionPerformed(ActionEvent e)\r
92       {\r
93         minColour_actionPerformed(e);\r
94       }\r
95     });\r
96     maxColour.setBounds(new Rectangle(235, 5, 89, 25));\r
97     maxColour.setMargin(new Insets(2, 2, 2, 2));\r
98     maxColour.setText("Max Colour");\r
99     maxColour.addActionListener(new ActionListener()\r
100     {\r
101       public void actionPerformed(ActionEvent e)\r
102       {\r
103         maxColour_actionPerformed(e);\r
104       }\r
105     });\r
106     thresholdCheck.setOpaque(false);\r
107     thresholdCheck.setHorizontalTextPosition(SwingConstants.LEADING);\r
108     thresholdCheck.setSelectedIcon(null);\r
109     thresholdCheck.setText("Above Threshold");\r
110     thresholdCheck.setBounds(new Rectangle(329, 6, 109, 23));\r
111     apply.setOpaque(false);\r
112     apply.setSelectedIcon(null);\r
113     apply.setText("Apply");\r
114     apply.addActionListener(new ActionListener()\r
115     {\r
116       public void actionPerformed(ActionEvent e)\r
117       {\r
118         apply_actionPerformed(e);\r
119       }\r
120     });\r
121     ok.setOpaque(false);\r
122     ok.setText("OK");\r
123     ok.addActionListener(new ActionListener()\r
124     {\r
125       public void actionPerformed(ActionEvent e)\r
126       {\r
127         ok_actionPerformed(e);\r
128       }\r
129     });\r
130     cancel.setOpaque(false);\r
131     cancel.setText("Cancel");\r
132     cancel.addActionListener(new ActionListener()\r
133     {\r
134       public void actionPerformed(ActionEvent e)\r
135       {\r
136         cancel_actionPerformed(e);\r
137       }\r
138     });\r
139     this.setLayout(borderLayout1);\r
140     jPanel2.setLayout(null);\r
141     annotations.setBounds(new Rectangle(5, 7, 135, 21));\r
142     jPanel1.setBackground(Color.white);\r
143     jPanel2.setBackground(Color.white);\r
144     jPanel1.add(apply);\r
145     jPanel1.add(ok);\r
146     jPanel1.add(cancel);\r
147     jPanel2.add(annotations);\r
148     jPanel2.add(minColour);\r
149     jPanel2.add(maxColour);\r
150     jPanel2.add(thresholdCheck);\r
151     this.add(jPanel1, java.awt.BorderLayout.SOUTH);\r
152     this.add(jPanel2, java.awt.BorderLayout.CENTER);\r
153   }\r
154 \r
155   JComboBox annotations = new JComboBox();\r
156   JButton minColour = new JButton();\r
157   JButton maxColour = new JButton();\r
158   JCheckBox thresholdCheck = new JCheckBox();\r
159   JButton apply = new JButton();\r
160   JButton ok = new JButton();\r
161   JButton cancel = new JButton();\r
162   JPanel jPanel1 = new JPanel();\r
163   JPanel jPanel2 = new JPanel();\r
164   BorderLayout borderLayout1 = new BorderLayout();\r
165   public void apply_actionPerformed(ActionEvent e)\r
166   {\r
167     changeColour();\r
168   }\r
169 \r
170   public void minColour_actionPerformed(ActionEvent e)\r
171   {\r
172     Color col = JColorChooser.showDialog(this,\r
173                                          "Select Colour for Minimum Value",\r
174                                          minColour.getBackground());\r
175     if (col != null)\r
176       minColour.setBackground(col);\r
177     minColour.repaint();\r
178   }\r
179 \r
180   public void maxColour_actionPerformed(ActionEvent e)\r
181   {\r
182     Color col = JColorChooser.showDialog(this,\r
183                                          "Select Colour for Maximum Value",\r
184                                          maxColour.getBackground());\r
185     if (col != null)\r
186       maxColour.setBackground(col);\r
187     maxColour.repaint();\r
188   }\r
189 \r
190   void changeColour()\r
191   {\r
192     jalview.datamodel.AlignmentAnnotation aa = av.alignment.getAlignmentAnnotation()[annotations.getSelectedIndex()];\r
193 \r
194     AnnotationColourGradient acg = new AnnotationColourGradient(aa,\r
195      minColour.getBackground(),\r
196      maxColour.getBackground(),\r
197      thresholdCheck.isSelected());\r
198 \r
199    if(thresholdCheck.isSelected() && aa.graphLines==null)\r
200    {\r
201      aa.addGraphLine(new jalview.datamodel.GraphLine((aa.graphMax-aa.graphMin)/2f, "Threshold", Color.black));\r
202    }\r
203 \r
204    av.setGlobalColourScheme(acg);\r
205    ap.repaint();\r
206 \r
207   }\r
208 \r
209   public void ok_actionPerformed(ActionEvent e)\r
210   {\r
211     changeColour();\r
212     try{\r
213       frame.setClosed(true);\r
214     }catch(Exception ex){}\r
215   }\r
216 \r
217   public void cancel_actionPerformed(ActionEvent e)\r
218   {\r
219     av.setGlobalColourScheme(oldcs);\r
220     try{\r
221       frame.setClosed(true);\r
222     }catch(Exception ex){}\r
223   }\r
224 \r
225 }\r