intial source from http://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / TextAction.java
1 /**
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser.internal;
12
13 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowser;
14
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;
19 /**
20  * Text actions (cut, copy, paste) for the Web browser.
21  */
22 public class TextAction extends Action {
23         protected WebBrowser browser;
24         protected byte type;
25
26         public static final byte CUT = 0;
27         public static final byte COPY = 1;
28         public static final byte PASTE = 2;
29         
30         /**
31          * TextAction constructor comment.
32          */
33         protected TextAction(WebBrowser browser, byte type) {
34                 super(type + "!");
35                 this.browser = browser;
36                 this.type = type;
37         }
38         
39         /**
40          * Copies the selected text to the clipboard.  The text will be put in the 
41          * clipboard in plain text format.
42          * <p>
43          *
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>
47          * </ul>
48          */
49         public void copy() {
50                 Point selection = browser.combo.getSelection();
51         
52                 int length = selection.y - selection.x;
53                 if (length > 0) {
54                         TextTransfer plainTextTransfer = TextTransfer.getInstance();
55                         try {
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.
62                                 // Fixes 1GDQAVN
63                         }
64                 }
65         }
66         
67         /**
68          * Moves the selected text to the clipboard.  The text will be put in the 
69          * clipboard in plain text format and RTF format.
70          * <p>
71          *
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>
75          * </ul>
76          */
77         public void cut(){
78                 Point selection = browser.combo.getSelection();
79         
80                 if (selection.y > selection.x) {
81                         copy();
82                         delete();
83                 }
84         }
85         
86         /**
87          * Deletes the character to the right of the caret. Delete the selected text if any.
88          */
89         public void delete() {
90                 Point selection = browser.combo.getSelection();
91                 String text = browser.combo.getText();
92         
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));
97                 }
98         }
99         
100         /** 
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.
106          * <p>
107          *
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>
111          * </ul>
112          */
113         public void paste() {
114                 TextTransfer transfer = TextTransfer.getInstance();
115                 Point selection = browser.combo.getSelection();
116                 String text = browser.combo.getText();
117                 
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);
122         
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));
126                 }
127         }
128         
129         /**
130          * Implementation of method defined on <code>IAction</code>.
131          */
132         public void run() {
133                 if (browser == null || browser.combo == null)
134                         return;
135                 if (type == CUT)
136                         cut();
137                 else if (type == COPY)
138                         copy();
139                 else if (type == PASTE)
140                         paste();
141         }
142         
143         /**
144          * 
145          */
146         protected void update() {       }
147 }