JAL-244 Adjust wrapped alignment widths to always maintain a visible wrapped sequence...
[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   /**
50    * Creates a new IdwidthAdjuster object.
51    * 
52    * @param ap
53    *          DOCUMENT ME!
54    */
55   public IdwidthAdjuster(AlignmentPanel ap)
56   {
57     this.ap = ap;
58     setBackground(Color.white);
59     addMouseListener(this);
60     addMouseMotionListener(this);
61   }
62
63   /**
64    * Action on mouse pressed is to save the start position for any drag
65    * 
66    * @param evt
67    */
68   @Override
69   public void mousePressed(MouseEvent evt)
70   {
71     oldX = evt.getX();
72   }
73
74   /**
75    * On release of mouse drag to resize the width, if there is a complementary
76    * alignment in a split frame, sets the complement to the same id width and
77    * repaints the split frame. Note this is done whether or not the protein
78    * characters are scaled to codon width.
79    * 
80    * @param evt
81    */
82   @Override
83   public void mouseReleased(MouseEvent evt)
84   {
85     repaint();
86
87     /*
88      * If in a SplitFrame, set the other's id width to match
89      */
90     final AlignViewportI viewport = ap.getAlignViewport();
91     if (viewport.getCodingComplement() != null)
92     {
93       viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
94       SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer();
95       sf.repaint();
96     }
97   }
98
99   /**
100    * When this region is entered, repaints to show a left-right move cursor
101    * 
102    * @param evt
103    */
104   @Override
105   public void mouseEntered(MouseEvent evt)
106   {
107     repaint();
108   }
109
110   @Override
111   public void mouseExited(MouseEvent evt)
112   {
113   }
114
115   /**
116    * Adjusts the id panel width for a mouse drag left or right (subject to a
117    * minimum of 20 pixels) and repaints the alignment
118    * 
119    * @param evt
120    */
121   @Override
122   public void mouseDragged(MouseEvent evt)
123   {
124     int mouseX = evt.getX();
125     final AlignViewportI viewport = ap.getAlignViewport();
126     int curwidth = viewport.getIdWidth();
127     int dif = mouseX - oldX;
128
129     final int newWidth = curwidth + dif;
130
131     /*
132      * don't drag below minimum width
133      */
134     if (newWidth < MIN_ID_WIDTH)
135     {
136       return;
137     }
138
139     /*
140      * don't allow residue width to be < 1 in wrapped format
141      */
142     if (viewport.getWrapAlignment())
143     {
144       SeqCanvas sc = ap.getSeqPanel().seqCanvas;
145       if (sc != null && sc.getWrappedCanvasWidth(sc.getWidth() - dif) < 1)
146       {
147         return;
148       }
149     }
150
151     oldX = evt.getX();
152
153     /*
154      * don't drag right if mouse is to the left of the region
155      */
156     if (dif > 0 && mouseX < 0)
157     {
158       return;
159     }
160     viewport.setIdWidth(newWidth);
161     ap.validateAnnotationDimensions(false);
162     ap.paintAlignment(true, false);
163
164     ap.getIdPanel().getIdCanvas().setManuallyAdjusted(true);
165   }
166
167   public void setWidth(int newWidth)
168   {
169     if (newWidth < MIN_ID_WIDTH
170             || ap.getIdPanel().getIdCanvas().manuallyAdjusted())
171     {
172       return;
173     }
174     final AlignViewportI viewport = ap.getAlignViewport();
175     viewport.setIdWidth(newWidth);
176     ap.paintAlignment(true, false);
177   }
178
179   public boolean manuallyAdjusted()
180   {
181     return ap.getIdPanel().getIdCanvas().manuallyAdjusted();
182   }
183
184   @Override
185   public void mouseMoved(MouseEvent evt)
186   {
187   }
188
189   @Override
190   public void mouseClicked(MouseEvent evt)
191   {
192   }
193
194   /**
195    * Paints this region, showing a left-right move cursor if currently 'active'
196    * 
197    * @param g
198    */
199   @Override
200   public void paintComponent(Graphics g)
201   {
202     g.setColor(Color.white);
203     g.fillRect(0, 0, getWidth(), getHeight());
204     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
205   }
206 }