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