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