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