JAL-244 Changes to Annotation labels width now adjusts the overall idWidth if larger...
[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 java.awt.Color;
24 import java.awt.Cursor;
25 import java.awt.Graphics;
26 import java.awt.event.MouseEvent;
27 import java.awt.event.MouseListener;
28 import java.awt.event.MouseMotionListener;
29
30 import javax.swing.JPanel;
31
32 import jalview.api.AlignViewportI;
33
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class IdwidthAdjuster extends JPanel
41         implements MouseListener, MouseMotionListener
42 {
43   public static final int MIN_ID_WIDTH = 20;
44
45   int oldX = 0;
46
47   AlignmentPanel ap;
48
49   private boolean manuallyAdjusted = false;
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     setBackground(Color.white);
61     addMouseListener(this);
62     addMouseMotionListener(this);
63   }
64
65   /**
66    * Action on mouse pressed is to save the start position for any drag
67    * 
68    * @param evt
69    */
70   @Override
71   public void mousePressed(MouseEvent evt)
72   {
73     oldX = evt.getX();
74   }
75
76   /**
77    * On release of mouse drag to resize the width, if there is a complementary
78    * alignment in a split frame, sets the complement to the same id width and
79    * repaints the split frame. Note this is done whether or not the protein
80    * characters are scaled to codon width.
81    * 
82    * @param evt
83    */
84   @Override
85   public void mouseReleased(MouseEvent evt)
86   {
87     repaint();
88
89     /*
90      * If in a SplitFrame, set the other's id width to match
91      */
92     final AlignViewportI viewport = ap.getAlignViewport();
93     if (viewport.getCodingComplement() != null)
94     {
95       viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
96       SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer();
97       sf.repaint();
98     }
99   }
100
101   /**
102    * When this region is entered, repaints to show a left-right move cursor
103    * 
104    * @param evt
105    */
106   @Override
107   public void mouseEntered(MouseEvent evt)
108   {
109     repaint();
110   }
111
112   @Override
113   public void mouseExited(MouseEvent evt)
114   {
115   }
116
117   /**
118    * Adjusts the id panel width for a mouse drag left or right (subject to a
119    * minimum of 20 pixels) and repaints the alignment
120    * 
121    * @param evt
122    */
123   @Override
124   public void mouseDragged(MouseEvent evt)
125   {
126     int mouseX = evt.getX();
127     final AlignViewportI viewport = ap.getAlignViewport();
128     int curwidth = viewport.getIdWidth();
129     int dif = mouseX - oldX;
130
131     final int newWidth = curwidth + dif;
132
133     /*
134      * don't drag below minimum width
135      */
136     if (newWidth < MIN_ID_WIDTH)
137     {
138       return;
139     }
140
141     oldX = evt.getX();
142
143     /*
144      * don't drag right if mouse is to the left of the region
145      */
146     if (dif > 0 && mouseX < 0)
147     {
148       return;
149     }
150     viewport.setIdWidth(newWidth);
151     ap.validateAnnotationDimensions(false);
152     ap.paintAlignment(true, false);
153
154     manuallyAdjusted = true;
155   }
156
157   public void setWidth(int newWidth)
158   {
159     if (newWidth < MIN_ID_WIDTH)
160     {
161       return;
162     }
163     final AlignViewportI viewport = ap.getAlignViewport();
164     viewport.setIdWidth(newWidth);
165     ap.paintAlignment(true, false);
166   }
167
168   public boolean manuallyAdjusted()
169   {
170     return manuallyAdjusted;
171   }
172
173   @Override
174   public void mouseMoved(MouseEvent evt)
175   {
176   }
177
178   @Override
179   public void mouseClicked(MouseEvent evt)
180   {
181   }
182
183   /**
184    * Paints this region, showing a left-right move cursor if currently 'active'
185    * 
186    * @param g
187    */
188   @Override
189   public void paintComponent(Graphics g)
190   {
191     g.setColor(Color.white);
192     g.fillRect(0, 0, getWidth(), getHeight());
193     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
194   }
195 }