Jalview 2.6 source licence
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27
28 public class UserDefinedColours extends Panel implements ActionListener,
29         AdjustmentListener
30 {
31
32   AlignmentPanel ap;
33
34   SequenceGroup seqGroup;
35
36   Button selectedButton;
37
38   Vector oldColours = new Vector();
39
40   ColourSchemeI oldColourScheme;
41
42   Frame frame;
43
44   MCview.AppletPDBCanvas pdbcanvas;
45
46   AppletJmol jmol;
47
48   Dialog dialog;
49
50   Object caller;
51
52   String originalLabel;
53
54   Object originalColour;
55
56   int R = 0, G = 0, B = 0;
57
58   public ColourSchemeI loadDefaultColours()
59   {
60     // NOT IMPLEMENTED YET IN APPLET VERSION
61     return null;
62   }
63
64   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
65   {
66     this.ap = ap;
67     seqGroup = sg;
68
69     if (seqGroup != null)
70     {
71       oldColourScheme = seqGroup.cs;
72     }
73     else
74     {
75       oldColourScheme = ap.av.getGlobalColourScheme();
76     }
77
78     init();
79   }
80
81   public UserDefinedColours(MCview.AppletPDBCanvas pdb)
82   {
83     this.pdbcanvas = pdb;
84     init();
85   }
86
87   public UserDefinedColours(AppletJmol jmol)
88   {
89     this.jmol = jmol;
90     init();
91   }
92
93   public UserDefinedColours(FeatureRenderer fr, Frame alignframe)
94   {
95     caller = fr;
96     originalColour = fr.colourPanel.getBackground();
97     originalLabel = "Feature Colour";
98     setForDialog("Select Feature Colour", alignframe);
99     setTargetColour(fr.colourPanel.getBackground());
100     dialog.setVisible(true);
101   }
102
103   public UserDefinedColours(Component caller, Color col1, Frame alignframe)
104   {
105     this(caller, col1, alignframe, "Select Colour");
106   }
107
108   /**
109    * Makes a dialog to choose the colour
110    * 
111    * @param caller
112    *          - handles events
113    * @param col1
114    *          - original colour
115    * @param alignframe
116    *          - the parent Frame for the dialog
117    * @param title
118    *          - window title
119    */
120   public UserDefinedColours(Component caller, Color col1, Frame alignframe,
121           String title)
122   {
123     this.caller = caller;
124     originalColour = col1;
125     originalLabel = title;
126     setForDialog(title, alignframe);
127     setTargetColour(col1);
128     dialog.setVisible(true);
129   }
130
131   /**
132    * feature colour chooser
133    * 
134    * @param caller
135    * @param label
136    * @param colour
137    */
138   public UserDefinedColours(Object caller, String label, Color colour)
139   {
140     this(caller, label, colour, colour);
141   }
142
143   /**
144    * feature colour chooser when changing style to single color
145    * 
146    * @param me
147    * @param type
148    * @param graduatedColor
149    */
150   public UserDefinedColours(FeatureSettings me, String type,
151           GraduatedColor graduatedColor)
152   {
153     this(me, type, graduatedColor, graduatedColor.getMaxColor());
154   }
155
156   private UserDefinedColours(Object caller, String label, Object ocolour,
157           Color colour)
158   {
159     this.caller = caller;
160     originalColour = ocolour;
161     originalLabel = label;
162     init();
163     remove(buttonPanel);
164
165     setTargetColour(colour);
166
167     okcancelPanel.setBounds(new Rectangle(0, 113, 400, 35));
168     frame.setTitle("User Defined Colours - " + label);
169     frame.setSize(420, 200);
170   }
171
172   void setForDialog(String title, Container alignframe)
173   {
174     init();
175     frame.setVisible(false);
176     remove(buttonPanel);
177     if (alignframe instanceof Frame)
178     {
179       dialog = new Dialog((Frame) alignframe, title, true);
180     }
181     else
182     {
183       // if (alignframe instanceof JVDialog){
184       // // not 1.1 compatible!
185       // dialog = new Dialog(((JVDialog)alignframe), title, true);
186       // } else {
187       throw new Error("Unsupported owner for User Colour scheme dialog.");
188     }
189
190     dialog.add(this);
191     this.setSize(400, 123);
192     okcancelPanel.setBounds(new Rectangle(0, 123, 400, 35));
193     int height = 160 + alignframe.getInsets().top + getInsets().bottom;
194     int width = 400;
195
196     dialog.setBounds(alignframe.getBounds().x
197             + (alignframe.getSize().width - width) / 2, alignframe
198             .getBounds().y
199             + (alignframe.getSize().height - height) / 2, width, height);
200
201   }
202
203   public void actionPerformed(ActionEvent evt)
204   {
205     if (evt.getSource() == okButton)
206     {
207       okButton_actionPerformed();
208     }
209     else if (evt.getSource() == applyButton)
210     {
211       applyButton_actionPerformed();
212     }
213     else if (evt.getSource() == cancelButton)
214     {
215       cancelButton_actionPerformed();
216     }
217     else if (evt.getSource() == rText)
218     {
219       rText_actionPerformed();
220     }
221     else if (evt.getSource() == gText)
222     {
223       gText_actionPerformed();
224     }
225     else if (evt.getSource() == bText)
226     {
227       bText_actionPerformed();
228     }
229   }
230
231   public void adjustmentValueChanged(AdjustmentEvent evt)
232   {
233     if (evt.getSource() == rScroller)
234     {
235       rScroller_adjustmentValueChanged();
236     }
237     else if (evt.getSource() == gScroller)
238     {
239       gScroller_adjustmentValueChanged();
240     }
241     else if (evt.getSource() == bScroller)
242     {
243       bScroller_adjustmentValueChanged();
244     }
245   }
246
247   void init()
248   {
249     try
250     {
251       jbInit();
252     } catch (Exception e)
253     {
254       e.printStackTrace();
255     }
256     frame = new Frame();
257     frame.add(this);
258     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420,
259             345);
260
261     if (seqGroup != null)
262     {
263       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
264     }
265
266     for (int i = 0; i < 20; i++)
267     {
268       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
269               + "", ResidueProperties.aa[i]);
270     }
271
272     makeButton("B", "B");
273     makeButton("Z", "Z");
274     makeButton("X", "X");
275     makeButton("Gap", "'.','-',' '");
276
277     validate();
278   }
279
280   protected void rText_actionPerformed()
281   {
282     try
283     {
284       int i = Integer.parseInt(rText.getText());
285       rScroller.setValue(i);
286       rScroller_adjustmentValueChanged();
287     } catch (NumberFormatException ex)
288     {
289     }
290   }
291
292   protected void gText_actionPerformed()
293   {
294     try
295     {
296       int i = Integer.parseInt(gText.getText());
297       gScroller.setValue(i);
298       gScroller_adjustmentValueChanged();
299     } catch (NumberFormatException ex)
300     {
301     }
302
303   }
304
305   protected void bText_actionPerformed()
306   {
307     try
308     {
309       int i = Integer.parseInt(bText.getText());
310       bScroller.setValue(i);
311       bScroller_adjustmentValueChanged();
312     } catch (NumberFormatException ex)
313     {
314     }
315
316   }
317
318   protected void rScroller_adjustmentValueChanged()
319   {
320     R = rScroller.getValue();
321     rText.setText(R + "");
322     colourChanged();
323   }
324
325   protected void gScroller_adjustmentValueChanged()
326   {
327     G = gScroller.getValue();
328     gText.setText(G + "");
329     colourChanged();
330   }
331
332   protected void bScroller_adjustmentValueChanged()
333   {
334     B = bScroller.getValue();
335     bText.setText(B + "");
336     colourChanged();
337   }
338
339   public void colourChanged()
340   {
341     Color col = new Color(R, G, B);
342     target.setBackground(col);
343     target.repaint();
344
345     if (selectedButton != null)
346     {
347       selectedButton.setBackground(col);
348       selectedButton.repaint();
349     }
350   }
351
352   void setTargetColour(Color col)
353   {
354     R = col.getRed();
355     G = col.getGreen();
356     B = col.getBlue();
357
358     rScroller.setValue(R);
359     gScroller.setValue(G);
360     bScroller.setValue(B);
361     rText.setText(R + "");
362     gText.setText(G + "");
363     bText.setText(B + "");
364     colourChanged();
365   }
366
367   public void colourButtonPressed(MouseEvent e)
368   {
369     selectedButton = (Button) e.getSource();
370     setTargetColour(selectedButton.getBackground());
371   }
372
373   void makeButton(String label, String aa)
374   {
375     final Button button = new Button();
376     Color col = Color.white;
377     if (oldColourScheme != null)
378     {
379       try
380       {
381         col = oldColourScheme.findColour(aa.charAt(0), -1);
382       } catch (Exception ex)
383       {
384       }
385     }
386     button.setBackground(col);
387     oldColours.addElement(col);
388     button.setLabel(label);
389     button.setForeground(col.darker().darker().darker());
390     button.setFont(new java.awt.Font("Verdana", 1, 10));
391     button.addMouseListener(new java.awt.event.MouseAdapter()
392     {
393       public void mousePressed(MouseEvent e)
394       {
395         colourButtonPressed(e);
396       }
397     });
398
399     buttonPanel.add(button, null);
400   }
401
402   protected void okButton_actionPerformed()
403   {
404     applyButton_actionPerformed();
405     if (dialog != null)
406       dialog.setVisible(false);
407
408     frame.setVisible(false);
409   }
410
411   public Color getColor()
412   {
413     return new Color(R, G, B);
414   }
415
416   protected void applyButton_actionPerformed()
417   {
418     if (caller != null)
419     {
420       if (caller instanceof FeatureSettings)
421       {
422         ((FeatureSettings) caller).setUserColour(originalLabel, getColor());
423       }
424       else if (caller instanceof AnnotationColourChooser)
425       {
426         if (originalLabel.equals("Min Colour"))
427         {
428           ((AnnotationColourChooser) caller)
429                   .minColour_actionPerformed(getColor());
430         }
431         else
432         {
433           ((AnnotationColourChooser) caller)
434                   .maxColour_actionPerformed(getColor());
435         }
436       }
437       else if (caller instanceof FeatureRenderer)
438       {
439         ((FeatureRenderer) caller).colourPanel.updateColor(getColor());
440       }
441       else if (caller instanceof FeatureColourChooser)
442       {
443         if (originalLabel.indexOf("inimum") > -1)
444         {
445           ((FeatureColourChooser) caller)
446                   .minColour_actionPerformed(getColor());
447         }
448         else
449         {
450           ((FeatureColourChooser) caller)
451                   .maxColour_actionPerformed(getColor());
452         }
453       }
454
455       return;
456     }
457
458     Color[] newColours = new Color[24];
459     for (int i = 0; i < 24; i++)
460     {
461       Button button = (Button) buttonPanel.getComponent(i);
462       newColours[i] = button.getBackground();
463     }
464
465     UserColourScheme ucs = new UserColourScheme(newColours);
466     if (ap != null)
467     {
468       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
469     }
470
471     if (ap != null)
472     {
473       if (seqGroup != null)
474       {
475         seqGroup.cs = ucs;
476       }
477       else
478       {
479         ap.av.setGlobalColourScheme(ucs);
480       }
481       ap.seqPanel.seqCanvas.img = null;
482       ap.paintAlignment(true);
483     }
484     else if (jmol != null)
485     {
486       jmol.setJalviewColourScheme(ucs);
487     }
488     else if (pdbcanvas != null)
489     {
490       pdbcanvas.setColours(ucs);
491     }
492   }
493
494   protected void cancelButton_actionPerformed()
495   {
496     if (caller != null)
497     {
498       if (caller instanceof FeatureSettings)
499       {
500         ((FeatureSettings) caller).setUserColour(originalLabel,
501                 originalColour);
502       }
503       else if (caller instanceof AnnotationColourChooser)
504       {
505         if (originalLabel.equals("Min Colour"))
506         {
507           ((AnnotationColourChooser) caller)
508                   .minColour_actionPerformed((Color) originalColour);
509         }
510         else
511         {
512           ((AnnotationColourChooser) caller)
513                   .maxColour_actionPerformed((Color) originalColour);
514         }
515       }
516       else if (caller instanceof FeatureRenderer)
517       {
518         ((FeatureRenderer) caller).colourPanel.updateColor(originalColour);
519
520       }
521
522       else if (caller instanceof FeatureColourChooser)
523       {
524         if (originalLabel.indexOf("inimum") > -1)
525         {
526           ((FeatureColourChooser) caller)
527                   .minColour_actionPerformed((Color) originalColour);
528         }
529         else
530         {
531           ((FeatureColourChooser) caller)
532                   .maxColour_actionPerformed((Color) originalColour);
533         }
534       }
535       if (dialog != null)
536         dialog.setVisible(false);
537
538       frame.setVisible(false);
539       return;
540     }
541
542     Color[] newColours = new Color[24];
543     for (int i = 0; i < 24; i++)
544     {
545       newColours[i] = (Color) oldColours.elementAt(i);
546       buttonPanel.getComponent(i).setBackground(newColours[i]);
547     }
548
549     UserColourScheme ucs = new UserColourScheme(newColours);
550
551     if (ap != null)
552     {
553       if (seqGroup != null)
554       {
555         seqGroup.cs = ucs;
556       }
557       else
558       {
559         ap.av.setGlobalColourScheme(ucs);
560       }
561       ap.paintAlignment(true);
562     }
563     else if (jmol != null)
564     {
565       jmol.setJalviewColourScheme(ucs);
566     }
567     else if (pdbcanvas != null)
568     {
569       pdbcanvas.pdb.setColours(ucs);
570     }
571
572     frame.setVisible(false);
573   }
574
575   protected Panel buttonPanel = new Panel();
576
577   protected GridLayout gridLayout = new GridLayout();
578
579   Panel okcancelPanel = new Panel();
580
581   protected Button okButton = new Button();
582
583   protected Button applyButton = new Button();
584
585   protected Button cancelButton = new Button();
586
587   protected Scrollbar rScroller = new Scrollbar();
588
589   Label label1 = new Label();
590
591   protected TextField rText = new TextField();
592
593   Label label4 = new Label();
594
595   protected Scrollbar gScroller = new Scrollbar();
596
597   protected TextField gText = new TextField();
598
599   Label label5 = new Label();
600
601   protected Scrollbar bScroller = new Scrollbar();
602
603   protected TextField bText = new TextField();
604
605   protected Panel target = new Panel();
606
607   private void jbInit() throws Exception
608   {
609     this.setLayout(null);
610     buttonPanel.setLayout(gridLayout);
611     gridLayout.setColumns(6);
612     gridLayout.setRows(4);
613     okButton.setFont(new java.awt.Font("Verdana", 0, 11));
614     okButton.setLabel("OK");
615     okButton.addActionListener(this);
616     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
617     applyButton.setLabel("Apply");
618     applyButton.addActionListener(this);
619     cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));
620     cancelButton.setLabel("Cancel");
621     cancelButton.addActionListener(this);
622     this.setBackground(new Color(212, 208, 223));
623     okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));
624     buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));
625     rScroller.setMaximum(256);
626     rScroller.setMinimum(0);
627     rScroller.setOrientation(0);
628     rScroller.setUnitIncrement(1);
629     rScroller.setVisibleAmount(1);
630     rScroller.setBounds(new Rectangle(36, 27, 119, 19));
631     rScroller.addAdjustmentListener(this);
632     label1.setAlignment(Label.RIGHT);
633     label1.setText("R");
634     label1.setBounds(new Rectangle(19, 30, 16, 15));
635     rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
636     rText.setText("0        ");
637     rText.setBounds(new Rectangle(156, 27, 53, 19));
638     rText.addActionListener(this);
639     label4.setAlignment(Label.RIGHT);
640     label4.setText("G");
641     label4.setBounds(new Rectangle(15, 56, 20, 15));
642     gScroller.setMaximum(256);
643     gScroller.setMinimum(0);
644     gScroller.setOrientation(0);
645     gScroller.setUnitIncrement(1);
646     gScroller.setVisibleAmount(1);
647     gScroller.setBounds(new Rectangle(35, 52, 120, 20));
648     gScroller.addAdjustmentListener(this);
649     gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
650     gText.setText("0        ");
651     gText.setBounds(new Rectangle(156, 52, 53, 20));
652     gText.addActionListener(this);
653     label5.setAlignment(Label.RIGHT);
654     label5.setText("B");
655     label5.setBounds(new Rectangle(14, 82, 20, 15));
656     bScroller.setMaximum(256);
657     bScroller.setMinimum(0);
658     bScroller.setOrientation(0);
659     bScroller.setUnitIncrement(1);
660     bScroller.setVisibleAmount(1);
661     bScroller.setBounds(new Rectangle(35, 78, 120, 20));
662     bScroller.addAdjustmentListener(this);
663     bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
664     bText.setText("0        ");
665     bText.setBounds(new Rectangle(157, 78, 52, 20));
666     bText.addActionListener(this);
667     target.setBackground(Color.black);
668     target.setBounds(new Rectangle(229, 26, 134, 79));
669     this.add(okcancelPanel, null);
670     okcancelPanel.add(okButton, null);
671     okcancelPanel.add(applyButton, null);
672     okcancelPanel.add(cancelButton, null);
673     this.add(buttonPanel, null);
674     this.add(target, null);
675     this.add(gScroller);
676     this.add(rScroller);
677     this.add(bScroller);
678     this.add(label5);
679     this.add(label4);
680     this.add(label1);
681     this.add(gText);
682     this.add(rText);
683     this.add(bText);
684   }
685
686 }