001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.actions;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.jface.dialogs.MessageDialog;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017:
018: import org.eclipse.jface.text.ITextSelection;
019:
020: import org.eclipse.ui.IWorkbenchSite;
021: import org.eclipse.ui.PlatformUI;
022:
023: import org.eclipse.jdt.core.ICompilationUnit;
024: import org.eclipse.jdt.core.ITypeRoot;
025: import org.eclipse.jdt.core.dom.CompilationUnit;
026:
027: import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
028:
029: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
030: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
031: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
032: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
033: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
034: import org.eclipse.jdt.internal.ui.refactoring.actions.InlineConstantAction;
035: import org.eclipse.jdt.internal.ui.refactoring.actions.InlineMethodAction;
036:
037: /**
038: * Inlines a method, local variable or a static final field.
039: *
040: * <p>
041: * This class may be instantiated; it is not intended to be subclassed.
042: * </p>
043: *
044: * @since 2.1
045: */
046: public class InlineAction extends SelectionDispatchAction {
047:
048: private JavaEditor fEditor;
049: private final InlineTempAction fInlineTemp;
050: private final InlineMethodAction fInlineMethod;
051: private final InlineConstantAction fInlineConstant;
052:
053: /**
054: * Creates a new <code>InlineAction</code>. The action requires
055: * that the selection provided by the site's selection provider is of type <code>
056: * org.eclipse.jface.viewers.IStructuredSelection</code>.
057: *
058: * @param site the site providing context information for this action
059: */
060: public InlineAction(IWorkbenchSite site) {
061: super (site);
062: setText(RefactoringMessages.InlineAction_Inline);
063: fInlineTemp = new InlineTempAction(site);
064: fInlineConstant = new InlineConstantAction(site);
065: fInlineMethod = new InlineMethodAction(site);
066: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
067: IJavaHelpContextIds.INLINE_ACTION);
068: }
069:
070: /**
071: * Note: This constructor is for internal use only. Clients should not call this constructor.
072: * @param editor the compilation unit editor
073: */
074: public InlineAction(JavaEditor editor) {
075: //don't want to call 'this' here - it'd create useless action objects
076: super (editor.getEditorSite());
077: setText(RefactoringMessages.InlineAction_Inline);
078: fEditor = editor;
079: fInlineTemp = new InlineTempAction(editor);
080: fInlineConstant = new InlineConstantAction(editor);
081: fInlineMethod = new InlineMethodAction(editor);
082: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
083: IJavaHelpContextIds.INLINE_ACTION);
084: setEnabled(SelectionConverter
085: .getInputAsCompilationUnit(fEditor) != null);
086: }
087:
088: /*
089: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
090: */
091: public void selectionChanged(ISelection selection) {
092: fInlineConstant.update(selection);
093: fInlineMethod.update(selection);
094: fInlineTemp.update(selection);
095: setEnabled((fInlineTemp.isEnabled()
096: || fInlineConstant.isEnabled() || fInlineMethod
097: .isEnabled()));
098: }
099:
100: /*
101: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
102: */
103: public void run(ITextSelection selection) {
104: if (!ActionUtil.isEditable(fEditor))
105: return;
106:
107: ITypeRoot typeRoot = SelectionConverter
108: .getInputAsTypeRoot(fEditor);
109: if (typeRoot == null)
110: return;
111:
112: CompilationUnit node = RefactoringASTParser
113: .parseWithASTProvider(typeRoot, true, null);
114:
115: if (typeRoot instanceof ICompilationUnit) {
116: ICompilationUnit cu = (ICompilationUnit) typeRoot;
117: if (fInlineTemp.isEnabled()
118: && fInlineTemp.tryInlineTemp(cu, node, selection,
119: getShell()))
120: return;
121:
122: if (fInlineConstant.isEnabled()
123: && fInlineConstant.tryInlineConstant(cu, node,
124: selection, getShell()))
125: return;
126: }
127: //InlineMethod is last (also tries enclosing element):
128: if (fInlineMethod.isEnabled()
129: && fInlineMethod.tryInlineMethod(typeRoot, node,
130: selection, getShell()))
131: return;
132:
133: MessageDialog.openInformation(getShell(),
134: RefactoringMessages.InlineAction_dialog_title,
135: RefactoringMessages.InlineAction_select);
136: }
137:
138: /*
139: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
140: */
141: public void run(IStructuredSelection selection) {
142: if (fInlineConstant.isEnabled())
143: fInlineConstant.run(selection);
144: else if (fInlineMethod.isEnabled())
145: fInlineMethod.run(selection);
146: else
147: //inline temp will never be enabled on IStructuredSelection
148: //don't bother running it
149: Assert.isTrue(!fInlineTemp.isEnabled());
150: }
151: }
|