JAL-984 use cursor for drag regions to adjust annotation height or id width
[jalview.git] / src / jalview / appletgui / 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.appletgui;
22
23 import java.awt.Cursor;
24 import java.awt.Dimension;
25 import java.awt.Graphics;
26 import java.awt.Image;
27 import java.awt.Panel;
28 import java.awt.event.MouseEvent;
29 import java.awt.event.MouseListener;
30 import java.awt.event.MouseMotionListener;
31
32 public class IdwidthAdjuster extends Panel
33         implements MouseListener, MouseMotionListener
34 {
35   boolean active = false;
36
37   int oldX = 0;
38
39   Image image;
40
41   AlignmentPanel ap;
42
43   public IdwidthAdjuster(AlignmentPanel ap)
44   {
45     setLayout(null);
46     this.ap = ap;
47     java.net.URL url = getClass().getResource("/images/idwidth.gif");
48     if (url != null)
49     {
50       image = java.awt.Toolkit.getDefaultToolkit().getImage(url);
51     }
52
53     addMouseListener(this);
54     addMouseMotionListener(this);
55   }
56
57   @Override
58   public void mousePressed(MouseEvent evt)
59   {
60     oldX = evt.getX();
61   }
62
63   @Override
64   public void mouseReleased(MouseEvent evt)
65   {
66     active = false;
67     repaint();
68
69     /*
70      * If in a SplitFrame with co-scaled alignments, set the other's id width to
71      * match; note applet does not (yet) store this in ViewStyle
72      */
73     /*
74      * Code disabled for now as it doesn't work, don't know why; idCanvas width
75      * keeps resetting to a previous value (actually two alternating values!)
76      */
77     // final AlignViewportI viewport = ap.getAlignViewport();
78     // if (viewport.getCodingComplement() != null
79     // && viewport.isScaleProteinAsCdna())
80     // {
81     // Dimension d = ap.idPanel.idCanvas.getSize();
82     // SplitFrame sf = ap.alignFrame.getSplitFrame();
83     // final AlignmentPanel otherPanel =
84     // sf.getComplement(ap.alignFrame).alignPanel;
85     // otherPanel.setIdWidth(d.width, d.height);
86     // otherPanel.repaint();
87     // }
88   }
89
90   @Override
91   public void mouseEntered(MouseEvent evt)
92   {
93     active = true;
94     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
95
96     repaint();
97   }
98
99   @Override
100   public void mouseExited(MouseEvent evt)
101   {
102     active = false;
103     setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
104     repaint();
105   }
106
107   @Override
108   public void mouseDragged(MouseEvent evt)
109   {
110     active = true;
111     Dimension d = ap.idPanel.idCanvas.getSize();
112     int dif = evt.getX() - oldX;
113
114     final int newWidth = d.width + dif;
115     if (newWidth > 20 || dif > 0)
116     {
117       ap.setIdWidth(newWidth, d.height);
118       this.setSize(newWidth, getSize().height);
119       oldX = evt.getX();
120     }
121   }
122
123   @Override
124   public void mouseMoved(MouseEvent evt)
125   {
126   }
127
128   @Override
129   public void mouseClicked(MouseEvent evt)
130   {
131   }
132
133   @Override
134   public void paint(Graphics g)
135   {
136     // g.setColor(Color.white);
137     // g.fillRect(0, 0, getSize().width, getSize().height);
138     // if (active)
139     // {
140     // if (image != null)
141     // {
142     // g.drawImage(image, getSize().width - 20, 2, this);
143     // }
144     // }
145   }
146
147 }