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