Project

General

Profile

« Previous | Next » 

Revision ab9f98b3

Added by Ben Stöver over 8 years ago

Paste action for AlignmentEditor added.

View differences:

.gitattributes
936 936
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java -text
937 937
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AbstractFocusedAlignmentAreaHandler.java -text
938 938
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorCutHandler.java -text
939
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorPasteHandler.java -text
939 940
eu.etaxonomy.taxeditor.navigation/.classpath -text
940 941
eu.etaxonomy.taxeditor.navigation/.project -text
941 942
eu.etaxonomy.taxeditor.navigation/META-INF/MANIFEST.MF -text
eu.etaxonomy.taxeditor.molecular/plugin.xml
284 284
               </with>
285 285
            </activeWhen>
286 286
         </handler>
287
         <handler
288
               class="eu.etaxonomy.taxeditor.molecular.handler.AlignmentEditorPasteHandler"
289
               commandId="org.eclipse.ui.edit.paste">
290
            <activeWhen>
291
               <with
292
                     variable="activePartId">
293
                  <equals
294
                        value="eu.etaxonomy.taxeditor.molecular.AlignmentEditor">
295
                  </equals>
296
               </with>
297
            </activeWhen>
298
         </handler>
287 299
      </extension>
288 300
      <extension
289 301
            point="org.eclipse.core.expressions.definitions">
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/editor/AlignmentEditorActionUpdater.java
20 20
 * @date 25.08.2015
21 21
 */
