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