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 org.eclipse.jface.action.Action;
 
  14 import org.eclipse.swt.SWTError;
 
  15 import org.eclipse.swt.SWTException;
 
  16 import org.eclipse.swt.dnd.TextTransfer;
 
  17 import org.eclipse.swt.dnd.Transfer;
 
  18 import org.eclipse.swt.graphics.Point;
 
  21  * Text actions (cut, copy, paste) for the Web browser.
 
  23 public class TextAction extends Action {
 
  24         protected WebBrowser browser;
 
  28         public static final byte CUT = 0;
 
  30         public static final byte COPY = 1;
 
  32         public static final byte PASTE = 2;
 
  35          * TextAction constructor comment.
 
  37         protected TextAction(WebBrowser browser, byte type) {
 
  39                 this.browser = browser;
 
  44          * Copies the selected text to the clipboard. The text will be put in the
 
  45          * clipboard in plain text format.
 
  48          * @exception SWTException
 
  50          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
 
  52          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
 
  53          *                thread that created the receiver</li>
 
  57                 Point selection = browser.combo.getSelection();
 
  59                 int length = selection.y - selection.x;
 
  61                         TextTransfer plainTextTransfer = TextTransfer.getInstance();
 
  63                                 browser.clipboard.setContents(new String[] { browser.combo
 
  64                                                 .getText().substring(selection.x, selection.y) },
 
  65                                                 new Transfer[] { plainTextTransfer });
 
  66                         } catch (SWTError error) {
 
  67                                 // Copy to clipboard failed. This happens when another
 
  69                                 // is accessing the clipboard while we copy. Ignore the error.
 
  76          * Moves the selected text to the clipboard. The text will be put in the
 
  77          * clipboard in plain text format and RTF format.
 
  80          * @exception SWTException
 
  82          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
 
  84          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
 
  85          *                thread that created the receiver</li>
 
  89                 Point selection = browser.combo.getSelection();
 
  91                 if (selection.y > selection.x) {
 
  98          * Deletes the character to the right of the caret. Delete the selected text
 
 101         public void delete() {
 
 102                 Point selection = browser.combo.getSelection();
 
 103                 String text = browser.combo.getText();
 
 105                 if (selection.x != selection.y) {
 
 106                         text = text.substring(0, selection.x) + text.substring(selection.y);
 
 107                         browser.combo.setText(text);
 
 108                         browser.combo.setSelection(new Point(selection.x, selection.x));
 
 113          * Replaces the selection with the clipboard text or insert the text at the
 
 114          * current caret offset if there is no selection. If the widget has the
 
 115          * SWT.SINGLE style and the clipboard text contains more than one line, only
 
 116          * the first line without line delimiters is inserted in the widget.
 
 119          * @exception SWTException
 
 121          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
 
 123          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
 
 124          *                thread that created the receiver</li>
 
 127         public void paste() {
 
 128                 TextTransfer transfer = TextTransfer.getInstance();
 
 129                 Point selection = browser.combo.getSelection();
 
 130                 String text = browser.combo.getText();
 
 132                 String newText = (String) browser.clipboard.getContents(transfer);
 
 133                 if (newText != null && newText.length() > 0) {
 
 134                         text = text.substring(0, selection.x) + newText
 
 135                                         + text.substring(selection.y);
 
 136                         browser.combo.setText(text);
 
 138                         // set the selection to the end of the paste
 
 139                         int x = selection.x + newText.length();
 
 140                         browser.combo.setSelection(new Point(x, x));
 
 145          * Implementation of method defined on <code>IAction</code>.
 
 148                 if (browser == null || browser.combo == null)
 
 152                 else if (type == COPY)
 
 154                 else if (type == PASTE)
 
 161         protected void update() {