22 22
public class AlignmentEditorActionUpdater implements SelectionListener, Listener {
23
	private void updateEvents(String[] ids) {
23
	private static final String[] IDS = {ActionFactory.COPY.getCommandId(), ActionFactory.CUT.getCommandId(), 
24
		ActionFactory.PASTE.getCommandId()};
25
	
26
	
27
	private void updateEvents() {
24 28
		ICommandService service = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
25
		for (int i = 0; i < ids.length; i++) {
26
	        service.refreshElements(ids[i], null);
29
		for (int i = 0; i < IDS.length; i++) {
30
	        service.refreshElements(IDS[i], null);
27 31
		}
28 32
	}
29 33
	
......
32 36
	public void handleEvent(Event event) {
33 37
		AlignmentEditor editor = AbstractAlignmentEditorHandler.getActiveAlignmentEditor();
34 38
		if (editor != null) {
35
            updateEvents(new String[]{ActionFactory.COPY.getCommandId(), ActionFactory.CUT.getCommandId(), 
36
            		ActionFactory.PASTE.getCommandId()});
39
            updateEvents();
37 40
		}
38 41
	}
39 42

  
......
45 48
			if ((e.getSource() == editor.getReadsArea().getSelection()) || 
46 49
					(e.getSource() == editor.getEditableConsensusArea().getSelection())) {
47 50
				
48
				updateEvents(new String[]{ActionFactory.COPY.getCommandId(),  ActionFactory.CUT.getCommandId()});
51
				updateEvents();
49 52
			}
50 53
		}
51 54
	}
eu.etaxonomy.taxeditor.molecular/src/main/java/eu/etaxonomy/taxeditor/molecular/handler/AlignmentEditorPasteHandler.java
1
package eu.etaxonomy.taxeditor.molecular.handler;
2

  
3

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

  
9
import info.bioinfweb.libralign.alignmentarea.AlignmentArea;
10
import info.bioinfweb.libralign.alignmentarea.order.SequenceOrder;
11
import info.bioinfweb.libralign.alignmentarea.selection.SelectionModel;
12
import info.bioinfweb.libralign.model.AlignmentModel;
13
import info.bioinfweb.libralign.model.AlignmentModelUtils;
14

  
15
import org.eclipse.core.commands.ExecutionEvent;
16
import org.eclipse.jface.dialogs.MessageDialog;
17
import org.eclipse.swt.dnd.TextTransfer;
18
import org.eclipse.ui.commands.IElementUpdater;
19
import org.eclipse.ui.handlers.HandlerUtil;
20
import org.eclipse.ui.menus.UIElement;
21

  
22
import eu.etaxonomy.taxeditor.molecular.editor.AlignmentEditor;
23

  
24

  
25

  
26
/**
27
 * Handler that pastes the current contents of the clipboard into an active instance of {@link AlignmentEditor}.
28
 * 
29
 * @author Ben Stöver
30
 * @date 26.08.2015
31
 */
32
public class AlignmentEditorPasteHandler extends AbstractFocusedAlignmentAreaHandler implements IElementUpdater {
33
	private void pasteString(AlignmentArea area, int sequenceID, String content) {
34
		AlignmentModel<Object> alignmentModel = (AlignmentModel<Object>)area.getAlignmentModel();
35
		List<Object> tokens = AlignmentModelUtils.charSequenceToTokenList(content, alignmentModel.getTokenSet(),
36
				true, alignmentModel.getTokenSet().getGapToken()); 
37
		
38
		area.getActionProvider().deleteSelection();  // Overwrite selected tokens.
39
		area.getActionProvider().elongateSequence(sequenceID, area.getSelection().getCursorColumn());
40
		alignmentModel.insertTokensAt(sequenceID, area.getSelection().getCursorColumn(), tokens);
41
	}
42
	
43
	
44
	@Override
45
	protected void doExecute2(ExecutionEvent event, AlignmentEditor editor, AlignmentArea focusedArea) {
46
		SelectionModel selection = focusedArea.getSelection();
47
		String clipboardText = (String)editor.CLIPBOARD.getContents(TextTransfer.getInstance());
48
		if (clipboardText != null) {
49
			List<String> lines = new ArrayList<String>();
50
			Scanner scanner = new Scanner(clipboardText);
51
			try {
52
				while (scanner.hasNext()) {
53
					lines.add(scanner.nextLine());
54
				}
55
				if (lines.get(lines.size() - 1).equals("")) {
56
					lines.remove(lines.size() - 1);
57
				}
58
			}
59
			finally {
60
				scanner.close();
61
			}
62
			
63
			if (!lines.isEmpty()) { //TODO Can lines be empty? (Can an empty string "" be copied to the clipboard?)
64
				if (selection.getCursorHeight() == 1) {  // If the consensus sequence is focused, this is the only possible case.
65
					int sequenceID = focusedArea.getSequenceOrder().idByIndex(selection.getCursorRow());
66
					if (lines.size() == 1) {
67
						pasteString(focusedArea, sequenceID, lines.get(0));
68
					}
69
					else {
70
						MessageDialog dialog = new MessageDialog(HandlerUtil.getActiveWorkbenchWindow(event).getShell(),  //TODO Can the window be null? 
71
								"Pasting multiple lines", null,
72
								"The text to be pasted contains mutlitple lines (" + lines.size() + 
73
								") although the current cursor height is one. What do you want to do?", 
74
								MessageDialog.QUESTION, 
75
								new String[]{"Ingnore line breaks and paste as one sequence", 
76
										"Only paste the first line from the clipboard", "Cancel"}, 
77
								0);
78
						//TODO Does the dialog have to be disposed in some way?
79
						
80
						switch (dialog.open()) {
81
							case 0:  // Paste all lines in one sequence.
82
								pasteString(focusedArea, sequenceID, clipboardText);
83
								break;
84
							case 1:  // Paste only first line.
85
								pasteString(focusedArea, sequenceID, lines.get(0));
86
								break;
87
						}
88
					}
89
				}
90
				else {
91
					if (selection.getCursorHeight() == lines.size()) {
92
						SequenceOrder order = focusedArea.getSequenceOrder();
93
						for (int i = 0; i < selection.getCursorHeight(); i++) {
94
							pasteString(focusedArea, order.idByIndex(selection.getCursorRow() + i), lines.get(i));  // Multiple calls of deleteSelection() in here are unnecessary, but should have no effect.
95
						}
96
					}
97
					else {
98
						MessageDialog.openError(HandlerUtil.getActiveWorkbenchWindow(event).getShell(),  //TODO Can the window be null?
99
								"Unable to paste multiple lines", 
100
								"The current cursor height (" + selection.getCursorHeight() + 
101
								") does not match the number of lines to be pasted (" + lines.size() + ")." + 
102
								System.getProperty("line.separator") + System.getProperty("line.separator") + 
103
								"You can either change the cursor height accordingly or set the cursor height to one "
104
								+ "allowing you to paste all lines from the clipboad into one sequence.");
105
					}
106
				}
107
			}
108
		}
109
	}
110

  
111

  
112
	@Override
113
	public boolean isEnabled() {
114
		AlignmentEditor editor = getActiveAlignmentEditor();
115
		if (editor != null) {
116
			AlignmentArea focusedArea = editor.getFocusedArea();
117
			return (focusedArea != null);
118
		}
119
		else {
120
			return false;
121
		}
122
	}
123

  
124

  
125
	@Override
126
	public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
127
		setBaseEnabled(isEnabled());
128
    }
129
}

Also available in: Unified diff