78943b8eb2a06421dda22b9e09200470f4794ac7
[jalview.git] / src / jalview / appletgui / IdwidthAdjuster.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.awt.*;
23 import java.awt.event.*;
24
25 public class IdwidthAdjuster
26     extends Panel implements MouseListener, MouseMotionListener
27 {
28   boolean active = false;
29   int oldX = 0;
30   Image image;
31   AlignmentPanel ap;
32
33   public IdwidthAdjuster(AlignmentPanel ap)
34   {
35     setLayout(null);
36     this.ap = ap;
37     java.net.URL url = getClass().getResource("/images/idwidth.gif");
38     if (url != null)
39     {
40       image = java.awt.Toolkit.getDefaultToolkit().getImage(url);
41     }
42
43     addMouseListener(this);
44     addMouseMotionListener(this);
45   }
46
47   public void mousePressed(MouseEvent evt)
48   {
49     oldX = evt.getX();
50   }
51
52   public void mouseReleased(MouseEvent evt)
53   {
54     active = false;
55     repaint();
56   }
57
58   public void mouseEntered(MouseEvent evt)
59   {
60     active = true;
61     repaint();
62   }
63
64   public void mouseExited(MouseEvent evt)
65   {
66     active = false;
67     repaint();
68   }
69
70   public void mouseDragged(MouseEvent evt)
71   {
72     active = true;
73     Dimension d = ap.idPanel.idCanvas.getSize();
74     int dif = evt.getX() - oldX;
75
76     if (d.width + dif > 20 || dif > 0)
77     {
78       ap.setIdWidth(d.width + dif, d.height);
79       this.setSize(d.width + dif, getSize().height);
80     }
81
82     oldX = evt.getX();
83   }
84
85   public void mouseMoved(MouseEvent evt)
86   {}
87
88   public void mouseClicked(MouseEvent evt)
89   {}
90
91   public void paint(Graphics g)
92   {
93     g.setColor(Color.white);
94     g.fillRect(0, 0, getSize().width, getSize().height);
95     if (active)
96     {
97       if (image != null)
98       {
99         g.drawImage(image, getSize().width - 20, 2, this);
100       }
101     }
102   }
103
104 }