1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import java.util.ResourceBundle ; 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 29 public class DeleteLineAction extends TextEditorAction { 30 31 34 public static final int WHOLE= 0; 35 38 public static final int TO_BEGINNING= 1; 39 42 public static final int TO_END= 2; 43 44 47 private final int fType; 48 52 private final boolean fCopyToClipboard; 53 56 private DeleteLineTarget fTarget; 57 58 67 public DeleteLineAction(ResourceBundle bundle, String prefix, ITextEditor editor, int type) { 68 this(bundle, prefix, editor, type, true); 69 } 70 71 82 public DeleteLineAction(ResourceBundle bundle, String prefix, ITextEditor editor, int type, boolean copyToClipboard) { 83 super(bundle, prefix, editor); 84 fType= type; 85 fCopyToClipboard= copyToClipboard; 86 update(); 87 } 88 89 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 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 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 } 155 } 156 157 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 |