/**
* Copyright (c) 2003 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
+ * All rights reserved. � This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
- *
+ �*
* Contributors:
* IBM - Initial API and implementation
*/
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
+
/**
* Text actions (cut, copy, paste) for the Web browser.
*/
public class TextAction extends Action {
protected WebBrowser browser;
+
protected byte type;
public static final byte CUT = 0;
+
public static final byte COPY = 1;
+
public static final byte PASTE = 2;
-
+
/**
* TextAction constructor comment.
*/
this.browser = browser;
this.type = type;
}
-
+
/**
- * Copies the selected text to the clipboard. The text will be put in the
+ * Copies the selected text to the clipboard. The text will be put in the
* clipboard in plain text format.
* <p>
- *
- * @exception SWTException <ul>
- * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
- * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
- * </ul>
+ *
+ * @exception SWTException
+ * <ul>
+ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
+ * disposed</li>
+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
+ * thread that created the receiver</li>
+ * </ul>
*/
public void copy() {
Point selection = browser.combo.getSelection();
-
+
int length = selection.y - selection.x;
if (length > 0) {
TextTransfer plainTextTransfer = TextTransfer.getInstance();
try {
- browser.clipboard.setContents(
- new String[] { browser.combo.getText().substring(selection.x, selection.y) },
- new Transfer[] { plainTextTransfer });
+ browser.clipboard.setContents(new String[] { browser.combo
+ .getText().substring(selection.x, selection.y) },
+ new Transfer[] { plainTextTransfer });
} catch (SWTError error) {
- // Copy to clipboard failed. This happens when another application
+ // Copy to clipboard failed. This happens when another
+ // application
// is accessing the clipboard while we copy. Ignore the error.
// Fixes 1GDQAVN
}
}
}
-
+
/**
- * Moves the selected text to the clipboard. The text will be put in the
+ * Moves the selected text to the clipboard. The text will be put in the
* clipboard in plain text format and RTF format.
* <p>
- *
- * @exception SWTException <ul>
- * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
- * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
- * </ul>
+ *
+ * @exception SWTException
+ * <ul>
+ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
+ * disposed</li>
+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
+ * thread that created the receiver</li>
+ * </ul>
*/
- public void cut(){
+ public void cut() {
Point selection = browser.combo.getSelection();
-
+
if (selection.y > selection.x) {
copy();
delete();
}
}
-
+
/**
- * Deletes the character to the right of the caret. Delete the selected text if any.
+ * Deletes the character to the right of the caret. Delete the selected text
+ * if any.
*/
public void delete() {
Point selection = browser.combo.getSelection();
String text = browser.combo.getText();
-
+
if (selection.x != selection.y) {
text = text.substring(0, selection.x) + text.substring(selection.y);
browser.combo.setText(text);
browser.combo.setSelection(new Point(selection.x, selection.x));
}
}
-
- /**
- * Replaces the selection with the clipboard text or insert the text at
- * the current caret offset if there is no selection.
- * If the widget has the SWT.SINGLE style and the clipboard text contains
- * more than one line, only the first line without line delimiters is
- * inserted in the widget.
+
+ /**
+ * Replaces the selection with the clipboard text or insert the text at the
+ * current caret offset if there is no selection. If the widget has the
+ * SWT.SINGLE style and the clipboard text contains more than one line, only
+ * the first line without line delimiters is inserted in the widget.
* <p>
- *
- * @exception SWTException <ul>
- * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
- * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
- * </ul>
+ *
+ * @exception SWTException
+ * <ul>
+ * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
+ * disposed</li>
+ * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
+ * thread that created the receiver</li>
+ * </ul>
*/
public void paste() {
TextTransfer transfer = TextTransfer.getInstance();
Point selection = browser.combo.getSelection();
String text = browser.combo.getText();
-
+
String newText = (String) browser.clipboard.getContents(transfer);
if (newText != null && newText.length() > 0) {
- text = text.substring(0, selection.x) + newText + text.substring(selection.y);
+ text = text.substring(0, selection.x) + newText
+ + text.substring(selection.y);
browser.combo.setText(text);
-
+
// set the selection to the end of the paste
int x = selection.x + newText.length();
browser.combo.setSelection(new Point(x, x));
}
}
-
+
/**
* Implementation of method defined on <code>IAction</code>.
*/
else if (type == PASTE)
paste();
}
-
+
/**
*
*/
- protected void update() { }
+ protected void update() {
+ }
}
\ No newline at end of file