KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > DeleteLineAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.texteditor;
13
14
15 import java.util.ResourceBundle JavaDoc;
16
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextSelection;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22
23 /**
24  * An action to delete a whole line, the fraction of the line that is left from the cursor
25  * or the fraction that is right from the cursor.
26  *
27  * @since 2.0
28  */

29 public class DeleteLineAction extends TextEditorAction {
30
31     /**
32      * Delete the whole line.
33      */

34     public static final int WHOLE= 0;
35     /**
36      * Delete to the beginning of line.
37      */

38     public static final int TO_BEGINNING= 1;
39     /**
40      * Delete to the end of line.
41      */

42     public static final int TO_END= 2;
43
44     /**
45      * The type of deletion.
46      */

47     private final int fType;
48     /**
49      * Should the deleted line be copied to the clipboard.
50      * @since 2.1
51      */

52     private final boolean fCopyToClipboard;
53     /** The deletion target.
54      * @since 2.1
55      */

56     private DeleteLineTarget fTarget;
57
58     /**
59      * Creates a line delimiter conversion action.
60      *
61      * @param bundle the resource bundle for UI strings
62      * @param prefix the prefix for the property keys into <code>bundle</code>
63      * @param editor the editor
64      * @param type the line deletion type, must be one of
65      * <code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
66      */

67     public DeleteLineAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, int type) {
68         this(bundle, prefix, editor, type, true);
69     }
70
71     /**
72      * Creates a line deletion action.
73      *
74      * @param bundle the resource bundle for UI strings
75      * @param prefix the prefix for the property keys into <code>bundle</code>
76      * @param editor the editor
77      * @param type the line deletion type, must be one of
78      * <code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
79      * @param copyToClipboard if <code>true</code>, the contents of the deleted line are copied to the clipboard
80      * @since 2.1
81      */

82     public DeleteLineAction(ResourceBundle JavaDoc bundle, String JavaDoc prefix, ITextEditor editor, int type, boolean copyToClipboard) {
83         super(bundle, prefix, editor);
84         fType= type;
85         fCopyToClipboard= copyToClipboard;
86         update();
87     }
88
89     /**
90      * Returns the editor's document.
91      *
92      * @param editor the editor
93      * @return the editor's document
94      */

95     private static IDocument getDocument(ITextEditor editor) {
96
97         IDocumentProvider documentProvider= editor.getDocumentProvider();
98         if (documentProvider == null)
99             return null;
100
101         IDocument document= documentProvider.getDocument(editor.getEditorInput());
102         if (document == null)
103             return null;
104
105         return document;
106     }
107
108     /**
109      * Returns the editor's selection.
110      *
111      * @param editor the editor
112      * @return the editor's selection
113      */

114     private static ITextSelection getSelection(ITextEditor editor) {
115
116         ISelectionProvider selectionProvider= editor.getSelectionProvider();
117         if (selectionProvider == null)
118             return null;
119
120         ISelection selection= selectionProvider.getSelection();
121         if (!(selection instanceof ITextSelection))
122             return null;
123
124         return (ITextSelection) selection;
125     }
126
127     /*
128      * @see IAction#run()
129      */

130     public void run() {
131
132         if (fTarget == null)
133             return;
134
135         ITextEditor editor= getTextEditor();
136         if (editor == null)
137             return;
138
139         if (!validateEditorInputState())
140             return;
141
142         IDocument document= getDocument(editor);
143         if (document == null)
144             return;
145
146         ITextSelection selection= getSelection(editor);
147         if (selection == null)
148             return;
149
150         try {
151             fTarget.deleteLine(document, selection.getOffset(), selection.getLength(), fType, fCopyToClipboard);
152         } catch (BadLocationException e) {
153             // should not happen
154
}
155     }
156
157     /*
158      * @see IUpdate#update()
159      */

160     public void update() {
161
162         super.update();
163         if (!isEnabled())
164             return;
165
166         if (!canModifyEditor()) {
167             setEnabled(false);
168             return;
169         }
170
171         ITextEditor editor= getTextEditor();
172         if (editor != null)
173             fTarget= (DeleteLineTarget) editor.getAdapter(DeleteLineTarget.class);
174         else
175             fTarget= null;
176
177         setEnabled(fTarget != null);
178     }
179 }
180
Popular Tags