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