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