1 package fr.orsay.lri.varna.components;
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;
12 import javax.swing.JColorChooser;
13 import javax.swing.JPanel;
15 import fr.orsay.lri.varna.models.rna.ModeleColorMap;
17 public class GradientEditorPanel extends JPanel implements MouseListener, MouseMotionListener{
20 public GradientEditorPanel (ModeleColorMap mcm)
23 this.addMouseListener( this);
24 this.addMouseMotionListener(this);
27 public void setColorMap(ModeleColorMap mcm)
31 firePropertyChange("PaletteChanged","a","b");
34 public ModeleColorMap getColorMap()
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();
47 public int getStartChoose()
49 return getHeight()-PALETTE_HEIGHT-REMOVE_HEIGHT-GAP-1;
52 public int getEndChoose()
54 return getStartChoose()+PALETTE_HEIGHT;
57 public int getStartRemove()
59 return getEndChoose()+GAP;
62 public int getEndRemove()
64 return getStartRemove()+REMOVE_HEIGHT;
67 private int getStripeHeight()
69 return getHeight()-PALETTE_HEIGHT-REMOVE_HEIGHT-2*GAP-1;
72 private Color alterColor(Color c, int inc)
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);
80 public void paintComponent(Graphics g)
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++)
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);
96 for(int i=0;i<_mcm.getNumColors();i++)
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));
104 g2d.fillRect(x-TRIGGERS_SEMI_WIDTH+1, 0, 2*TRIGGERS_SEMI_WIDTH-1, getHeight()-1);
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());
113 g2d.drawRect(x,height+GAP,PALETTE_HEIGHT,2*PALETTE_HEIGHT+GAP);
115 g2d.fillRect(x+1,height+GAP+1,PALETTE_HEIGHT-1,2*PALETTE_HEIGHT+GAP-1);
117 else if (i==_mcm.getNumColors()-1)
121 g2d.drawRect(x-PALETTE_HEIGHT,height+GAP,PALETTE_HEIGHT,2*PALETTE_HEIGHT+GAP);
123 g2d.fillRect(x-PALETTE_HEIGHT+1,height+GAP+1,PALETTE_HEIGHT-1,2*PALETTE_HEIGHT+GAP-1);
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);
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);
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);
155 private boolean isChooseColor(int x, int y)
157 if (_selectedIndex != -1)
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())
163 return Math.abs(getXPos(_selectedIndex)-x)<=PALETTE_HEIGHT/2;
169 private boolean isRemove(int x, int y)
171 if (_selectedIndex != -1)
173 if ((_selectedIndex ==0)||(_selectedIndex == _mcm.getNumColors()-1))
175 if (y<=getEndRemove() && y>=getStartRemove())
177 return Math.abs(getXPos(_selectedIndex)-x)<=PALETTE_HEIGHT/2;
183 private int getXPos(int i)
185 double val = _mcm.getValueAt(i);
186 double norm = (val-_mcm.getMinValue())/(_mcm.getMaxValue()-_mcm.getMinValue());
187 return (int)(norm*(getWidth()-1));
191 private int locateSelectedIndex(int x, int y)
193 double dist = Double.MAX_VALUE;
195 for(int i=0;i<_mcm.getNumColors();i++)
198 double tmpDist = Math.abs(x-xp);
208 private int _selectedIndex = -1;
210 public void mouseClicked(MouseEvent arg0) {
211 _selectedIndex = locateSelectedIndex(arg0.getX(),arg0.getY());
212 if (_selectedIndex!=-1)
214 if (isRemove(arg0.getX(),arg0.getY()))
216 removeEntry(_selectedIndex);
218 else if (Math.abs(getXPos(_selectedIndex)-arg0.getX())>TOLERANCE)
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));
224 _mcm.addColor(val, nc);
226 firePropertyChange("PaletteChanged","a","b");
232 public void mouseEntered(MouseEvent arg0) {
233 // TODO Auto-generated method stub
237 public void mouseExited(MouseEvent arg0) {
238 // TODO Auto-generated method stub
242 public void mousePressed(MouseEvent arg0) {
244 _selectedIndex = locateSelectedIndex(arg0.getX(),arg0.getY());
245 if (_selectedIndex!=-1)
247 if (isChooseColor(arg0.getX(),arg0.getY()))
249 Color nc = JColorChooser.showDialog(this, "Choose new color" ,_mcm.getColorAt(_selectedIndex));
252 double nv = _mcm.getValueAt(_selectedIndex);
253 replaceEntry(_selectedIndex, nc, nv);
260 public void mouseReleased(MouseEvent arg0) {
264 private void replaceEntry(int index, Color nc, double nv)
266 ModeleColorMap cm = new ModeleColorMap();
267 for(int i=0;i<_mcm.getNumColors();i++)
271 double val = _mcm.getValueAt(i);
272 Color c = _mcm.getColorAt(i);
282 firePropertyChange("PaletteChanged","a","b");
285 private void removeEntry(int index)
287 ModeleColorMap cm = new ModeleColorMap();
288 for(int i=0;i<_mcm.getNumColors();i++)
292 double val = _mcm.getValueAt(i);
293 Color c = _mcm.getColorAt(i);
299 firePropertyChange("PaletteChanged","a","b");
303 public void mouseDragged(MouseEvent arg0) {
304 if ((_selectedIndex!=-1)&&(_selectedIndex!=0)&&(_selectedIndex!=_mcm.getNumColors()-1))
306 Color c = _mcm.getColorAt(_selectedIndex);
307 double val = _mcm.getMinValue()+ arg0.getX()*(_mcm.getMaxValue()-_mcm.getMinValue())/(getWidth()-1);
308 replaceEntry(_selectedIndex, c, val);
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)
317 if (isChooseColor(arg0.getX(),arg0.getY()))
319 c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
321 else if ((_selectedIndex != 0)&&(_selectedIndex != _mcm.getNumColors()-1))
323 if (isRemove(arg0.getX(),arg0.getY()))
325 c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
327 else if (Math.abs(getXPos(_selectedIndex)-arg0.getX())<=TOLERANCE)
329 c = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
331 else if (arg0.getY()<getHeight()-this.getStripeHeight())
333 c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
337 Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);