JAL-2794 PaintRefresher support, Aptx node selection now shown on align
[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   StructureSelectionManager ssm;
28
29   Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
30
31   Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
32
33   public JalviewAptxBinding(MainFrame archaeopteryx,
34           AlignmentViewport jalviewAlignmentViewport,
35           Map<SequenceI, PhylogenyNode> alignMappedToNodes,
36           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
54   @Override
55   public void mouseClicked(MouseEvent e)
56   {
57
58   }
59
60   @Override
61   public void mousePressed(MouseEvent e)
62   {
63     showNodeSelectionOnAlign(e);
64
65   }
66
67   @Override
68   public void mouseReleased(MouseEvent e)
69   {
70   }
71
72   @Override
73   public void mouseEntered(MouseEvent e)
74   {
75
76   }
77
78   @Override
79   public void mouseExited(MouseEvent e)
80   {
81
82   }
83
84   @Override
85   public void selection(SequenceGroup seqsel, ColumnSelection colsel,
86           HiddenColumns hidden, SelectionSource source)
87   {
88     if (source == parentAvport) // check if source is alignment from where the
89                                 // tree originates
90     {
91       treeView.setFoundNodes0(
92               new HashSet<Long>(seqsel.getSequences().size()));
93
94       for (SequenceI sequence : seqsel.getSequences())
95       {
96         PhylogenyNode matchingNode = sequencesBoundToNodes.get(sequence);
97         if (matchingNode != null)
98         {
99           treeView.getFoundNodes0().add(matchingNode.getId());
100         }
101
102       }
103       PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
104
105     }
106
107
108   }
109
110   @Override
111   public void showNodeSelectionOnAlign(MouseEvent e)
112   {
113     final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
114     if (node != null && node.isExternal())
115     {
116       SequenceI matchingSequence = nodesBoundToSequences.get(node);
117
118       if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) // shift is pressed
119       // (so multiple nodes
120       // can be selected)
121       {
122
123       }
124       else
125       {
126         parentAvport.setSelectionGroup(null); // reset selection if shift isn't
127                                               // pressed
128       }
129
130       treeSelectionChanged(matchingSequence);
131       parentAvport.sendSelection(); // not actually needed?
132
133       PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
134     }
135
136     }
137
138   /**
139    * Refactored from TreeCanvas
140    * 
141    * @param sequence
142    */
143   public void treeSelectionChanged(SequenceI sequence)
144   {
145     SequenceGroup selected = parentAvport.getSelectionGroup();
146
147     if (selected == null)
148     {
149       selected = new SequenceGroup();
150       parentAvport.setSelectionGroup(selected);
151     }
152
153     selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
154     selected.addOrRemove(sequence, true);
155
156   }
157
158   public AlignmentViewport getParentAvport()
159   {
160     return parentAvport;
161   }
162
163   public void setParentAvport(AlignmentViewport parentAvport)
164   {
165     this.parentAvport = parentAvport;
166   }
167
168 }
169
170
171