Workaround for feature request #1154254 (sticky browser URL) that solves bug #1827766...
[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 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;
19
20 /**
21  * Text actions (cut, copy, paste) for the Web browser.
22  */
23 public class TextAction extends Action {
24         protected WebBrowser browser;
25
26         protected byte type;
27
28         public static final byte CUT = 0;
29
30         public static final byte COPY = 1;
31
32         public static final byte PASTE = 2;
33
34         /**
35          * TextAction constructor comment.
36          */
37         protected TextAction(WebBrowser browser, byte type) {
38                 super(type + "!");
39                 this.browser = browser;
40                 this.type = type;
41         }
42
43         /**
44          * Copies the selected text to the clipboard. The text will be put in the
45          * clipboard in plain text format.
46          * <p>
47          * 
48          * @exception SWTException
49          *                <ul>
50          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
51          *                disposed</li>
52          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
53          *                thread that created the receiver</li>
54          *                </ul>
55          */
56         public void copy() {
57                 Point selection = browser.combo.getSelection();
58
59                 int length = selection.y - selection.x;
60                 if (length > 0) {
61                         TextTransfer plainTextTransfer = TextTransfer.getInstance();
62                         try {
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
68                                 // application
69                                 // is accessing the clipboard while we copy. Ignore the error.
70                                 // Fixes 1GDQAVN
71                         }
72                 }
73         }
74
75         /**
76          * Moves the selected text to the clipboard. The text will be put in the
77          * clipboard in plain text format and RTF format.
78          * <p>
79          * 
80          * @exception SWTException
81          *                <ul>
82          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
83          *                disposed</li>
84          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
85          *                thread that created the receiver</li>
86          *                </ul>
87          */
88         public void cut() {
89                 Point selection = browser.combo.getSelection();
90
91                 if (selection.y > selection.x) {
92                         copy();
93                         delete();
94                 }
95         }
96
97         /**
98          * Deletes the character to the right of the caret. Delete the selected text
99          * if any.
100          */
101         public void delete() {
102                 Point selection = browser.combo.getSelection();
103                 String text = browser.combo.getText();
104
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));
109                 }
110         }
111
112         /**
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.
117          * <p>
118          * 
119          * @exception SWTException
120          *                <ul>
121          *                <li>ERROR_WIDGET_DISPOSED - if the receiver has been
122          *                disposed</li>
123          *                <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
124          *                thread that created the receiver</li>
125          *                </ul>
126          */
127         public void paste() {
128                 TextTransfer transfer = TextTransfer.getInstance();
129                 Point selection = browser.combo.getSelection();
130                 String text = browser.combo.getText();
131
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);
137
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));
141                 }
142         }
143
144         /**
145          * Implementation of method defined on <code>IAction</code>.
146          */
147         public void run() {
148                 if (browser == null || browser.combo == null)
149                         return;
150                 if (type == CUT)
151                         cut();
152                 else if (type == COPY)
153                         copy();
154                 else if (type == PASTE)
155                         paste();
156         }
157
158         /**
159          * 
160          */
161         protected void update() {
162         }
163 }