1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.dialogs.MessageDialog; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 19 import org.eclipse.jface.text.ITextSelection; 20 21 import org.eclipse.ui.IWorkbenchSite; 22 import org.eclipse.ui.PlatformUI; 23 24 import org.eclipse.jdt.core.ICompilationUnit; 25 import org.eclipse.jdt.core.ITypeRoot; 26 import org.eclipse.jdt.core.dom.CompilationUnit; 27 28 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; 29 30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 31 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 32 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 35 import org.eclipse.jdt.internal.ui.refactoring.actions.InlineConstantAction; 36 import org.eclipse.jdt.internal.ui.refactoring.actions.InlineMethodAction; 37 38 47 public class InlineAction extends SelectionDispatchAction { 48 49 private JavaEditor fEditor; 50 private final InlineTempAction fInlineTemp; 51 private final InlineMethodAction fInlineMethod; 52 private final InlineConstantAction fInlineConstant; 53 54 61 public InlineAction(IWorkbenchSite site) { 62 super(site); 63 setText(RefactoringMessages.InlineAction_Inline); 64 fInlineTemp = new InlineTempAction(site); 65 fInlineConstant = new InlineConstantAction(site); 66 fInlineMethod = new InlineMethodAction(site); 67 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION); 68 } 69 70 74 public InlineAction(JavaEditor editor) { 75 super(editor.getEditorSite()); 77 setText(RefactoringMessages.InlineAction_Inline); 78 fEditor= editor; 79 fInlineTemp = new InlineTempAction(editor); 80 fInlineConstant = new InlineConstantAction(editor); 81 fInlineMethod = new InlineMethodAction(editor); 82 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION); 83 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 84 } 85 86 89 public void selectionChanged(ISelection selection) { 90 fInlineConstant.update(selection); 91 fInlineMethod.update(selection); 92 fInlineTemp.update(selection); 93 setEnabled((fInlineTemp.isEnabled() || fInlineConstant.isEnabled() || fInlineMethod.isEnabled())); 94 } 95 96 99 public void run(ITextSelection selection) { 100 if (!ActionUtil.isEditable(fEditor)) 101 return; 102 103 ITypeRoot typeRoot= SelectionConverter.getInputAsTypeRoot(fEditor); 104 if (typeRoot == null) 105 return; 106 107 CompilationUnit node= RefactoringASTParser.parseWithASTProvider(typeRoot, true, null); 108 109 if (typeRoot instanceof ICompilationUnit) { 110 ICompilationUnit cu= (ICompilationUnit) typeRoot; 111 if (fInlineTemp.isEnabled() && fInlineTemp.tryInlineTemp(cu, node, selection, getShell())) 112 return; 113 114 if (fInlineConstant.isEnabled() && fInlineConstant.tryInlineConstant(cu, node, selection, getShell())) 115 return; 116 } 117 if (fInlineMethod.isEnabled() && fInlineMethod.tryInlineMethod(typeRoot, node, selection, getShell())) 119 return; 120 121 MessageDialog.openInformation(getShell(), RefactoringMessages.InlineAction_dialog_title, RefactoringMessages.InlineAction_select); 122 } 123 124 127 public void run(IStructuredSelection selection) { 128 if (fInlineConstant.isEnabled()) 129 fInlineConstant.run(selection); 130 else if (fInlineMethod.isEnabled()) 131 fInlineMethod.run(selection); 132 else 133 Assert.isTrue(! fInlineTemp.isEnabled()); 136 } 137 } 138 | Popular Tags |