JAL-4367 Limit right and top adjustments
[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 import jalview.bin.Cache;
34
35 /**
36  * DOCUMENT ME!
37  * 
38  * @author $author$
39  * @version $Revision$
40  */
41 public class IdwidthAdjuster extends JPanel
42         implements MouseListener, MouseMotionListener
43 {
44   public static final int MIN_ID_WIDTH = 20;
45
46   int oldX = 0;
47
48   AlignmentPanel ap;
49
50   /**
51    * Creates a new IdwidthAdjuster object.
52    * 
53    * @param ap
54    *          DOCUMENT ME!
55    */
56   public IdwidthAdjuster(AlignmentPanel ap)
57   {
58     this.ap = ap;
59     setBackground(Color.white);
60     addMouseListener(this);
61     addMouseMotionListener(this);
62   }
63
64   /**
65    * Action on mouse pressed is to save the start position for any drag
66    * 
67    * @param evt
68    */
69   @Override
70   public void mousePressed(MouseEvent evt)
71   {
72     oldX = evt.getX();
73   }
74
75   /**
76    * On release of mouse drag to resize the width, if there is a complementary
77    * alignment in a split frame, sets the complement to the same id width and
78    * repaints the split frame. Note this is done whether or not the protein
79    * characters are scaled to codon width.
80    * 
81    * @param evt
82    */
83   @Override
84   public void mouseReleased(MouseEvent evt)
85   {
86     repaint();
87
88     /*
89      * If in a SplitFrame, set the other's id width to match
90      */
91     final AlignViewportI viewport = ap.getAlignViewport();
92     if (viewport.getCodingComplement() != null)
93     {
94       viewport.getCodingComplement().setIdWidth(viewport.getIdWidth());
95       SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer();
96       sf.repaint();
97     }
98   }
99
100   /**
101    * When this region is entered, repaints to show a left-right move cursor
102    * 
103    * @param evt
104    */
105   @Override
106   public void mouseEntered(MouseEvent evt)
107   {
108     repaint();
109   }
110
111   @Override
112   public void mouseExited(MouseEvent evt)
113   {
114   }
115
116   /**
117    * Adjusts the id panel width for a mouse drag left or right (subject to a
118    * minimum of 20 pixels) and repaints the alignment
119    * 
120    * @param evt
121    */
122   @Override
123   public void mouseDragged(MouseEvent evt)
124   {
125     int mouseX = evt.getX();
126     final AlignViewportI viewport = ap.getAlignViewport();
127     int curwidth = viewport.getIdWidth();
128     int dif = mouseX - oldX;
129
130     final int newWidth = curwidth + dif;
131
132     /*
133      * don't drag below minimum width
134      */
135     if (newWidth < MIN_ID_WIDTH || newWidth > this.ap.getWidth())
136     {
137       return;
138     }
139
140     /*
141      * don't allow residue width to be < 1 in wrapped format
142      */
143     if (viewport.getWrapAlignment())
144     {
145       SeqCanvas sc = ap.getSeqPanel().seqCanvas;
146       if (sc != null && sc.getWrappedCanvasWidth(sc.getWidth() - dif) < 1)
147       {
148         return;
149       }
150     }
151
152     oldX = evt.getX();
153
154     /*
155      * don't drag right if mouse is to the left of the region
156      */
157     if (dif > 0 && mouseX < 0)
158     {
159       return;
160     }
161
162     // TODO JAL-4260 - work out how to trigger recomputation of wrapped pages !
163     int curCol = viewport.getRanges().getStartRes()
164             + viewport.getRanges().getViewportWidth();
165
166     viewport.setIdWidth(newWidth);
167
168     ap.validateAnnotationDimensions(false);
169     if (viewport.getWrapAlignment())
170     {
171       viewport.getRanges().scrollToWrappedVisible(
172               curCol - viewport.getRanges().getViewportWidth());
173     }
174     ap.paintAlignment(true, false);
175
176     ap.getIdPanel().getIdCanvas().setManuallyAdjusted(true);
177   }
178
179   public void setWidth(int newWidth)
180   {
181     if (newWidth < MIN_ID_WIDTH
182             || ap.getIdPanel().getIdCanvas().isManuallyAdjusted())
183     {
184       return;
185     }
186     final AlignViewportI viewport = ap.getAlignViewport();
187     viewport.setIdWidth(newWidth);
188     ap.paintAlignment(true, false);
189   }
190
191   public boolean manuallyAdjusted()
192   {
193     return ap.getIdPanel().getIdCanvas().isManuallyAdjusted();
194   }
195
196   @Override
197   public void mouseMoved(MouseEvent evt)
198   {
199   }
200
201   @Override
202   public void mouseClicked(MouseEvent evt)
203   {
204   }
205
206   /**
207    * Paints this region, showing a left-right move cursor if currently 'active'
208    * 
209    * @param g
210    */
211   @Override
212   public void paintComponent(Graphics g)
213   {
214     int width = getWidth();
215     int height = getHeight();
216     g.setColor(Color.white);
217     g.fillRect(0, 0, width, height);
218
219     if (!Cache.getDefault(AnnotationLabels.RESIZE_MARGINS_MARK_PREF, false))
220     // && !ap.getAlignViewport().getWrapAlignment()) // now allowing adjustment
221     // in wrap mode
222     {
223       int spacer = Math.max(2, AnnotationLabels.HEIGHT_ADJUSTER_HEIGHT / 4);
224       g.setColor(Color.LIGHT_GRAY);
225       g.drawLine(width - 3 * spacer, 0, width - 3 * spacer, height / 2);
226       g.drawLine(width - spacer, 0, width - spacer, height / 2);
227     }
228
229     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
230   }
231 }