6eb5645c8f5ed4fc03c286e53c71cd489dbdd91b
[taxeditor.git] / eu.etaxonomy.taxeditor.molecular / src / main / java / eu / etaxonomy / taxeditor / molecular / handler / AlignmentEditorCopyHandler.java
1 package eu.etaxonomy.taxeditor.molecular.handler;
2
3
4 import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
5 import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
6 import info.bioinfweb.libralign.model.AlignmentModel;
7 import info.bioinfweb.libralign.model.tokenset.AbstractTokenSet;
8
9 import org.eclipse.core.commands.AbstractHandler;
10 import org.eclipse.core.commands.ExecutionEvent;
11 import org.eclipse.core.commands.ExecutionException;
12 import org.eclipse.swt.dnd.Clipboard;
13 import org.eclipse.swt.dnd.TextTransfer;
14 import org.eclipse.swt.dnd.Transfer;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.handlers.HandlerUtil;
18
19 import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
20
21
22
23 /**
24 * Handler that copies the currently selected nucleotides from an alignment editor to the clipboard.
25 * <p>
26 * The copied contents either come from the single reads or the consensus sequence alignment area,
27 * depending on which component currently has the focus. If none of these components has the focus,
28 * nothing will be copied, even if nucleotides are currently selected.
29 * <p>
30 * If the selection contains parts of multiple sequence, these are separated by the line separator
31 * if the current operating system.
32 *
33 * @author Ben Stöver
34 * @date 25.08.2015
35 */
36 public class AlignmentEditorCopyHandler extends AbstractHandler {
37 @Override
38 public Object execute(ExecutionEvent event) throws ExecutionException {
39 IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
40 if(activeEditor instanceof AlignmentEditor){
41 AlignmentArea focusedArea = ((AlignmentEditor)activeEditor).getFocusedArea();
42 if (focusedArea != null) {
43 copySelectionAsString(focusedArea);
44 }
45 }
46 return null;
47 }
48
49
50 @SuppressWarnings("unchecked")
51 public static void copySelectionAsString(AlignmentArea area) {
52 SelectionModel selection = area.getSelection();
53 if (!selection.isEmpty()) {
54 StringBuilder selectedCharacters = new StringBuilder();
55 AlignmentModel<Character> alignmentModel = (AlignmentModel<Character>)area.getAlignmentModel();
56 for (int row = selection.getFirstRow(); row <= selection.getLastRow(); row++) {
57 int id = area.getSequenceOrder().idByIndex(row);
58 for (int column = selection.getFirstColumn(); column <= selection.getLastColumn(); column++) {
59 if (alignmentModel.getSequenceLength(id) > column) {
60 selectedCharacters.append(alignmentModel.getTokenAt(id, column));
61 }
62 else { // Add gaps if selection is behind the end of the sequence.
63 selectedCharacters.append(AbstractTokenSet.DEFAULT_GAP_REPRESENTATION);
64 }
65 }
66 if (row < selection.getLastRow()) {
67 selectedCharacters.append(System.getProperty("line.separator"));
68 }
69 }
70 new Clipboard(Display.getCurrent()).setContents(new Object[]{selectedCharacters.toString()},
71 new Transfer[]{TextTransfer.getInstance()});
72 }
73 }
74 }