1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.ResourceBundle ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 import org.eclipse.jface.text.*; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.ISelectionProvider; 22 23 import org.eclipse.ui.IEditorInput; 24 import org.eclipse.ui.texteditor.IDocumentProvider; 25 import org.eclipse.ui.texteditor.ITextEditor; 26 import org.eclipse.ui.texteditor.ITextEditorExtension2; 27 import org.eclipse.ui.texteditor.TextEditorAction; 28 29 30 35 public abstract class BlockCommentAction extends TextEditorAction { 36 37 43 public BlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) { 44 super(bundle, prefix, editor); 45 } 46 47 53 static class Edit extends DocumentEvent { 54 55 63 public static class EditFactory { 64 65 66 private static final String CATEGORY= "__positionalEditPositionCategory"; 68 69 private static int fgCount= 0; 70 71 72 private final String fCategory; 73 private IDocument fDocument; 74 private IPositionUpdater fUpdater; 75 76 80 public EditFactory(IDocument document) { 81 fCategory= CATEGORY + fgCount++; 82 fDocument= document; 83 } 84 85 93 public Edit createEdit(int offset, int length, String text) throws BadLocationException { 94 95 if (!fDocument.containsPositionCategory(fCategory)) { 96 fDocument.addPositionCategory(fCategory); 97 fUpdater= new DefaultPositionUpdater(fCategory); 98 fDocument.addPositionUpdater(fUpdater); 99 } 100 101 Position position= new Position(offset); 102 try { 103 fDocument.addPosition(fCategory, position); 104 } catch (BadPositionCategoryException e) { 105 Assert.isTrue(false); 106 } 107 return new Edit(fDocument, length, text, position); 108 } 109 110 114 public void release() { 115 if (fDocument != null && fDocument.containsPositionCategory(fCategory)) { 116 fDocument.removePositionUpdater(fUpdater); 117 try { 118 fDocument.removePositionCategory(fCategory); 119 } catch (BadPositionCategoryException e) { 120 Assert.isTrue(false); 121 } 122 fDocument= null; 123 fUpdater= null; 124 } 125 } 126 } 127 128 129 private Position fPosition; 130 131 139 protected Edit(IDocument document, int length, String text, Position position) { 140 super(document, 0, length, text); 141 fPosition= position; 142 } 143 144 147 public int getOffset() { 148 return fPosition.getOffset(); 149 } 150 151 156 public void perform() throws BadLocationException { 157 getDocument().replace(getOffset(), getLength(), getText()); 158 } 159 160 } 161 162 public void run() { 163 if (!isEnabled()) 164 return; 165 166 ITextEditor editor= getTextEditor(); 167 if (editor == null || !ensureEditable(editor)) 168 return; 169 170 ITextSelection selection= getCurrentSelection(); 171 if (!isValidSelection(selection)) 172 return; 173 174 if (!validateEditorInputState()) 175 return; 176 177 IDocumentProvider docProvider= editor.getDocumentProvider(); 178 IEditorInput input= editor.getEditorInput(); 179 if (docProvider == null || input == null) 180 return; 181 182 IDocument document= docProvider.getDocument(input); 183 if (document == null) 184 return; 185 186 IDocumentExtension3 docExtension; 187 if (document instanceof IDocumentExtension3) 188 docExtension= (IDocumentExtension3) document; 189 else 190 return; 191 192 IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class); 193 if (target != null) { 194 target.beginCompoundChange(); 195 } 196 197 Edit.EditFactory factory= new Edit.EditFactory(document); 198 199 try { 200 runInternal(selection, docExtension, factory); 201 202 } catch (BadLocationException e) { 203 } catch (BadPartitioningException e) { 206 Assert.isTrue(false, "bad partitioning"); } finally { 209 factory.release(); 210 211 if (target != null) { 212 target.endCompoundChange(); 213 } 214 } 215 } 216 217 223 protected void executeEdits(List edits) throws BadLocationException { 224 for (Iterator it= edits.iterator(); it.hasNext();) { 225 Edit edit= (Edit) it.next(); 226 edit.perform(); 227 } 228 } 229 230 238 protected boolean ensureEditable(ITextEditor editor) { 239 Assert.isNotNull(editor); 240 241 if (editor instanceof ITextEditorExtension2) { 242 ITextEditorExtension2 ext= (ITextEditorExtension2) editor; 243 return ext.validateEditorInputState(); 244 } 245 246 return editor.isEditable(); 247 } 248 249 252 public void update() { 253 super.update(); 254 255 if (isEnabled()) { 256 if (!canModifyEditor() || !isValidSelection(getCurrentSelection())) 257 setEnabled(false); 258 } 259 } 260 261 267 protected ITextSelection getCurrentSelection() { 268 ITextEditor editor= getTextEditor(); 269 if (editor != null) { 270 ISelectionProvider provider= editor.getSelectionProvider(); 271 if (provider != null) { 272 ISelection selection= provider.getSelection(); 273 if (selection instanceof ITextSelection) 274 return (ITextSelection) selection; 275 } 276 } 277 return null; 278 } 279 280 289 protected abstract void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException; 290 291 297 protected abstract boolean isValidSelection(ITextSelection selection); 298 299 304 protected String getCommentStart() { 305 return "/*"; } 308 309 314 protected String getCommentEnd() { 315 return "*/"; } 318 319 } 320 | Popular Tags |