diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java deleted file mode 100644 index dd66e7057300..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * 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. - */ - -/* - * Created by IntelliJ IDEA. - * User: spleaner - * Date: Aug 22, 2007 - * Time: 3:44:10 PM - */ -package com.intellij.codeInsight.intention.impl; - -import com.intellij.codeInsight.CodeInsightBundle; -import com.intellij.codeInsight.FileModificationService; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.text.StringUtil; -import com.intellij.patterns.PsiExpressionPattern; -import com.intellij.patterns.PsiJavaPatterns; -import com.intellij.patterns.PsiMethodPattern; -import com.intellij.psi.*; -import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.ui.ColorChooser; -import com.intellij.ui.JBColor; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; - -import javax.swing.*; -import java.awt.*; - -/** - * @author spleaner - * @author Konstantin Bulenkov - */ -public class ColorChooserIntentionAction extends BaseColorIntentionAction { - private static final PsiMethodPattern DECODE_METHOD = PsiJavaPatterns.psiMethod() - .definedInClass(JAVA_AWT_COLOR) - .withName("decode"); - private static final PsiExpressionPattern.Capture DECODE_METHOD_CALL_PARAMETER = - PsiJavaPatterns.psiExpression().methodCallParameter(0, DECODE_METHOD); - - private static final PsiMethodPattern GET_COLOR_METHOD = PsiJavaPatterns.psiMethod() - .definedInClass(JAVA_AWT_COLOR) - .withName("getColor"); - private static final PsiExpressionPattern.Capture GET_METHOD_CALL_PARAMETER = - PsiJavaPatterns.psiExpression().methodCallParameter(0, GET_COLOR_METHOD); - - - public ColorChooserIntentionAction() { - setText(CodeInsightBundle.message("intention.color.chooser.dialog")); - } - - @Override - public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) { - return super.isAvailable(project, editor, element) || isInsideDecodeOrGetColorMethod(element); - } - - public static boolean isInsideDecodeOrGetColorMethod(PsiElement element) { - if (element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.STRING_LITERAL) { - element = element.getParent(); - } - - return DECODE_METHOD_CALL_PARAMETER.accepts(element) || - GET_METHOD_CALL_PARAMETER.accepts(element); - } - - @Override - @NotNull - public String getFamilyName() { - return getText(); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { - if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return; - - final JComponent editorComponent = editor.getComponent(); - if (isInsideDecodeOrGetColorMethod(element)) { - invokeForMethodParam(editorComponent, element); - } - else { - invokeForConstructor(editorComponent, element); - } - } - - private void invokeForMethodParam(JComponent editorComponent, PsiElement element) { - final PsiLiteralExpression literal = PsiTreeUtil.getParentOfType(element, PsiLiteralExpression.class); - if (literal == null) return; - final String text = StringUtil.unquoteString(literal.getText()); - final int radix = text.startsWith("0x") || text.startsWith("0X") || text.startsWith("#") ? 16 : text.startsWith("0") ? 8 : 10; - final String hexPrefix = radix == 16 ? text.startsWith("#") ? "#" : text.substring(0, 2) : null; - - Color oldColor; - try { - oldColor = Color.decode(text); - } - catch (NumberFormatException e) { - oldColor = JBColor.GRAY; - } - Color color = ColorChooser.chooseColor(editorComponent, getText(), oldColor, true); - if (color == null) return; - final int rgb = color.getRGB() - ((255 & 0xFF) << 24); - if (color != null && rgb != oldColor.getRGB()) { - final String newText = radix == 16 ? hexPrefix + String.format("%6s", Integer.toHexString(rgb)).replace(' ', '0') - : radix == 8 ? "0" + Integer.toOctalString(rgb) - : Integer.toString(rgb); - final PsiManager manager = literal.getManager(); - final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); - final PsiExpression newLiteral = factory.createExpressionFromText("\"" + newText + "\"", literal); - literal.replace(newLiteral); - } - } - - private void invokeForConstructor(JComponent editorComponent, PsiElement element) { - final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class); - if (expression == null) return; - - Color c = null; - - final PsiExpressionList argumentList = expression.getArgumentList(); - if (argumentList != null) { - final PsiExpression[] expressions = argumentList.getExpressions(); - int[] values = new int[expressions.length]; - float[] values2 = new float[expressions.length]; - int i = 0; - int j = 0; - for (final PsiExpression each : expressions) { - if (each instanceof PsiLiteralExpression) { - final Object o = ((PsiLiteralExpression)each).getValue(); - if (o instanceof Integer) { - values[i] = ((Integer)o).intValue(); - i++; - } - else if (o instanceof Float) { - values2[j] = ((Float)o).floatValue(); - j++; - } - } - } - - try { - if (i == expressions.length) { - switch (values.length) { - case 1: - c = new Color(values[0]); - break; - case 3: - c = new Color(values[0], values[1], values[2]); - break; - case 4: - c = new Color(values[0], values[1], values[2], values[3]); - break; - default: - break; - } - } - else if (j == expressions.length) { - switch (values2.length) { - case 3: - c = new Color(values2[0], values2[1], values2[2]); - break; - case 4: - c = new Color(values2[0], values2[1], values2[2], values2[3]); - break; - default: - break; - } - } - } - catch (Exception e) { - c = JBColor.GRAY; - } - } - - c = (c == null) ? JBColor.GRAY : c; - - replaceColor(editorComponent, expression, c); - } - - private void replaceColor(JComponent editorComponent, PsiNewExpression expression, Color oldColor) { - final Color color = ColorChooser.chooseColor(editorComponent, getText(), oldColor, true); - if (color != null) { - final PsiManager manager = expression.getManager(); - final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); - final PsiExpression newCall = factory.createExpressionFromText( - "new " + getFqn(expression.getClassOrAnonymousClassReference()) + "(" - + color.getRed() + ", " - + color.getGreen() + ", " - + color.getBlue() - + (color.getAlpha() < 255 ? ", " + color.getAlpha() : "") - + ")", expression); - final PsiElement insertedElement = expression.replace(newCall); - final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); - codeStyleManager.reformat(insertedElement); - } - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/preview/JavaPreviewHintProvider.java b/java/java-impl/src/com/intellij/codeInsight/preview/JavaPreviewHintProvider.java index 8578149c4f71..b996c2219083 100644 --- a/java/java-impl/src/com/intellij/codeInsight/preview/JavaPreviewHintProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/preview/JavaPreviewHintProvider.java @@ -15,9 +15,11 @@ */ package com.intellij.codeInsight.preview; -import com.intellij.codeInsight.intention.impl.ColorChooserIntentionAction; import com.intellij.openapi.util.text.StringUtil; import com.intellij.patterns.PlatformPatterns; +import com.intellij.patterns.PsiExpressionPattern; +import com.intellij.patterns.PsiJavaPatterns; +import com.intellij.patterns.PsiMethodPattern; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.ArrayUtil; @@ -31,6 +33,27 @@ import java.awt.*; * @author yole */ public class JavaPreviewHintProvider implements PreviewHintProvider { + + private static final PsiMethodPattern DECODE_METHOD = PsiJavaPatterns.psiMethod() + .definedInClass(Color.class.getName()) + .withName("decode"); + private static final PsiExpressionPattern.Capture DECODE_METHOD_CALL_PARAMETER = + PsiJavaPatterns.psiExpression().methodCallParameter(0, DECODE_METHOD); + private static final PsiMethodPattern GET_COLOR_METHOD = PsiJavaPatterns.psiMethod() + .definedInClass(Color.class.getName()) + .withName("getColor"); + private static final PsiExpressionPattern.Capture GET_METHOD_CALL_PARAMETER = + PsiJavaPatterns.psiExpression().methodCallParameter(0, GET_COLOR_METHOD); + + private static boolean isInsideDecodeOrGetColorMethod(PsiElement element) { + if (element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.STRING_LITERAL) { + element = element.getParent(); + } + + return DECODE_METHOD_CALL_PARAMETER.accepts(element) || + GET_METHOD_CALL_PARAMETER.accepts(element); + } + @Override public boolean isSupportedFile(PsiFile file) { return file instanceof PsiJavaFile; @@ -120,7 +143,7 @@ public class JavaPreviewHintProvider implements PreviewHintProvider { } } - if (ColorChooserIntentionAction.isInsideDecodeOrGetColorMethod(element)) { + if (isInsideDecodeOrGetColorMethod(element)) { final String color = StringUtil.unquoteString(element.getText()); try { return new ColorPreviewComponent(Color.decode(color)); diff --git a/platform/lang-api/src/com/intellij/codeInsight/daemon/NavigateAction.java b/platform/lang-api/src/com/intellij/codeInsight/daemon/NavigateAction.java index 90a06ae7b13d..90ccfbffbf8b 100644 --- a/platform/lang-api/src/com/intellij/codeInsight/daemon/NavigateAction.java +++ b/platform/lang-api/src/com/intellij/codeInsight/daemon/NavigateAction.java @@ -20,6 +20,7 @@ import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.ShortcutSet; import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.Nullable; import java.awt.event.MouseEvent; @@ -31,11 +32,13 @@ public class NavigateAction extends AnAction { public NavigateAction(String text, LineMarkerInfo info, - String originalActionId) { + @Nullable String originalActionId) { super(text); myInfo = info; - ShortcutSet set = ActionManager.getInstance().getAction(originalActionId).getShortcutSet(); - setShortcutSet(set); + if (originalActionId != null) { + ShortcutSet set = ActionManager.getInstance().getAction(originalActionId).getShortcutSet(); + setShortcutSet(set); + } } public NavigateAction(LineMarkerInfo info) { @@ -53,7 +56,7 @@ public class NavigateAction extends AnAction { } } - public static LineMarkerInfo setNavigateAction(LineMarkerInfo info, String text, String originalActionId) { + public static LineMarkerInfo setNavigateAction(LineMarkerInfo info, String text, @Nullable String originalActionId) { NavigateAction action = new NavigateAction(text, info, originalActionId); info.setNavigateAction(action); return info; diff --git a/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java b/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java index 5bd893ee1128..4ef4704b162b 100644 --- a/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java +++ b/platform/lang-impl/src/com/intellij/ui/ColorLineMarkerProvider.java @@ -16,10 +16,7 @@ package com.intellij.ui; import com.intellij.codeHighlighting.Pass; -import com.intellij.codeInsight.daemon.GutterIconNavigationHandler; -import com.intellij.codeInsight.daemon.LineMarkerInfo; -import com.intellij.codeInsight.daemon.LineMarkerProvider; -import com.intellij.codeInsight.daemon.MergeableLineMarkerInfo; +import com.intellij.codeInsight.daemon.*; import com.intellij.icons.AllIcons; import com.intellij.openapi.application.AccessToken; import com.intellij.openapi.application.ApplicationManager; @@ -44,6 +41,7 @@ import java.util.List; * @author Konstantin Bulenkov */ public final class ColorLineMarkerProvider implements LineMarkerProvider { + private final ElementColorProvider[] myExtensions = ElementColorProvider.EP_NAME.getExtensions(); @Override @@ -51,7 +49,9 @@ public final class ColorLineMarkerProvider implements LineMarkerProvider { for (ElementColorProvider colorProvider : myExtensions) { final Color color = colorProvider.getColorFrom(element); if (color != null) { - return new MyInfo(element, color, colorProvider); + MyInfo info = new MyInfo(element, color, colorProvider); + NavigateAction.setNavigateAction(info, "Choose color", null); + return info; } } return null; @@ -78,7 +78,7 @@ public final class ColorLineMarkerProvider implements LineMarkerProvider { final Editor editor = PsiUtilBase.findEditor(element); assert editor != null; - final Color c = ColorChooser.chooseColor(editor.getComponent(), "Choose color", color, true); + final Color c = ColorChooser.chooseColor(editor.getComponent(), "Choose Color", color, true); if (c != null) { AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(ColorLineMarkerProvider.class); try { diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 5012164ab5ae..d9ed97e9257a 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -931,11 +931,6 @@ Declaration - - com.intellij.codeInsight.intention.impl.ColorChooserIntentionAction - Declaration - - com.intellij.codeInsight.intention.impl.ConvertColorRepresentationIntentionAction Declaration