Reduced jar size by 20%
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import java.util.*;\r
23 \r
24 import java.awt.*;\r
25 import java.awt.event.*;\r
26 \r
27 import jalview.datamodel.*;\r
28 import jalview.schemes.*;\r
29 \r
30 public class UserDefinedColours\r
31     extends Panel implements ActionListener, AdjustmentListener\r
32 {\r
33 \r
34   AlignmentPanel ap;\r
35   SequenceGroup seqGroup;\r
36   Button selectedButton;\r
37   Vector oldColours = new Vector();\r
38   ColourSchemeI oldColourScheme;\r
39   Frame frame;\r
40   MCview.AppletPDBCanvas pdbcanvas;\r
41 \r
42   int R = 0, G = 0, B = 0;\r
43 \r
44   public ColourSchemeI loadDefaultColours()\r
45   {\r
46     // NOT IMPLEMENTED YET IN APPLET VERSION\r
47     return null;\r
48   }\r
49 \r
50   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
51   {\r
52     this.ap = ap;\r
53     seqGroup = sg;\r
54 \r
55     if (seqGroup != null)\r
56     {\r
57       oldColourScheme = seqGroup.cs;\r
58     }\r
59     else\r
60     {\r
61       oldColourScheme = ap.av.getGlobalColourScheme();\r
62     }\r
63 \r
64     init();\r
65   }\r
66 \r
67   public UserDefinedColours(MCview.AppletPDBCanvas pdb)\r
68   {\r
69     this.pdbcanvas = pdb;\r
70     init();\r
71   }\r
72 \r
73   public void actionPerformed(ActionEvent evt)\r
74   {\r
75     if(evt.getSource()==okButton)\r
76      okButton_actionPerformed();\r
77    else if(evt.getSource()==applyButton)\r
78       applyButton_actionPerformed();\r
79    else if(evt.getSource()==cancelButton)\r
80      cancelButton_actionPerformed();\r
81    else if(evt.getSource()==rText)\r
82      rText_actionPerformed();\r
83    else if (evt.getSource() == gText)\r
84      gText_actionPerformed();\r
85    else if (evt.getSource() == bText)\r
86      bText_actionPerformed();\r
87 }\r
88 \r
89   public void adjustmentValueChanged(AdjustmentEvent evt)\r
90   {\r
91     if(evt.getSource()==rScroller)\r
92     rScroller_adjustmentValueChanged();\r
93   else if(evt.getSource()==gScroller)\r
94     gScroller_adjustmentValueChanged();\r
95   else if(evt.getSource()==bScroller)\r
96     bScroller_adjustmentValueChanged();\r
97 }\r
98 \r
99   void init()\r
100   {\r
101     try\r
102     {\r
103       jbInit();\r
104     }\r
105     catch (Exception e)\r
106     {\r
107       e.printStackTrace();\r
108     }\r
109     frame = new Frame();\r
110     frame.add(this);\r
111     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420, 345);\r
112 \r
113     if (seqGroup != null)\r
114     {\r
115       frame.setTitle(frame.getTitle() + " (" + seqGroup.getName() + ")");\r
116     }\r
117 \r
118     for (int i = 0; i < 20; i++)\r
119     {\r
120       makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
121                  "", ResidueProperties.aa[i]);\r
122     }\r
123 \r
124     makeButton("B", "B");\r
125     makeButton("Z", "Z");\r
126     makeButton("X", "X");\r
127     makeButton("Gap", "'.','-',' '");\r
128 \r
129     validate();\r
130   }\r
131   protected void rText_actionPerformed()\r
132   {\r
133     try\r
134     {\r
135       int i = Integer.parseInt(rText.getText());\r
136       rScroller.setValue(i);\r
137       rScroller_adjustmentValueChanged();\r
138     }\r
139     catch (NumberFormatException ex)\r
140     {}\r
141   }\r
142 \r
143   protected void gText_actionPerformed()\r
144   {\r
145     try\r
146     {\r
147       int i = Integer.parseInt(gText.getText());\r
148       gScroller.setValue(i);\r
149       gScroller_adjustmentValueChanged();\r
150     }\r
151     catch (NumberFormatException ex)\r
152     {}\r
153 \r
154   }\r
155 \r
156   protected void bText_actionPerformed()\r
157   {\r
158     try\r
159     {\r
160       int i = Integer.parseInt(bText.getText());\r
161       bScroller.setValue(i);\r
162       bScroller_adjustmentValueChanged();\r
163     }\r
164     catch (NumberFormatException ex)\r
165     {}\r
166 \r
167   }\r
168 \r
169   protected void rScroller_adjustmentValueChanged()\r
170   {\r
171     R = rScroller.getValue();\r
172     rText.setText(R + "");\r
173     colourChanged();\r
174   }\r
175 \r
176   protected void gScroller_adjustmentValueChanged()\r
177   {\r
178     G = gScroller.getValue();\r
179     gText.setText(G + "");\r
180     colourChanged();\r
181   }\r
182 \r
183   protected void bScroller_adjustmentValueChanged()\r
184   {\r
185     B = bScroller.getValue();\r
186     bText.setText(B + "");\r
187     colourChanged();\r
188   }\r
189 \r
190   public void colourChanged()\r
191   {\r
192     Color col = new Color(R, G, B);\r
193     target.setBackground(col);\r
194     target.repaint();\r
195 \r
196     if (selectedButton != null)\r
197     {\r
198       selectedButton.setBackground(col);\r
199       selectedButton.repaint();\r
200     }\r
201   }\r
202 \r
203   public void colourButtonPressed(MouseEvent e)\r
204   {\r
205     selectedButton = (Button) e.getSource();\r
206     Color col = selectedButton.getBackground();\r
207     R = col.getRed();\r
208     G = col.getGreen();\r
209     B = col.getBlue();\r
210     rScroller.setValue(R);\r
211     gScroller.setValue(G);\r
212     bScroller.setValue(B);\r
213     rText.setText(R + "");\r
214     gText.setText(G + "");\r
215     bText.setText(B + "");\r
216 \r
217     colourChanged();\r
218   }\r
219 \r
220   void makeButton(String label, String aa)\r
221   {\r
222     final Button button = new Button();\r
223     Color col = Color.white;\r
224 \r
225     try\r
226     {\r
227       col = oldColourScheme.findColour(aa, -1);\r
228     }\r
229     catch (Exception ex)\r
230     {}\r
231 \r
232     button.setBackground(col);\r
233     oldColours.addElement(col);\r
234     button.setLabel(label);\r
235     button.setForeground(col.darker().darker().darker());\r
236     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
237     button.addMouseListener(new java.awt.event.MouseAdapter()\r
238     {\r
239       public void mousePressed(MouseEvent e)\r
240       {\r
241         colourButtonPressed(e);\r
242       }\r
243     });\r
244 \r
245     buttonPanel.add(button, null);\r
246   }\r
247 \r
248   protected void okButton_actionPerformed()\r
249   {\r
250     applyButton_actionPerformed();\r
251     frame.setVisible(false);\r
252   }\r
253 \r
254   protected void applyButton_actionPerformed()\r
255   {\r
256     Color[] newColours = new Color[24];\r
257     for (int i = 0; i < 24; i++)\r
258     {\r
259       Button button = (Button) buttonPanel.getComponent(i);\r
260       newColours[i] = button.getBackground();\r
261     }\r
262 \r
263     UserColourScheme ucs = new UserColourScheme(newColours);\r
264     if(ap!=null)\r
265       ucs.setThreshold(0, ap.av.getIgnoreGapsConsensus());\r
266 \r
267     if(ap!=null)\r
268     {\r
269       if (seqGroup != null)\r
270       {\r
271         seqGroup.cs = ucs;\r
272       }\r
273       else\r
274       {\r
275         ap.av.setGlobalColourScheme(ucs);\r
276       }\r
277       ap.seqPanel.seqCanvas.img = null;\r
278       ap.repaint();\r
279     }\r
280     else if(pdbcanvas!=null)\r
281     {\r
282       pdbcanvas.pdb.setColours(ucs);\r
283       pdbcanvas.updateSeqColours();\r
284     }\r
285   }\r
286 \r
287 \r
288   protected void cancelButton_actionPerformed()\r
289   {\r
290 \r
291     Color[] newColours = new Color[24];\r
292     for (int i = 0; i < 24; i++)\r
293     {\r
294       newColours[i] = (Color) oldColours.elementAt(i);\r
295       buttonPanel.getComponent(i).setBackground(newColours[i]);\r
296     }\r
297 \r
298     UserColourScheme ucs = new UserColourScheme(newColours);\r
299 \r
300    if(ap!=null)\r
301     {\r
302       if (seqGroup != null)\r
303       {\r
304         seqGroup.cs = ucs;\r
305       }\r
306       else\r
307       {\r
308         ap.av.setGlobalColourScheme(ucs);\r
309       }\r
310       ap.repaint();\r
311     }\r
312     else if(pdbcanvas!=null)\r
313     {\r
314       pdbcanvas.pdb.setColours(ucs);\r
315     }\r
316 \r
317     frame.setVisible(false);\r
318   }\r
319 \r
320 \r
321   protected Panel buttonPanel = new Panel();\r
322   protected GridLayout gridLayout = new GridLayout();\r
323   Panel jPanel2 = new Panel();\r
324   protected Button okButton = new Button();\r
325   protected Button applyButton = new Button();\r
326   protected Button cancelButton = new Button();\r
327   protected Scrollbar rScroller = new Scrollbar();\r
328   Label label1 = new Label();\r
329   protected TextField rText = new TextField();\r
330   Label label4 = new Label();\r
331   protected Scrollbar gScroller = new Scrollbar();\r
332   protected TextField gText = new TextField();\r
333   Label label5 = new Label();\r
334   protected Scrollbar bScroller = new Scrollbar();\r
335   protected TextField bText = new TextField();\r
336   protected Panel target = new Panel();\r
337 \r
338   private void jbInit() throws Exception {\r
339       this.setLayout(null);\r
340       buttonPanel.setLayout(gridLayout);\r
341       gridLayout.setColumns(6);\r
342       gridLayout.setRows(4);\r
343       okButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
344       okButton.setLabel("OK");\r
345       okButton.addActionListener(this);\r
346       applyButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
347       applyButton.setLabel("Apply");\r
348       applyButton.addActionListener(this);\r
349       cancelButton.setFont(new java.awt.Font("Verdana", 0, 11));\r
350       cancelButton.setLabel("Cancel");\r
351       cancelButton.addActionListener(this);\r
352       this.setBackground(new Color(212, 208, 223));\r
353       jPanel2.setBounds(new Rectangle(0, 265, 400, 35));\r
354       buttonPanel.setBounds(new Rectangle(0, 123, 400, 142));\r
355       rScroller.setMaximum(256);\r
356       rScroller.setMinimum(0);\r
357       rScroller.setOrientation(0);\r
358       rScroller.setUnitIncrement(1);\r
359   rScroller.setVisibleAmount(1);\r
360   rScroller.setBounds(new Rectangle(36, 27, 119, 19));\r
361   rScroller.addAdjustmentListener(this);\r
362   label1.setAlignment(Label.RIGHT);\r
363   label1.setText("R");\r
364   label1.setBounds(new Rectangle(19, 30, 16, 15));\r
365   rText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
366   rText.setText("0        ");\r
367   rText.setBounds(new Rectangle(156, 27, 53, 19));\r
368   rText.addActionListener(this);\r
369   label4.setAlignment(Label.RIGHT);\r
370   label4.setText("G");\r
371   label4.setBounds(new Rectangle(15, 56, 20, 15));\r
372   gScroller.setMaximum(256);\r
373       gScroller.setMinimum(0);\r
374       gScroller.setOrientation(0);\r
375       gScroller.setUnitIncrement(1);\r
376   gScroller.setVisibleAmount(1);\r
377   gScroller.setBounds(new Rectangle(35, 52, 120, 20));\r
378   gScroller.addAdjustmentListener(this);\r
379   gText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
380   gText.setText("0        ");\r
381   gText.setBounds(new Rectangle(156, 52, 53, 20));\r
382   gText.addActionListener(this);\r
383   label5.setAlignment(Label.RIGHT);\r
384   label5.setText("B");\r
385   label5.setBounds(new Rectangle(14, 82, 20, 15));\r
386   bScroller.setMaximum(256);\r
387       bScroller.setMinimum(0);\r
388       bScroller.setOrientation(0);\r
389       bScroller.setUnitIncrement(1);\r
390   bScroller.setVisibleAmount(1);\r
391   bScroller.setBounds(new Rectangle(35, 78, 120, 20));\r
392   bScroller.addAdjustmentListener(this);\r
393   bText.setFont(new java.awt.Font("Dialog", Font.PLAIN, 10));\r
394   bText.setText("0        ");\r
395   bText.setBounds(new Rectangle(157, 78, 52, 20));\r
396   bText.addActionListener(this);\r
397   target.setBackground(Color.black);\r
398       target.setBounds(new Rectangle(229, 26, 134, 79));\r
399       this.add(jPanel2, null);\r
400       jPanel2.add(okButton, null);\r
401       jPanel2.add(applyButton, null);\r
402       jPanel2.add(cancelButton, null);\r
403       this.add(buttonPanel, null);\r
404   this.add(target, null);\r
405   this.add(gScroller);\r
406   this.add(rScroller);\r
407   this.add(bScroller);\r
408   this.add(label5);\r
409   this.add(label4);\r
410   this.add(label1);\r
411   this.add(gText);\r
412   this.add(rText);\r
413   this.add(bText);\r
414 }\r
415 \r
416 }\r