6a6b3f14025614a3e5102794fa1684f3bc7cc5e9
[jalview.git] / src / jalview / ext / archaeopteryx / JalviewAptxBinding.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.gui.PaintRefresher;
8 import jalview.structure.SelectionSource;
9 import jalview.structure.StructureSelectionManager;
10 import jalview.viewmodel.AlignmentViewport;
11
12 import java.awt.event.ActionEvent;
13 import java.awt.event.InputEvent;
14 import java.awt.event.MouseEvent;
15 import java.util.HashSet;
16 import java.util.Map;
17
18 import org.forester.archaeopteryx.MainFrame;
19 import org.forester.phylogeny.PhylogenyNode;
20
21 /**
22  * Class for binding the Archaeopteryx tree viewer to the Jalview alignment that
23  * it originates from, meaning that selecting sequences in the tree viewer also
24  * selects them in the alignment view and vice versa.
25  * 
26  * @author kjvanderheide
27  *
28  */
29 public final class JalviewAptxBinding implements JalviewTreeViewerBindingI
30 {
31   private org.forester.archaeopteryx.TreePanel treeView;
32
33   private AlignmentViewport parentAvport;
34
35   private final StructureSelectionManager ssm;
36
37   private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
38
39   private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
40
41   /**
42    * 
43    * @param archaeopteryx
44    * 
45    * @param jalviewAlignmentViewport
46    *          alignment viewport from which the tree was calculated.
47    * 
48    * @param alignMappedToNodes
49    *          map with sequences used to calculate the tree and matching tree
50    *          nodes as key, value pair respectively.
51    * 
52    * @param nodesMappedToAlign
53    *          map with tree nodes and matching sequences used to calculate the
54    *          tree as key, value pair respectively.
55    */
56   public JalviewAptxBinding(final MainFrame archaeopteryx,
57           final AlignmentViewport jalviewAlignmentViewport,
58           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
59           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
60   {
61     // deal with/prohibit null values here as that will cause problems
62     parentAvport = jalviewAlignmentViewport;
63     sequencesBoundToNodes = alignMappedToNodes;
64     nodesBoundToSequences = nodesMappedToAlign;
65     treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
66     ssm = parentAvport.getStructureSelectionManager();
67     ssm.addSelectionListener(this);
68     treeView.addMouseListener(this);
69     PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
70
71   }
72
73   @Override
74   public void actionPerformed(ActionEvent e)
75   {
76   }
77
78   @Override
79   public void mouseClicked(MouseEvent e)
80   {
81   }
82
83   @Override
84   public void mousePressed(final MouseEvent e)
85   {
86     showNodeSelectionOnAlign(e);
87   }
88
89   @Override
90   public void mouseReleased(MouseEvent e)
91   {
92   }
93
94   @Override
95   public void mouseEntered(MouseEvent e)
96   {
97   }
98
99   @Override
100   public void mouseExited(MouseEvent e)
101   {
102   }
103
104
105   @Override
106   public void selection(final SequenceGroup seqsel,
107           final ColumnSelection colsel, final HiddenColumns hidden,
108           final SelectionSource source)
109   {
110     if (source == parentAvport) // check if source is alignment from where the
111     // tree originates
112     {
113       treeView.setFoundNodes0(
114               new HashSet<Long>(seqsel.getSequences().size()));
115
116       for (SequenceI selectedSequence : seqsel.getSequences())
117       {
118         PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
119         if (matchingNode != null)
120         {
121           treeView.getFoundNodes0().add(matchingNode.getId());
122         }
123
124       }
125       PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
126
127     }
128
129
130   }
131
132   /**
133    * If a node is selected in the tree panel this method highlights the
134    * corresponding sequence in the Jalview alignment view.
135    */
136   @Override
137   public void showNodeSelectionOnAlign(final MouseEvent e)
138   {
139     final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
140     if (node != null)
141     {
142       SequenceI matchingSequence = nodesBoundToSequences.get(node);
143       if (matchingSequence != null)
144       {
145
146         if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) // shift is pressed
147         // (so multiple nodes
148         // can be selected)
149         {
150           // something?
151
152         }
153         else
154         {
155           parentAvport.setSelectionGroup(null); // reset selection if shift
156                                                 // isn't pressed
157         }
158
159         treeSelectionChanged(matchingSequence);
160         parentAvport.sendSelection(); // not actually needed?
161         PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
162
163
164       }
165     }
166
167
168   }
169
170   /**
171    * Refactored from TreeCanvas.
172    * 
173    * @param sequence
174    *          of the node selected in the tree viewer.
175    */
176   public void treeSelectionChanged(final SequenceI sequence)
177   {
178     SequenceGroup selected = parentAvport.getSelectionGroup();
179
180     if (selected == null)
181     {
182       selected = new SequenceGroup();
183       parentAvport.setSelectionGroup(selected);
184     }
185
186     selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
187     selected.addOrRemove(sequence, true);
188
189   }
190
191   public AlignmentViewport getParentAvport()
192   {
193     return parentAvport;
194   }
195
196   public void setParentAvport(final AlignmentViewport parentAvport)
197   {
198     this.parentAvport = parentAvport;
199   }
200
201 }
202
203
204