Project

General

Profile

Download (3.09 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.molecular.handler;
2

    
3

    
4
import java.util.Map;
5

    
6
import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
7
import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
8
import info.bioinfweb.libralign.model.AlignmentModel;
9
import info.bioinfweb.libralign.model.tokenset.AbstractTokenSet;
10

    
11
import org.eclipse.core.commands.ExecutionEvent;
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.commands.IElementUpdater;
17
import org.eclipse.ui.menus.UIElement;
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 AbstractFocusedAlignmentAreaHandler implements IElementUpdater {
37
    @Override
38
	@SuppressWarnings("unchecked")
39
	protected void doExecute2(ExecutionEvent event, AlignmentEditor editor, AlignmentArea focusedArea) {
40
    	SelectionModel selection = focusedArea.getSelection();
41
    	if (!selection.isEmpty()) {
42
            StringBuilder selectedCharacters = new StringBuilder();
43
			AlignmentModel<Character> alignmentModel = (AlignmentModel<Character>)focusedArea.getAlignmentModel();
44
    		for (int row = selection.getFirstRow(); row <= selection.getLastRow(); row++) {
45
            	int id = focusedArea.getSequenceOrder().idByIndex(row);
46
            	for (int column = selection.getFirstColumn(); column <= selection.getLastColumn(); column++) {
47
            		if (alignmentModel.getSequenceLength(id) > column) {
48
            			selectedCharacters.append(alignmentModel.getTokenAt(id, column));
49
            		}
50
            		else {  // Add gaps if selection is behind the end of the sequence.
51
            			selectedCharacters.append(AbstractTokenSet.DEFAULT_GAP_REPRESENTATION);
52
            		}
53
				}
54
            	if (row < selection.getLastRow()) {
55
            		selectedCharacters.append(System.getProperty("line.separator"));
56
            	}
57
			}
58
            new Clipboard(Display.getCurrent()).setContents(new Object[]{selectedCharacters.toString()}, 
59
            		new Transfer[]{TextTransfer.getInstance()});
60
    	}
61
	}
62

    
63

    
64
	@Override
65
	public boolean isEnabled() {
66
		AlignmentEditor editor = getActiveAlignmentEditor();
67
		if (editor != null) {
68
			AlignmentArea focusedArea = editor.getFocusedArea();
69
			if (focusedArea != null) {
70
				return !focusedArea.getSelection().isEmpty();
71
			}
72
		}
73
		return false;
74
	}
75

    
76

    
77
	@Override
78
	public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
79
		setBaseEnabled(isEnabled());
80
    }
81
}
(4-4/20)