| /* |
| * Copyright 2000-2014 JetBrains s.r.o. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.jetbrains.plugins.groovy.actions; |
| |
| import com.intellij.openapi.actionSystem.AnAction; |
| import com.intellij.openapi.actionSystem.AnActionEvent; |
| import com.intellij.openapi.actionSystem.CommonDataKeys; |
| import com.intellij.openapi.editor.Editor; |
| import com.intellij.openapi.project.DumbAware; |
| import com.intellij.openapi.project.Project; |
| import com.intellij.openapi.ui.MessageType; |
| import com.intellij.openapi.ui.popup.Balloon; |
| import com.intellij.openapi.ui.popup.JBPopupFactory; |
| import com.intellij.openapi.util.Pass; |
| import com.intellij.openapi.util.text.StringUtil; |
| import com.intellij.openapi.wm.WindowManager; |
| import com.intellij.psi.PsiFile; |
| import com.intellij.psi.PsiType; |
| import com.intellij.refactoring.IntroduceTargetChooser; |
| import com.intellij.ui.awt.RelativePoint; |
| import com.intellij.util.Function; |
| import org.jetbrains.plugins.groovy.editor.HandlerUtils; |
| import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; |
| import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; |
| import org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceHandlerBase; |
| |
| import javax.swing.*; |
| import java.awt.*; |
| import java.util.List; |
| |
| /** |
| * @author Maxim.Medvedev |
| */ |
| public class GrGetPsiTypeAction extends AnAction implements DumbAware { |
| @Override |
| public void actionPerformed(AnActionEvent e) { |
| final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext()); |
| if (editor == null) return; |
| |
| final PsiFile psiFile = HandlerUtils.getPsiFile(editor, e.getDataContext()); |
| if (!(psiFile instanceof GroovyFile)) return; |
| |
| int offset = editor.getCaretModel().getOffset(); |
| |
| List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(psiFile, editor, offset, true); |
| if (expressions.isEmpty()) return; |
| if (expressions.size() == 1) { |
| passInner(expressions.get(0)); |
| } |
| else { |
| IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() { |
| @Override |
| public void pass(GrExpression grExpression) { |
| passInner(grExpression); |
| } |
| }, new Function<GrExpression, String>() { |
| @Override |
| public String fun(GrExpression grExpression) { |
| return grExpression.getText(); |
| } |
| }); |
| } |
| } |
| |
| /** |
| * Shows an information balloon in a reasonable place at the top right of the window. |
| * |
| * @param project our project |
| * @param message the text, HTML markup allowed |
| * @param messageType message type, changes the icon and the background. |
| */ |
| // TODO: move to a better place |
| public static void showBalloon(Project project, String message, MessageType messageType) { |
| // ripped from com.intellij.openapi.vcs.changes.ui.ChangesViewBalloonProblemNotifier |
| final JFrame frame = WindowManager.getInstance().getFrame(project.isDefault() ? null : project); |
| if (frame == null) return; |
| final JComponent component = frame.getRootPane(); |
| if (component == null) return; |
| final Rectangle rect = component.getVisibleRect(); |
| final Point p = new Point(rect.x + rect.width - 10, rect.y + 10); |
| final RelativePoint point = new RelativePoint(component, p); |
| |
| JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(message, messageType.getDefaultIcon(), messageType.getPopupBackground(), null) |
| .setShowCallout(false).setCloseButtonEnabled(true) |
| .createBalloon().show(point, Balloon.Position.atLeft); |
| } |
| |
| public static void passInner(GrExpression expression) { |
| PsiType type = expression.getType(); |
| String text = type == null ? "type is null" : type.getCanonicalText(); |
| |
| showBalloon(expression.getProject(), StringUtil.escapeXml(text), MessageType.INFO); |
| } |
| |
| @Override |
| public void update(AnActionEvent e) { |
| Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext()); |
| if (editor != null) { |
| PsiFile psiFile = HandlerUtils.getPsiFile(editor, e.getDataContext()); |
| if (psiFile instanceof GroovyFile) { |
| e.getPresentation().setEnabled(true); |
| return; |
| } |
| } |
| e.getPresentation().setEnabled(false); |
| } |
| } |