1 11 12 13 package org.eclipse.ui.texteditor; 14 15 16 import java.util.ResourceBundle ; 17 18 import org.eclipse.swt.custom.BusyIndicator; 19 import org.eclipse.swt.widgets.Display; 20 import org.eclipse.swt.widgets.Shell; 21 22 import org.eclipse.jface.text.ITextOperationTarget; 23 24 import org.eclipse.ui.IWorkbenchPartSite; 25 26 27 37 public final class TextOperationAction extends TextEditorAction { 38 39 40 private int fOperationCode= -1; 41 42 private ITextOperationTarget fOperationTarget; 43 47 private boolean fRunsOnReadOnly= false; 48 49 54 private boolean fAllowUpdate= false; 55 56 72 public TextOperationAction(ResourceBundle bundle, String prefix, ITextEditor editor, int operationCode) { 73 super(bundle, prefix, editor); 74 fOperationCode= operationCode; 75 fAllowUpdate= true; 76 update(); 77 } 78 79 98 public TextOperationAction(ResourceBundle bundle, String prefix, ITextEditor editor, int operationCode, boolean runsOnReadOnly) { 99 super(bundle, prefix, editor); 100 fOperationCode= operationCode; 101 fRunsOnReadOnly= runsOnReadOnly; 102 fAllowUpdate= true; 103 update(); 104 } 105 106 111 public void run() { 112 if (fOperationCode == -1 || fOperationTarget == null) 113 return; 114 115 ITextEditor editor= getTextEditor(); 116 if (editor == null) 117 return; 118 119 if (!fRunsOnReadOnly && !validateEditorInputState()) 120 return; 121 122 Display display= null; 123 124 IWorkbenchPartSite site= editor.getSite(); 125 Shell shell= site.getShell(); 126 if (shell != null && !shell.isDisposed()) 127 display= shell.getDisplay(); 128 129 BusyIndicator.showWhile(display, new Runnable () { 130 public void run() { 131 fOperationTarget.doOperation(fOperationCode); 132 } 133 }); 134 } 135 136 142 public void update() { 143 if (!fAllowUpdate) 144 return; 145 146 super.update(); 147 148 if (!fRunsOnReadOnly && !canModifyEditor()) { 149 setEnabled(false); 150 return; 151 } 152 153 ITextEditor editor= getTextEditor(); 154 if (fOperationTarget == null && editor!= null && fOperationCode != -1) 155 fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); 156 157 boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode)); 158 setEnabled(isEnabled); 159 } 160 161 164 public void setEditor(ITextEditor editor) { 165 super.setEditor(editor); 166 fOperationTarget= null; 167 } 168 } 169 | Popular Tags |