update author list in license for (JAL-826)
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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,
198             alignframe.getBounds().y
199                     + (alignframe.getSize().height - height) / 2, width,
200             height);
201
202   }
203
204   public void actionPerformed(ActionEvent evt)
205   {
206     if (evt.getSource() == okButton)
207     {
208       okButton_actionPerformed();
209     }
210     else if (evt.getSource() == applyButton)
211     {
212       applyButton_actionPerformed();
213     }
214     else if (evt.getSource() == cancelButton)
215     {
216       cancelButton_actionPerformed();
217     }
218     else if (evt.getSource() == rText)
219     {
220       rText_actionPerformed();
221     }
222     else if (evt.getSource() == gText)
223     {
224       gText_actionPerformed();
225     }
226     else if (evt.getSource() == bText)
227     {
228       bText_actionPerformed();
229     }
230   }
231
232   public void adjustmentValueChanged(AdjustmentEvent evt)
233   {
234     if (evt.getSource() == rScroller)
235     {
236       rScroller_adjustmentValueChanged();
237     }
238     else if (evt.getSource() == gScroller)
239     {
240       gScroller_adjustmentValueChanged();
241     }
242     else if (evt.getSource() == bScroller)
243     {
244       bScroller_adjustmentValueChanged();
245     }
246   }
247
248   void init()
249   {
250     try
251     {
252       jbInit();
253     } catch (Exception e)
254     {
255       e.printStackTrace();
256     }
257     frame = new Frame();
258     frame.add(this);
259     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420,
260             345);
261
262     if (seqGroup != null)
263     {
264       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
265     }
266
267     for (int i = 0; i < 20; i++)
268     {
269       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i])
270               + "", ResidueProperties.aa[i]);
271     }
272
273     makeButton("B", "B");
274     makeButton("Z", "Z");
275     makeButton("X", "X");
276     makeButton("Gap", "'.','-',' '");
277
278     validate();
279   }
280
281   protected void rText_actionPerformed()
282   {
283     try
284     {
285       int i = Integer.parseInt(rText.getText());
286       rScroller.setValue(i);
287       rScroller_adjustmentValueChanged();
288     } catch (NumberFormatException ex)
289     {
290     }
291   }
292
293   protected void gText_actionPerformed()
294   {
295     try
296     {
297       int i = Integer.parseInt(gText.getText());
298       gScroller.setValue(i);
299       gScroller_adjustmentValueChanged();
300     } catch (NumberFormatException ex)
301     {
302     }
303
304   }
305
306   protected void bText_actionPerformed()
307   {
308     try
309     {
310       int i = Integer.parseInt(bText.getText());
311       bScroller.setValue(i);
312       bScroller_adjustmentValueChanged();
313     } catch (NumberFormatException ex)
314     {
315     }
316
317   }
318
319   protected void rScroller_adjustmentValueChanged()
320   {
321     R = rScroller.getValue();
322     rText.setText(R + "");
323     colourChanged();
324   }
325
326   protected void gScroller_adjustmentValueChanged()
327   {
328     G = gScroller.getValue();
329     gText.setText(G + "");
330     colourChanged();
331   }
332
333   protected void bScroller_adjustmentValueChanged()
334   {
335     B = bScroller.getValue();
336     bText.setText(B + "");
337     colourChanged();
338   }
339
340   public void colourChanged()
341   {
342     Color col = new Color(R, G, B);
343     target.setBackground(col);
344     target.repaint();
345
346     if (selectedButton != null)
347     {
348       selectedButton.setBackground(col);
349       selectedButton.repaint();
350     }
351   }
352
353   void setTargetColour(Color col)
354   {
355     R = col.getRed();
356     G = col.getGreen();
357     B = col.getBlue();
358
359     rScroller.setValue(R);
360     gScroller.setValue(G);
361     bScroller.setValue(B);
362     rText.setText(R + "");
363     gText.setText(G + "");
364     bText.setText(B + "");
365     colourChanged();
366   }
367
368   public void colourButtonPressed(MouseEvent e)
369   {
370     selectedButton = (Button) e.getSource();
371     setTargetColour(selectedButton.getBackground());
372   }
373
374   void makeButton(String label, String aa)
375   {
376     final Button button = new Button();
377     Color col = Color.white;
378     if (oldColourScheme != null)
379     {
380       try
381       {
382         col = oldColourScheme.findColour(aa.charAt(0), -1);
383       } catch (Exception ex)
384       {
385       }
386     }
387     button.setBackground(col);
388     oldColours.addElement(col);
389     button.setLabel(label);
390     button.setForeground(col.darker().darker().darker());
391     button.setFont(new java.awt.Font("Verdana", 1, 10));
392     button.addMouseListener(new java.awt.event.MouseAdapter()
393     {
394       public void mousePressed(MouseEvent e)
395       {
396         colourButtonPressed(e);
397       }
398     });
399
400     buttonPanel.add(button, null);
401   }
402
403   protected void okButton_actionPerformed()
404   {
405     applyButton_actionPerformed();
406     if (dialog != null)
407       dialog.setVisible(false);
408
409     frame.setVisible(false);
410   }
411
412   public Color getColor()
413   {
414     return new Color(R, G, B);
415   }
416
417   protected void applyButton_actionPerformed()
418   {
419     if (caller != null)
420     {
421       if (caller instanceof FeatureSettings)
422       {
423         ((FeatureSettings) caller).setUserColour(originalLabel, getColor());
424       }
425       else if (caller instanceof AnnotationColourChooser)
426       {
427         if (originalLabel.equals("Min Colour"))
428         {
429           ((AnnotationColourChooser) caller)
430                   .minColour_actionPerformed(getColor());
431         }
432         else
433         {
434           ((AnnotationColourChooser) caller)
435                   .maxColour_actionPerformed(getColor());
436         }
437       }
438       else if (caller instanceof FeatureRenderer)
439       {
440         ((FeatureRenderer) caller).colourPanel.updateColor(getColor());
441       }
442       else if (caller instanceof FeatureColourChooser)
443       {
444         if (originalLabel.indexOf("inimum") > -1)
445         {
446           ((FeatureColourChooser) caller)
447                   .minColour_actionPerformed(getColor());
448         }
449         else
450         {
451           ((FeatureColourChooser) caller)
452                   .maxColour_actionPerformed(getColor());
453         }
454       }
455
456       return;
457     }
458
459     Color[] newColours = new Color[24];
460     for (int i = 0; i < 24; i++)
461     {
462       Button button = (Button) buttonPanel.getComponent(i);
463       newColours[i] = button.getBackground();
464     }
465
466     UserColourScheme ucs = new UserColourScheme(newColours);
467     if (ap != null)
468     {
469       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
470     }
471
472     if (ap != null)
473     {
474       if (seqGroup != null)
475       {
476         seqGroup.cs = ucs;
477       }
478       else
479       {
480         ap.av.setGlobalColourScheme(ucs);
481       }
482       ap.seqPanel.seqCanvas.img = null;
483       ap.paintAlignment(true);
484     }
485     else if (jmol != null)
486     {
487       jmol.setJalviewColourScheme(ucs);
488     }
489     else if (pdbcanvas != null)
490     {
491       pdbcanvas.setColours(ucs);
492     }
493   }
494
495   protected void cancelButton_actionPerformed()
496   {
497     if (caller != null)
498     {
499       if (caller instanceof FeatureSettings)
500       {
501         ((FeatureSettings) caller).setUserColour(originalLabel,
502                 originalColour);
503       }
504       else if (caller instanceof AnnotationColourChooser)
505       {
506         if (originalLabel.equals("Min Colour"))
507         {
508           ((AnnotationColourChooser) caller)
509                   .minColour_actionPerformed((Color) originalColour);
510         }
511         else
512         {
513           ((AnnotationColourChooser) caller)
514                   .maxColour_actionPerformed((Color) originalColour);
515         }
516       }
517       else if (caller instanceof FeatureRenderer)
518       {
519         ((FeatureRenderer) caller).colourPanel.updateColor(originalColour);
520
521       }
522
523       else if (caller instanceof FeatureColourChooser)
524       {
525         if (originalLabel.indexOf("inimum") > -1)
526         {
527           ((FeatureColourChooser) caller)
528                   .minColour_actionPerformed((Color) originalColour);
529         }
530         else
531         {
532           ((FeatureColourChooser) caller)
533                   .maxColour_actionPerformed((Color) originalColour);
534         }
535       }
536       if (dialog != null)
537         dialog.setVisible(false);
538
539       frame.setVisible(false);
540       return;
541     }
542
543     Color[] newColours = new Color[24];
544     for (int i = 0; i < 24; i++)
545     {
546       newColours[i] = (Color) oldColours.elementAt(i);
547       buttonPanel.getComponent(i).setBackground(newColours[i]);
548     }
549
550     UserColourScheme ucs = new UserColourScheme(newColours);
551
552     if (ap != null)
553     {
554       if (seqGroup != null)
555       {
556         seqGroup.cs = ucs;
557       }
558       else
559       {
560         ap.av.setGlobalColourScheme(ucs);
561       }
562       ap.paintAlignment(true);
563     }
564     else if (jmol != null)
565     {
566       jmol.setJalviewColourScheme(ucs);
567     }
568     else if (pdbcanvas != null)
569     {
570       pdbcanvas.pdb.setColours(ucs);
571     }
572
573     frame.setVisible(false);
574   }
575
576   protected Panel buttonPanel = new Panel();
577
578   protected GridLayout gridLayout = new GridLayout();
579
580   Panel okcancelPanel = new Panel();
581
582   protected Button okButton = new Button();
583
584   protected Button applyButton = new Button();
585
586   protected Button cancelButton = new Button();
587
588   protected Scrollbar rScroller = new Scrollbar();
589
590   Label label1 = new Label();
591
592   protected TextField rText = new TextField();
593
594   Label label4 = new Label();
595
596   protected Scrollbar gScroller = new Scrollbar();
597
598   protected TextField gText = new TextField();
599
600   Label label5 = new Label();
601
602   protected Scrollbar bScroller = new Scrollbar();
603
604   protected TextField bText = new TextField();
605
606   protected Panel target = new Panel();
607
608   private void jbInit() throws Exception
609   {
610     this.setLayout(null);
611     buttonPanel.setLayout(gridLayout);
612     gridLayout.setColumns(6);
613     gridLayout.setRows(4);
614     okButton.setFont(new java.awt.Font("Verdana", 0, 11));
615     okButton.setLabel("OK");
616     okButton.addActionListener(this);
617     applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
618     applyButton.setLabel("Apply");
619     applyButton.addActionListener(this);
620     cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));
621     cancelButton.setLabel("Cancel");
622     cancelButton.addActionListener(this);
623     this.setBackground(new Color(212, 208, 223));
624     okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));
625     buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));
626     rScroller.setMaximum(256);
627     rScroller.setMinimum(0);
628     rScroller.setOrientation(0);
629     rScroller.setUnitIncrement(1);
630     rScroller.setVisibleAmount(1);
631     rScroller.setBounds(new Rectangle(36, 27, 119, 19));
632     rScroller.addAdjustmentListener(this);
633     label1.setAlignment(Label.RIGHT);
634     label1.setText("R");
635     label1.setBounds(new Rectangle(19, 30, 16, 15));
636     rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
637     rText.setText("0        ");
638     rText.setBounds(new Rectangle(156, 27, 53, 19));
639     rText.addActionListener(this);
640     label4.setAlignment(Label.RIGHT);
641     label4.setText("G");
642     label4.setBounds(new Rectangle(15, 56, 20, 15));
643     gScroller.setMaximum(256);
644     gScroller.setMinimum(0);
645     gScroller.setOrientation(0);
646     gScroller.setUnitIncrement(1);
647     gScroller.setVisibleAmount(1);
648     gScroller.setBounds(new Rectangle(35, 52, 120, 20));
649     gScroller.addAdjustmentListener(this);
650     gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
651     gText.setText("0        ");
652     gText.setBounds(new Rectangle(156, 52, 53, 20));
653     gText.addActionListener(this);
654     label5.setAlignment(Label.RIGHT);
655     label5.setText("B");
656     label5.setBounds(new Rectangle(14, 82, 20, 15));
657     bScroller.setMaximum(256);
658     bScroller.setMinimum(0);
659     bScroller.setOrientation(0);
660     bScroller.setUnitIncrement(1);
661     bScroller.setVisibleAmount(1);
662     bScroller.setBounds(new Rectangle(35, 78, 120, 20));
663     bScroller.addAdjustmentListener(this);
664     bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
665     bText.setText("0        ");
666     bText.setBounds(new Rectangle(157, 78, 52, 20));
667     bText.addActionListener(this);
668     target.setBackground(Color.black);
669     target.setBounds(new Rectangle(229, 26, 134, 79));
670     this.add(okcancelPanel, null);
671     okcancelPanel.add(okButton, null);
672     okcancelPanel.add(applyButton, null);
673     okcancelPanel.add(cancelButton, null);
674     this.add(buttonPanel, null);
675     this.add(target, null);
676     this.add(gScroller);
677     this.add(rScroller);
678     this.add(bScroller);
679     this.add(label5);
680     this.add(label4);
681     this.add(label1);
682     this.add(gText);
683     this.add(rText);
684     this.add(bText);
685   }
686
687 }