JAL-2794 most method parameters/class variables made final
[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 public class JalviewAptxBinding implements JalviewTreeViewerBindingI
22 {
23   org.forester.archaeopteryx.TreePanel treeView;
24
25   AlignmentViewport parentAvport;
26
27   final StructureSelectionManager ssm;
28
29   Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
30
31   Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
32
33   public JalviewAptxBinding(final MainFrame archaeopteryx,
34           final AlignmentViewport jalviewAlignmentViewport,
35           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
36           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
37   {
38     parentAvport = jalviewAlignmentViewport;
39     sequencesBoundToNodes = alignMappedToNodes;
40     nodesBoundToSequences = nodesMappedToAlign;
41     treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
42     ssm = parentAvport.getStructureSelectionManager();
43     ssm.addSelectionListener(this);
44     treeView.addMouseListener(this);
45     PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
46   }
47
48   @Override
49   public void actionPerformed(ActionEvent e)
50   {
51   }
52
53   @Override
54   public void mouseClicked(MouseEvent e)
55   {
56   }
57
58   @Override
59   public void mousePressed(final MouseEvent e)
60   {
61     showNodeSelectionOnAlign(e);
62   }
63
64   @Override
65   public void mouseReleased(MouseEvent e)
66   {
67   }
68
69   @Override
70   public void mouseEntered(MouseEvent e)
71   {
72   }
73
74   @Override
75   public void mouseExited(MouseEvent e)
76   {
77   }
78
79
80   @Override
81   public void selection(final SequenceGroup seqsel,
82           final ColumnSelection colsel, final HiddenColumns hidden,
83           final SelectionSource source)
84   {
85     if (source == parentAvport) // check if source is alignment from where the
86     // tree originates
87     {
88       treeView.setFoundNodes0(
89               new HashSet<Long>(seqsel.getSequences().size()));
90
91       for (SequenceI selectedSequence : seqsel.getSequences())
92       {
93         PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
94         if (matchingNode != null)
95         {
96           treeView.getFoundNodes0().add(matchingNode.getId());
97         }
98
99       }
100       PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
101
102     }
103
104
105   }
106
107   /**
108    * If a node is selected in the tree panel this method highlights the
109    * corresponding sequence in the Jalview alignment view.
110    */
111   @Override
112   public void showNodeSelectionOnAlign(final MouseEvent e)
113   {
114     final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
115     if (node != null)
116     {
117       SequenceI matchingSequence = nodesBoundToSequences.get(node);
118       if (matchingSequence != null)
119       {
120
121         if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) // shift is pressed
122         // (so multiple nodes
123         // can be selected)
124         {
125           // something?
126
127         }
128         else
129         {
130           parentAvport.setSelectionGroup(null); // reset selection if shift
131                                                 // isn't
132           // pressed
133         }
134
135         treeSelectionChanged(matchingSequence);
136         parentAvport.sendSelection(); // not actually needed?
137         PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
138
139
140       }
141     }
142
143
144   }
145
146   /**
147    * Refactored from TreeCanvas
148    * 
149    * @param sequence
150    */
151   public void treeSelectionChanged(final SequenceI sequence)
152   {
153     SequenceGroup selected = parentAvport.getSelectionGroup();
154
155     if (selected == null)
156     {
157       selected = new SequenceGroup();
158       parentAvport.setSelectionGroup(selected);
159     }
160
161     selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
162     selected.addOrRemove(sequence, true);
163
164   }
165
166   public AlignmentViewport getParentAvport()
167   {
168     return parentAvport;
169   }
170
171   public void setParentAvport(final AlignmentViewport parentAvport)
172   {
173     this.parentAvport = parentAvport;
174   }
175
176 }
177
178
179