Revert "JAL-2799 all tabs now get mouse listeners and Jalview switches tree view"
[jalview.git] / src / jalview / ext / archaeopteryx / JalviewBinding.java
1 package jalview.ext.archaeopteryx;
2
3 import jalview.datamodel.ColumnSelection;
4 import jalview.datamodel.HiddenColumns;
5 import jalview.datamodel.SequenceGroup;
6 import jalview.datamodel.SequenceI;
7 import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
8 import jalview.gui.PaintRefresher;
9 import jalview.structure.SelectionSource;
10 import jalview.structure.StructureSelectionManager;
11 import jalview.viewmodel.AlignmentViewport;
12
13 import java.awt.event.ActionEvent;
14 import java.awt.event.InputEvent;
15 import java.awt.event.MouseEvent;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.swing.JTabbedPane;
22 import javax.swing.SwingUtilities;
23 import javax.swing.event.ChangeEvent;
24 import javax.swing.event.ChangeListener;
25
26 import org.forester.archaeopteryx.MainFrame;
27 import org.forester.phylogeny.Phylogeny;
28 import org.forester.phylogeny.PhylogenyMethods;
29 import org.forester.phylogeny.PhylogenyNode;
30
31 /**
32  * Class for binding the Archaeopteryx tree viewer to the Jalview alignment that
33  * it originates from, meaning that selecting sequences in the tree viewer also
34  * selects them in the alignment view and vice versa.
35  * 
36  * @author kjvanderheide
37  *
38  */
39 public final class JalviewBinding
40         implements ExternalTreeViewerBindingI<PhylogenyNode>
41 {
42   private org.forester.archaeopteryx.TreePanel treeView;
43
44   private AlignmentViewport parentAvport;
45
46   private JTabbedPane treeTabs;
47
48   private final StructureSelectionManager ssm;
49
50   private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
51
52   private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
53
54   /**
55    * 
56    * @param archaeopteryx
57    * 
58    * @param jalviewAlignmentViewport
59    *          alignment viewport from which the tree was calculated.
60    * 
61    * @param alignMappedToNodes
62    *          map with sequences used to calculate the tree and matching tree
63    *          nodes as key, value pair respectively.
64    * 
65    * @param nodesMappedToAlign
66    *          map with tree nodes and matching sequences used to calculate the
67    *          tree as key, value pair respectively.
68    */
69   public JalviewBinding(final MainFrame archaeopteryx,
70           final AlignmentViewport jalviewAlignmentViewport,
71           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
72           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
73   {
74     // deal with/prohibit null values here as that will cause problems
75     parentAvport = jalviewAlignmentViewport;
76     sequencesBoundToNodes = alignMappedToNodes;
77     nodesBoundToSequences = nodesMappedToAlign;
78
79     treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
80     treeTabs = archaeopteryx.getMainPanel().getTabbedPane();
81     ssm = parentAvport.getStructureSelectionManager();
82
83     // archaeopteryx.getMainPanel().getControlPanel().setColorBranches(true);
84     
85     ssm.addSelectionListener(this);
86     treeView.addMouseListener(this);
87     PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
88
89
90     treeTabs.addChangeListener(new ChangeListener()
91     {
92
93       @Override
94       public void stateChanged(ChangeEvent e)
95       {
96
97         SwingUtilities.invokeLater(new Runnable()
98         {
99
100           @Override
101           /**
102            * Resend the selection to the tree view when tabs get switched, this
103            * has to be buried in invokeLater as Forester first resets the tree
104            * view on switching tabs, without invokeLater this would get called
105            * before Forester resets which would nullify the selection.
106            */
107           public void run()
108           {
109             parentAvport.sendSelection();
110             // PaintRefresher.Refresh(treeView,
111             // parentAvport.getSequenceSetId());
112
113           }
114         });
115
116       }
117       
118     });
119
120   }
121
122   @Override
123   public void actionPerformed(ActionEvent e)
124   {
125   }
126
127   @Override
128   public void mouseClicked(MouseEvent e)
129   {
130     SwingUtilities.invokeLater(new Runnable() {
131
132       @Override
133       /**
134        * invokeLater so that this always runs after Forester's mouseClicked
135        */
136       public void run()
137       {
138         final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
139         if (node != null)
140         {
141           if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
142           // selection if shift
143           // IS NOT pressed
144           {
145             parentAvport.setSelectionGroup(null);
146
147           }
148           showNodeSelectionOnAlign(node);
149         }
150         else
151         {
152           partitionTree(e);
153         
154         
155       }
156       
157       }
158     });
159
160
161   }
162
163   @Override
164   public void mousePressed(final MouseEvent e)
165   {
166
167   }
168   @Override
169   public void mouseReleased(MouseEvent e)
170   {
171   }
172
173   @Override
174   public void mouseEntered(MouseEvent e)
175   {
176   }
177
178   @Override
179   public void mouseExited(MouseEvent e)
180   {
181   }
182
183
184   @Override
185   public void selection(final SequenceGroup seqsel,
186           final ColumnSelection colsel, final HiddenColumns hidden,
187           final SelectionSource source)
188   {
189     if (source == parentAvport) // check if source is alignment from where the
190     // tree originates
191     {
192       treeView.setFoundNodes0(
193               new HashSet<Long>(seqsel.getSequences().size()));
194
195       for (SequenceI selectedSequence : seqsel.getSequences())
196       {
197         PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
198         if (matchingNode != null)
199         {
200           treeView.getFoundNodes0().add(matchingNode.getId());
201         }
202
203       }
204       treeView.repaint();
205
206     }
207
208
209   }
210
211   /**
212    * Partially refactored from TreeCanvas
213    */
214   public void partitionTree(final MouseEvent e)
215   {
216     int x = e.getX();
217     int lineLength = treeView.getHeight();
218
219     Phylogeny tree = treeView.getPhylogeny();
220     double treeHeight = tree.calculateHeight(true);
221
222
223
224     if (treeHeight != 0)
225     {
226       int viewWidth = treeView.getWidth();
227
228       // treeView.validate();
229
230       // System.out.println("selection");
231       // System.out.println(x);
232       // System.out.println("-------------");
233       // System.out.println("width");
234       // System.out.println(viewWidth);
235
236     }
237
238
239   }
240   
241
242
243   @Override
244   public void showNodeSelectionOnAlign(final PhylogenyNode node)
245   {
246
247       if (node.isInternal())
248       {
249         showMatchingChildSequences(node);
250       }
251
252       else
253       {
254         showMatchingSequence(node);
255       }
256
257     }
258
259
260
261
262
263   @Override
264   public void showMatchingSequence(final PhylogenyNode nodeToMatch)
265   {
266     SequenceI matchingSequence = nodesBoundToSequences.get(nodeToMatch);
267     if (matchingSequence != null)
268     {
269       long nodeId = nodeToMatch.getId();
270       addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
271       treeSelectionChanged(matchingSequence);
272       parentAvport.sendSelection();
273
274     }
275   }
276
277   @Override
278   public void showMatchingChildSequences(final PhylogenyNode parentNode)
279   {
280     List<PhylogenyNode> childNodes = PhylogenyMethods
281             .getAllDescendants(parentNode);
282
283
284     for (PhylogenyNode childNode : childNodes)
285     {
286       // childNode.getBranchData().setBranchColor(new BranchColor(Color.BLUE));
287
288       SequenceI matchingSequence = nodesBoundToSequences.get(childNode);
289       if (matchingSequence != null)
290       {
291         long nodeId = childNode.getId();
292         addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
293
294         treeSelectionChanged(matchingSequence);
295
296       }
297
298     }
299     parentAvport.sendSelection();
300
301
302   }
303
304   /**
305    * Refactored from TreeCanvas.
306    * 
307    * @param sequence
308    *          of the node selected in the tree viewer.
309    */
310   @Override
311   public void treeSelectionChanged(final SequenceI sequence)
312   {
313     if (!parentAvport.isClosed()) // alignment view could be closed
314     {
315       SequenceGroup selected = parentAvport.getSelectionGroup();
316
317       if (selected == null)
318       {
319         selected = new SequenceGroup();
320         parentAvport.setSelectionGroup(selected);
321       }
322
323       selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
324         selected.addOrRemove(sequence, true);
325     }
326
327   }
328   public void sortByTree_actionPerformed() {
329     // parentAvport.mirrorCommand(command, undo, ssm, source);
330
331     // alignFrame
332     // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
333     
334   }
335   
336
337   /**
338    * sort the associated alignment view by the current tree.
339    * 
340    * @param e
341    */
342   // @Override
343   // public void sortByTree_actionPerformed()// modify for Aptx
344   // {
345   //
346   // // if (treeCanvas.applyToAllViews)
347   //
348   // final ArrayList<CommandI> commands = new ArrayList<>();
349   // for (AlignmentPanel ap : PaintRefresher
350   // .getAssociatedPanels(parentAvport.getSequenceSetId()))
351   // {
352   // commands.add(sortAlignmentIn(ap.av.getAlignPanel()));
353   // }
354   // av.getAlignPanel().alignFrame.addHistoryItem(new CommandI()
355   // {
356   //
357   // @Override
358   // public void undoCommand(AlignmentI[] views)
359   // {
360   // for (CommandI tsort : commands)
361   // {
362   // tsort.undoCommand(views);
363   // }
364   // }
365   //
366   // @Override
367   // public int getSize()
368   // {
369   // return commands.size();
370   // }
371   //
372   // @Override
373   // public String getDescription()
374   // {
375   // return "Tree Sort (many views)";
376   // }
377   //
378   // @Override
379   // public void doCommand(AlignmentI[] views)
380   // {
381   //
382   // for (CommandI tsort : commands)
383   // {
384   // tsort.doCommand(views);
385   // }
386   // }
387   // });
388   // for (AlignmentPanel ap : PaintRefresher
389   // .getAssociatedPanels(av.getSequenceSetId()))
390   // {
391   // // ensure all the alignFrames refresh their GI after adding an undo item
392   // ap.alignFrame.updateEditMenuBar();
393   // }
394   // }
395   // else
396   // {
397   // treeCanvas.ap.alignFrame
398   // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
399   // }
400
401
402
403   /**
404    * TO BE MOVED
405    * 
406    * @param set
407    * @param objectToCheck
408    */
409   public static <E> void addOrRemoveInSet(Set<E> set, E objectToCheck)
410   {
411     if (set.contains(objectToCheck))
412     {
413       set.remove(objectToCheck);
414     }
415     else
416     {
417       set.add(objectToCheck);
418     }
419
420   }
421
422   public AlignmentViewport getParentAvport()
423   {
424     return parentAvport;
425   }
426
427   public void setParentAvport(final AlignmentViewport parentAvport)
428   {
429     this.parentAvport = parentAvport;
430   }
431 }
432
433
434