JAL-3253 simpler coding using MouseAdapter. Using JLabel.setOpaque(true)
[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.Cursor;
27 import java.awt.event.MouseAdapter;
28 import java.awt.event.MouseEvent;
29
30 import javax.swing.JPanel;
31
32 /**
33  * DOCUMENT ME!
34  * 
35  * @author $author$
36  * @version $Revision$
37  */
38 @SuppressWarnings("serial")
39 public class IdwidthAdjuster extends JPanel
40 {
41
42   int oldX = 0;
43
44   /**
45    * Creates a new IdwidthAdjuster object above the id panel that can be dragged
46    * to change the panel's width
47    * 
48    * @param ap
49    *          The associated AlignmentPanel
50    * 
51    */
52   public IdwidthAdjuster(AlignmentPanel ap)
53   {
54     // BH 2019.07.01 no need for overridden paintComponent, especially as it was
55     // overriding paint(g) so
56     // allowing strange component painting in its place.
57
58     setBackground(Color.white);
59     setOpaque(true);
60
61     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
62
63     // BH 2019.07.01 MouseAdapter is cleaner - no need for fields active or ap
64
65     MouseAdapter ma = (new MouseAdapter()
66     {
67       @Override
68       public void mousePressed(MouseEvent evt)
69       {
70         oldX = evt.getX();
71       }
72
73       @Override
74       public void mouseDragged(MouseEvent evt)
75       {
76         AlignViewportI viewport = ap.getAlignViewport();
77         int curwidth = viewport.getIdWidth();
78         int dif = evt.getX() - oldX;
79         int newWidth = curwidth + dif;
80         if ((newWidth > 20) || (dif > 0))
81         {
82           viewport.setIdWidth(newWidth);
83           ap.paintAlignment(true, false);
84         }
85         oldX = evt.getX();
86       }
87
88       @Override
89       public void mouseReleased(MouseEvent evt)
90       {
91
92         // If in a SplitFrame with co-scaled alignments, set the other's id
93         // width to match
94
95         AlignViewportI viewport = ap.getAlignViewport();
96         if (viewport.getCodingComplement() != null
97                 && viewport.isScaleProteinAsCdna())
98         {
99           viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
100           SplitFrame sf = (SplitFrame) ap.alignFrame
101                   .getSplitViewContainer();
102           sf.repaint();
103         }
104
105       }
106
107     });
108     addMouseListener(ma);
109     addMouseMotionListener(ma);
110   }
111 }