KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > TextAction


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import org.eclipse.swt.SWTError;
14 import org.eclipse.swt.dnd.TextTransfer;
15 import org.eclipse.swt.dnd.Transfer;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.jface.action.Action;
18 /**
19  * Text actions (cut, copy, paste) for the Web browser.
20  */

21 public class TextAction extends Action {
22     protected BrowserViewer browser;
23     protected byte type;
24
25     public static final byte CUT = 0;
26     public static final byte COPY = 1;
27     public static final byte PASTE = 2;
28     
29     /**
30      * TextAction constructor comment.
31      */

32     protected TextAction(BrowserViewer browser, byte type) {
33         super(type + "!"); //$NON-NLS-1$
34
this.browser = browser;
35         this.type = type;
36     }
37     
38     /**
39      * Copies the selected text to the clipboard. The text will be put in the
40      * clipboard in plain text format.
41      * <p>
42      *
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>
46      * </ul>
47      */

48     public void copy() {
49         Point selection = browser.combo.getSelection();
50     
51         int length = selection.y - selection.x;
52         if (length > 0) {
53             TextTransfer plainTextTransfer = TextTransfer.getInstance();
54             try {
55                 browser.clipboard.setContents(
56                     new String JavaDoc[] { 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.
61
// Fixes 1GDQAVN
62
}
63         }
64     }
65     
66     /**
67      * Moves the selected text to the clipboard. The text will be put in the
68      * clipboard in plain text format and RTF format.
69      * <p>
70      *
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>
74      * </ul>
75      */

76     public void cut(){
77         Point selection = browser.combo.getSelection();
78     
79         if (selection.y > selection.x) {
80             copy();
81             delete();
82         }
83     }
84     
85     /**
86      * Deletes the character to the right of the caret. Delete the selected text if any.
87      */

88     public void delete() {
89         Point selection = browser.combo.getSelection();
90         String JavaDoc text = browser.combo.getText();
91     
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));
96         }
97     }
98     
99     /**
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.
105      * <p>
106      *
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>
110      * </ul>
111      */

112     public void paste() {
113         TextTransfer transfer = TextTransfer.getInstance();
114         Point selection = browser.combo.getSelection();
115         String JavaDoc text = browser.combo.getText();
116         
117         String JavaDoc newText = (String JavaDoc) 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);
121     
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));
125         }
126     }
127     
128     /**
129      * Implementation of method defined on <code>IAction</code>.
130      */

131     public void run() {
132         if (browser == null || browser.combo == null)
133             return;
134         if (type == CUT)
135             cut();
136         else if (type == COPY)
137             copy();
138         else if (type == PASTE)
139             paste();
140     }
141 }
Popular Tags