JAL-2664 Updates following review
[jalview.git] / src / jalview / appletgui / ScalePanel.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.appletgui;
22
23 import jalview.datamodel.ColumnSelection;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.renderer.ScaleRenderer;
27 import jalview.renderer.ScaleRenderer.ScaleMark;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.ViewportListenerI;
30 import jalview.viewmodel.ViewportRanges;
31
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.MenuItem;
36 import java.awt.Panel;
37 import java.awt.PopupMenu;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.InputEvent;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.awt.event.MouseMotionListener;
44 import java.beans.PropertyChangeEvent;
45 import java.util.List;
46
47 public class ScalePanel extends Panel implements MouseMotionListener,
48         MouseListener, ViewportListenerI
49 {
50
51   protected int offy = 4;
52
53   public int width;
54
55   protected AlignViewport av;
56
57   AlignmentPanel ap;
58
59   boolean stretchingGroup = false;
60
61   int min; // used by mouseDragged to see if user
62
63   int max; // used by mouseDragged to see if user
64
65   boolean mouseDragging = false;
66
67   int[] reveal;
68
69   public ScalePanel(AlignViewport av, AlignmentPanel ap)
70   {
71     setLayout(null);
72     this.av = av;
73     this.ap = ap;
74
75     addMouseListener(this);
76     addMouseMotionListener(this);
77
78     av.getRanges().addPropertyChangeListener(this);
79   }
80
81   @Override
82   public void mousePressed(MouseEvent evt)
83   {
84     int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
85     final int res;
86
87     if (av.hasHiddenColumns())
88     {
89       res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(x);
90     }
91     else
92     {
93       res = x;
94     }
95
96     min = res;
97     max = res;
98     if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
99     {
100       rightMouseButtonPressed(evt, res);
101     }
102     else
103     {
104       leftMouseButtonPressed(evt, res);
105     }
106   }
107
108   /**
109    * Handles left mouse button pressed (selection / clear selections)
110    * 
111    * @param evt
112    * @param res
113    */
114   protected void leftMouseButtonPressed(MouseEvent evt, final int res)
115   {
116     if (!evt.isControlDown() && !evt.isShiftDown())
117     {
118       av.getColumnSelection().clear();
119     }
120
121     av.getColumnSelection().addElement(res);
122     SequenceGroup sg = new SequenceGroup();
123     for (int i = 0; i < av.getAlignment().getSequences().size(); i++)
124     {
125       sg.addSequence(av.getAlignment().getSequenceAt(i), false);
126     }
127
128     sg.setStartRes(res);
129     sg.setEndRes(res);
130     av.setSelectionGroup(sg);
131
132     if (evt.isShiftDown())
133     {
134       int min = Math.min(av.getColumnSelection().getMin(), res);
135       int max = Math.max(av.getColumnSelection().getMax(), res);
136       for (int i = min; i < max; i++)
137       {
138         av.getColumnSelection().addElement(i);
139       }
140       sg.setStartRes(min);
141       sg.setEndRes(max);
142     }
143     ap.paintAlignment(false);
144     av.sendSelection();
145   }
146
147   /**
148    * Handles right mouse button press. If pressed in a selected column, opens
149    * context menu for 'Hide Columns'. If pressed on a hidden columns marker,
150    * opens context menu for 'Reveal / Reveal All'. Else does nothing.
151    * 
152    * @param evt
153    * @param res
154    */
155   protected void rightMouseButtonPressed(MouseEvent evt, final int res)
156   {
157     PopupMenu pop = new PopupMenu();
158     if (reveal != null)
159     {
160       MenuItem item = new MenuItem(MessageManager.getString("label.reveal"));
161       item.addActionListener(new ActionListener()
162       {
163         @Override
164         public void actionPerformed(ActionEvent e)
165         {
166           av.showColumn(reveal[0]);
167           reveal = null;
168           ap.paintAlignment(true);
169           av.sendSelection();
170         }
171       });
172       pop.add(item);
173
174       if (av.getAlignment().getHiddenColumns().hasManyHiddenColumns())
175       {
176         item = new MenuItem(MessageManager.getString("action.reveal_all"));
177         item.addActionListener(new ActionListener()
178         {
179           @Override
180           public void actionPerformed(ActionEvent e)
181           {
182             av.showAllHiddenColumns();
183             reveal = null;
184             ap.paintAlignment(true);
185             av.sendSelection();
186           }
187         });
188         pop.add(item);
189       }
190       this.add(pop);
191       pop.show(this, evt.getX(), evt.getY());
192     }
193     else if (av.getColumnSelection().contains(res))
194     {
195       MenuItem item = new MenuItem(
196               MessageManager.getString("label.hide_columns"));
197       item.addActionListener(new ActionListener()
198       {
199         @Override
200         public void actionPerformed(ActionEvent e)
201         {
202           av.hideColumns(res, res);
203           if (av.getSelectionGroup() != null
204                   && av.getSelectionGroup().getSize() == av.getAlignment()
205                           .getHeight())
206           {
207             av.setSelectionGroup(null);
208           }
209
210           ap.paintAlignment(true);
211           av.sendSelection();
212         }
213       });
214       pop.add(item);
215       this.add(pop);
216       pop.show(this, evt.getX(), evt.getY());
217     }
218   }
219
220   @Override
221   public void mouseReleased(MouseEvent evt)
222   {
223     mouseDragging = false;
224
225     int res = (evt.getX() / av.getCharWidth())
226             + av.getRanges().getStartRes();
227
228     if (res > av.getAlignment().getWidth())
229     {
230       res = av.getAlignment().getWidth() - 1;
231     }
232
233     if (av.hasHiddenColumns())
234     {
235       res = av.getAlignment().getHiddenColumns()
236               .adjustForHiddenColumns(res);
237     }
238
239     if (!stretchingGroup)
240     {
241       ap.paintAlignment(false);
242
243       return;
244     }
245
246     SequenceGroup sg = av.getSelectionGroup();
247
248     if (res > sg.getStartRes())
249     {
250       sg.setEndRes(res);
251     }
252     else if (res < sg.getStartRes())
253     {
254       sg.setStartRes(res);
255     }
256
257     stretchingGroup = false;
258     ap.paintAlignment(false);
259     av.sendSelection();
260   }
261
262   /**
263    * Action on dragging the mouse in the scale panel is to expand or shrink the
264    * selection group range (including any hidden columns that it spans)
265    * 
266    * @param evt
267    */
268   @Override
269   public void mouseDragged(MouseEvent evt)
270   {
271     mouseDragging = true;
272     ColumnSelection cs = av.getColumnSelection();
273
274     int res = (evt.getX() / av.getCharWidth())
275             + av.getRanges().getStartRes();
276     res = Math.max(0, res);
277     res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
278     res = Math.min(res, av.getAlignment().getWidth() - 1);
279     min = Math.min(res, min);
280     max = Math.max(res, max);
281
282     SequenceGroup sg = av.getSelectionGroup();
283     if (sg != null)
284     {
285       stretchingGroup = true;
286       cs.stretchGroup(res, sg, min, max);
287       ap.paintAlignment(false);
288     }
289   }
290
291   @Override
292   public void mouseEntered(MouseEvent evt)
293   {
294     if (mouseDragging)
295     {
296       ap.seqPanel.scrollCanvas(null);
297     }
298   }
299
300   @Override
301   public void mouseExited(MouseEvent evt)
302   {
303     if (mouseDragging)
304     {
305       ap.seqPanel.scrollCanvas(evt);
306     }
307   }
308
309   @Override
310   public void mouseClicked(MouseEvent evt)
311   {
312
313   }
314
315   @Override
316   public void mouseMoved(MouseEvent evt)
317   {
318     if (!av.hasHiddenColumns())
319     {
320       return;
321     }
322
323     int res = (evt.getX() / av.getCharWidth())
324             + av.getRanges().getStartRes();
325
326     reveal = av.getAlignment().getHiddenColumns()
327             .getRegionWithEdgeAtRes(res);
328
329     repaint();
330   }
331
332   @Override
333   public void update(Graphics g)
334   {
335     paint(g);
336   }
337
338   @Override
339   public void paint(Graphics g)
340   {
341     /*
342      * shouldn't get called in wrapped mode as the scale above is
343      * drawn instead by SeqCanvas.drawNorthScale
344      */
345     if (!av.getWrapAlignment())
346     {
347       drawScale(g, av.getRanges().getStartRes(),
348               av.getRanges().getEndRes(), getSize().width, getSize().height);
349     }
350   }
351
352   // scalewidth will normally be screenwidth,
353   public void drawScale(Graphics gg, int startx, int endx, int width,
354           int height)
355   {
356     gg.setFont(av.getFont());
357     // Fill in the background
358     gg.setColor(Color.white);
359     gg.fillRect(0, 0, width, height);
360     gg.setColor(Color.black);
361
362     // Fill the selected columns
363     ColumnSelection cs = av.getColumnSelection();
364     HiddenColumns hidden = av.getAlignment().getHiddenColumns();
365     int avCharWidth = av.getCharWidth();
366     int avcharHeight = av.getCharHeight();
367     if (cs != null)
368     {
369       gg.setColor(new Color(220, 0, 0));
370       boolean hasHiddenColumns = hidden.hasHiddenColumns();
371       for (int sel : cs.getSelected())
372       {
373         // TODO: JAL-2001 - provide a fast method to list visible selected in a
374         // given range
375         if (hasHiddenColumns)
376         {
377           if (hidden.isVisible(sel))
378           {
379             sel = hidden.findColumnPosition(sel);
380           }
381           else
382           {
383             continue;
384           }
385         }
386
387         if ((sel >= startx) && (sel <= endx))
388         {
389           gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
390                   getSize().height);
391         }
392       }
393     }
394
395     // Draw the scale numbers
396     gg.setColor(Color.black);
397
398     int maxX = 0;
399     List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
400             endx);
401
402     FontMetrics fm = gg.getFontMetrics(av.getFont());
403     int y = avcharHeight;
404     int yOf = fm.getDescent();
405     y -= yOf;
406     for (ScaleMark mark : marks)
407     {
408       boolean major = mark.major;
409       int mpos = mark.column; // (i - startx - 1)
410       String mstring = mark.text;
411       if (mstring != null)
412       {
413         if (mpos * avCharWidth > maxX)
414         {
415           gg.drawString(mstring, mpos * avCharWidth, y);
416           maxX = (mpos + 2) * avCharWidth + fm.stringWidth(mstring);
417         }
418       }
419       if (major)
420       {
421         gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + 2,
422                 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
423       }
424       else
425       {
426         gg.drawLine((mpos * avCharWidth) + (avCharWidth / 2), y + yOf,
427                 (mpos * avCharWidth) + (avCharWidth / 2), y + (yOf * 2));
428       }
429     }
430
431     if (av.hasHiddenColumns())
432     {
433       gg.setColor(Color.blue);
434       int res;
435       if (av.getShowHiddenMarkers())
436       {
437         int widthx = 1 + endx - startx;
438         List<Integer> positions = hidden.findHiddenRegionPositions();
439         for (int pos : positions)
440         {
441
442           res = pos - startx;
443
444           if (res < 0 || res > widthx)
445           {
446             continue;
447           }
448
449           gg.fillPolygon(new int[] {
450               -1 + res * avCharWidth - avcharHeight / 4,
451               -1 + res * avCharWidth + avcharHeight / 4,
452               -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
453         }
454       }
455     }
456   }
457
458   @Override
459   public void propertyChange(PropertyChangeEvent evt)
460   {
461     // Respond to viewport change events (e.g. alignment panel was scrolled)
462     // Both scrolling and resizing change viewport ranges: scrolling changes
463     // both start and end points, but resize only changes end values.
464     // Here we only want to fastpaint on a scroll, with resize using a normal
465     // paint, so scroll events are identified as changes to the horizontal or
466     // vertical start value.
467     if (evt.getPropertyName().equals(ViewportRanges.STARTRES))
468     {
469       // scroll event, repaint panel
470       repaint();
471     }
472   }
473
474 }