6544993bf8a858f982289f7536651bace4ecad76
[jalview.git] / src / jalview / appletgui / UserDefinedColours.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 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 import jalview.schemes.*;\r
29 \r
30 public class UserDefinedColours\r
31     extends Panel implements ActionListener, AdjustmentListener\r
32 {\r
33 \r
34   AlignmentPanel ap;\r
35   SequenceGroup seqGroup;\r
36   Button selectedButton;\r
37   Vector oldColours = new Vector();\r
38   ColourSchemeI oldColourScheme;\r
39   Frame frame;\r
40   MCview.AppletPDBCanvas pdbcanvas;\r
41   FeatureSettings featureSettings;\r
42   String feature;\r
43   Color originalFeatureCol;\r
44 \r
45   int R = 0, G = 0, B = 0;\r
46 \r
47   public ColourSchemeI loadDefaultColours()\r
48   {\r
49     // NOT IMPLEMENTED YET IN APPLET VERSION\r
50     return null;\r
51   }\r
52 \r
53   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
54   {\r
55     this.ap = ap;\r
56     seqGroup = sg;\r
57 \r
58     if (seqGroup != null)\r
59     {\r
60       oldColourScheme = seqGroup.cs;\r
61     }\r
62     else\r
63     {\r
64       oldColourScheme = ap.av.getGlobalColourScheme();\r
65     }\r
66 \r
67     init();\r
68   }\r
69 \r
70   public UserDefinedColours(MCview.AppletPDBCanvas pdb)\r
71   {\r
72     this.pdbcanvas = pdb;\r
73     init();\r
74   }\r
75 \r
76   public UserDefinedColours(FeatureSettings featureSettings,\r
77                             String feature,\r
78                             Color col)\r
79   {\r
80     this.featureSettings = featureSettings;\r
81     this.feature = feature;\r
82     init();\r
83     remove(buttonPanel);\r
84     originalFeatureCol = col;\r
85     setTargetColour(col);\r
86 \r
87     okcancelPanel.setBounds(new Rectangle(0, 113, 400, 35));\r
88     frame.setTitle("User Defined Colours - "+feature);\r
89     frame.setSize(420, 200);\r
90   }\r
91 \r
92   public void actionPerformed(ActionEvent evt)\r
93   {\r
94     if(evt.getSource()==okButton)\r
95      okButton_actionPerformed();\r
96    else if(evt.getSource()==applyButton)\r
97       applyButton_actionPerformed();\r
98    else if(evt.getSource()==cancelButton)\r
99      cancelButton_actionPerformed();\r
100    else if(evt.getSource()==rText)\r
101      rText_actionPerformed();\r
102    else if (evt.getSource() == gText)\r
103      gText_actionPerformed();\r
104    else if (evt.getSource() == bText)\r
105      bText_actionPerformed();\r
106 }\r
107 \r
108   public void adjustmentValueChanged(AdjustmentEvent evt)\r
109   {\r
110     if(evt.getSource()==rScroller)\r
111     rScroller_adjustmentValueChanged();\r
112   else if(evt.getSource()==gScroller)\r
113     gScroller_adjustmentValueChanged();\r
114   else if(evt.getSource()==bScroller)\r
115     bScroller_adjustmentValueChanged();\r
116 }\r
117 \r
118   void init()\r
119   {\r
120     try\r
121     {\r
122       jbInit();\r
123     }\r
124     catch (Exception e)\r
125     {\r
126       e.printStackTrace();\r
127     }\r
128     frame = new Frame();\r
129     frame.add(this);\r
130     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420, 345);\r
131 \r
132     if (seqGroup != null)\r
133     {\r
134       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");\r
135     }\r
136 \r
137     for (int i = 0; i < 20; i++)\r
138     {\r
139       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
140                  "", ResidueProperties.aa[i]);\r
141     }\r
142 \r
143     makeButton("B", "B");\r
144     makeButton("Z", "Z");\r
145     makeButton("X", "X");\r
146     makeButton("Gap", "'.','-',' '");\r
147 \r
148     validate();\r
149   }\r
150   protected void rText_actionPerformed()\r
151   {\r
152     try\r
153     {\r
154       int i = Integer.parseInt(rText.getText());\r
155       rScroller.setValue(i);\r
156       rScroller_adjustmentValueChanged();\r
157     }\r
158     catch (NumberFormatException ex)\r
159     {}\r
160   }\r
161 \r
162   protected void gText_actionPerformed()\r
163   {\r
164     try\r
165     {\r
166       int i = Integer.parseInt(gText.getText());\r
167       gScroller.setValue(i);\r
168       gScroller_adjustmentValueChanged();\r
169     }\r
170     catch (NumberFormatException ex)\r
171     {}\r
172 \r
173   }\r
174 \r
175   protected void bText_actionPerformed()\r
176   {\r
177     try\r
178     {\r
179       int i = Integer.parseInt(bText.getText());\r
180       bScroller.setValue(i);\r
181       bScroller_adjustmentValueChanged();\r
182     }\r
183     catch (NumberFormatException ex)\r
184     {}\r
185 \r
186   }\r
187 \r
188   protected void rScroller_adjustmentValueChanged()\r
189   {\r
190     R = rScroller.getValue();\r
191     rText.setText(R + "");\r
192     colourChanged();\r
193   }\r
194 \r
195   protected void gScroller_adjustmentValueChanged()\r
196   {\r
197     G = gScroller.getValue();\r
198     gText.setText(G + "");\r
199     colourChanged();\r
200   }\r
201 \r
202   protected void bScroller_adjustmentValueChanged()\r
203   {\r
204     B = bScroller.getValue();\r
205     bText.setText(B + "");\r
206     colourChanged();\r
207   }\r
208 \r
209   public void colourChanged()\r
210   {\r
211     Color col = new Color(R, G, B);\r
212     target.setBackground(col);\r
213     target.repaint();\r
214 \r
215     if (selectedButton != null)\r
216     {\r
217       selectedButton.setBackground(col);\r
218       selectedButton.repaint();\r
219     }\r
220   }\r
221 \r
222   void setTargetColour(Color col)\r
223   {\r
224     R = col.getRed();\r
225     G = col.getGreen();\r
226     B = col.getBlue();\r
227     rScroller.setValue(R);\r
228     gScroller.setValue(G);\r
229     bScroller.setValue(B);\r
230     rText.setText(R + "");\r
231     gText.setText(G + "");\r
232     bText.setText(B + "");\r
233     colourChanged();\r
234   }\r
235 \r
236   public void colourButtonPressed(MouseEvent e)\r
237   {\r
238     selectedButton = (Button) e.getSource();\r
239     setTargetColour(selectedButton.getBackground());\r
240   }\r
241 \r
242   void makeButton(String label, String aa)\r
243   {\r
244     final Button button = new Button();\r
245     Color col = Color.white;\r
246 \r
247     try\r
248     {\r
249       col = oldColourScheme.findColour(aa, -1);\r
250     }\r
251     catch (Exception ex)\r
252     {}\r
253 \r
254     button.setBackground(col);\r
255     oldColours.addElement(col);\r
256     button.setLabel(label);\r
257     button.setForeground(col.darker().darker().darker());\r
258     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
259     button.addMouseListener(new java.awt.event.MouseAdapter()\r
260     {\r
261       public void mousePressed(MouseEvent e)\r
262       {\r
263         colourButtonPressed(e);\r
264       }\r
265     });\r
266 \r
267     buttonPanel.add(button, null);\r
268   }\r
269 \r
270   protected void okButton_actionPerformed()\r
271   {\r
272     applyButton_actionPerformed();\r
273     frame.setVisible(false);\r
274   }\r
275 \r
276   protected void applyButton_actionPerformed()\r
277   {\r
278     if (featureSettings != null)\r
279     {\r
280       featureSettings.setUserColour(feature, new Color(R,G,B));\r
281       return;\r
282     }\r
283 \r
284     Color[] newColours = new Color[24];\r
285     for (int i = 0; i < 24; i++)\r
286     {\r
287       Button button = (Button) buttonPanel.getComponent(i);\r
288       newColours[i] = button.getBackground();\r
289     }\r
290 \r
291     UserColourScheme ucs = new UserColourScheme(newColours);\r
292     if(ap!=null)\r
293       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
294 \r
295     if(ap!=null)\r
296     {\r
297       if (seqGroup != null)\r
298       {\r
299         seqGroup.cs = ucs;\r
300       }\r
301       else\r
302       {\r
303         ap.av.setGlobalColourScheme(ucs);\r
304       }\r
305       ap.seqPanel.seqCanvas.img = null;\r
306       ap.repaint();\r
307     }\r
308     else if(pdbcanvas!=null)\r
309     {\r
310       pdbcanvas.pdb.setColours(ucs);\r
311       pdbcanvas.updateSeqColours();\r
312     }\r
313   }\r
314 \r
315 \r
316   protected void cancelButton_actionPerformed()\r
317   {\r
318     if(featureSettings!=null)\r
319     {\r
320       featureSettings.setUserColour(feature, originalFeatureCol);\r
321       frame.setVisible(false);\r
322       return;\r
323     }\r
324 \r
325     Color[] newColours = new Color[24];\r
326     for (int i = 0; i < 24; i++)\r
327     {\r
328       newColours[i] = (Color) oldColours.elementAt(i);\r
329       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
330     }\r
331 \r
332     UserColourScheme ucs = new UserColourScheme(newColours);\r
333 \r
334    if(ap!=null)\r
335     {\r
336       if (seqGroup != null)\r
337       {\r
338         seqGroup.cs = ucs;\r
339       }\r
340       else\r
341       {\r
342         ap.av.setGlobalColourScheme(ucs);\r
343       }\r
344       ap.repaint();\r
345     }\r
346     else if(pdbcanvas!=null)\r
347     {\r
348       pdbcanvas.pdb.setColours(ucs);\r
349     }\r
350 \r
351     frame.setVisible(false);\r
352   }\r
353 \r
354 \r
355   protected Panel buttonPanel = new Panel();\r
356   protected GridLayout gridLayout = new GridLayout();\r
357   Panel okcancelPanel = new Panel();\r
358   protected Button okButton = new Button();\r
359   protected Button applyButton = new Button();\r
360   protected Button cancelButton = new Button();\r
361   protected Scrollbar rScroller = new Scrollbar();\r
362   Label label1 = new Label();\r
363   protected TextField rText = new TextField();\r
364   Label label4 = new Label();\r
365   protected Scrollbar gScroller = new Scrollbar();\r
366   protected TextField gText = new TextField();\r
367   Label label5 = new Label();\r
368   protected Scrollbar bScroller = new Scrollbar();\r
369   protected TextField bText = new TextField();\r
370   protected Panel target = new Panel();\r
371 \r
372   private void jbInit() throws Exception {\r
373       this.setLayout(null);\r
374       buttonPanel.setLayout(gridLayout);\r
375       gridLayout.setColumns(6);\r
376       gridLayout.setRows(4);\r
377       okButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
378       okButton.setLabel("OK");\r
379       okButton.addActionListener(this);\r
380       applyButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
381       applyButton.setLabel("Apply");\r
382       applyButton.addActionListener(this);\r
383       cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
384       cancelButton.setLabel("Cancel");\r
385       cancelButton.addActionListener(this);\r
386       this.setBackground(new Color(212, 208, 223));\r
387       okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));\r
388       buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));\r
389       rScroller.setMaximum(256);\r
390       rScroller.setMinimum(0);\r
391       rScroller.setOrientation(0);\r
392       rScroller.setUnitIncrement(1);\r
393   rScroller.setVisibleAmount(1);\r
394   rScroller.setBounds(new Rectangle(36, 27, 119, 19));\r
395   rScroller.addAdjustmentListener(this);\r
396   label1.setAlignment(Label.RIGHT);\r
397   label1.setText("R");\r
398   label1.setBounds(new Rectangle(19, 30, 16, 15));\r
399   rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
400   rText.setText("0        ");\r
401   rText.setBounds(new Rectangle(156, 27, 53, 19));\r
402   rText.addActionListener(this);\r
403   label4.setAlignment(Label.RIGHT);\r
404   label4.setText("G");\r
405   label4.setBounds(new Rectangle(15, 56, 20, 15));\r
406   gScroller.setMaximum(256);\r
407       gScroller.setMinimum(0);\r
408       gScroller.setOrientation(0);\r
409       gScroller.setUnitIncrement(1);\r
410   gScroller.setVisibleAmount(1);\r
411   gScroller.setBounds(new Rectangle(35, 52, 120, 20));\r
412   gScroller.addAdjustmentListener(this);\r
413   gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
414   gText.setText("0        ");\r
415   gText.setBounds(new Rectangle(156, 52, 53, 20));\r
416   gText.addActionListener(this);\r
417   label5.setAlignment(Label.RIGHT);\r
418   label5.setText("B");\r
419   label5.setBounds(new Rectangle(14, 82, 20, 15));\r
420   bScroller.setMaximum(256);\r
421       bScroller.setMinimum(0);\r
422       bScroller.setOrientation(0);\r
423       bScroller.setUnitIncrement(1);\r
424   bScroller.setVisibleAmount(1);\r
425   bScroller.setBounds(new Rectangle(35, 78, 120, 20));\r
426   bScroller.addAdjustmentListener(this);\r
427   bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
428   bText.setText("0        ");\r
429   bText.setBounds(new Rectangle(157, 78, 52, 20));\r
430   bText.addActionListener(this);\r
431   target.setBackground(Color.black);\r
432       target.setBounds(new Rectangle(229, 26, 134, 79));\r
433       this.add(okcancelPanel, null);\r
434       okcancelPanel.add(okButton, null);\r
435       okcancelPanel.add(applyButton, null);\r
436       okcancelPanel.add(cancelButton, null);\r
437       this.add(buttonPanel, null);\r
438   this.add(target, null);\r
439   this.add(gScroller);\r
440   this.add(rScroller);\r
441   this.add(bScroller);\r
442   this.add(label5);\r
443   this.add(label4);\r
444   this.add(label1);\r
445   this.add(gText);\r
446   this.add(rText);\r
447   this.add(bText);\r
448 }\r
449 \r
450 }\r