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