1 11 package org.eclipse.jdt.internal.ui.refactoring.actions; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.swt.widgets.Shell; 16 17 import org.eclipse.jface.dialogs.MessageDialog; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 20 import org.eclipse.jface.text.ITextSelection; 21 22 import org.eclipse.ui.IWorkbenchSite; 23 import org.eclipse.ui.PlatformUI; 24 25 import org.eclipse.jdt.core.ICompilationUnit; 26 import org.eclipse.jdt.core.IField; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.core.dom.CompilationUnit; 29 30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 32 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 35 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 36 37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 40 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 42 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 43 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 45 46 54 public class InlineConstantAction extends SelectionDispatchAction { 55 56 private JavaEditor fEditor; 57 58 63 public InlineConstantAction(JavaEditor editor) { 64 this(editor.getEditorSite()); 65 fEditor= editor; 66 setEnabled(SelectionConverter.canOperateOn(fEditor)); 67 } 68 69 public InlineConstantAction(IWorkbenchSite site) { 70 super(site); 71 setText(RefactoringMessages.InlineConstantAction_inline_Constant); 72 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION); 73 } 74 75 77 80 public void selectionChanged(IStructuredSelection selection) { 81 try { 82 setEnabled(RefactoringAvailabilityTester.isInlineConstantAvailable(selection)); 83 } catch (JavaModelException e) { 84 if (JavaModelUtil.isExceptionToBeLogged(e)) 86 JavaPlugin.log(e); 87 setEnabled(false); } 89 } 90 91 94 public void run(IStructuredSelection selection) { 95 try { 96 Assert.isTrue(RefactoringAvailabilityTester.isInlineConstantAvailable(selection)); 97 98 Object first= selection.getFirstElement(); 99 Assert.isTrue(first instanceof IField); 100 101 IField field= (IField) first; 102 run(field.getNameRange().getOffset(), field.getNameRange().getLength(), field.getCompilationUnit()); 103 } catch (JavaModelException e) { 104 ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_unexpected_exception); 105 } 106 } 107 108 110 113 public void selectionChanged(ITextSelection selection) { 114 setEnabled(true); 115 } 116 117 121 public void selectionChanged(JavaTextSelection selection) { 122 try { 123 setEnabled(RefactoringAvailabilityTester.isInlineConstantAvailable(selection)); 124 } catch (JavaModelException e) { 125 setEnabled(false); 126 } 127 } 128 129 132 public void run(ITextSelection selection) { 133 run(selection.getOffset(), selection.getLength(), SelectionConverter.getInputAsCompilationUnit(fEditor)); 134 } 135 136 private void run(int offset, int length, ICompilationUnit cu) { 137 Assert.isNotNull(cu); 138 Assert.isTrue(offset >= 0); 139 Assert.isTrue(length >= 0); 140 if (!ActionUtil.isEditable(fEditor, getShell(), cu)) 141 return; 142 try { 143 CompilationUnit node= RefactoringASTParser.parseWithASTProvider(cu, true, null); 144 if (! RefactoringExecutionStarter.startInlineConstantRefactoring(cu, node, offset, length, getShell())) { 145 MessageDialog.openInformation(getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_no_constant_reference_or_declaration); 146 } 147 148 } catch (JavaModelException e) { 149 ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_unexpected_exception); 150 } 151 } 152 153 public boolean tryInlineConstant(ICompilationUnit unit, CompilationUnit node, ITextSelection selection, Shell shell) { 154 try { 155 if (RefactoringExecutionStarter.startInlineConstantRefactoring(unit, node, selection.getOffset(), selection.getLength(), shell)) { 156 return true; 157 } 158 } catch (JavaModelException e) { 159 ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_unexpected_exception); 160 } 161 return false; 162 } 163 } 164 | Popular Tags |