JAL-2446 merged to spike branch
[jalview.git] / src / jalview / gui / OverviewPanel.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 jalview.util.MessageManager;
24 import jalview.util.Platform;
25 import jalview.viewmodel.OverviewDimensions;
26 import jalview.viewmodel.OverviewDimensionsHideHidden;
27 import jalview.viewmodel.OverviewDimensionsShowHidden;
28 import jalview.viewmodel.ViewportListenerI;
29
30 import java.awt.BorderLayout;
31 import java.awt.Dimension;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.event.ComponentAdapter;
35 import java.awt.event.ComponentEvent;
36 import java.awt.event.MouseAdapter;
37 import java.awt.event.MouseEvent;
38 import java.awt.event.MouseMotionAdapter;
39 import java.beans.PropertyChangeEvent;
40
41 import javax.swing.JCheckBoxMenuItem;
42 import javax.swing.JPanel;
43 import javax.swing.JPopupMenu;
44 import javax.swing.SwingUtilities;
45
46 /**
47  * Panel displaying an overview of the full alignment, with an interactive box
48  * representing the viewport onto the alignment.
49  * 
50  * @author $author$
51  * @version $Revision$
52  */
53 public class OverviewPanel extends JPanel implements Runnable,
54         ViewportListenerI
55 {
56   private OverviewDimensions od;
57
58   private OverviewCanvas oviewCanvas;
59
60   private AlignViewport av;
61
62   private AlignmentPanel ap;
63
64   private JCheckBoxMenuItem displayToggle;
65
66   private boolean showHidden = true;
67
68   /**
69    * Creates a new OverviewPanel object.
70    * 
71    * @param alPanel
72    *          The alignment panel which is shown in the overview panel
73    */
74   public OverviewPanel(AlignmentPanel alPanel)
75   {
76     this.av = alPanel.av;
77     this.ap = alPanel;
78
79     od = new OverviewDimensionsShowHidden(av.getRanges(),
80             (av.isShowAnnotation() && av
81                     .getAlignmentConservationAnnotation() != null));
82
83     setSize(od.getWidth(), od.getHeight());
84
85     oviewCanvas = new OverviewCanvas(od, av);
86     setLayout(new BorderLayout());
87     add(oviewCanvas, BorderLayout.CENTER);
88
89     av.getRanges().addPropertyChangeListener(this);
90
91     addComponentListener(new ComponentAdapter()
92     {
93       @Override
94       public void componentResized(ComponentEvent evt)
95       {
96         if ((getWidth() != od.getWidth())
97                 || (getHeight() != (od.getHeight())))
98         {
99           updateOverviewImage();
100           setBoxPosition();
101         }
102       }
103     });
104
105     addMouseMotionListener(new MouseMotionAdapter()
106     {
107       @Override
108       public void mouseDragged(MouseEvent evt)
109       {
110         if (!SwingUtilities.isRightMouseButton(evt)
111                 && !av.getWrapAlignment())
112         {
113           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
114                   .getAlignment().getHiddenSequences(), av.getAlignment()
115                   .getHiddenColumns());
116
117         }
118       }
119     });
120
121     addMouseListener(new MouseAdapter()
122     {
123       @Override
124       public void mousePressed(MouseEvent evt)
125       {
126         if (SwingUtilities.isRightMouseButton(evt))
127         {
128           if (!Platform.isAMac())
129           {
130             showPopupMenu(evt);
131           }
132         }
133         else if (!av.getWrapAlignment())
134         {
135           od.updateViewportFromMouse(evt.getX(), evt.getY(), av
136                   .getAlignment().getHiddenSequences(), av.getAlignment()
137                   .getHiddenColumns());
138         }
139       }
140
141       @Override
142       public void mouseClicked(MouseEvent evt)
143       {
144         if (SwingUtilities.isRightMouseButton(evt))
145         {
146           showPopupMenu(evt);
147         }
148       }
149     });
150
151
152     updateOverviewImage();
153   }
154
155   /*
156    * Displays the popup menu and acts on user input
157    */
158   private void showPopupMenu(MouseEvent e)
159   {
160     JPopupMenu popup = new JPopupMenu();
161     ActionListener menuListener = new ActionListener()
162     {
163       @Override
164       public void actionPerformed(ActionEvent event)
165       {
166         // switch on/off the hidden columns view
167         toggleHiddenColumns();
168         displayToggle.setSelected(showHidden);
169       }
170     };
171     displayToggle = new JCheckBoxMenuItem(
172             MessageManager.getString("label.togglehidden"));
173     displayToggle.setEnabled(true);
174     displayToggle.setSelected(showHidden);
175     popup.add(displayToggle);
176     displayToggle.addActionListener(menuListener);
177     popup.show(this, e.getX(), e.getY());
178   }
179
180   /*
181    * Toggle overview display between showing hidden columns and hiding hidden columns
182    */
183   private void toggleHiddenColumns()
184   {
185     if (showHidden)
186     {
187       showHidden = false;
188       od = new OverviewDimensionsHideHidden(av.getRanges(),
189               (av.isShowAnnotation() && av
190                       .getAlignmentConservationAnnotation() != null));
191     }
192     else
193     {
194       showHidden = true;
195       od = new OverviewDimensionsShowHidden(av.getRanges(),
196               (av.isShowAnnotation() && av
197                       .getAlignmentConservationAnnotation() != null));
198     }
199     oviewCanvas.resetOviewDims(od);
200     updateOverviewImage();
201     setBoxPosition();
202   }
203
204   /**
205    * Updates the overview image when the related alignment panel is updated
206    */
207   public void updateOverviewImage()
208   {
209     if ((getWidth() > 0) && (getHeight() > 0))
210     {
211       od.setWidth(getWidth());
212       od.setHeight(getHeight());
213     }
214     
215     setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
216
217     if (oviewCanvas.restartDraw())
218     {
219       return;
220     }
221
222     Thread thread = new Thread(this);
223     thread.start();
224     repaint();
225
226   }
227
228   @Override
229   public void run()
230   {
231     oviewCanvas.draw(av.isShowSequenceFeatures(),
232             (av.isShowAnnotation() && av
233                     .getAlignmentConservationAnnotation() != null), ap
234                     .getSeqPanel().seqCanvas.getFeatureRenderer());
235     setBoxPosition();
236   }
237
238   /**
239    * Update the overview panel box when the associated alignment panel is
240    * changed
241    * 
242    */
243   private void setBoxPosition()
244   {
245     od.setBoxPosition(av.getAlignment().getHiddenSequences(), av
246             .getAlignment().getHiddenColumns());
247     repaint();
248   }
249
250   @Override
251   public void propertyChange(PropertyChangeEvent evt)
252   {
253     setBoxPosition();
254   }
255 }