0c03db17026eb5729b5577e4aa3dc76466655fce
[jalview.git] / src / jalview / gui / IdwidthAdjuster.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import jalview.api.AlignViewportI;
24
25 import java.awt.Color;
26 import java.awt.Graphics;
27 import java.awt.Image;
28 import java.awt.event.MouseEvent;
29 import java.awt.event.MouseListener;
30 import java.awt.event.MouseMotionListener;
31
32 import javax.swing.JPanel;
33
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class IdwidthAdjuster extends JPanel implements MouseListener,
41         MouseMotionListener
42 {
43   boolean active = false;
44
45   int oldX = 0;
46
47   Image image;
48
49   AlignmentPanel ap;
50
51   /**
52    * Creates a new IdwidthAdjuster object.
53    * 
54    * @param ap
55    *          DOCUMENT ME!
56    */
57   public IdwidthAdjuster(AlignmentPanel ap)
58   {
59     this.ap = ap;
60
61     java.net.URL url = getClass().getResource("/images/idwidth.gif");
62
63     if (url != null)
64     {
65       image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
66     }
67
68     addMouseListener(this);
69     addMouseMotionListener(this);
70   }
71
72   /**
73    * DOCUMENT ME!
74    * 
75    * @param evt
76    *          DOCUMENT ME!
77    */
78   public void mousePressed(MouseEvent evt)
79   {
80     oldX = evt.getX();
81   }
82
83   /**
84    * DOCUMENT ME!
85    * 
86    * @param evt
87    *          DOCUMENT ME!
88    */
89   public void mouseReleased(MouseEvent evt)
90   {
91     active = false;
92     repaint();
93
94     /*
95      * If in a SplitFrame with co-scaled alignments, set the other's id width to
96      * match
97      */
98     final AlignViewportI viewport = ap.getAlignViewport();
99     if (viewport.getCodingComplement() != null
100             && viewport.isScaleProteinAsCdna())
101     {
102       viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
103       SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer();
104       sf.repaint();
105     }
106
107   }
108
109   /**
110    * DOCUMENT ME!
111    * 
112    * @param evt
113    *          DOCUMENT ME!
114    */
115   public void mouseEntered(MouseEvent evt)
116   {
117     active = true;
118     repaint();
119   }
120
121   /**
122    * DOCUMENT ME!
123    * 
124    * @param evt
125    *          DOCUMENT ME!
126    */
127   public void mouseExited(MouseEvent evt)
128   {
129     active = false;
130     repaint();
131   }
132
133   /**
134    * DOCUMENT ME!
135    * 
136    * @param evt
137    *          DOCUMENT ME!
138    */
139   public void mouseDragged(MouseEvent evt)
140   {
141     active = true;
142
143     final AlignViewportI viewport = ap.getAlignViewport();
144     int curwidth = viewport.getIdWidth();
145     int dif = evt.getX() - oldX;
146
147     final int newWidth = curwidth + dif;
148     if ((newWidth > 20) || (dif > 0))
149     {
150       viewport.setIdWidth(newWidth);
151
152       ap.paintAlignment(true);
153     }
154
155     oldX = evt.getX();
156   }
157
158   /**
159    * DOCUMENT ME!
160    * 
161    * @param evt
162    *          DOCUMENT ME!
163    */
164   public void mouseMoved(MouseEvent evt)
165   {
166   }
167
168   /**
169    * DOCUMENT ME!
170    * 
171    * @param evt
172    *          DOCUMENT ME!
173    */
174   public void mouseClicked(MouseEvent evt)
175   {
176   }
177
178   /**
179    * DOCUMENT ME!
180    * 
181    * @param g
182    *          DOCUMENT ME!
183    */
184   public void paintComponent(Graphics g)
185   {
186     g.setColor(Color.white);
187     g.fillRect(0, 0, getWidth(), getHeight());
188
189     if (active)
190     {
191       if (image != null)
192       {
193         g.drawImage(image, getWidth() - 20, 2, this);
194       }
195     }
196   }
197 }