Project

General

Profile

Download (3.34 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.core.commands.ExecutionException;
13
import org.eclipse.swt.dnd.Clipboard;
14
import org.eclipse.swt.dnd.TextTransfer;
15
import org.eclipse.swt.dnd.Transfer;
16
import org.eclipse.swt.widgets.Display;
17
import org.eclipse.ui.commands.IElementUpdater;
18
import org.eclipse.ui.menus.UIElement;
19

    
20
import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
21

    
22

    
23

    
24
/**
25
 * Handler that copies the currently selected nucleotides from an alignment editor to the clipboard.
26
 * <p>
27
 * The copied contents either come from the single reads or the consensus sequence alignment area,
28
 * depending on which component currently has the focus. If none of these components has the focus,
29
 * nothing will be copied, even if nucleotides are currently selected.
30
 * <p>
31
 * If the selection contains parts of multiple sequence, these are separated by the line separator
32
 * if the current operating system.  
33
 * 
34
 * @author Ben Stöver
35
 * @date 25.08.2015
36
 */
37
public class AlignmentEditorCopyHandler extends AbstractAlignmentEditorHandler implements IElementUpdater {
38
	@SuppressWarnings("unchecked")
39
    public static void copySelectionAsString(AlignmentArea area) {
40
    	SelectionModel selection = area.getSelection();
41
    	if (!selection.isEmpty()) {
42
            StringBuilder selectedCharacters = new StringBuilder();
43
			AlignmentModel<Character> alignmentModel = (AlignmentModel<Character>)area.getAlignmentModel();
44
    		for (int row = selection.getFirstRow(); row <= selection.getLastRow(); row++) {
45
            	int id = area.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 void doExecute(ExecutionEvent event, AlignmentEditor editor) throws ExecutionException {
66
        AlignmentArea focusedArea = editor.getFocusedArea();
67
        if (focusedArea != null) {
68
        	copySelectionAsString(focusedArea);
69
        }
70
	}
71

    
72

    
73
	@Override
74
	public boolean isEnabled() {
75
		AlignmentEditor editor = getActiveAlignmentEditor();
76
		if (editor != null) {
77
			AlignmentArea focusedArea = editor.getFocusedArea();
78
			if (focusedArea != null) {
79
				return !focusedArea.getSelection().isEmpty();
80
			}
81
		}
82
		return false;
83
	}
84

    
85

    
86
	@Override
87
	public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
88
		setBaseEnabled(isEnabled());
89
    }
90
}
(3-3/19)