2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
11 package org.eclipse.webbrowser.internal;
13 import org.eclipse.swt.SWTError;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.dnd.*;
16 import org.eclipse.webbrowser.internal.WebBrowser;
17 import org.eclipse.jface.action.Action;
19 * Text actions (cut, copy, paste) for the Web browser.
21 public class TextAction extends Action {
22 protected WebBrowser browser;
25 public static final byte CUT = 0;
26 public static final byte COPY = 1;
27 public static final byte PASTE = 2;
30 * TextAction constructor comment.
32 protected TextAction(WebBrowser browser, byte type) {
34 this.browser = browser;
39 * Copies the selected text to the clipboard. The text will be put in the
40 * clipboard in plain text format.
43 * @exception SWTException <ul>
44 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
45 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
49 Point selection = browser.combo.getSelection();
51 int length = selection.y - selection.x;
53 TextTransfer plainTextTransfer = TextTransfer.getInstance();
55 browser.clipboard.setContents(
56 new String[] { browser.combo.getText().substring(selection.x, selection.y) },
57 new Transfer[] { plainTextTransfer });
58 } catch (SWTError error) {
59 // Copy to clipboard failed. This happens when another application
60 // is accessing the clipboard while we copy. Ignore the error.
67 * Moves the selected text to the clipboard. The text will be put in the
68 * clipboard in plain text format and RTF format.
71 * @exception SWTException <ul>
72 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
73 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
77 Point selection = browser.combo.getSelection();
79 if (selection.y > selection.x) {
86 * Deletes the character to the right of the caret. Delete the selected text if any.
88 public void delete() {
89 Point selection = browser.combo.getSelection();
90 String text = browser.combo.getText();
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));
100 * Replaces the selection with the clipboard text or insert the text at
101 * the current caret offset if there is no selection.
102 * If the widget has the SWT.SINGLE style and the clipboard text contains
103 * more than one line, only the first line without line delimiters is
104 * inserted in the widget.
107 * @exception SWTException <ul>
108 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
109 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
112 public void paste() {
113 TextTransfer transfer = TextTransfer.getInstance();
114 Point selection = browser.combo.getSelection();
115 String text = browser.combo.getText();
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);
122 // set the selection to the end of the paste
123 int x = selection.x + newText.length();
124 browser.combo.setSelection(new Point(x, x));
129 * Implementation of method defined on <code>IAction</code>.
132 if (browser == null || browser.combo == null)
136 else if (type == COPY)
138 else if (type == PASTE)
145 protected void update() { }