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