Applet files
[jalview.git] / src / jalview / appletgui / UserDefinedColours.java
1 package jalview.appletgui;\r
2 \r
3 import jalview.jbappletgui.GUserDefinedColours;\r
4 import jalview.datamodel.*;\r
5 import jalview.schemes.*;\r
6 import jalview.io.*;\r
7 import java.io.*;\r
8 import java.awt.*;\r
9 import java.awt.event.*;\r
10 import java.util.*;\r
11 \r
12 \r
13 public class UserDefinedColours extends GUserDefinedColours\r
14 {\r
15 \r
16   AlignmentPanel ap;\r
17   SequenceGroup seqGroup;\r
18   Button selectedButton;\r
19   Vector oldColours = new Vector();\r
20   ColourSchemeI oldColourScheme;\r
21   Frame frame;\r
22 \r
23   int R = 0, G = 0, B = 0;\r
24 \r
25   public UserDefinedColours(AlignmentPanel ap, SequenceGroup sg)\r
26   {\r
27     super();\r
28     frame = new Frame();\r
29     frame.add(this);\r
30     jalview.bin.JalviewLite.addFrame(frame, "User defined colours", 420,345 );\r
31 \r
32     if(sg!=null)\r
33         frame.setTitle( frame.getTitle()+ " ("+sg.getName()+")");\r
34 \r
35 \r
36     this.ap = ap;\r
37     seqGroup = sg;\r
38 \r
39 \r
40       if (seqGroup != null)\r
41         oldColourScheme = seqGroup.cs;\r
42       else\r
43         oldColourScheme = ap.av.getGlobalColourScheme();\r
44 \r
45       for (int i = 0; i < 20; i++)\r
46         makeButton(ResidueProperties.aa2Triplet.get(ResidueProperties.aa[i]) +\r
47                    "", ResidueProperties.aa[i]);\r
48 \r
49       makeButton("B", "B");\r
50       makeButton("Z", "Z");\r
51       makeButton("X", "X");\r
52       makeButton("Gap", "'.','-',' '");\r
53 \r
54   }\r
55 \r
56   protected void rText_actionPerformed(ActionEvent e) {\r
57     try{\r
58       int i = Integer.parseInt(rText.getText());\r
59       rScroller.setValue(i);\r
60     }catch(NumberFormatException ex){}\r
61   }\r
62 \r
63   protected void gText_actionPerformed(ActionEvent e) {\r
64     try{\r
65       int i = Integer.parseInt(gText.getText());\r
66       gScroller.setValue(i);\r
67     }catch(NumberFormatException ex){}\r
68 \r
69   }\r
70 \r
71   protected void bText_actionPerformed(ActionEvent e) {\r
72     try{\r
73       int i = Integer.parseInt(bText.getText());\r
74       bScroller.setValue(i);\r
75     }catch(NumberFormatException ex){}\r
76 \r
77   }\r
78 \r
79 \r
80   protected void rScroller_adjustmentValueChanged(AdjustmentEvent e) {\r
81     R = rScroller.getValue();\r
82     rText.setText(R+"");\r
83     colourChanged();\r
84   }\r
85 \r
86   protected void gScroller_adjustmentValueChanged(AdjustmentEvent e) {\r
87     G = gScroller.getValue();\r
88     gText.setText(G+"");\r
89     colourChanged();\r
90   }\r
91 \r
92   protected void bScroller_adjustmentValueChanged(AdjustmentEvent e) {\r
93     B = bScroller.getValue();\r
94     bText.setText(B+"");\r
95     colourChanged();\r
96   }\r
97 \r
98  public void colourChanged()\r
99  {\r
100    Color col = new Color(R,G,B);\r
101    target.setBackground( col );\r
102 \r
103    if(selectedButton!=null)\r
104      selectedButton.setBackground( col );\r
105  }\r
106 \r
107   public void colourButtonPressed(MouseEvent e)\r
108   {\r
109      selectedButton = (Button)e.getSource();\r
110      Color col = selectedButton.getBackground();\r
111      R = col.getRed();\r
112      G = col.getGreen();\r
113      B = col.getBlue();\r
114      rScroller.setValue(R);\r
115      gScroller.setValue(G);\r
116      bScroller.setValue(B);\r
117      rText.setText(R+"");\r
118      gText.setText(G+"");\r
119      bText.setText(B+"");\r
120 \r
121      colourChanged();\r
122   }\r
123 \r
124   void makeButton(String label, String aa)\r
125   {\r
126     final Button button = new Button();\r
127     Color col = Color.white;\r
128 \r
129 \r
130     try{\r
131       col = oldColourScheme.findColour(aa, -1);\r
132     }catch(Exception ex){}\r
133 \r
134     button.setBackground(  col );\r
135     oldColours.addElement( col );\r
136     button.setLabel( label );\r
137     button.setForeground( col.darker().darker().darker() );\r
138     button.setFont(new java.awt.Font("Verdana", 1, 10));\r
139     button.addMouseListener(new java.awt.event.MouseAdapter()\r
140     {\r
141       public void mouseClicked(MouseEvent e)\r
142       {\r
143         colourButtonPressed(e);\r
144       }\r
145     });\r
146 \r
147     buttonPanel.add(button, null);\r
148   }\r
149 \r
150 \r
151   protected void okButton_actionPerformed(ActionEvent e)\r
152   {\r
153     applyButton_actionPerformed(null);\r
154    frame.setVisible(false);\r
155   }\r
156 \r
157   protected void applyButton_actionPerformed(ActionEvent e)\r
158   {\r
159 \r
160     Color [] newColours = new Color[24];\r
161     for(int i=0; i<24; i++)\r
162     {\r
163           Button button = (Button)buttonPanel.getComponent(i);\r
164           newColours[i] = button.getBackground();\r
165     }\r
166 \r
167     UserColourScheme ucs = new UserColourScheme();\r
168     ucs.setThreshold(0);\r
169     ucs.setColourScheme( newColours );\r
170 \r
171     if(seqGroup!=null)\r
172       seqGroup.cs = ucs;\r
173     else\r
174       ap.av.setGlobalColourScheme(ucs);\r
175 \r
176     ap.repaint();\r
177   }\r
178 \r
179 \r
180   public static UserColourScheme loadDefaultColours(String file)\r
181   {\r
182     UserColourScheme ucs = null;\r
183     try\r
184     {\r
185       BufferedReader in = new BufferedReader(new FileReader(file));\r
186       Color [] newColours = new Color[24];\r
187       for (int i = 0; i < 24; i++)\r
188       {\r
189         newColours[i] = new Color(Integer.parseInt(in.readLine()));\r
190       }\r
191 \r
192       ucs = new UserColourScheme();\r
193       ucs.setThreshold(0);\r
194       ucs.setColourScheme(newColours);\r
195 \r
196     }\r
197     catch (Exception ex)\r
198     {}\r
199 \r
200     return ucs;\r
201   }\r
202 \r
203 \r
204   protected void cancelButton_actionPerformed(ActionEvent e)\r
205   {\r
206     UserColourScheme ucs = new UserColourScheme();\r
207     Color [] newColours = new Color[24];\r
208     for(int i=0; i<24; i++)\r
209     {\r
210           newColours[i] = (Color)oldColours.elementAt(i);\r
211           buttonPanel.getComponent(i).setBackground(newColours[i]);\r
212     }\r
213 \r
214     ucs.setColourScheme( newColours );\r
215     if (seqGroup != null)\r
216       seqGroup.cs = ucs;\r
217     else\r
218       ap.av.setGlobalColourScheme(ucs);\r
219 \r
220     ap.repaint();\r
221 \r
222     frame.setVisible(false);\r
223   }\r
224 \r
225 \r
226 }\r