755828dee147f223f604245e16902e2e8940e6ed
[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   @Override
54   public void mouseClicked(MouseEvent e)
55   {
56   }
57
58   @Override
59   public void mousePressed(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   @Override
80   public void selection(SequenceGroup seqsel, ColumnSelection colsel,
81           HiddenColumns hidden, SelectionSource source)
82   {
83     if (source == parentAvport) // check if source is alignment from where the
84                                 // tree originates
85     {
86       treeView.setFoundNodes0(
87               new HashSet<Long>(seqsel.getSequences().size()));
88
89       for (SequenceI selectedSequence : seqsel.getSequences())
90       {
91         PhylogenyNode matchingNode = sequencesBoundToNodes.get(selectedSequence);
92         if (matchingNode != null)
93         {
94           treeView.getFoundNodes0().add(matchingNode.getId());
95         }
96
97       }
98       PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
99
100     }
101
102
103   }
104
105   /**
106    * Note that this currently only checks external nodes
107    */
108   @Override
109   public void showNodeSelectionOnAlign(MouseEvent e)
110   {
111     final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
112     if (node != null)
113     {
114       SequenceI matchingSequence = nodesBoundToSequences.get(node);
115       if (matchingSequence != null)
116       {
117
118         if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) // shift is pressed
119         // (so multiple nodes
120         // can be selected)
121         {
122           // something?
123
124         }
125         else
126         {
127           parentAvport.setSelectionGroup(null); // reset selection if shift
128                                                 // isn't
129           // pressed
130         }
131
132         treeSelectionChanged(matchingSequence);
133         parentAvport.sendSelection(); // not actually needed?
134
135         PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
136       }
137     }
138
139   }
140
141   /**
142    * Refactored from TreeCanvas
143    * 
144    * @param sequence
145    */
146   public void treeSelectionChanged(SequenceI sequence)
147   {
148     SequenceGroup selected = parentAvport.getSelectionGroup();
149
150     if (selected == null)
151     {
152       selected = new SequenceGroup();
153       parentAvport.setSelectionGroup(selected);
154     }
155
156     selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
157     selected.addOrRemove(sequence, true);
158
159   }
160
161   public AlignmentViewport getParentAvport()
162   {
163     return parentAvport;
164   }
165
166   public void setParentAvport(AlignmentViewport parentAvport)
167   {
168     this.parentAvport = parentAvport;
169   }
170
171 }
172
173
174