[hg] main-silver: #218915 - Paste command in rectangular mode de...

  • From: Miloslav Metelka < >
  • To: ,
  • Subject: [hg] main-silver: #218915 - Paste command in rectangular mode de...
  • Date: Mon, 03 Dec 2012 12:45:04 -0800

changeset 6d5165db8d44 in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/6d5165db8d44
description:
        #218915 - Paste command in rectangular mode deletes lines.

diffstat:

 
editor.lib2/src/org/netbeans/modules/editor/lib2/RectangularSelectionTransferHandler.java
 |  56 +++++++++-
 1 files changed, 53 insertions(+), 3 deletions(-)

diffs (84 lines):

diff --git 
a/editor.lib2/src/org/netbeans/modules/editor/lib2/RectangularSelectionTransferHandler.java
 
b/editor.lib2/src/org/netbeans/modules/editor/lib2/RectangularSelectionTransferHandler.java
--- 
a/editor.lib2/src/org/netbeans/modules/editor/lib2/RectangularSelectionTransferHandler.java
+++ 
b/editor.lib2/src/org/netbeans/modules/editor/lib2/RectangularSelectionTransferHandler.java
@@ -49,6 +49,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.StringTokenizer;
 import java.util.logging.Logger;
 import javax.swing.Icon;
 import javax.swing.JComponent;
@@ -227,8 +228,9 @@
     public boolean importData(TransferSupport support) {
         Component c = support.getComponent();
         Transferable t = support.getTransferable();
+        if (c instanceof JTextComponent) {
+            JTextComponent tc = (JTextComponent) c;
         if (t.isDataFlavorSupported(RECTANGULAR_SELECTION_FLAVOR) && c 
instanceof JTextComponent) {
-            JTextComponent tc = (JTextComponent) c;
             boolean result = false;
             try {
                 if 
(Boolean.TRUE.equals(tc.getClientProperty(RECTANGULAR_SELECTION_PROPERTY))) {
@@ -273,11 +275,59 @@
 
             return result;
 
-        } else { // Regular importData()
-            return delegate.importData(support);
+            } else if (RectangularSelectionUtils.isRectangularSelection(tc)) 
{
+                if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
+                    try {
+                        // Paste individual lines into rectangular selection
+                        String s = (String) 
t.getTransferData(DataFlavor.stringFlavor); // There should be string flavor
+                        final List<String> dataLines = splitByLines(s);
+                        final List<Position> regions = 
RectangularSelectionUtils.regionsCopy(tc);
+                        final Document doc = tc.getDocument();
+                        final int dataLinesSize = dataLines.size();
+                        final int regionsSize = regions.size();
+                        if (dataLinesSize > 0 && regionsSize > 0) { // 
Otherwise do nothing
+                            DocUtils.runAtomicAsUser(doc, new Runnable() {
+                                @Override
+                                public void run() {
+                                    try {
+                                        
RectangularSelectionUtils.removeSelection(doc, regions);
+                                        int dataLineIndex = 0;
+                                        for (int i = 0; i < regionsSize; i 
+= 2) {
+                                            Position linePos = 
regions.get(i);
+                                            
doc.insertString(linePos.getOffset(),
+                                                    
dataLines.get(dataLineIndex++), null);
+                                            if (dataLineIndex >= 
dataLinesSize) {
+                                                dataLineIndex = 0;
         }
     }
+                                    } catch (BadLocationException ex) {
+                                        // Ignore
+                                    }
+                                }
+                            });
+                            return true;
+                        } else {
+                            return false;
+                        }
+                    } catch (UnsupportedFlavorException ex) {
+                        Exceptions.printStackTrace(ex);
+                    } catch (IOException ex) {
+                        Exceptions.printStackTrace(ex);
+                    }
+                }
+            }
+        }
+        return delegate.importData(support); // Regular importData()
+    }
     
+    private static List<String> splitByLines(String s) {
+        StringTokenizer splitByLines = new StringTokenizer(s, "\n", false);
+        List<String> lines = new ArrayList<String>();
+        while (splitByLines.hasMoreTokens()) {
+            lines.add(splitByLines.nextToken());
+        }
+        return lines;
+    }
 
     private static final class WrappedTransferable implements Transferable {
 

[hg] main-silver: #218915 - Paste command in rectangular mode de...

Miloslav Metelka 12/03/2012

Project Features

About this Project

Editor was started in November 2009, is owned by Martin Ryzl, and has 147 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close