JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / components / GradientEditorPanel.java
1 package fr.orsay.lri.varna.components;
2
3 import java.awt.Color;
4 import java.awt.Cursor;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.RenderingHints;
8 import java.awt.event.MouseEvent;
9 import java.awt.event.MouseListener;
10 import java.awt.event.MouseMotionListener;
11
12 import javax.swing.JColorChooser;
13 import javax.swing.JPanel;
14
15 import fr.orsay.lri.varna.models.rna.ModeleColorMap;
16
17 public class GradientEditorPanel extends JPanel implements MouseListener, MouseMotionListener{
18                 ModeleColorMap _mcm;
19                 
20                 public GradientEditorPanel (ModeleColorMap mcm)
21                 { 
22                         _mcm = mcm;
23                         this.addMouseListener( this);
24                         this.addMouseMotionListener(this);
25                 }
26                 
27                 public void setColorMap(ModeleColorMap mcm)
28                 {
29                         _mcm = mcm;
30                         repaint();
31                         firePropertyChange("PaletteChanged","a","b");
32                 }
33                 
34                 public ModeleColorMap getColorMap()
35                 {
36                         return _mcm;
37                 }
38
39                 private final static int TRIGGERS_SEMI_WIDTH = 2;
40                 private final static int PALETTE_HEIGHT = 11;
41                 private final static int REMOVE_HEIGHT = 11;
42                 private final static int TOLERANCE = 5;
43                 private final static int GAP = 4;
44                 private final Color EDGES = Color.gray.brighter();
45                 private final Color BUTTONS = Color.LIGHT_GRAY.brighter();
46                 
47                 public int getStartChoose()
48                 {
49                         return getHeight()-PALETTE_HEIGHT-REMOVE_HEIGHT-GAP-1;
50                 }
51                 
52                 public int getEndChoose()
53                 {
54                         return getStartChoose()+PALETTE_HEIGHT;
55                 }
56                 
57                 public int getStartRemove()
58                 {
59                         return getEndChoose()+GAP;
60                 }
61
62                 public int getEndRemove()
63                 {
64                         return getStartRemove()+REMOVE_HEIGHT;
65                 }
66                 
67                 private int getStripeHeight()
68                 {
69                         return getHeight()-PALETTE_HEIGHT-REMOVE_HEIGHT-2*GAP-1;
70                 }
71                 
72                 private Color alterColor(Color c, int inc)
73                 {
74                         int nr = Math.min(Math.max(c.getRed()+inc, 0),255);
75                         int ng = Math.min(Math.max(c.getGreen()+inc, 0),255);
76                         int nb = Math.min(Math.max(c.getBlue()+inc, 0),255);
77                         return new Color(nr,ng,nb);
78                 }
79
80                 public void paintComponent(Graphics g)
81                 {
82                         super.paintComponent(g);
83                         Graphics2D g2d = (Graphics2D)  g;
84                         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
85                                         RenderingHints.VALUE_ANTIALIAS_ON);
86                         int height = getStripeHeight();
87                         double v1 = _mcm.getMinValue();
88                         double v2 = _mcm.getMaxValue();
89                         for (int i=0;i<=getWidth();i++)
90                         {
91                                 double ratio = (((double)i)/((double)getWidth()));
92                                 double val = v1+(v2-v1)*ratio;
93                                 g2d.setColor(_mcm.getColorForValue(val));
94                                 g2d.drawLine(i, 0, i, height);  
95                         }
96                         for(int i=0;i<_mcm.getNumColors();i++)
97                         {
98                                 double val = _mcm.getValueAt(i);
99                                 Color c = _mcm.getColorAt(i);
100                                 double norm = (val-_mcm.getMinValue())/(_mcm.getMaxValue()-_mcm.getMinValue());
101                                 int x = (int)(norm*(getWidth()-1));
102                                 // Target
103                                 g2d.setColor(c);
104                                 g2d.fillRect(x-TRIGGERS_SEMI_WIDTH+1, 0, 2*TRIGGERS_SEMI_WIDTH-1, getHeight()-1);                                       
105                                 g2d.setColor(EDGES);
106                                 g2d.drawLine(x-TRIGGERS_SEMI_WIDTH, 0, x-TRIGGERS_SEMI_WIDTH, getHeight());                                     
107                                 g2d.drawLine(x+TRIGGERS_SEMI_WIDTH, 0, x+TRIGGERS_SEMI_WIDTH, getHeight());
108                                 
109                                 if (i==0)
110                                 {
111                                         // Choose Color
112                                         g2d.setColor(EDGES);
113                                         g2d.drawRect(x,height+GAP,PALETTE_HEIGHT,2*PALETTE_HEIGHT+GAP);                                 
114                                         g2d.setColor(c);
115                                         g2d.fillRect(x+1,height+GAP+1,PALETTE_HEIGHT-1,2*PALETTE_HEIGHT+GAP-1);
116                                 }
117                                 else if (i==_mcm.getNumColors()-1)
118                                 {
119                                         // Choose Color
120                                         g2d.setColor(EDGES);
121                                         g2d.drawRect(x-PALETTE_HEIGHT,height+GAP,PALETTE_HEIGHT,2*PALETTE_HEIGHT+GAP);                                  
122                                         g2d.setColor(c);
123                                         g2d.fillRect(x-PALETTE_HEIGHT+1,height+GAP+1,PALETTE_HEIGHT-1,2*PALETTE_HEIGHT+GAP-1);                                  
124                                 }
125                                 else
126                                 {
127                                 // Choose Color
128                                 g2d.setColor(EDGES);
129                                 g2d.drawRect(x-PALETTE_HEIGHT/2,height+GAP,PALETTE_HEIGHT,PALETTE_HEIGHT);                                      
130                                 g2d.setColor(alterColor(c,-15));
131                                 g2d.fillRect(x-PALETTE_HEIGHT/2+1,height+GAP+1,PALETTE_HEIGHT-1,PALETTE_HEIGHT-1);                                      
132                                 g2d.setColor(c);
133                                 g2d.fillOval(x-PALETTE_HEIGHT/2+1,height+GAP+1,PALETTE_HEIGHT-1,PALETTE_HEIGHT-1);
134                                 g2d.setColor(alterColor(c,10));
135                                 g2d.fillOval(x-PALETTE_HEIGHT/2+1+2,height+GAP+1+2,PALETTE_HEIGHT-1-4,PALETTE_HEIGHT-1-4);
136
137
138                                 // Remove Color
139                                 g2d.setColor(EDGES);
140                                 g2d.drawRect(x-PALETTE_HEIGHT/2,height+2*GAP+PALETTE_HEIGHT,REMOVE_HEIGHT,REMOVE_HEIGHT);                                       
141                                 g2d.setColor(BUTTONS);
142                                 g2d.fillRect(x-PALETTE_HEIGHT/2+1,height+2*GAP+1+PALETTE_HEIGHT,REMOVE_HEIGHT-1,REMOVE_HEIGHT-1);                                               
143                                 int xcross1 = x-PALETTE_HEIGHT/2+2;
144                                 int ycross1 = height+2*GAP+PALETTE_HEIGHT +2;
145                                 int xcross2 = xcross1+REMOVE_HEIGHT-4;
146                                 int ycross2 = ycross1+REMOVE_HEIGHT-4;
147                                 g2d.setColor(Color.red);
148                                 g2d.drawLine(xcross1, ycross1, xcross2 , ycross2);                              
149                                 g2d.drawLine(xcross1, ycross2, xcross2 , ycross1);
150                                 }
151                         }
152
153                 }
154                 
155                 private boolean isChooseColor(int x, int y)
156                 {
157                         if (_selectedIndex != -1)
158                         {
159                                 if ((_selectedIndex ==0)||(_selectedIndex == _mcm.getNumColors()-1))
160                                         return (y<=getEndRemove() && y>=getStartChoose() && Math.abs(getXPos(_selectedIndex)-x)<=PALETTE_HEIGHT);
161                                 if (y<=getEndChoose() && y>=getStartChoose())
162                                 {
163                                         return Math.abs(getXPos(_selectedIndex)-x)<=PALETTE_HEIGHT/2;
164                                 }
165                         }
166                         return false;
167                 }
168                 
169                 private boolean isRemove(int x, int y)
170                 {
171                         if (_selectedIndex != -1)
172                         {
173                                 if ((_selectedIndex ==0)||(_selectedIndex == _mcm.getNumColors()-1))
174                                         return false;
175                                 if (y<=getEndRemove() && y>=getStartRemove())
176                                 {
177                                         return Math.abs(getXPos(_selectedIndex)-x)<=PALETTE_HEIGHT/2;
178                                 }
179                         }
180                         return false;
181                 }
182                 
183                 private int getXPos(int i)
184                 {
185                         double val = _mcm.getValueAt(i);
186                         double norm = (val-_mcm.getMinValue())/(_mcm.getMaxValue()-_mcm.getMinValue());
187                         return (int)(norm*(getWidth()-1));                      
188                 }
189
190                 
191                 private int locateSelectedIndex(int x, int y)
192                 {
193                         double dist = Double.MAX_VALUE;
194                         int index = -1;
195                         for(int i=0;i<_mcm.getNumColors();i++)
196                         {
197                                 int xp = getXPos(i);
198                                 double tmpDist = Math.abs(x-xp);
199                                 if (tmpDist<dist)
200                                 {
201                                         index = i;
202                                         dist = tmpDist; 
203                                 }
204                         }       
205                         return index;
206                 }
207                 
208                 private int _selectedIndex = -1;
209                 
210                 public void mouseClicked(MouseEvent arg0) {
211                         _selectedIndex = locateSelectedIndex(arg0.getX(),arg0.getY());
212                         if (_selectedIndex!=-1)
213                         {
214                                 if (isRemove(arg0.getX(),arg0.getY()))
215                                 {
216                                         removeEntry(_selectedIndex);
217                                 }
218                                 else if (Math.abs(getXPos(_selectedIndex)-arg0.getX())>TOLERANCE)
219                                 {
220                                         double val = _mcm.getMinValue()+ arg0.getX()*(_mcm.getMaxValue()-_mcm.getMinValue())/(getWidth()-1);
221                                         Color nc = JColorChooser.showDialog(this, "Choose new color" ,_mcm.getColorAt(_selectedIndex));
222                                         if (nc != null)
223                                         {
224                                                 _mcm.addColor(val, nc);
225                                                 repaint();
226                                                 firePropertyChange("PaletteChanged","a","b");
227                                         }                                       
228                                 }
229                         }
230                 }
231
232                 public void mouseEntered(MouseEvent arg0) {
233                         // TODO Auto-generated method stub
234                         
235                 }
236
237                 public void mouseExited(MouseEvent arg0) {
238                         // TODO Auto-generated method stub
239                         
240                 }
241
242                 public void mousePressed(MouseEvent arg0) {
243                         requestFocus();
244                         _selectedIndex = locateSelectedIndex(arg0.getX(),arg0.getY());
245                         if (_selectedIndex!=-1)
246                         {
247                                 if (isChooseColor(arg0.getX(),arg0.getY()))
248                                 {
249                                         Color nc = JColorChooser.showDialog(this, "Choose new color" ,_mcm.getColorAt(_selectedIndex));
250                                         if (nc != null)
251                                         {
252                                                 double nv = _mcm.getValueAt(_selectedIndex);
253                                                 replaceEntry(_selectedIndex, nc, nv);
254                                                 _selectedIndex = -1;
255                                         }
256                                 }
257                         }
258                 }
259
260                 public void mouseReleased(MouseEvent arg0) {
261                         _selectedIndex = -1;
262                 }
263
264                 private void replaceEntry(int index, Color nc, double nv)
265                 {
266                         ModeleColorMap cm = new ModeleColorMap();
267                         for(int i=0;i<_mcm.getNumColors();i++)
268                         {
269                                 if (i!=index)
270                                 {
271                                         double val = _mcm.getValueAt(i);
272                                         Color c = _mcm.getColorAt(i);
273                                         cm.addColor(val, c);
274                                 }
275                                 else
276                                 {
277                                         cm.addColor(nv, nc);
278                                 }
279                         }
280                         _mcm = cm;
281                         repaint();
282                         firePropertyChange("PaletteChanged","a","b");
283                 }
284
285                 private void removeEntry(int index)
286                 {
287                         ModeleColorMap cm = new ModeleColorMap();
288                         for(int i=0;i<_mcm.getNumColors();i++)
289                         {
290                                 if (i!=index)
291                                 {
292                                         double val = _mcm.getValueAt(i);
293                                         Color c = _mcm.getColorAt(i);
294                                         cm.addColor(val, c);
295                                 }
296                         }
297                         _mcm = cm;
298                         repaint();
299                         firePropertyChange("PaletteChanged","a","b");
300                 }
301                 
302                 
303                 public void mouseDragged(MouseEvent arg0) {
304                         if ((_selectedIndex!=-1)&&(_selectedIndex!=0)&&(_selectedIndex!=_mcm.getNumColors()-1))
305                         {
306                                 Color c = _mcm.getColorAt(_selectedIndex);
307                                 double val = _mcm.getMinValue()+ arg0.getX()*(_mcm.getMaxValue()-_mcm.getMinValue())/(getWidth()-1);
308                                 replaceEntry(_selectedIndex, c, val);
309                         }
310                 }
311
312                 public void mouseMoved(MouseEvent arg0) {
313                         Cursor c = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
314                         _selectedIndex = locateSelectedIndex(arg0.getX(),arg0.getY());
315                         if (_selectedIndex!=-1)
316                         {
317                                 if (isChooseColor(arg0.getX(),arg0.getY()))
318                                 {
319                                         c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
320                                 }
321                                 else if ((_selectedIndex != 0)&&(_selectedIndex != _mcm.getNumColors()-1))
322                                 {
323                                         if (isRemove(arg0.getX(),arg0.getY()))
324                                         {
325                                                 c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);                                     
326                                         }
327                                         else if (Math.abs(getXPos(_selectedIndex)-arg0.getX())<=TOLERANCE)
328                                         {
329                                                 c = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
330                                         }
331                                         else if (arg0.getY()<getHeight()-this.getStripeHeight())
332                                         {
333                                                 c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);                                                                             
334                                         }
335                                         else
336                                         {
337                                                 Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
338                                         }
339                                 }
340                         }
341                         setCursor(c);
342                 }
343         }
344