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 net.sourceforge.phpeclipse.webbrowser.internal;
13 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowser;
15 import org.eclipse.swt.SWTError;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.dnd.*;
18 import org.eclipse.jface.action.Action;
20 * Text actions (cut, copy, paste) for the Web browser.
22 public class TextAction extends Action {
23 protected WebBrowser browser;
26 public static final byte CUT = 0;
27 public static final byte COPY = 1;
28 public static final byte PASTE = 2;
31 * TextAction constructor comment.
33 protected TextAction(WebBrowser browser, byte type) {
35 this.browser = browser;
40 * Copies the selected text to the clipboard. The text will be put in the
41 * clipboard in plain text format.
44 * @exception SWTException <ul>
45 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
46 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
50 Point selection = browser.combo.getSelection();
52 int length = selection.y - selection.x;
54 TextTransfer plainTextTransfer = TextTransfer.getInstance();
56 browser.clipboard.setContents(
57 new String[] { browser.combo.getText().substring(selection.x, selection.y) },
58 new Transfer[] { plainTextTransfer });
59 } catch (SWTError error) {
60 // Copy to clipboard failed. This happens when another application
61 // is accessing the clipboard while we copy. Ignore the error.
68 * Moves the selected text to the clipboard. The text will be put in the
69 * clipboard in plain text format and RTF format.
72 * @exception SWTException <ul>
73 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
74 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
78 Point selection = browser.combo.getSelection();
80 if (selection.y > selection.x) {
87 * Deletes the character to the right of the caret. Delete the selected text if any.
89 public void delete() {
90 Point selection = browser.combo.getSelection();
91 String text = browser.combo.getText();
93 if (selection.x != selection.y) {
94 text = text.substring(0, selection.x) + text.substring(selection.y);
95 browser.combo.setText(text);
96 browser.combo.setSelection(new Point(selection.x, selection.x));
101 * Replaces the selection with the clipboard text or insert the text at
102 * the current caret offset if there is no selection.
103 * If the widget has the SWT.SINGLE style and the clipboard text contains
104 * more than one line, only the first line without line delimiters is
105 * inserted in the widget.
108 * @exception SWTException <ul>
109 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
110 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
113 public void paste() {
114 TextTransfer transfer = TextTransfer.getInstance();
115 Point selection = browser.combo.getSelection();
116 String text = browser.combo.getText();
118 String newText = (String) browser.clipboard.getContents(transfer);
119 if (newText != null && newText.length() > 0) {
120 text = text.substring(0, selection.x) + newText + text.substring(selection.y);
121 browser.combo.setText(text);
123 // set the selection to the end of the paste
124 int x = selection.x + newText.length();
125 browser.combo.setSelection(new Point(x, x));
130 * Implementation of method defined on <code>IAction</code>.
133 if (browser == null || browser.combo == null)
137 else if (type == COPY)
139 else if (type == PASTE)
146 protected void update() { }