001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.texteditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.jface.text.BadLocationException;
015: import org.eclipse.jface.text.IDocument;
016: import org.eclipse.jface.text.ITextSelection;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.ISelectionProvider;
019:
020: /**
021: * An action to delete a whole line, the fraction of the line that is left from the cursor
022: * or the fraction that is right from the cursor.
023: *
024: * @since 2.0
025: */
026: public class DeleteLineAction extends TextEditorAction {
027:
028: /**
029: * Delete the whole line.
030: */
031: public static final int WHOLE = 0;
032: /**
033: * Delete to the beginning of line.
034: */
035: public static final int TO_BEGINNING = 1;
036: /**
037: * Delete to the end of line.
038: */
039: public static final int TO_END = 2;
040:
041: /**
042: * The type of deletion.
043: */
044: private final int fType;
045: /**
046: * Should the deleted line be copied to the clipboard.
047: * @since 2.1
048: */
049: private final boolean fCopyToClipboard;
050: /** The deletion target.
051: * @since 2.1
052: */
053: private DeleteLineTarget fTarget;
054:
055: /**
056: * Creates a line delimiter conversion action.
057: *
058: * @param bundle the resource bundle for UI strings
059: * @param prefix the prefix for the property keys into <code>bundle</code>
060: * @param editor the editor
061: * @param type the line deletion type, must be one of
062: * <code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
063: */
064: public DeleteLineAction(ResourceBundle bundle, String prefix,
065: ITextEditor editor, int type) {
066: this (bundle, prefix, editor, type, true);
067: }
068:
069: /**
070: * Creates a line deletion action.
071: *
072: * @param bundle the resource bundle for UI strings
073: * @param prefix the prefix for the property keys into <code>bundle</code>
074: * @param editor the editor
075: * @param type the line deletion type, must be one of
076: * <code>WHOLE_LINE</code>, <code>TO_BEGINNING</code> or <code>TO_END</code>
077: * @param copyToClipboard if <code>true</code>, the contents of the deleted line are copied to the clipboard
078: * @since 2.1
079: */
080: public DeleteLineAction(ResourceBundle bundle, String prefix,
081: ITextEditor editor, int type, boolean copyToClipboard) {
082: super (bundle, prefix, editor);
083: fType = type;
084: fCopyToClipboard = copyToClipboard;
085: update();
086: }
087:
088: /**
089: * Returns the editor's document.
090: *
091: * @param editor the editor
092: * @return the editor's document
093: */
094: private static IDocument getDocument(ITextEditor editor) {
095:
096: IDocumentProvider documentProvider = editor
097: .getDocumentProvider();
098: if (documentProvider == null)
099: return null;
100:
101: IDocument document = documentProvider.getDocument(editor
102: .getEditorInput());
103: if (document == null)
104: return null;
105:
106: return document;
107: }
108:
109: /**
110: * Returns the editor's selection.
111: *
112: * @param editor the editor
113: * @return the editor's selection
114: */
115: private static ITextSelection getSelection(ITextEditor editor) {
116:
117: ISelectionProvider selectionProvider = editor
118: .getSelectionProvider();
119: if (selectionProvider == null)
120: return null;
121:
122: ISelection selection = selectionProvider.getSelection();
123: if (!(selection instanceof ITextSelection))
124: return null;
125:
126: return (ITextSelection) selection;
127: }
128:
129: /*
130: * @see IAction#run()
131: */
132: public void run() {
133:
134: if (fTarget == null)
135: return;
136:
137: ITextEditor editor = getTextEditor();
138: if (editor == null)
139: return;
140:
141: if (!validateEditorInputState())
142: return;
143:
144: IDocument document = getDocument(editor);
145: if (document == null)
146: return;
147:
148: ITextSelection selection = getSelection(editor);
149: if (selection == null)
150: return;
151:
152: try {
153: fTarget.deleteLine(document, selection.getOffset(),
154: selection.getLength(), fType, fCopyToClipboard);
155: } catch (BadLocationException e) {
156: // should not happen
157: }
158: }
159:
160: /*
161: * @see IUpdate#update()
162: */
163: public void update() {
164:
165: super .update();
166: if (!isEnabled())
167: return;
168:
169: if (!canModifyEditor()) {
170: setEnabled(false);
171: return;
172: }
173:
174: ITextEditor editor = getTextEditor();
175: if (editor != null)
176: fTarget = (DeleteLineTarget) editor
177: .getAdapter(DeleteLineTarget.class);
178: else
179: fTarget = null;
180:
181: setEnabled(fTarget != null);
182: }
183: }
|