diff --git a/java/idea-ui/src/com/intellij/ide/util/projectWizard/AbstractStepWithProgress.java b/java/idea-ui/src/com/intellij/ide/util/projectWizard/AbstractStepWithProgress.java index f1c4b5f146fc..1068a14f7331 100644 --- a/java/idea-ui/src/com/intellij/ide/util/projectWizard/AbstractStepWithProgress.java +++ b/java/idea-ui/src/com/intellij/ide/util/projectWizard/AbstractStepWithProgress.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -178,7 +178,7 @@ public abstract class AbstractStepWithProgress extends ModuleWizardStep if (isProgressRunning()) { final int answer = Messages.showOkCancelDialog(getComponent(), myPromptStopSearch, IdeBundle.message("title.question"), IdeBundle.message("action.continue.searching"), IdeBundle.message("action.stop.searching"), Messages.getWarningIcon()); - if (answer == 1) { // terminate + if (answer != Messages.OK) { // terminate cancelSearch(); } return false; diff --git a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/TodoCheckinHandler.java b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/TodoCheckinHandler.java index 80cc42ebf789..1a9fa06f0a69 100644 --- a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/TodoCheckinHandler.java +++ b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/TodoCheckinHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -192,11 +192,11 @@ public class TodoCheckinHandler extends CheckinHandler { } final int answer = Messages.showOkCancelDialog(myCheckinProjectPanel.getComponent(), text, "TODO", buttons[0], buttons[1], UIUtil.getWarningIcon()); - if (thereAreTodoFound && answer == 0) { + if (thereAreTodoFound && answer == Messages.OK) { showTodo(worker); return ReturnResult.CLOSE_WINDOW; } - else if (thereAreTodoFound && ((answer == 2 || answer == -1)) || (! thereAreTodoFound) && answer == 1) { + if (!thereAreTodoFound && answer != Messages.OK) { return ReturnResult.CANCEL; } else { diff --git a/platform/platform-api/src/com/intellij/openapi/ui/MessageBuilder.java b/platform/platform-api/src/com/intellij/openapi/ui/MessageBuilder.java deleted file mode 100644 index 649081915c32..000000000000 --- a/platform/platform-api/src/com/intellij/openapi/ui/MessageBuilder.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2000-2013 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 com.intellij.openapi.ui; - -import com.intellij.openapi.project.Project; -import com.intellij.openapi.wm.WindowManager; -import com.intellij.ui.mac.MacMessages; -import org.intellij.lang.annotations.MagicConstant; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; - -public final class MessageBuilder { - private final String myMessage; - private final String myTitle; - private Project myProject; - private Icon myIcon; - private DialogWrapper.DoNotAskOption myDoNotAskOption; - - private MessageBuilder(@NotNull String title, @NotNull String message) { - myTitle = title; - myMessage = message; - } - - public static MessageBuilder yesNo(@NotNull String title, @NotNull String message) { - return new MessageBuilder(title, message); - } - - public MessageBuilder project(@Nullable Project project) { - myProject = project; - return this; - } - - /** - * @see {@link Messages#getInformationIcon()} - * @see {@link Messages#getWarningIcon()} - * @see {@link Messages#getErrorIcon()} - * @see {@link Messages#getQuestionIcon()} - */ - public MessageBuilder icon(@Nullable Icon icon) { - myIcon = icon; - return this; - } - - public MessageBuilder doNotAskOption(@NotNull DialogWrapper.DoNotAskOption doNotAskOption) { - myDoNotAskOption = doNotAskOption; - return this; - } - - @MagicConstant(intValues = {Messages.YES, Messages.NO, Messages.CANCEL, Messages.OK}) - public int show() { - if (Messages.canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(myTitle, myMessage, Messages.YES_BUTTON, Messages.NO_BUTTON, WindowManager.getInstance().suggestParentWindow(myProject), myDoNotAskOption); - } - else { - //noinspection MagicConstant - return Messages.showDialog(myProject, myMessage, myTitle, new String[]{Messages.YES_BUTTON, Messages.NO_BUTTON}, 0, myIcon, myDoNotAskOption); - } - } -} \ No newline at end of file diff --git a/platform/platform-api/src/com/intellij/openapi/ui/Messages.java b/platform/platform-api/src/com/intellij/openapi/ui/Messages.java index 07d3d3e48403..a23ac12c6795 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/Messages.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/Messages.java @@ -38,6 +38,7 @@ import com.intellij.util.Function; import com.intellij.util.PairFunction; import com.intellij.util.execution.ParametersListUtil; import com.intellij.util.ui.UIUtil; +import org.intellij.lang.annotations.MagicConstant; import org.jetbrains.annotations.*; import javax.swing.*; @@ -92,47 +93,72 @@ public class Messages { return oldValue; } + @NotNull public static Icon getErrorIcon() { return UIUtil.getErrorIcon(); } + @NotNull public static Icon getInformationIcon() { return UIUtil.getInformationIcon(); } + @NotNull public static Icon getWarningIcon() { return UIUtil.getWarningIcon(); } + @NotNull public static Icon getQuestionIcon() { return UIUtil.getQuestionIcon(); } /** * Please, use {@link #showOkCancelDialog} or {@link #showYesNoCancelDialog} if possible (these dialogs implements native OS behavior)! + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel */ - public static int showDialog(@Nullable Project project, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon) { + public static int showDialog(@Nullable Project project, + String message, + String title, + @NotNull String[] options, + int defaultOptionIndex, + @Nullable Icon icon) { return showDialog(project, message, title, options, defaultOptionIndex, icon, null); } /** * Please, use {@link #showOkCancelDialog} or {@link #showYesNoCancelDialog} if possible (these dialogs implements native OS behavior)! + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel */ - public static int showDialog(@Nullable Project project, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon, + public static int showDialog(@Nullable Project project, + String message, + @NotNull String title, + @NotNull String[] options, + int defaultOptionIndex, + @Nullable Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { if (isApplicationInUnitTestOrHeadless()) { return ourTestImplementation.show(message); } if (canShowMacSheetPanel()) { + Window parentWindow = WindowManager.getInstance().suggestParentWindow(project); return MacMessages.getInstance() - .showMessageDialog(title, message, options, false, WindowManager.getInstance().suggestParentWindow(project), defaultOptionIndex, defaultOptionIndex, doNotAskOption); + .showMessageDialog(title, message, options, false, parentWindow, defaultOptionIndex, defaultOptionIndex, doNotAskOption); } return showIdeaMessageDialog(project, message, title, options, defaultOptionIndex, icon, doNotAskOption); } - public static int showIdeaMessageDialog(@Nullable Project project, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon, + /** + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel + */ + public static int showIdeaMessageDialog(@Nullable Project project, + String message, + String title, + @NotNull String[] options, + int defaultOptionIndex, + @Nullable Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { MessageDialog dialog = new MessageDialog(project, message, title, options, defaultOptionIndex, -1, icon, doNotAskOption, false); dialog.show(); @@ -150,7 +176,17 @@ public class Messages { return SystemInfo.isMac && Registry.is("ide.mac.message.dialogs.as.sheets") && Registry.is("ide.mac.message.sheets.java.emulation"); } - public static int showDialog(Project project, String message, String title, String moreInfo, String[] options, int defaultOptionIndex, int focusedOptionIndex, Icon icon) { + /** + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel + */ + public static int showDialog(Project project, + String message, + @NotNull String title, + String moreInfo, + @NotNull String[] options, + int defaultOptionIndex, + int focusedOptionIndex, + Icon icon) { if (isApplicationInUnitTestOrHeadless()) { return ourTestImplementation.show(message); } @@ -170,7 +206,10 @@ public class Messages { return application != null && (application.isUnitTestMode() || application.isHeadlessEnvironment()); } - public static int showDialog(Component parent, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon) { + /** + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel + */ + public static int showDialog(Component parent, String message, @NotNull String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon) { if (isApplicationInUnitTestOrHeadless()) { return ourTestImplementation.show(message); } @@ -191,21 +230,26 @@ public class Messages { * * @see #showDialog(Project, String, String, String[], int, Icon, DialogWrapper.DoNotAskOption) * @see #showDialog(Component, String, String, String[], int, Icon) + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel */ - public static int showDialog(String message, String title, String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { + public static int showDialog(String message, + @NotNull String title, + @NotNull String[] options, + int defaultOptionIndex, + int focusedOptionIndex, + @Nullable Icon icon, + @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { if (isApplicationInUnitTestOrHeadless()) { return ourTestImplementation.show(message); } - else { - if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showMessageDialog(title, message, options, false, null, defaultOptionIndex, focusedOptionIndex, doNotAskOption); - } - - //what's it? if (application.isUnitTestMode()) throw new RuntimeException(message); - MessageDialog dialog = new MessageDialog(message, title, options, defaultOptionIndex, focusedOptionIndex, icon, doNotAskOption); - dialog.show(); - return dialog.getExitCode(); + if (canShowMacSheetPanel()) { + return MacMessages.getInstance().showMessageDialog(title, message, options, false, null, defaultOptionIndex, focusedOptionIndex, doNotAskOption); } + + //what's it? if (application.isUnitTestMode()) throw new RuntimeException(message); + MessageDialog dialog = new MessageDialog(message, title, options, defaultOptionIndex, focusedOptionIndex, icon, doNotAskOption); + dialog.show(); + return dialog.getExitCode(); } /** @@ -213,8 +257,9 @@ public class Messages { * * @see #showDialog(Project, String, String, String[], int, Icon) * @see #showDialog(Component, String, String, String[], int, Icon) + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel */ - public static int showDialog(String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { + public static int showDialog(String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { return showDialog(message, title, options, defaultOptionIndex, defaultOptionIndex, icon, doNotAskOption); } @@ -223,15 +268,16 @@ public class Messages { * * @see #showDialog(Project, String, String, String[], int, Icon) * @see #showDialog(Component, String, String, String[], int, Icon) + * @return number of button pressed: from 0 up to options.length-1 inclusive, or -1 for Cancel */ - public static int showDialog(String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon) { + public static int showDialog(String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon) { return showDialog(message, title, options, defaultOptionIndex, icon, null); } /** * @see com.intellij.openapi.ui.DialogWrapper#DialogWrapper(Project,boolean) */ - public static void showMessageDialog(@Nullable Project project, String message, String title, @Nullable Icon icon) { + public static void showMessageDialog(@Nullable Project project, String message, @NotNull String title, @Nullable Icon icon) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showOkMessageDialog(title, message, OK_BUTTON, WindowManager.getInstance().suggestParentWindow(project)); return; @@ -240,7 +286,7 @@ public class Messages { showDialog(project, message, title, new String[]{OK_BUTTON}, 0, icon); } - public static void showMessageDialog(Component parent, String message, String title, @Nullable Icon icon) { + public static void showMessageDialog(Component parent, String message, @NotNull String title, @Nullable Icon icon) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showOkMessageDialog(title, message, OK_BUTTON, SwingUtilities.getWindowAncestor(parent)); return; @@ -255,7 +301,7 @@ public class Messages { * @see #showMessageDialog(Project, String, String, Icon) * @see #showMessageDialog(Component, String, String, Icon) */ - public static void showMessageDialog(String message, String title, @Nullable Icon icon) { + public static void showMessageDialog(String message, @NotNull String title, @Nullable Icon icon) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showOkMessageDialog(title, message, OK_BUTTON); return; @@ -264,62 +310,72 @@ public class Messages { showDialog(message, title, new String[]{OK_BUTTON}, 0, icon); } - /** - * @return Messages.YES if user pressed "Yes" and returns Messages.NO if user pressed "No" button. - */ - public static int showYesNoDialog(@Nullable Project project, String message, String title, String yesText, String noText, @Nullable Icon icon) { - if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, yesText, noText, WindowManager.getInstance().suggestParentWindow(project)); - } - - return showDialog(project, message, title, new String[]{yesText, noText}, 0, icon); + @MagicConstant(intValues = {YES, NO}) + public @interface YesNoResult { } /** - * @return Messages.YES if user pressed "Yes" and returns Messages.NO if user pressed "No" button. + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No" button. */ - public static int showYesNoDialog(@Nullable Project project, String message, String title, @Nullable Icon icon) { - if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, YES_BUTTON, NO_BUTTON, WindowManager.getInstance().suggestParentWindow(project)); - } - - return showYesNoDialog(project, message, title, YES_BUTTON, NO_BUTTON, icon); + @YesNoResult + public static int showYesNoDialog(@Nullable Project project, String message, @NotNull String title, @NotNull String yesText, @NotNull String noText, @Nullable Icon icon) { + int result = canShowMacSheetPanel() + ? MacMessages.getInstance().showYesNoDialog(title, message, yesText, noText, WindowManager.getInstance().suggestParentWindow(project)) + : showDialog(project, message, title, new String[]{yesText, noText}, 0, icon) == 0 ? YES : NO; + LOG.assertTrue(result == YES || result == NO, result); + return result; } /** - * @return Messages.YES if user pressed "Yes" and returns Messages.NO if user pressed "No" button. + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No" button. */ - public static int showYesNoDialog(Component parent, String message, String title, @Nullable Icon icon) { - if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, YES_BUTTON, NO_BUTTON, SwingUtilities.getWindowAncestor(parent)); - } - - return showDialog(parent, message, title, new String[]{YES_BUTTON, NO_BUTTON}, 0, icon); + @YesNoResult + public static int showYesNoDialog(@Nullable Project project, String message, @NotNull String title, @Nullable Icon icon) { + int result = canShowMacSheetPanel() + ? MacMessages.getInstance().showYesNoDialog(title, message, YES_BUTTON, NO_BUTTON, WindowManager.getInstance().suggestParentWindow(project)) + : showYesNoDialog(project, message, title, YES_BUTTON, NO_BUTTON, icon); + LOG.assertTrue(result == YES || result == NO, result); + return result; } + /** + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No" button. + */ + @YesNoResult + public static int showYesNoDialog(Component parent, String message, @NotNull String title, @Nullable Icon icon) { + int result = canShowMacSheetPanel() + ? MacMessages.getInstance().showYesNoDialog(title, message, YES_BUTTON, NO_BUTTON, SwingUtilities.getWindowAncestor(parent)) + : showDialog(parent, message, title, new String[]{YES_BUTTON, NO_BUTTON}, 0, icon) == 0 ? YES : NO; + LOG.assertTrue(result == YES || result == NO, result); + return result; + } + /** * Use this method only if you do not know project or component * * @see #showYesNoDialog(com.intellij.openapi.project.Project, String, String, javax.swing.Icon) * @see #showYesNoCancelDialog(java.awt.Component, String, String, javax.swing.Icon) + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No" button. */ - public static int showYesNoDialog(String message, String title, String yesText, String noText, @Nullable Icon icon, + @YesNoResult + public static int showYesNoDialog(String message, @NotNull String title, @NotNull String yesText, @NotNull String noText, @Nullable Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { - if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, yesText, noText, null, doNotAskOption); - } - - return showDialog(message, title, new String[]{yesText, noText}, 0, icon, doNotAskOption); + int result = canShowMacSheetPanel() + ? MacMessages.getInstance().showYesNoDialog(title, message, yesText, noText, null, doNotAskOption) + : showDialog(message, title, new String[]{yesText, noText}, 0, icon, doNotAskOption) == 0 ? YES : NO; + LOG.assertTrue(result == YES || result == NO, result); + return result; } /** * Use this method only if you do not know project or component * - * @return Messages.YES if user pressed "Yes" and returns Messages.NO if user pressed "No" button. * @see #showYesNoDialog(Project, String, String, String, String, Icon) * @see #showYesNoDialog(java.awt.Component, String, String, javax.swing.Icon) + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No" button. */ + @YesNoResult public static int showYesNoDialog(String message, String title, String yesText, String noText, @Nullable Icon icon) { return showYesNoDialog(message, title, yesText, noText, icon, null); } @@ -327,49 +383,83 @@ public class Messages { /** * Use this method only if you do not know project or component * - * @return Messages.YES if user pressed "Yes" and returns Messages.NO if user pressed "No" button. * @see #showYesNoDialog(Project, String, String, Icon) * @see #showYesNoDialog(Component, String, String, Icon) + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No" button. */ - public static int showYesNoDialog(String message, String title, @Nullable Icon icon) { - if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, YES_BUTTON, NO_BUTTON, null); - } - - return showYesNoDialog(message, title, YES_BUTTON, NO_BUTTON, icon); + @YesNoResult + public static int showYesNoDialog(String message, @NotNull String title, @Nullable Icon icon) { + int result = canShowMacSheetPanel() + ? MacMessages.getInstance().showYesNoDialog(title, message, YES_BUTTON, NO_BUTTON, null) + : showYesNoDialog(message, title, YES_BUTTON, NO_BUTTON, icon); + LOG.assertTrue(result == YES || result == NO, result); + return result; } - public static int showOkCancelDialog(Project project, String message, String title, String okText, String cancelText, Icon icon, + @MagicConstant(intValues = {OK, CANCEL}) + public @interface OkCancelResult { + } + + /** + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. + */ + @OkCancelResult + public static int showOkCancelDialog(Project project, + String message, + @NotNull String title, + @NotNull String okText, + @NotNull String cancelText, + Icon icon, DialogWrapper.DoNotAskOption doNotAskOption) { if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, okText, cancelText, WindowManager.getInstance().suggestParentWindow(project), - doNotAskOption); + int result = MacMessages.getInstance() + .showYesNoDialog(title, message, okText, cancelText, WindowManager.getInstance().suggestParentWindow(project), + doNotAskOption); + return result == YES ? OK : CANCEL; } - return showDialog(project, message, title, new String[]{okText, cancelText}, 0, icon, doNotAskOption); + return showDialog(project, message, title, new String[]{okText, cancelText}, 0, icon, doNotAskOption) == 0 ? OK : CANCEL; } - public static int showOkCancelDialog(Project project, String message, String title, String okText, String cancelText, Icon icon) { + /** + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. + */ + @OkCancelResult + public static int showOkCancelDialog(Project project, String message, @NotNull String title, @NotNull String okText, @NotNull String cancelText, Icon icon) { if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, okText, cancelText, WindowManager.getInstance().suggestParentWindow(project)); + int result = MacMessages.getInstance() + .showYesNoDialog(title, message, okText, cancelText, WindowManager.getInstance().suggestParentWindow(project)); + return result == YES ? OK : CANCEL; } - return showDialog(project, message, title, new String[]{okText, cancelText}, 0, icon); + return showDialog(project, message, title, new String[]{okText, cancelText}, 0, icon) == 0 ? OK : CANCEL; } + /** + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. + */ + @OkCancelResult public static int showOkCancelDialog(Project project, String message, String title, Icon icon) { return showOkCancelDialog(project, message, title, OK_BUTTON, CANCEL_BUTTON, icon); } - public static int showOkCancelDialog(Component parent, String message, String title, String okText, String cancelText, Icon icon) { + /** + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. + */ + @OkCancelResult + public static int showOkCancelDialog(Component parent, String message, @NotNull String title, @NotNull String okText, @NotNull String cancelText, Icon icon) { if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, okText, cancelText, SwingUtilities.getWindowAncestor(parent)); + int result = MacMessages.getInstance().showYesNoDialog(title, message, okText, cancelText, SwingUtilities.getWindowAncestor(parent)); + return result == YES ? OK : CANCEL; } - - return showDialog(parent, message, title, new String[]{okText, cancelText}, 0, icon); + return showDialog(parent, message, title, new String[]{okText, cancelText}, 0, icon) == 0 ? OK : CANCEL; } + /** + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. + */ + @OkCancelResult public static int showOkCancelDialog(Component parent, String message, String title, Icon icon) { return showOkCancelDialog(parent, message, title, OK_BUTTON, CANCEL_BUTTON, icon); } @@ -379,7 +469,9 @@ public class Messages { * * @see #showOkCancelDialog(Project, String, String, Icon) * @see #showOkCancelDialog(Component, String, String, Icon) + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. */ + @OkCancelResult public static int showOkCancelDialog(String message, String title, Icon icon) { return showOkCancelDialog(message, title, OK_BUTTON, CANCEL_BUTTON, icon, null); } @@ -389,7 +481,9 @@ public class Messages { * * @see #showOkCancelDialog(Project, String, String, String, String, Icon) * @see #showOkCancelDialog(Component, String, String, String, String, Icon) + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. */ + @OkCancelResult public static int showOkCancelDialog(String message, String title, String okText, String cancelText, Icon icon) { return showOkCancelDialog(message, title, okText, cancelText, icon, null); } @@ -399,13 +493,16 @@ public class Messages { * * @see #showOkCancelDialog(Project, String, String, String, String, Icon, DialogWrapper.DoNotAskOption) * @see #showOkCancelDialog(Component, String, String, String, String, Icon) + * @return {@link #OK} if user pressed "Ok" or {@link #CANCEL} if user pressed "Cancel" button. */ - public static int showOkCancelDialog(String message, String title, String okText, String cancelText, Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { + @OkCancelResult + public static int showOkCancelDialog(String message, @NotNull String title, @NotNull String okText, @NotNull String cancelText, Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { if (canShowMacSheetPanel()) { - return MacMessages.getInstance().showYesNoDialog(title, message, okText, cancelText, null, doNotAskOption); + int result = MacMessages.getInstance().showYesNoDialog(title, message, okText, cancelText, null, doNotAskOption); + return result == YES ? OK : CANCEL; } - return showDialog(message, title, new String[]{okText, cancelText}, 0, icon, doNotAskOption); + return showDialog(message, title, new String[]{okText, cancelText}, 0, icon, doNotAskOption) == 0 ? OK : CANCEL; } public static int showCheckboxOkCancelDialog(String message, String title, String checkboxText, final boolean checked, @@ -420,7 +517,7 @@ public class Messages { }); } - public static int showCheckboxMessageDialog(String message, String title, String[] options, String checkboxText, final boolean checked, + public static int showCheckboxMessageDialog(String message, String title, @NotNull String[] options, String checkboxText, final boolean checked, final int defaultOptionIndex, final int focusedOptionIndex, Icon icon, @Nullable final PairFunction exitFunc) { if (isApplicationInUnitTestOrHeadless()) { @@ -439,7 +536,7 @@ public class Messages { return showCheckboxMessageDialog(message, title, new String[]{OK_BUTTON}, checkboxText, true, -1, -1, icon, null); } - public static void showErrorDialog(@Nullable Project project, @Nls String message, @Nls String title) { + public static void showErrorDialog(@Nullable Project project, @Nls String message, @Nls @NotNull String title) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showErrorDialog(title, message, OK_BUTTON, WindowManager.getInstance().suggestParentWindow(project)); return; @@ -448,7 +545,7 @@ public class Messages { showDialog(project, message, title, new String[]{OK_BUTTON}, 0, getErrorIcon()); } - public static void showErrorDialog(Component component, String message, @Nls String title) { + public static void showErrorDialog(Component component, String message, @Nls @NotNull String title) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showErrorDialog(title, message, OK_BUTTON, SwingUtilities.getWindowAncestor(component)); return; @@ -459,7 +556,8 @@ public class Messages { public static void showErrorDialog(Component component, String message) { if (canShowMacSheetPanel()) { - MacMessages.getInstance().showErrorDialog(CommonBundle.getErrorTitle(), message, OK_BUTTON, SwingUtilities.getWindowAncestor(component)); + MacMessages.getInstance().showErrorDialog(CommonBundle.getErrorTitle(), message, OK_BUTTON, SwingUtilities.getWindowAncestor( + component)); return; } @@ -472,27 +570,27 @@ public class Messages { * @see #showErrorDialog(Project, String, String) * @see #showErrorDialog(Component, String, String) */ - public static void showErrorDialog(String message, String title) { + public static void showErrorDialog(String message, @NotNull String title) { if (canShowMacSheetPanel()) { - MacMessages.getInstance().showErrorDialog(CommonBundle.getErrorTitle(), message, OK_BUTTON, null); + MacMessages.getInstance().showErrorDialog(title, message, OK_BUTTON, null); return; } showDialog(message, title, new String[]{OK_BUTTON}, 0, getErrorIcon()); } - public static void showWarningDialog(@Nullable Project project, String message, String title) { + public static void showWarningDialog(@Nullable Project project, String message, @NotNull String title) { if (canShowMacSheetPanel()) { - MacMessages.getInstance().showErrorDialog(CommonBundle.getWarningTitle(), message, OK_BUTTON, WindowManager.getInstance().suggestParentWindow(project)); + MacMessages.getInstance().showErrorDialog(title, message, OK_BUTTON, WindowManager.getInstance().suggestParentWindow(project)); return; } showDialog(project, message, title, new String[]{OK_BUTTON}, 0, getWarningIcon()); } - public static void showWarningDialog(Component component, String message, String title) { + public static void showWarningDialog(Component component, String message, @NotNull String title) { if (canShowMacSheetPanel()) { - MacMessages.getInstance().showErrorDialog(CommonBundle.getWarningTitle(), message, OK_BUTTON, SwingUtilities.getWindowAncestor(component)); + MacMessages.getInstance().showErrorDialog(title, message, OK_BUTTON, SwingUtilities.getWindowAncestor(component)); return; } @@ -505,38 +603,72 @@ public class Messages { * @see #showWarningDialog(Project, String, String) * @see #showWarningDialog(Component, String, String) */ - public static void showWarningDialog(String message, String title) { + public static void showWarningDialog(String message, @NotNull String title) { if (canShowMacSheetPanel()) { - MacMessages.getInstance().showErrorDialog(CommonBundle.getWarningTitle(), message, OK_BUTTON, null); + MacMessages.getInstance().showErrorDialog(title, message, OK_BUTTON, null); return; } showDialog(message, title, new String[]{OK_BUTTON}, 0, getWarningIcon()); } - public static int showYesNoCancelDialog(Project project, String message, String title, String yes, String no, String cancel, @Nullable Icon icon) { + @MagicConstant(intValues = {YES, NO, CANCEL}) + public @interface YesNoCancelResult { + } + + + /** + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. + */ + @YesNoCancelResult + public static int showYesNoCancelDialog(Project project, + String message, + @NotNull String title, + @NotNull String yes, + @NotNull String no, + @NotNull String cancel, + @Nullable Icon icon) { if (canShowMacSheetPanel()) { return MacMessages.getInstance().showYesNoCancelDialog(title, message, yes, no, cancel, WindowManager.getInstance().suggestParentWindow(project), null); } - return showDialog(project, message, title, new String[]{yes, no, cancel}, 0, icon); + int buttonNumber = showDialog(project, message, title, new String[]{yes, no, cancel}, 0, icon); + return buttonNumber == 0 ? YES : buttonNumber == 1 ? NO : CANCEL; } - + /** + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. + */ + @YesNoCancelResult public static int showYesNoCancelDialog(Project project, String message, String title, Icon icon) { return showYesNoCancelDialog(project, message, title, YES_BUTTON, NO_BUTTON, CANCEL_BUTTON, icon); } - public static int showYesNoCancelDialog(Component parent, String message, String title, String yes, String no, String cancel, Icon icon) { + /** + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. + */ + @YesNoCancelResult + public static int showYesNoCancelDialog(Component parent, + String message, + @NotNull String title, + @NotNull String yes, + @NotNull String no, + @NotNull String cancel, + Icon icon) { if (canShowMacSheetPanel()) { return MacMessages.getInstance().showYesNoCancelDialog(title, message, yes, no, cancel, SwingUtilities.getWindowAncestor(parent), null); } - return showDialog(parent, message, title, new String[]{yes, no, cancel}, 0, icon); + int buttonNumber = showDialog(parent, message, title, new String[]{yes, no, cancel}, 0, icon); + return buttonNumber == 0 ? YES : buttonNumber == 1 ? NO : CANCEL; } + /** + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. + */ + @YesNoCancelResult public static int showYesNoCancelDialog(Component parent, String message, String title, Icon icon) { return showYesNoCancelDialog(parent, message, title, YES_BUTTON, NO_BUTTON, CANCEL_BUTTON, icon); } @@ -547,14 +679,22 @@ public class Messages { * * @see #showYesNoCancelDialog(Project, String, String, String, String, String, Icon) * @see #showYesNoCancelDialog(Component, String, String, String, String, String, Icon) + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. */ - public static int showYesNoCancelDialog(String message, String title, String yes, String no, String cancel, Icon icon, + @YesNoCancelResult + public static int showYesNoCancelDialog(String message, + @NotNull String title, + @NotNull String yes, + @NotNull String no, + @NotNull String cancel, + Icon icon, @Nullable DialogWrapper.DoNotAskOption doNotAskOption) { if (canShowMacSheetPanel()) { return MacMessages.getInstance().showYesNoCancelDialog(title, message, yes, no, cancel, null, doNotAskOption); } - return showDialog(message, title, new String[]{yes, no, cancel}, 0, icon, doNotAskOption); + int buttonNumber = showDialog(message, title, new String[]{yes, no, cancel}, 0, icon, doNotAskOption); + return buttonNumber == 0 ? YES : buttonNumber == 1 ? NO : CANCEL; } /** @@ -562,7 +702,9 @@ public class Messages { * * @see #showYesNoCancelDialog(Project, String, String, String, String, String, Icon) * @see #showYesNoCancelDialog(Component, String, String, String, String, String, Icon) + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. */ + @YesNoCancelResult public static int showYesNoCancelDialog(String message, String title, String yes, String no, String cancel, Icon icon) { return showYesNoCancelDialog(message, title, yes, no, cancel, icon, null); } @@ -572,7 +714,9 @@ public class Messages { * * @see #showYesNoCancelDialog(Project, String, String, Icon) * @see #showYesNoCancelDialog(Component, String, String, Icon) + * @return {@link #YES} if user pressed "Yes" or {@link #NO} if user pressed "No", or {@link #CANCEL} if user pressed "Cancel" button. */ + @YesNoCancelResult public static int showYesNoCancelDialog(String message, String title, Icon icon) { return showYesNoCancelDialog(message, title, YES_BUTTON, NO_BUTTON, CANCEL_BUTTON, icon); } @@ -827,7 +971,7 @@ public class Messages { /** * Shows dialog with given message and title, information icon {@link #getInformationIcon()} and OK button */ - public static void showInfoMessage(Component component, String message, String title) { + public static void showInfoMessage(Component component, String message, @NotNull String title) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showOkMessageDialog(title, message, OK_BUTTON, SwingUtilities.getWindowAncestor(component)); return; @@ -839,7 +983,7 @@ public class Messages { /** * Shows dialog with given message and title, information icon {@link #getInformationIcon()} and OK button */ - public static void showInfoMessage(@Nullable Project project, @Nls String message, @Nls String title) { + public static void showInfoMessage(@Nullable Project project, @Nls String message, @Nls @NotNull String title) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showOkMessageDialog(title, message, OK_BUTTON, WindowManager.getInstance().suggestParentWindow(project)); return; @@ -856,7 +1000,7 @@ public class Messages { * @see #showInputDialog(Project, String, String, Icon, String, InputValidator) * @see #showInputDialog(Component, String, String, Icon, String, InputValidator) */ - public static void showInfoMessage(String message, String title) { + public static void showInfoMessage(String message, @NotNull String title) { if (canShowMacSheetPanel()) { MacMessages.getInstance().showOkMessageDialog(title, message, OK_BUTTON, null); return; @@ -917,7 +1061,7 @@ public class Messages { String message, String title, String moreInfo, - String[] options, + @NotNull String[] options, int defaultOptionIndex, int focusedOptionIndex, Icon icon) { super(project); myInfoText = moreInfo; @@ -958,51 +1102,64 @@ public class Messages { protected Icon myIcon; private MyBorderLayout myLayout; - public MessageDialog(@Nullable Project project, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon, boolean canBeParent) { + public MessageDialog(@Nullable Project project, String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon, boolean canBeParent) { this(project, message, title, options, defaultOptionIndex, -1, icon, canBeParent); } - public MessageDialog(@Nullable Project project, String message, String title, String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, - @Nullable DoNotAskOption doNotAskOption, boolean canBeParent) { + public MessageDialog(@Nullable Project project, + String message, + String title, + @NotNull String[] options, + int defaultOptionIndex, + int focusedOptionIndex, + @Nullable Icon icon, + @Nullable DoNotAskOption doNotAskOption, + boolean canBeParent) { super(project, canBeParent); _init(title, message, options, defaultOptionIndex, focusedOptionIndex, icon, doNotAskOption); } - public MessageDialog(@Nullable Project project, String message, String title, String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, + public MessageDialog(@Nullable Project project, String message, String title, @NotNull String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, boolean canBeParent) { super(project, canBeParent); _init(title, message, options, defaultOptionIndex, focusedOptionIndex, icon, null); } - public MessageDialog(Component parent, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon) { + public MessageDialog(Component parent, String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon) { this(parent, message, title, options, defaultOptionIndex, icon, false); } - public MessageDialog(Component parent, String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon, boolean canBeParent) { + public MessageDialog(Component parent, String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon, boolean canBeParent) { this(parent, message, title, options, defaultOptionIndex, -1, icon, canBeParent); } - public MessageDialog(Component parent, String message, String title, String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, + public MessageDialog(Component parent, + String message, + String title, + @NotNull String[] options, + int defaultOptionIndex, + int focusedOptionIndex, + @Nullable Icon icon, boolean canBeParent) { super(parent, canBeParent); _init(title, message, options, defaultOptionIndex, focusedOptionIndex, icon, null); } - public MessageDialog(String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon) { + public MessageDialog(String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon) { this(message, title, options, defaultOptionIndex, icon, false); } - public MessageDialog(String message, String title, String[] options, int defaultOptionIndex, @Nullable Icon icon, boolean canBeParent) { + public MessageDialog(String message, String title, @NotNull String[] options, int defaultOptionIndex, @Nullable Icon icon, boolean canBeParent) { super(canBeParent); _init(title, message, options, defaultOptionIndex, -1, icon, null); } - public MessageDialog(String message, String title, String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, @Nullable DoNotAskOption doNotAskOption) { + public MessageDialog(String message, String title, @NotNull String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, @Nullable DoNotAskOption doNotAskOption) { super(false); _init(title, message, options, defaultOptionIndex, focusedOptionIndex, icon, doNotAskOption); } - public MessageDialog(String message, String title, String[] options, int defaultOptionIndex, Icon icon, DoNotAskOption doNotAskOption) { + public MessageDialog(String message, String title, @NotNull String[] options, int defaultOptionIndex, Icon icon, DoNotAskOption doNotAskOption) { this(message, title, options, defaultOptionIndex, -1, icon, doNotAskOption); } @@ -1014,7 +1171,13 @@ public class Messages { super(project, false); } - protected void _init(String title, String message, String[] options, int defaultOptionIndex, int focusedOptionIndex, @Nullable Icon icon, @Nullable DoNotAskOption doNotAskOption) { + protected void _init(String title, + String message, + @NotNull String[] options, + int defaultOptionIndex, + int focusedOptionIndex, + @Nullable Icon icon, + @Nullable DoNotAskOption doNotAskOption) { setTitle(title); if (isMacSheetEmulation()) { setUndecorated(true); @@ -1058,12 +1221,12 @@ public class Messages { return actions; } - private static void assignMnemonic(String option, Action action) { + private static void assignMnemonic(@NotNull String option, Action action) { int mnemoPos = option.indexOf("&"); if (mnemoPos >= 0 && mnemoPos < option.length() - 2) { String mnemoChar = option.substring(mnemoPos + 1, mnemoPos + 2).trim(); if (mnemoChar.length() == 1) { - action.putValue(Action.MNEMONIC_KEY, new Integer(mnemoChar.charAt(0))); + action.putValue(Action.MNEMONIC_KEY, Integer.valueOf(mnemoChar.charAt(0))); } } } @@ -1237,10 +1400,12 @@ public class Messages { configureMessagePaneUi(messageComponent, ""); } + @NotNull public static JTextPane configureMessagePaneUi(JTextPane messageComponent, String message) { return configureMessagePaneUi(messageComponent, message, true); } + @NotNull public static JTextPane configureMessagePaneUi(JTextPane messageComponent, String message, final boolean addBrowserHyperlinkListener) { @@ -1275,10 +1440,10 @@ public class Messages { protected static class TwoStepConfirmationDialog extends MessageDialog { private JCheckBox myCheckBox; private final String myCheckboxText; - private boolean myChecked; - private PairFunction myExitFunc; + private final boolean myChecked; + private final PairFunction myExitFunc; - public TwoStepConfirmationDialog(String message, String title, String[] options, String checkboxText, boolean checked, final int defaultOptionInxed, + public TwoStepConfirmationDialog(String message, String title, @NotNull String[] options, String checkboxText, boolean checked, final int defaultOptionInxed, final int focusedOptionIndex, Icon icon, @Nullable final PairFunction exitFunc) { myCheckboxText = checkboxText; myChecked = checked; @@ -1348,7 +1513,7 @@ public class Messages { @Nullable Icon icon, @Nullable String initialValue, @Nullable InputValidator validator, - String[] options, + @NotNull String[] options, int defaultOption) { super(project, message, title, options, defaultOption, icon, true); myValidator = validator; @@ -1495,9 +1660,7 @@ public class Messages { if (getExitCode() == 0) { return myField.getText().trim(); } - else { - return null; - } + return null; } } @@ -1508,7 +1671,7 @@ public class Messages { @Nullable Icon icon, @Nullable String initialValue, @Nullable InputValidator validator, - String[] options, + @NotNull String[] options, int defaultOption) { super(project, message, title, icon, initialValue, validator, options, defaultOption); } @@ -1592,7 +1755,7 @@ public class Messages { @Nullable Icon icon, String[] values, String initialValue, - String[] options, + @NotNull String[] options, int defaultOption) { super(project, message, title, options, defaultOption, icon, true); myComboBox.setModel(new DefaultComboBoxModel(values)); @@ -1708,18 +1871,14 @@ public class Messages { if (getExitCode() == 0) { return myComboBox.getSelectedItem().toString(); } - else { - return null; - } + return null; } public int getSelectedIndex() { if (getExitCode() == 0) { return myComboBox.getSelectedIndex(); } - else { - return -1; - } + return -1; } public JComboBox getComboBox() { diff --git a/platform/platform-api/src/com/intellij/ui/mac/MacMessages.java b/platform/platform-api/src/com/intellij/ui/mac/MacMessages.java index 34a27eaf9417..03a390cb1b9a 100644 --- a/platform/platform-api/src/com/intellij/ui/mac/MacMessages.java +++ b/platform/platform-api/src/com/intellij/ui/mac/MacMessages.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -18,7 +18,7 @@ package com.intellij.ui.mac; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; -import org.intellij.lang.annotations.MagicConstant; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.*; @@ -27,9 +27,10 @@ import java.awt.*; * @author pegov */ public abstract class MacMessages { - public abstract int showYesNoCancelDialog(String title, + @Messages.YesNoCancelResult + public abstract int showYesNoCancelDialog(@NotNull String title, String message, - String defaultButton, + @NotNull String defaultButton, String alternateButton, String otherButton, @Nullable Window window, @@ -52,20 +53,29 @@ public abstract class MacMessages { * http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSAlert_Class/Reference/Reference.html * * Please, note that Cancel is supposed to be the last button! + * + * @return number of button pressed: from 0 up to buttons.length-1 inclusive, or -1 for Cancel */ - public abstract int showMessageDialog(String title, String message, String[] buttons, boolean errorStyle, - @Nullable Window window, int defaultOptionIndex, int focusedOptionIndex, - @Nullable DialogWrapper.DoNotAskOption doNotAskDialogOption); + public abstract int showMessageDialog(@NotNull String title, String message, @NotNull String[] buttons, boolean errorStyle, + @Nullable Window window, int defaultOptionIndex, int focusedOptionIndex, + @Nullable DialogWrapper.DoNotAskOption doNotAskDialogOption); - public abstract void showOkMessageDialog(String title, String message, String okText, @Nullable Window window); + public abstract void showOkMessageDialog(@NotNull String title, String message, @NotNull String okText, @Nullable Window window); - public abstract void showOkMessageDialog(String title, String message, String okText); + public abstract void showOkMessageDialog(@NotNull String title, String message, @NotNull String okText); - public abstract int showYesNoDialog(String title, String message, String yesButton, String noButton, @Nullable Window window); + /** + * @return {@link Messages#YES} if user pressed "Yes" or {@link Messages#NO} if user pressed "No" button. + */ + @Messages.YesNoResult + public abstract int showYesNoDialog(@NotNull String title, String message, @NotNull String yesButton, @NotNull String noButton, @Nullable Window window); - @MagicConstant(intValues = {Messages.YES, Messages.NO}) - public abstract int showYesNoDialog(String title, String message, String yesButton, String noButton, @Nullable Window window, + /** + * @return {@link Messages#YES} if user pressed "Yes" or {@link Messages#NO} if user pressed "No" button. + */ + @Messages.YesNoResult + public abstract int showYesNoDialog(@NotNull String title, String message, @NotNull String yesButton, @NotNull String noButton, @Nullable Window window, @Nullable DialogWrapper.DoNotAskOption doNotAskDialogOption); - public abstract void showErrorDialog(String title, String message, String okButton, @Nullable Window window); + public abstract void showErrorDialog(@NotNull String title, String message, @NotNull String okButton, @Nullable Window window); } \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/ui/mac/MacMessagesImpl.java b/platform/platform-impl/src/com/intellij/ui/mac/MacMessagesImpl.java index 01fd7dfbcd4f..de16a2d78daf 100644 --- a/platform/platform-impl/src/com/intellij/ui/mac/MacMessagesImpl.java +++ b/platform/platform-impl/src/com/intellij/ui/mac/MacMessagesImpl.java @@ -27,7 +27,6 @@ import com.intellij.ui.mac.foundation.ID; import com.intellij.ui.mac.foundation.MacUtil; import com.intellij.util.ui.UIUtil; import com.sun.jna.Callback; -import org.intellij.lang.annotations.MagicConstant; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -36,6 +35,7 @@ import java.awt.*; import java.awt.event.InputEvent; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.Map; import static com.intellij.ui.mac.foundation.Foundation.*; @@ -51,22 +51,21 @@ public class MacMessagesImpl extends MacMessages { myReturnCode = returnCode; mySuppress = suppress; } - int myReturnCode; - boolean mySuppress; + private final int myReturnCode; + private final boolean mySuppress; } - private static final HashMap resultsFromDocumentRoot = new HashMap (); - private static final HashMap> queuesFromDocumentRoot = - new HashMap>(); + private static final Map resultsFromDocumentRoot = new HashMap (); + private static final Map> queuesFromDocumentRoot = new HashMap>(); private static final Callback SHEET_DID_END = new Callback() { + @SuppressWarnings("UnusedDeclaration") public void callback(ID self, String selector, ID alert, ID returnCode, ID contextInfo) { synchronized (lock) { Window documentRoot = windowFromId.get(contextInfo.longValue()); processResult(documentRoot); ID suppressState = invoke(invoke(alert, "suppressionButton"), "state"); - resultsFromDocumentRoot.put(documentRoot, new MessageResult(returnCode.intValue(), - suppressState.intValue() == 1)); + resultsFromDocumentRoot.put(documentRoot, new MessageResult(returnCode.intValue(), suppressState.intValue() == 1)); queuesFromDocumentRoot.get(windowFromId.get(contextInfo.longValue())).runFromQueue(); } cfRelease(self); @@ -74,6 +73,7 @@ public class MacMessagesImpl extends MacMessages { }; private static final Callback VARIABLE_BUTTONS_SHEET_PANEL = new Callback() { + @SuppressWarnings("UnusedDeclaration") public void callback(ID self, String selector, ID params) { ID title = invoke(params, "objectAtIndex:", 0); ID message = invoke(params, "objectAtIndex:", 1); @@ -130,6 +130,7 @@ public class MacMessagesImpl extends MacMessages { }; private static final Callback SIMPLE_SHEET_PANEL = new Callback() { + @SuppressWarnings("UnusedDeclaration") public void callback(ID self, String selector, ID params) { ID title = invoke(params, "objectAtIndex:", 0); ID defaultText = invoke(params, "objectAtIndex:", 1); @@ -214,36 +215,38 @@ public class MacMessagesImpl extends MacMessages { } @Override - public void showOkMessageDialog(String title, String message, String okText, @Nullable Window window) { - showMessageDialog(title, okText, null, null, message, window); + public void showOkMessageDialog(@NotNull String title, String message, @NotNull String okText, @Nullable Window window) { + showAlertDialog(title, okText, null, null, message, window); } @Override - public void showOkMessageDialog(String title, String message, String okText) { - showMessageDialog(title, okText, null, null, message, null); + public void showOkMessageDialog(@NotNull String title, String message, @NotNull String okText) { + showAlertDialog(title, okText, null, null, message, null); } @Override - public int showYesNoDialog(String title, String message, String yesButton, String noButton, @Nullable Window window) { - return showMessageDialog(title, yesButton, null, noButton, message, window); + @Messages.YesNoResult + public int showYesNoDialog(@NotNull String title, String message, @NotNull String yesButton, @NotNull String noButton, @Nullable Window window) { + return showAlertDialog(title, yesButton, null, noButton, message, window) == Messages.YES ? Messages.YES : Messages.NO; } @Override - public int showYesNoDialog(String title, String message, String yesButton, String noButton, @Nullable Window window, + @Messages.YesNoResult + public int showYesNoDialog(@NotNull String title, String message, @NotNull String yesButton, @NotNull String noButton, @Nullable Window window, @Nullable DialogWrapper.DoNotAskOption doNotAskDialogOption) { - //noinspection MagicConstant - return showAlertDialog(title, yesButton, null, noButton, message, window, false, doNotAskDialogOption); + return showAlertDialog(title, yesButton, null, noButton, message, window, false, doNotAskDialogOption) == Messages.YES ? Messages.YES : Messages.NO; } @Override - public void showErrorDialog(String title, String message, String okButton, @Nullable Window window) { + public void showErrorDialog(@NotNull String title, String message, @NotNull String okButton, @Nullable Window window) { showAlertDialog(title, okButton, null, null, message, window, true, null); } @Override - public int showYesNoCancelDialog(String title, + @Messages.YesNoCancelResult + public int showYesNoCancelDialog(@NotNull String title, String message, - String defaultButton, + @NotNull String defaultButton, String alternateButton, String otherButton, Window window, @@ -251,11 +254,11 @@ public class MacMessagesImpl extends MacMessages { return showAlertDialog(title, defaultButton, alternateButton, otherButton, message, window, false, doNotAskOption); } - final private static Object lock = new Object(); + private static final Object lock = new Object(); - final private static HashMap blockedDocumentRoots = new HashMap(); + private static final HashMap blockedDocumentRoots = new HashMap(); - final private static HashMap windowFromId = new HashMap(); + private static final HashMap windowFromId = new HashMap(); public static void pumpEventsDocumentExclusively (Window documentRoot) { @@ -263,10 +266,9 @@ public class MacMessagesImpl extends MacMessages { EventQueue theQueue = documentRoot.getToolkit().getSystemEventQueue(); - AWTEvent event; do { try { - event = theQueue.getNextEvent(); + AWTEvent event = theQueue.getNextEvent(); boolean eventOk = true; if (event instanceof InputEvent) { final Object s = event.getSource(); @@ -304,7 +306,7 @@ public class MacMessagesImpl extends MacMessages { private static Window findDocumentRoot (final Component c) { if (c == null) return null; - Window w = (c instanceof Window) ? (Window)c : getContainingWindow(c); + Window w = c instanceof Window ? (Window)c : getContainingWindow(c); synchronized (c.getTreeLock()) { while (w.getOwner() != null) { w = w.getOwner(); @@ -358,15 +360,15 @@ public class MacMessagesImpl extends MacMessages { private static class DialogParamsWrapper { private ID window = null; - private HashMap params = null; - private DialogType dialogType = null; + private final Map params; + private final DialogType dialogType; private enum DialogType { alert, message } - private DialogParamsWrapper(@NotNull DialogType t, @NotNull HashMap p) { + private DialogParamsWrapper(@NotNull DialogType t, @NotNull Map p) { dialogType = t; params = p; } @@ -382,20 +384,18 @@ public class MacMessagesImpl extends MacMessages { ID paramsAsID = null; switch (dialogType) { - case alert: { + case alert: paramsAsID = getParamsForAlertDialog(params); break; - } - case message: { + case message: paramsAsID = getParamsForMessageDialog(params); break; - } } return paramsAsID; } - private static ID getParamsForAlertDialog(HashMap params) { + private static ID getParamsForAlertDialog(@NotNull Map params) { return invoke("NSArray", "arrayWithObjects:", params.get(COMMON_DIALOG_PARAM_TYPE.title), params.get(ALERT_DIALOG_PARAM_TYPE.defaultText), @@ -410,7 +410,7 @@ public class MacMessagesImpl extends MacMessages { null); } - private static ID getParamsForMessageDialog(HashMap params) { + private static ID getParamsForMessageDialog(@NotNull Map params) { return invoke("NSArray", "arrayWithObjects:", params.get(COMMON_DIALOG_PARAM_TYPE.title), params.get(COMMON_DIALOG_PARAM_TYPE.message), @@ -426,21 +426,20 @@ public class MacMessagesImpl extends MacMessages { } } - @MagicConstant(intValues = {Messages.YES, Messages.NO, Messages.CANCEL, Messages.OK, OPERATION_CANCELED}) - public static int showAlertDialog(final String title, - final String defaultText, + @Messages.YesNoCancelResult + public static int showAlertDialog(@NotNull String title, + @NotNull String defaultText, @Nullable final String alternateText, @Nullable final String otherText, final String message, - @Nullable Window window , + @Nullable Window window, final boolean errorStyle, @Nullable final DialogWrapper.DoNotAskOption doNotAskDialogOption) { - HashMap params = new HashMap (); + Map params = new HashMap (); ID pool = invoke(invoke("NSAutoreleasePool", "alloc"), "init"); try { - params.put(COMMON_DIALOG_PARAM_TYPE.title, nsString(title)); params.put(ALERT_DIALOG_PARAM_TYPE.defaultText, nsString(UIUtil.removeMnemonic(defaultText))); params.put(ALERT_DIALOG_PARAM_TYPE.alternateText, nsString(otherText == null ? "-1" : UIUtil.removeMnemonic(otherText))); @@ -457,12 +456,10 @@ public class MacMessagesImpl extends MacMessages { MessageResult result = resultsFromDocumentRoot.remove(showDialog(window, "showSheet:", new DialogParamsWrapper(DialogParamsWrapper.DialogType.alert, params))); - Integer convertedResult = convertReturnCodeFromNativeAlertDialog(result.myReturnCode, alternateText); - - boolean operationCanceled = (alternateText == null && convertedResult == 1) - || (alternateText != null && convertedResult == 2); + int convertedResult = convertReturnCodeFromNativeAlertDialog(result.myReturnCode, alternateText); if (doNotAskDialogOption != null && doNotAskDialogOption.canBeHidden()) { + boolean operationCanceled = convertedResult == Messages.CANCEL; if (!operationCanceled || doNotAskDialogOption.shouldSaveOptionsOnCancel()) { doNotAskDialogOption.setToBeShown(!result.mySuppress, convertedResult); } @@ -475,18 +472,15 @@ public class MacMessagesImpl extends MacMessages { } } - private final static int OPERATION_CANCELED = 444; - - public int showMessageDialog(final String title, + @Override + public int showMessageDialog(@NotNull final String title, final String message, - final String[] buttons, + @NotNull final String[] buttons, final boolean errorStyle, @Nullable Window window, final int defaultOptionIndex, final int focusedOptionIndex, - @Nullable final DialogWrapper.DoNotAskOption doNotAskDialogOption) - { - + @Nullable final DialogWrapper.DoNotAskOption doNotAskDialogOption) { ID pool = invoke(invoke("NSAutoreleasePool", "alloc"), "init"); try { final ID buttonsArray = invoke("NSMutableArray", "array"); @@ -495,7 +489,7 @@ public class MacMessagesImpl extends MacMessages { invoke(buttonsArray, "addObject:", s1); } - HashMap params = new HashMap (); + Map params = new HashMap(); params.put(COMMON_DIALOG_PARAM_TYPE.title, nsString(title)); // replace % -> %% to avoid formatted parameters (causes SIGTERM) @@ -532,8 +526,7 @@ public class MacMessagesImpl extends MacMessages { } //title, message, errorStyle, window, paramsArray, doNotAskDialogOption, "showVariableButtonsSheet:" - private static Window showDialog(@Nullable Window window, - final String methodName, DialogParamsWrapper paramsWrapper) { + private static Window showDialog(@Nullable Window window, final String methodName, DialogParamsWrapper paramsWrapper) { Window foremostWindow = getForemostWindow(window); @@ -582,8 +575,8 @@ public class MacMessagesImpl extends MacMessages { return windowTitle; } - @MagicConstant(intValues = {Messages.YES, Messages.NO, Messages.CANCEL, Messages.OK, OPERATION_CANCELED}) - private static int convertReturnCodeFromNativeAlertDialog(Integer returnCode, String alternateText) { + @Messages.YesNoCancelResult + private static int convertReturnCodeFromNativeAlertDialog(int returnCode, String alternateText) { // DEFAULT = 1 // ALTERNATE = 0 // OTHER = -1 (cancel) @@ -597,10 +590,6 @@ public class MacMessagesImpl extends MacMessages { cancelCode = Messages.CANCEL; - if (returnCode == null) { - returnCode = Messages.CANCEL; - } - switch (returnCode) { case 1: code = Messages.YES; @@ -620,10 +609,6 @@ public class MacMessagesImpl extends MacMessages { cancelCode = 1; - if (returnCode == null) { - returnCode = -1; - } - switch (returnCode) { case 1: code = Messages.YES; @@ -635,7 +620,11 @@ public class MacMessagesImpl extends MacMessages { } } - return cancelCode == code ? OPERATION_CANCELED : code; + if (cancelCode == code) { + code = Messages.CANCEL; + } + LOG.assertTrue(code == Messages.YES || code == Messages.NO || code == Messages.CANCEL, code); + return code; } private static void runOrPostponeForWindow(Window documentRoot, Runnable task) { @@ -707,12 +696,13 @@ public class MacMessagesImpl extends MacMessages { return findDocumentRoot(window); } - public static int showMessageDialog(String title, - String okText, - @Nullable String alternateText, - @Nullable String cancelText, - String message, - @Nullable Window window) { + @Messages.YesNoCancelResult + private static int showAlertDialog(@NotNull String title, + @NotNull String okText, + @Nullable String alternateText, + @Nullable String cancelText, + String message, + @Nullable Window window) { return showAlertDialog(title, okText, alternateText, cancelText, message, window, false, null); } } diff --git a/platform/usageView/src/com/intellij/usages/UsageLimitUtil.java b/platform/usageView/src/com/intellij/usages/UsageLimitUtil.java index 4477fe6d9d83..85bf70da46f1 100644 --- a/platform/usageView/src/com/intellij/usages/UsageLimitUtil.java +++ b/platform/usageView/src/com/intellij/usages/UsageLimitUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -42,13 +42,16 @@ public class UsageLimitUtil { } @NotNull - public static Result showTooManyUsagesWarning(@NotNull Project project, @NotNull String message) { - String[] buttons = {UsageViewBundle.message("button.text.continue"), UsageViewBundle.message("button.text.abort")}; - int r = showMessage(project, message, UsageViewBundle.message("find.excessive.usages.title"), buttons); - if (r == 1) { - return Result.ABORT; - } - return Result.CONTINUE; + public static Result showTooManyUsagesWarning(@NotNull final Project project, @NotNull final String message) { + final String[] buttons = {UsageViewBundle.message("button.text.continue"), UsageViewBundle.message("button.text.abort")}; + int result = runOrInvokeAndWait(new Computable() { + @Override + public Integer compute() { + return Messages.showOkCancelDialog(project, message, UsageViewBundle.message("find.excessive.usages.title"), buttons[0], buttons[1], + Messages.getWarningIcon()); + } + }); + return result == Messages.OK ? Result.CONTINUE : Result.ABORT; } private static int runOrInvokeAndWait(final Computable f) { @@ -67,13 +70,4 @@ public class UsageLimitUtil { return answer[0]; } - - private static int showMessage(final Project project, final String message, final String title, final String[] buttons) { - return runOrInvokeAndWait(new Computable() { - @Override - public Integer compute() { - return Messages.showOkCancelDialog(project, message, title, buttons[0], buttons[1], Messages.getWarningIcon()); - } - }); - } } diff --git a/platform/util/src/com/intellij/ui/mac/foundation/Foundation.java b/platform/util/src/com/intellij/ui/mac/foundation/Foundation.java index 3b2e8cf98c3d..03494f4902ba 100644 --- a/platform/util/src/com/intellij/ui/mac/foundation/Foundation.java +++ b/platform/util/src/com/intellij/ui/mac/foundation/Foundation.java @@ -144,7 +144,7 @@ public class Foundation { return isPackageAtPath(file.getPath()); } - public static ID nsString(String s) { + public static ID nsString(@NotNull String s) { // Use a byte[] rather than letting jna do the String -> char* marshalling itself. // Turns out about 10% quicker for long strings. try { diff --git a/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java b/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java index ae9d267a18ae..b11098edf492 100644 --- a/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java +++ b/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -301,7 +301,7 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory { final int choice = Messages.showOkCancelDialog(myPanel.getComponent(), XmlStringUtil.wrapInHtml(message), title, "Cancel", "Commit", Messages.getWarningIcon()); - if (choice == 1) { + if (choice != Messages.OK) { return ReturnResult.COMMIT; } else { return ReturnResult.CLOSE_WINDOW; diff --git a/plugins/xpath/xpath-view/src/org/intellij/plugins/xpathView/XPathEvalAction.java b/plugins/xpath/xpath-view/src/org/intellij/plugins/xpathView/XPathEvalAction.java index 83a52404abea..3bbc8b85308f 100644 --- a/plugins/xpath/xpath-view/src/org/intellij/plugins/xpathView/XPathEvalAction.java +++ b/plugins/xpath/xpath-view/src/org/intellij/plugins/xpathView/XPathEvalAction.java @@ -21,7 +21,6 @@ import com.intellij.lang.Language; import com.intellij.navigation.ItemPresentation; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; @@ -242,7 +241,7 @@ public class XPathEvalAction extends XPathAction { } } else { return Messages.showOkCancelDialog(project, "Sorry, your expression did not return any result", "XPath Result", - "OK", "Edit Expression", Messages.getInformationIcon()) == 1; + "OK", "Edit Expression", Messages.getInformationIcon()) != Messages.OK; } } else if (result instanceof String) { Messages.showMessageDialog("'" + result.toString() + "'", "XPath result (String)", Messages.getInformationIcon());