updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2006 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
20 package jalview.appletgui;
21
22 import java.util.*;
23
24 import java.awt.*;
25 import java.awt.event.*;
26
27 import jalview.datamodel.*;
28 import jalview.schemes.*;
29
30 public class UserDefinedColours
31     extends Panel implements ActionListener, AdjustmentListener
32 {
33
34   AlignmentPanel ap;
35   SequenceGroup seqGroup;
36   Button selectedButton;
37   Vector oldColours = new Vector();
38   ColourSchemeI oldColourScheme;
39   Frame frame;
40   MCview.AppletPDBCanvas pdbcanvas;
41
42   Component caller;
43   String originalLabel;
44   Color originalColour;
45
46   int R = 0, G = 0, B = 0;
47
48   public ColourSchemeI loadDefaultColours()
49   {
50     // NOT IMPLEMENTED YET IN APPLET VERSION
51     return null;
52   }
53
54   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)
55   {
56     this.ap = ap;
57     seqGroup = sg;
58
59     if (seqGroup != null)
60     {
61       oldColourScheme = seqGroup.cs;
62     }
63     else
64     {
65       oldColourScheme = ap.av.getGlobalColourScheme();
66     }
67
68     init();
69   }
70
71   public UserDefinedColours(MCview.AppletPDBCanvas pdb)
72   {
73     this.pdbcanvas = pdb;
74     init();
75   }
76
77   public UserDefinedColours(Component caller,
78                             String label,
79                             Color colour)
80   {
81     this.caller = caller;
82     originalColour = colour;
83     originalLabel = label;
84     init();
85     remove(buttonPanel);
86
87     setTargetColour(colour);
88
89     okcancelPanel.setBounds(new Rectangle(0, 113, 400, 35));
90     frame.setTitle("User Defined Colours - "+label);
91     frame.setSize(420, 200);
92   }
93
94   public void actionPerformed(ActionEvent evt)
95   {
96     if(evt.getSource()==okButton)
97      okButton_actionPerformed();
98    else if(evt.getSource()==applyButton)
99       applyButton_actionPerformed();
100    else if(evt.getSource()==cancelButton)
101      cancelButton_actionPerformed();
102    else if(evt.getSource()==rText)
103      rText_actionPerformed();
104    else if (evt.getSource() == gText)
105      gText_actionPerformed();
106    else if (evt.getSource() == bText)
107      bText_actionPerformed();
108 }
109
110   public void adjustmentValueChanged(AdjustmentEvent evt)
111   {
112     if(evt.getSource()==rScroller)
113     rScroller_adjustmentValueChanged();
114   else if(evt.getSource()==gScroller)
115     gScroller_adjustmentValueChanged();
116   else if(evt.getSource()==bScroller)
117     bScroller_adjustmentValueChanged();
118 }
119
120   void init()
121   {
122     try
123     {
124       jbInit();
125     }
126     catch (Exception e)
127     {
128       e.printStackTrace();
129     }
130     frame = new Frame();
131     frame.add(this);
132     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420, 345);
133
134     if (seqGroup != null)
135     {
136       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");
137     }
138
139     for (int i = 0; i < 20; i++)
140     {
141       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +
142                  "", ResidueProperties.aa[i]);
143     }
144
145     makeButton("B", "B");
146     makeButton("Z", "Z");
147     makeButton("X", "X");
148     makeButton("Gap", "'.','-',' '");
149
150     validate();
151   }
152   protected void rText_actionPerformed()
153   {
154     try
155     {
156       int i = Integer.parseInt(rText.getText());
157       rScroller.setValue(i);
158       rScroller_adjustmentValueChanged();
159     }
160     catch (NumberFormatException ex)
161     {}
162   }
163
164   protected void gText_actionPerformed()
165   {
166     try
167     {
168       int i = Integer.parseInt(gText.getText());
169       gScroller.setValue(i);
170       gScroller_adjustmentValueChanged();
171     }
172     catch (NumberFormatException ex)
173     {}
174
175   }
176
177   protected void bText_actionPerformed()
178   {
179     try
180     {
181       int i = Integer.parseInt(bText.getText());
182       bScroller.setValue(i);
183       bScroller_adjustmentValueChanged();
184     }
185     catch (NumberFormatException ex)
186     {}
187
188   }
189
190   protected void rScroller_adjustmentValueChanged()
191   {
192     R = rScroller.getValue();
193     rText.setText(R + "");
194     colourChanged();
195   }
196
197   protected void gScroller_adjustmentValueChanged()
198   {
199     G = gScroller.getValue();
200     gText.setText(G + "");
201     colourChanged();
202   }
203
204   protected void bScroller_adjustmentValueChanged()
205   {
206     B = bScroller.getValue();
207     bText.setText(B + "");
208     colourChanged();
209   }
210
211   public void colourChanged()
212   {
213     Color col = new Color(R, G, B);
214     target.setBackground(col);
215     target.repaint();
216
217     if (selectedButton != null)
218     {
219       selectedButton.setBackground(col);
220       selectedButton.repaint();
221     }
222   }
223
224   void setTargetColour(Color col)
225   {
226     R = col.getRed();
227     G = col.getGreen();
228     B = col.getBlue();
229     rScroller.setValue(R);
230     gScroller.setValue(G);
231     bScroller.setValue(B);
232     rText.setText(R + "");
233     gText.setText(G + "");
234     bText.setText(B + "");
235     colourChanged();
236   }
237
238   public void colourButtonPressed(MouseEvent e)
239   {
240     selectedButton = (Button) e.getSource();
241     setTargetColour(selectedButton.getBackground());
242   }
243
244   void makeButton(String label, String aa)
245   {
246     final Button button = new Button();
247     Color col = Color.white;
248
249     try
250     {
251       col = oldColourScheme.findColour(aa, -1);
252     }
253     catch (Exception ex)
254     {}
255
256     button.setBackground(col);
257     oldColours.addElement(col);
258     button.setLabel(label);
259     button.setForeground(col.darker().darker().darker());
260     button.setFont(new java.awt.Font("Verdana", 1, 10));
261     button.addMouseListener(new java.awt.event.MouseAdapter()
262     {
263       public void mousePressed(MouseEvent e)
264       {
265         colourButtonPressed(e);
266       }
267     });
268
269     buttonPanel.add(button, null);
270   }
271
272   protected void okButton_actionPerformed()
273   {
274     applyButton_actionPerformed();
275     frame.setVisible(false);
276   }
277
278   protected void applyButton_actionPerformed()
279   {
280     if (caller != null)
281     {
282       if(caller instanceof FeatureSettings)
283         ((FeatureSettings)caller).setUserColour
284             (originalLabel, new Color(R,G,B));
285       else if(caller instanceof AnnotationColourChooser)
286       {
287         if (originalLabel.equals("Min Colour"))
288           ( (AnnotationColourChooser) caller).minColour_actionPerformed
289               (new Color(R, G, B));
290         else
291           ( (AnnotationColourChooser) caller).maxColour_actionPerformed
292               (new Color(R, G, B));
293       }
294       return;
295     }
296
297
298     Color[] newColours = new Color[24];
299     for (int i = 0; i < 24; i++)
300     {
301       Button button = (Button) buttonPanel.getComponent(i);
302       newColours[i] = button.getBackground();
303     }
304
305     UserColourScheme ucs = new UserColourScheme(newColours);
306     if(ap!=null)
307       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
308
309     if(ap!=null)
310     {
311       if (seqGroup != null)
312       {
313         seqGroup.cs = ucs;
314       }
315       else
316       {
317         ap.av.setGlobalColourScheme(ucs);
318       }
319       ap.seqPanel.seqCanvas.img = null;
320       ap.repaint();
321     }
322     else if(pdbcanvas!=null)
323     {
324       pdbcanvas.pdb.setColours(ucs);
325       pdbcanvas.updateSeqColours();
326     }
327   }
328
329
330   protected void cancelButton_actionPerformed()
331   {
332     if (caller != null)
333     {
334       if(caller instanceof FeatureSettings)
335         ((FeatureSettings)caller).setUserColour
336             (originalLabel, originalColour);
337       else if (caller instanceof AnnotationColourChooser)
338       {
339         if (originalLabel.equals("Min Colour"))
340           ( (AnnotationColourChooser) caller).minColour_actionPerformed
341               (originalColour);
342         else
343           ( (AnnotationColourChooser) caller).maxColour_actionPerformed
344               (originalColour);
345       }
346       frame.setVisible(false);
347       return;
348     }
349
350     Color[] newColours = new Color[24];
351     for (int i = 0; i < 24; i++)
352     {
353       newColours[i] = (Color) oldColours.elementAt(i);
354       buttonPanel.getComponent(i).setBackground(newColours[i]);
355     }
356
357     UserColourScheme ucs = new UserColourScheme(newColours);
358
359    if(ap!=null)
360     {
361       if (seqGroup != null)
362       {
363         seqGroup.cs = ucs;
364       }
365       else
366       {
367         ap.av.setGlobalColourScheme(ucs);
368       }
369       ap.repaint();
370     }
371     else if(pdbcanvas!=null)
372     {
373       pdbcanvas.pdb.setColours(ucs);
374     }
375
376     frame.setVisible(false);
377   }
378
379
380   protected Panel buttonPanel = new Panel();
381   protected GridLayout gridLayout = new GridLayout();
382   Panel okcancelPanel = new Panel();
383   protected Button okButton = new Button();
384   protected Button applyButton = new Button();
385   protected Button cancelButton = new Button();
386   protected Scrollbar rScroller = new Scrollbar();
387   Label label1 = new Label();
388   protected TextField rText = new TextField();
389   Label label4 = new Label();
390   protected Scrollbar gScroller = new Scrollbar();
391   protected TextField gText = new TextField();
392   Label label5 = new Label();
393   protected Scrollbar bScroller = new Scrollbar();
394   protected TextField bText = new TextField();
395   protected Panel target = new Panel();
396
397   private void jbInit() throws Exception {
398       this.setLayout(null);
399       buttonPanel.setLayout(gridLayout);
400       gridLayout.setColumns(6);
401       gridLayout.setRows(4);
402       okButton.setFont(new java.awt.Font("Verdana", 0, 11));
403       okButton.setLabel("OK");
404       okButton.addActionListener(this);
405       applyButton.setFont(new java.awt.Font("Verdana", 0, 11));
406       applyButton.setLabel("Apply");
407       applyButton.addActionListener(this);
408       cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));
409       cancelButton.setLabel("Cancel");
410       cancelButton.addActionListener(this);
411       this.setBackground(new Color(212, 208, 223));
412       okcancelPanel.setBounds(new Rectangle(0, 265, 400, 35));
413       buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));
414       rScroller.setMaximum(256);
415       rScroller.setMinimum(0);
416       rScroller.setOrientation(0);
417       rScroller.setUnitIncrement(1);
418   rScroller.setVisibleAmount(1);
419   rScroller.setBounds(new Rectangle(36, 27, 119, 19));
420   rScroller.addAdjustmentListener(this);
421   label1.setAlignment(Label.RIGHT);
422   label1.setText("R");
423   label1.setBounds(new Rectangle(19, 30, 16, 15));
424   rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
425   rText.setText("0        ");
426   rText.setBounds(new Rectangle(156, 27, 53, 19));
427   rText.addActionListener(this);
428   label4.setAlignment(Label.RIGHT);
429   label4.setText("G");
430   label4.setBounds(new Rectangle(15, 56, 20, 15));
431   gScroller.setMaximum(256);
432       gScroller.setMinimum(0);
433       gScroller.setOrientation(0);
434       gScroller.setUnitIncrement(1);
435   gScroller.setVisibleAmount(1);
436   gScroller.setBounds(new Rectangle(35, 52, 120, 20));
437   gScroller.addAdjustmentListener(this);
438   gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
439   gText.setText("0        ");
440   gText.setBounds(new Rectangle(156, 52, 53, 20));
441   gText.addActionListener(this);
442   label5.setAlignment(Label.RIGHT);
443   label5.setText("B");
444   label5.setBounds(new Rectangle(14, 82, 20, 15));
445   bScroller.setMaximum(256);
446       bScroller.setMinimum(0);
447       bScroller.setOrientation(0);
448       bScroller.setUnitIncrement(1);
449   bScroller.setVisibleAmount(1);
450   bScroller.setBounds(new Rectangle(35, 78, 120, 20));
451   bScroller.addAdjustmentListener(this);
452   bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));
453   bText.setText("0        ");
454   bText.setBounds(new Rectangle(157, 78, 52, 20));
455   bText.addActionListener(this);
456   target.setBackground(Color.black);
457       target.setBounds(new Rectangle(229, 26, 134, 79));
458       this.add(okcancelPanel, null);
459       okcancelPanel.add(okButton, null);
460       okcancelPanel.add(applyButton, null);
461       okcancelPanel.add(cancelButton, null);
462       this.add(buttonPanel, null);
463   this.add(target, null);
464   this.add(gScroller);
465   this.add(rScroller);
466   this.add(bScroller);
467   this.add(label5);
468   this.add(label4);
469   this.add(label1);
470   this.add(gText);
471   this.add(rText);
472   this.add(bText);
473 }
474
475 }