Project

General

Profile

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

    
3

    
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.Scanner;
7

    
8
import javax.inject.Named;
9

    
10
import org.eclipse.e4.core.di.annotations.CanExecute;
11
import org.eclipse.e4.core.di.annotations.Execute;
12
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
13
import org.eclipse.e4.ui.services.IServiceConstants;
14
import org.eclipse.jface.dialogs.MessageDialog;
15
import org.eclipse.swt.dnd.TextTransfer;
16
import org.eclipse.swt.widgets.Shell;
17

    
18
import eu.etaxonomy.taxeditor.molecular.editor.e4.AlignmentEditorE4;
19
import eu.etaxonomy.taxeditor.molecular.l10n.Messages;
20
import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
21
import info.bioinfweb.libralign.alignmentarea.order.SequenceOrder;
22
import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
23
import info.bioinfweb.libralign.model.AlignmentModel;
24
import info.bioinfweb.libralign.model.utils.AlignmentModelUtils;
25

    
26

    
27

    
28
/**
29
 * Handler that pastes the current contents of the clipboard into an active instance of {@link AlignmentEditorE4}.
30
 *
31
 * @author Ben Stöver
32
 * @date 26.08.2015
33
 */
34
public class AlignmentEditorPasteHandlerE4 {
35

    
36
	private void pasteString(AlignmentArea area, String sequenceID, String content) {
37
		area.getActionProvider().deleteSelection();  // Overwrite selected tokens.
38
		area.getActionProvider().elongateSequence(sequenceID, area.getSelection().getCursorColumn());
39

    
40
		@SuppressWarnings("unchecked")
41
		AlignmentModel<Object> alignmentModel = (AlignmentModel<Object>)area.getAlignmentModel();
42
		alignmentModel.insertTokensAt(sequenceID, area.getSelection().getCursorColumn(),
43
				AlignmentModelUtils.charSequenceToTokenList(content, alignmentModel.getTokenSet(),
44
						true, alignmentModel.getTokenSet().getGapToken()));
45
	}
46

    
47

    
48
	@Execute
49
	protected void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
50
	        @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
51
        AlignmentEditorE4 editor = (AlignmentEditorE4) activePart.getObject();
52
        AlignmentArea focusedArea = editor.getFocusedArea();
53

    
54
		SelectionModel selection = focusedArea.getSelection();
55
		String clipboardText = (String)editor.CLIPBOARD.getContents(TextTransfer.getInstance());
56
		if (clipboardText != null) {
57
			List<String> lines = new ArrayList<String>();
58
			Scanner scanner = new Scanner(clipboardText);
59
			try {
60
				while (scanner.hasNext()) {
61
					lines.add(scanner.nextLine());
62
				}
63
				if (lines.get(lines.size() - 1).equals("")) { //$NON-NLS-1$
64
					lines.remove(lines.size() - 1);
65
				}
66
			}
67
			finally {
68
				scanner.close();
69
			}
70

    
71
			if (!lines.isEmpty()) { //TODO Can lines be empty? (Can an empty string "" be copied to the clipboard?)
72
				if (selection.getCursorHeight() == 1) {  // If the consensus sequence is focused, this is the only possible case.
73
					String sequenceID = focusedArea.getSequenceOrder().idByIndex(selection.getCursorRow());
74
					if (lines.size() == 1) {
75
						pasteString(focusedArea, sequenceID, lines.get(0));
76
					}
77
					else {
78
						MessageDialog dialog = new MessageDialog(shell,
79
								Messages.AlignmentEditorPasteHandler_PASTING_LINES, null,
80
								String.format(Messages.AlignmentEditorPasteHandler_PASTING_LINES_QUESTION, lines.size()),
81
								MessageDialog.QUESTION,
82
								new String[]{Messages.AlignmentEditorPasteHandler_PASTING_LINES_IGNORE,
83
										Messages.AlignmentEditorPasteHandler_PASTING_LINES_FIRST_LINE, Messages.AlignmentEditorPasteHandler_CANCEL},
84
								0);
85
						//TODO Does the dialog have to be disposed in some way?
86

    
87
						switch (dialog.open()) {
88
							case 0:  // Paste all lines in one sequence.
89
								pasteString(focusedArea, sequenceID, clipboardText);
90
								break;
91
							case 1:  // Paste only first line.
92
								pasteString(focusedArea, sequenceID, lines.get(0));
93
								break;
94
						}
95
					}
96
				}
97
				else {
98
					if (selection.getCursorHeight() == lines.size()) {
99
						SequenceOrder order = focusedArea.getSequenceOrder();
100
						for (int i = 0; i < selection.getCursorHeight(); i++) {
101
							pasteString(focusedArea, order.idByIndex(selection.getCursorRow() + i), lines.get(i));  // Multiple calls of deleteSelection() in here are unnecessary, but should have no effect.
102
						}
103
					}
104
					else {
105
						MessageDialog.openError(shell,
106
								Messages.AlignmentEditorPasteHandler_PASTE_FAILURE,
107
								String.format(Messages.AlignmentEditorPasteHandler_PASTE_FAILURE_MESSAGE, selection.getCursorHeight(), lines.size(), System.getProperty("line.separator"))); //$NON-NLS-1$
108
					}
109
				}
110
			}
111
		}
112
	}
113

    
114

    
115
	@CanExecute
116
	public boolean isEnabled(@Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
117
        AlignmentEditorE4 editor = (AlignmentEditorE4) activePart.getObject();
118
        AlignmentArea focusedArea = editor.getFocusedArea();
119
        focusedArea = editor.getFocusedArea();
120
        return (focusedArea != null);
121
	}
122

    
123
}
(3-3/17)