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