1 11 package org.eclipse.ui.internal.browser; 12 13 import org.eclipse.swt.SWTError; 14 import org.eclipse.swt.dnd.TextTransfer; 15 import org.eclipse.swt.dnd.Transfer; 16 import org.eclipse.swt.graphics.Point; 17 import org.eclipse.jface.action.Action; 18 21 public class TextAction extends Action { 22 protected BrowserViewer browser; 23 protected byte type; 24 25 public static final byte CUT = 0; 26 public static final byte COPY = 1; 27 public static final byte PASTE = 2; 28 29 32 protected TextAction(BrowserViewer browser, byte type) { 33 super(type + "!"); this.browser = browser; 35 this.type = type; 36 } 37 38 48 public void copy() { 49 Point selection = browser.combo.getSelection(); 50 51 int length = selection.y - selection.x; 52 if (length > 0) { 53 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 54 try { 55 browser.clipboard.setContents( 56 new String [] { browser.combo.getText().substring(selection.x, selection.y) }, 57 new Transfer[] { plainTextTransfer }); 58 } catch (SWTError error) { 59 } 63 } 64 } 65 66 76 public void cut(){ 77 Point selection = browser.combo.getSelection(); 78 79 if (selection.y > selection.x) { 80 copy(); 81 delete(); 82 } 83 } 84 85 88 public void delete() { 89 Point selection = browser.combo.getSelection(); 90 String text = browser.combo.getText(); 91 92 if (selection.x != selection.y) { 93 text = text.substring(0, selection.x) + text.substring(selection.y); 94 browser.combo.setText(text); 95 browser.combo.setSelection(new Point(selection.x, selection.x)); 96 } 97 } 98 99 112 public void paste() { 113 TextTransfer transfer = TextTransfer.getInstance(); 114 Point selection = browser.combo.getSelection(); 115 String text = browser.combo.getText(); 116 117 String newText = (String ) browser.clipboard.getContents(transfer); 118 if (newText != null && newText.length() > 0) { 119 text = text.substring(0, selection.x) + newText + text.substring(selection.y); 120 browser.combo.setText(text); 121 122 int x = selection.x + newText.length(); 124 browser.combo.setSelection(new Point(x, x)); 125 } 126 } 127 128 131 public void run() { 132 if (browser == null || browser.combo == null) 133 return; 134 if (type == CUT) 135 cut(); 136 else if (type == COPY) 137 copy(); 138 else if (type == PASTE) 139 paste(); 140 } 141 } | Popular Tags |