4596e1f096ed8b5629681fcf0ee147173b23f0ff
[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)
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     viewport.setIdWidth(newWidth);
162     ap.validateAnnotationDimensions(false);
163     ap.paintAlignment(true, false);
164
165     ap.getIdPanel().getIdCanvas().setManuallyAdjusted(true);
166   }
167
168   public void setWidth(int newWidth)
169   {
170     if (newWidth < MIN_ID_WIDTH
171             || ap.getIdPanel().getIdCanvas().manuallyAdjusted())
172     {
173       return;
174     }
175     final AlignViewportI viewport = ap.getAlignViewport();
176     viewport.setIdWidth(newWidth);
177     ap.paintAlignment(true, false);
178   }
179
180   public boolean manuallyAdjusted()
181   {
182     return ap.getIdPanel().getIdCanvas().manuallyAdjusted();
183   }
184
185   @Override
186   public void mouseMoved(MouseEvent evt)
187   {
188   }
189
190   @Override
191   public void mouseClicked(MouseEvent evt)
192   {
193   }
194
195   /**
196    * Paints this region, showing a left-right move cursor if currently 'active'
197    * 
198    * @param g
199    */
200   @Override
201   public void paintComponent(Graphics g)
202   {
203     int width = getWidth();
204     int height = getHeight();
205     g.setColor(Color.white);
206     g.fillRect(0, 0, width, height);
207
208     if (!Cache.getDefault(AnnotationLabels.RESIZE_MARGINS_MARK_PREF, false))
209     // && !ap.getAlignViewport().getWrapAlignment()) // now allowing adjustment
210     // in wrap mode
211     {
212       int spacer = Math.max(2, AnnotationLabels.HEIGHT_ADJUSTER_HEIGHT / 4);
213       g.setColor(Color.LIGHT_GRAY);
214       g.drawLine(width - 3 * spacer, 0, width - 3 * spacer, height / 2);
215       g.drawLine(width - spacer, 0, width - spacer, height / 2);
216     }
217
218     setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
219   }
220 }