f2c5154cd384a2b0b8c923484a9ed12e1f90c47e
[jalview.git] / src / jalview / gui / ScalePanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.*;
24 import java.awt.event.*;
25
26 import javax.swing.*;
27
28 import jalview.datamodel.*;
29 import jalview.util.MessageManager;
30
31 /**
32  * DOCUMENT ME!
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class ScalePanel extends JPanel implements MouseMotionListener,
38         MouseListener
39 {
40   protected int offy = 4;
41
42   /** DOCUMENT ME!! */
43   public int width;
44
45   protected AlignViewport av;
46
47   AlignmentPanel ap;
48
49   boolean stretchingGroup = false;
50
51   int min; // used by mouseDragged to see if user
52
53   int max; // used by mouseDragged to see if user
54
55   boolean mouseDragging = false;
56
57   // wants to delete columns
58   public ScalePanel(AlignViewport av, AlignmentPanel ap)
59   {
60     this.av = av;
61     this.ap = ap;
62
63     addMouseListener(this);
64     addMouseMotionListener(this);
65   }
66
67   /**
68    * DOCUMENT ME!
69    * 
70    * @param evt
71    *          DOCUMENT ME!
72    */
73   public void mousePressed(MouseEvent evt)
74   {
75     int x = (evt.getX() / av.getCharWidth()) + av.getStartRes();
76     final int res;
77
78     if (av.hasHiddenColumns())
79     {
80       x = av.getColumnSelection().adjustForHiddenColumns(x);
81     }
82
83     if (x >= av.getAlignment().getWidth())
84     {
85       res = av.getAlignment().getWidth() - 1;
86     }
87     else
88     {
89       res = x;
90     }
91
92     min = res;
93     max = res;
94
95     if (SwingUtilities.isRightMouseButton(evt))
96     {
97       JPopupMenu pop = new JPopupMenu();
98       if (reveal != null)
99       {
100         JMenuItem item = new JMenuItem(
101                 MessageManager.getString("label.reveal"));
102         item.addActionListener(new ActionListener()
103         {
104           public void actionPerformed(ActionEvent e)
105           {
106             av.showColumn(reveal[0]);
107             reveal = null;
108             ap.paintAlignment(true);
109             if (ap.overviewPanel != null)
110             {
111               ap.overviewPanel.updateOverviewImage();
112             }
113           }
114         });
115         pop.add(item);
116
117         if (av.getColumnSelection().getHiddenColumns().size() > 1)
118         {
119           item = new JMenuItem(
120                   MessageManager.getString("action.reveal_all"));
121           item.addActionListener(new ActionListener()
122           {
123             public void actionPerformed(ActionEvent e)
124             {
125               av.showAllHiddenColumns();
126               reveal = null;
127               ap.paintAlignment(true);
128               if (ap.overviewPanel != null)
129               {
130                 ap.overviewPanel.updateOverviewImage();
131               }
132             }
133           });
134           pop.add(item);
135         }
136         pop.show(this, evt.getX(), evt.getY());
137       }
138       else if (av.getColumnSelection().contains(res))
139       {
140         JMenuItem item = new JMenuItem(
141                 MessageManager.getString("label.hide_columns"));
142         item.addActionListener(new ActionListener()
143         {
144           public void actionPerformed(ActionEvent e)
145           {
146             av.hideColumns(res, res);
147             if (av.getSelectionGroup() != null
148                     && av.getSelectionGroup().getSize() == av
149                             .getAlignment().getHeight())
150             {
151               av.setSelectionGroup(null);
152             }
153
154             ap.paintAlignment(true);
155             if (ap.overviewPanel != null)
156             {
157               ap.overviewPanel.updateOverviewImage();
158             }
159           }
160         });
161         pop.add(item);
162         pop.show(this, evt.getX(), evt.getY());
163       }
164     }
165     else
166     // LEFT MOUSE TO SELECT
167     {
168       if (!evt.isControlDown() && !evt.isShiftDown())
169       {
170         av.getColumnSelection().clear();
171       }
172
173       av.getColumnSelection().addElement(res);
174       SequenceGroup sg = new SequenceGroup();
175       // try to be as quick as possible
176       SequenceI[] iVec = av.getAlignment().getSequencesArray();
177       for (int i = 0; i < iVec.length; i++)
178       {
179         sg.addSequence(iVec[i], false);
180         iVec[i] = null;
181       }
182       iVec = null;
183       sg.setStartRes(res);
184       sg.setEndRes(res);
185
186       if (evt.isShiftDown())
187       {
188         int min = Math.min(av.getColumnSelection().getMin(), res);
189         int max = Math.max(av.getColumnSelection().getMax(), res);
190         for (int i = min; i < max; i++)
191         {
192           av.getColumnSelection().addElement(i);
193         }
194         sg.setStartRes(min);
195         sg.setEndRes(max);
196       }
197       av.setSelectionGroup(sg);
198     }
199
200     ap.paintAlignment(false);
201     av.sendSelection();
202   }
203
204   /**
205    * DOCUMENT ME!
206    * 
207    * @param evt
208    *          DOCUMENT ME!
209    */
210   public void mouseReleased(MouseEvent evt)
211   {
212     mouseDragging = false;
213
214     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
215
216     if (av.hasHiddenColumns())
217     {
218       res = av.getColumnSelection().adjustForHiddenColumns(res);
219     }
220
221     if (res >= av.getAlignment().getWidth())
222     {
223       res = av.getAlignment().getWidth() - 1;
224     }
225
226     if (!stretchingGroup)
227     {
228       ap.paintAlignment(false);
229
230       return;
231     }
232
233     SequenceGroup sg = av.getSelectionGroup();
234
235     if (sg != null)
236     {
237       if (res > sg.getStartRes())
238       {
239         sg.setEndRes(res);
240       }
241       else if (res < sg.getStartRes())
242       {
243         sg.setStartRes(res);
244       }
245     }
246     stretchingGroup = false;
247     ap.paintAlignment(false);
248     av.sendSelection();
249   }
250
251   /**
252    * DOCUMENT ME!
253    * 
254    * @param evt
255    *          DOCUMENT ME!
256    */
257   public void mouseDragged(MouseEvent evt)
258   {
259     mouseDragging = true;
260
261     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
262     if (res < 0)
263     {
264       res = 0;
265     }
266
267     if (av.hasHiddenColumns())
268     {
269       res = av.getColumnSelection().adjustForHiddenColumns(res);
270     }
271
272     if (res >= av.getAlignment().getWidth())
273     {
274       res = av.getAlignment().getWidth() - 1;
275     }
276
277     if (res < min)
278     {
279       min = res;
280     }
281
282     if (res > max)
283     {
284       max = res;
285     }
286
287     SequenceGroup sg = av.getSelectionGroup();
288
289     if (sg != null)
290     {
291       stretchingGroup = true;
292
293       if (!av.getColumnSelection().contains(res))
294       {
295         av.getColumnSelection().addElement(res);
296       }
297
298       if (res > sg.getStartRes())
299       {
300         sg.setEndRes(res);
301       }
302       if (res < sg.getStartRes())
303       {
304         sg.setStartRes(res);
305       }
306
307       int col;
308       for (int i = min; i <= max; i++)
309       {
310         col = i; // av.getColumnSelection().adjustForHiddenColumns(i);
311
312         if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
313         {
314           av.getColumnSelection().removeElement(col);
315         }
316         else
317         {
318           av.getColumnSelection().addElement(col);
319         }
320       }
321
322       ap.paintAlignment(false);
323     }
324   }
325
326   public void mouseEntered(MouseEvent evt)
327   {
328     if (mouseDragging)
329     {
330       ap.seqPanel.scrollCanvas(null);
331     }
332   }
333
334   public void mouseExited(MouseEvent evt)
335   {
336     if (mouseDragging)
337     {
338       ap.seqPanel.scrollCanvas(evt);
339     }
340   }
341
342   public void mouseClicked(MouseEvent evt)
343   {
344   }
345
346   public void mouseMoved(MouseEvent evt)
347   {
348     if (!av.hasHiddenColumns())
349     {
350       return;
351     }
352
353     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
354
355     res = av.getColumnSelection().adjustForHiddenColumns(res);
356
357     reveal = null;
358     for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
359     {
360       int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
361               .elementAt(i);
362       if (res + 1 == region[0] || res - 1 == region[1])
363       {
364         reveal = region;
365         ToolTipManager.sharedInstance().registerComponent(this);
366         this.setToolTipText(MessageManager
367                 .getString("label.reveal_hidden_columns"));
368         break;
369       }
370       else
371       {
372         this.setToolTipText(null);
373       }
374
375     }
376
377     repaint();
378   }
379
380   int[] reveal;
381
382   /**
383    * DOCUMENT ME!
384    * 
385    * @param g
386    *          DOCUMENT ME!
387    */
388   public void paintComponent(Graphics g)
389   {
390     drawScale(g, av.getStartRes(), av.getEndRes(), getWidth(), getHeight());
391   }
392
393   // scalewidth will normally be screenwidth,
394   public void drawScale(Graphics g, int startx, int endx, int width,
395           int height)
396   {
397     Graphics2D gg = (Graphics2D) g;
398     gg.setFont(av.getFont());
399
400     if (av.antiAlias)
401     {
402       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
403               RenderingHints.VALUE_ANTIALIAS_ON);
404     }
405
406     // Fill in the background
407     gg.setColor(Color.white);
408     gg.fillRect(0, 0, width, height);
409     gg.setColor(Color.black);
410
411     // Fill the selected columns
412     ColumnSelection cs = av.getColumnSelection();
413     int s;
414     if (cs != null)
415     {
416       gg.setColor(new Color(220, 0, 0));
417
418       for (int i = 0; i < cs.size(); i++)
419       {
420         int sel = cs.columnAt(i);
421         if (av.hasHiddenColumns())
422         {
423           if (cs.isVisible(sel))
424           {
425             sel = cs.findColumnPosition(sel);
426           }
427           else
428           {
429             continue;
430           }
431         }
432
433         if ((sel >= startx) && (sel <= endx))
434         {
435           gg.fillRect((sel - startx) * av.charWidth, 0, av.charWidth,
436                   getHeight());
437         }
438       }
439     }
440     // Draw the scale numbers
441     gg.setColor(Color.black);
442
443     int scalestartx = (startx / 10) * 10;
444
445     FontMetrics fm = gg.getFontMetrics(av.getFont());
446     int y = av.charHeight - fm.getDescent();
447
448     if ((scalestartx % 10) == 0)
449     {
450       scalestartx += 5;
451     }
452
453     String string;
454     int maxX = 0;
455
456     for (int i = scalestartx; i < endx; i += 5)
457     {
458       if ((i % 10) == 0)
459       {
460         string = String.valueOf(av.getColumnSelection()
461                 .adjustForHiddenColumns(i));
462         if ((i - startx - 1) * av.charWidth > maxX)
463         {
464           gg.drawString(string, (i - startx - 1) * av.charWidth, y);
465           maxX = (i - startx + 1) * av.charWidth + fm.stringWidth(string);
466         }
467
468         gg.drawLine(
469                 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
470                 y + 2,
471                 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
472                 y + (fm.getDescent() * 2));
473
474       }
475       else
476       {
477         gg.drawLine(
478                 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
479                 y + fm.getDescent(),
480                 (int) (((i - startx - 1) * av.charWidth) + (av.charWidth / 2)),
481                 y + (fm.getDescent() * 2));
482       }
483     }
484
485     if (av.hasHiddenColumns())
486     {
487       gg.setColor(Color.blue);
488       int res;
489       if (av.getShowHiddenMarkers())
490       {
491         for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
492                 .size(); i++)
493         {
494
495           res = av.getColumnSelection().findHiddenRegionPosition(i)
496                   - startx;
497
498           if (res < 0 || res > endx - scalestartx)
499           {
500             continue;
501           }
502
503           gg.fillPolygon(new int[]
504           { res * av.charWidth - av.charHeight / 4,
505               res * av.charWidth + av.charHeight / 4, res * av.charWidth },
506                   new int[]
507                   { y - av.charHeight / 2, y - av.charHeight / 2, y + 8 },
508                   3);
509
510         }
511       }
512
513       if (reveal != null && reveal[0] > startx && reveal[0] < endx)
514       {
515         gg.drawString(MessageManager.getString("label.reveal_columns"),
516                 reveal[0] * av.charWidth, 0);
517       }
518     }
519
520   }
521 }