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 28 32 public class ShiftAction extends TextEditorAction implements IReadOnlyDependent { 33 34 35 private int fOperationCode= -1; 36 37 private ITextOperationTarget fOperationTarget; 38 39 54 public ShiftAction(ResourceBundle bundle, String prefix, ITextEditor editor, int operationCode) { 55 super(bundle, prefix, editor); 56 fOperationCode= operationCode; 57 update(); 58 } 59 60 65 public void run() { 66 if (fOperationCode == -1 || fOperationTarget == null) 67 return; 68 69 ITextEditor editor= getTextEditor(); 70 if (editor == null) 71 return; 72 73 if (!validateEditorInputState()) 74 return; 75 76 Display display= null; 77 78 IWorkbenchPartSite site= editor.getSite(); 79 Shell shell= site.getShell(); 80 if (shell != null && !shell.isDisposed()) 81 display= shell.getDisplay(); 82 83 BusyIndicator.showWhile(display, new Runnable () { 84 public void run() { 85 fOperationTarget.doOperation(fOperationCode); 86 } 87 }); 88 } 89 90 93 public void update() { 94 super.update(); 95 if (!isEnabled()) 96 return; 97 98 if (!canModifyEditor()) { 99 setEnabled(false); 100 return; 101 } 102 103 ITextEditor editor= getTextEditor(); 104 if (fOperationTarget == null && editor != null && fOperationCode != -1) 105 fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); 106 107 } 108 109 114 protected void updateForTab() { 115 super.update(); 116 117 if (isEnabled()) { 118 if (!canModifyEditor()) { 119 setEnabled(false); 120 return; 121 } 122 123 ITextEditor editor= getTextEditor(); 124 if (fOperationTarget == null && editor != null && fOperationCode != -1) 125 fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); 126 127 boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode)); 128 setEnabled(isEnabled); 129 } 130 131 } 132 133 136 public void setEditor(ITextEditor editor) { 137 super.setEditor(editor); 138 fOperationTarget= null; 139 } 140 141 144 public boolean isEnabled(boolean isWritable) { 145 146 if (!isWritable) 147 return false; 148 149 155 ITextEditor editor= getTextEditor(); 156 if (fOperationTarget == null && editor!= null && fOperationCode != -1) 157 fOperationTarget= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class); 158 159 return (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode)); 160 } 161 } 162 | Popular Tags |