/**
   * Displays an information bubble that alerts the user that the attribute specified in the
   * operator parameters was not found. The bubble is located at the operator and the process view
   * will change to said operator. This method is used after the error occurred during process
   * execution.
   *
   * @param error the error containing all the information about the operator, the parameter and the
   *     name of the attribute which was not found
   * @param i18nKey the i18n key which defines the title, text and button label for the bubble.
   *     Format is "gui.bubble.{i18nKey}.title", "gui.bubble.{i18nKey}.body" and
   *     "gui.bubble.{i18nKey}.button.label".
   * @param isError if {@code true}, an error bubble will be shown; otherwise a warning bubble is
   *     displayed
   * @param arguments optional i18n arguments
   * @return the {@link OperatorInfoBubble} instance, never {@code null}
   */
  private static OperatorInfoBubble displayAttributeNotFoundParameterInformation(
      final AttributeNotFoundError error,
      final boolean isError,
      final String i18nKey,
      final Object... arguments) {
    final Operator op = error.getOperator();
    final ParameterType param = op.getParameterType(error.getKey());
    final JButton ackButton =
        new JButton(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.label", arguments));
    ackButton.setToolTipText(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.tip"));

    String decoratorKey =
        param instanceof CombinedParameterType || param instanceof ParameterTypeAttributes
            ? "attributes_not_found_decoration"
            : "attribute_not_found_decoration";

    ParameterErrorBubbleBuilder builder =
        new ParameterErrorBubbleBuilder(
            RapidMinerGUI.getMainFrame(), op, param, decoratorKey, i18nKey, arguments);
    final ParameterErrorInfoBubble attributeNotFoundParameterBubble =
        builder
            .setHideOnDisable(true)
            .setAlignment(AlignedSide.BOTTOM)
            .setStyle(isError ? BubbleStyle.ERROR : BubbleStyle.WARNING)
            .setEnsureVisible(true)
            .hideCloseButton()
            .setHideOnProcessRun(true)
            .setAdditionalComponents(new JComponent[] {ackButton})
            .build();

    ackButton.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            attributeNotFoundParameterBubble.killBubble(true);
          }
        });

    attributeNotFoundParameterBubble.setVisible(true);
    return attributeNotFoundParameterBubble;
  }
  /**
   * Displays an information bubble that alerts the user that a mandatory operator parameter was not
   * set and had no default value. The bubble is located at the operator and the process view will
   * change to said operator. This method is used after the error occurred during process execution.
   *
   * @param op the operator for which to display the error
   * @param param the parameter for which to display the error
   * @param i18nKey the i18n key which defines the title, text and button label for the bubble.
   *     Format is "gui.bubble.{i18nKey}.title", "gui.bubble.{i18nKey}.body" and
   *     "gui.bubble.{i18nKey}.button.label".
   * @param isError if {@code true}, an error bubble will be shown; otherwise a warning bubble is
   *     displayed
   * @param arguments optional i18n arguments
   * @return the {@link OperatorInfoBubble} instance, never {@code null}
   */
  private static OperatorInfoBubble displayMissingMandatoryParameterInformation(
      final Operator op,
      final ParameterType param,
      final boolean isError,
      final String i18nKey,
      final Object... arguments) {
    final JButton ackButton =
        new JButton(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.label", arguments));
    ackButton.setToolTipText(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.tip"));

    ParameterErrorBubbleBuilder builder =
        new ParameterErrorBubbleBuilder(
            RapidMinerGUI.getMainFrame(),
            op,
            param,
            "mandatory_parameter_decoration",
            i18nKey,
            arguments);
    final OperatorInfoBubble missingParameterBubble =
        builder
            .setHideOnDisable(true)
            .setAlignment(AlignedSide.BOTTOM)
            .setStyle(isError ? BubbleStyle.ERROR : BubbleStyle.WARNING)
            .setEnsureVisible(true)
            .hideCloseButton()
            .setHideOnProcessRun(true)
            .setAdditionalComponents(new JComponent[] {ackButton})
            .build();

    ackButton.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            missingParameterBubble.killBubble(true);
          }
        });

    missingParameterBubble.setVisible(true);
    return missingParameterBubble;
  }
  /**
   * Displays an information bubble pointing to an operator to indicate a {@link ParameterError} was
   * thrown by that operator. It will also select the operator and highlight the parameter in
   * question.
   *
   * @param error the error instance
   * @param op the operator for which to display the error
   * @param param the parameter for which to display the error
   * @param i18nKey the i18n key which defines the title, text and button label for the bubble.
   *     Format is "gui.bubble.{i18nKey}.title", "gui.bubble.{i18nKey}.body" and
   *     "gui.bubble.{i18nKey}.button.label".
   * @return the {@link OperatorInfoBubble} instance, never {@code null}
   */
  private static OperatorInfoBubble displayGenericParameterError(
      final ParameterError error,
      final Operator op,
      final ParameterType param,
      final String i18nKey) {
    final JButton ackButton =
        new JButton(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.label"));
    ackButton.setToolTipText(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.tip"));

    final BubbleDelegator bubbleDelegator = new BubbleDelegator();
    final String message = removeTerminationCharacters(error.getMessage());
    final JPanel linkPanel = new JPanel();
    LinkButton showDetailsButton =
        new LinkButton(
            new ResourceAction(i18nKey + ".button_show_details") {

              private static final long serialVersionUID = 1L;

              @Override
              public void actionPerformed(final ActionEvent e) {
                BubbleWindow bubble = bubbleDelegator.getBubble();
                if (bubble != null) {
                  String text =
                      I18N.getMessage(
                          I18N.getGUIBundle(),
                          "gui.bubble." + i18nKey + ".body",
                          message,
                          error.getDetails());
                  bubble.setMainText(text);

                  linkPanel.removeAll();
                  bubble.pack();
                }
              }
            });
    showDetailsButton.setToolTipText(
        I18N.getGUIMessage("gui.action." + i18nKey + ".button_show_details.tip"));
    linkPanel.add(showDetailsButton);

    ParameterErrorBubbleBuilder builder =
        new ParameterErrorBubbleBuilder(
            RapidMinerGUI.getMainFrame(),
            error.getOperator(),
            param,
            "generic_parameter_decoration",
            i18nKey,
            message,
            "");
    // if no operator or root operator, show in middle, otherwise below
    AlignedSide prefSide =
        error.getOperator() == null || error.getOperator() instanceof ProcessRootOperator
            ? AlignedSide.MIDDLE
            : AlignedSide.BOTTOM;
    final OperatorInfoBubble userErrorBubble =
        builder
            .setHideOnDisable(true)
            .setAlignment(prefSide)
            .setStyle(BubbleStyle.ERROR)
            .setEnsureVisible(true)
            .hideCloseButton()
            .setHideOnProcessRun(true)
            .setAdditionalComponents(new JComponent[] {ackButton, linkPanel})
            .build();
    ackButton.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            userErrorBubble.killBubble(true);
          }
        });
    bubbleDelegator.setBubbleWindow(userErrorBubble);

    userErrorBubble.setVisible(true);
    return userErrorBubble;
  }
  /**
   * Displays a warning bubble that alerts the user that a mandatory parameter of an operator has no
   * value and no default value. The bubble is located at the operator and the process view will
   * change to said port.
   *
   * @param op the operator for which to display the warning
   * @param param the parameter for which to display the warning
   * @param i18nKey the i18n key which defines the title, text and button label for the bubble.
   *     Format is "gui.bubble.{i18nKey}.title", "gui.bubble.{i18nKey}.body" and
   *     "gui.bubble.{i18nKey}.button.label".
   * @param isError if {@code true}, an error bubble will be shown; otherwise a warning bubble is
   *     displayed
   * @param arguments optional i18n arguments
   * @return the {@link OperatorInfoBubble} instance, never {@code null}
   */
  private static OperatorInfoBubble displayPrecheckMissingParameterWarning(
      final Operator op,
      final ParameterType param,
      final boolean isError,
      final String i18nKey,
      final Object... arguments) {
    final BubbleDelegator bubbleDelegator = new BubbleDelegator();
    ResourceAction runAnywayAction =
        new ResourceAction(i18nKey + ".button_run_anyway", "F11") {

          private static final long serialVersionUID = 1L;

          @Override
          public void actionPerformed(final ActionEvent e) {
            BubbleWindow bubble = bubbleDelegator.getBubble();
            if (bubble != null) {
              bubble.killBubble(true);
            }

            // run process without checking for problems
            RapidMinerGUI.getMainFrame().runProcess(false);
          }
        };
    LinkButton runAnywayButton = new LinkButton(runAnywayAction);
    runAnywayButton.setToolTipText(
        I18N.getGUIMessage("gui.bubble." + i18nKey + ".button_run_anyway.tip"));
    runAnywayButton.registerKeyboardAction(
        runAnywayAction,
        KeyStroke.getKeyStroke(KeyEvent.VK_F11, 0),
        JComponent.WHEN_IN_FOCUSED_WINDOW);

    final JButton ackButton =
        new JButton(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.label", arguments));
    ackButton.setToolTipText(I18N.getGUIMessage("gui.bubble." + i18nKey + ".button.tip"));

    ParameterErrorBubbleBuilder builder =
        new ParameterErrorBubbleBuilder(
            RapidMinerGUI.getMainFrame(),
            op,
            param,
            "mandatory_parameter_decoration",
            i18nKey,
            arguments);
    final OperatorInfoBubble missingParameterBubble =
        builder
            .setHideOnDisable(true)
            .setAlignment(AlignedSide.BOTTOM)
            .setStyle(isError ? BubbleStyle.ERROR : BubbleStyle.WARNING)
            .setEnsureVisible(true)
            .hideCloseButton()
            .setHideOnProcessRun(true)
            .setAdditionalComponents(new JComponent[] {ackButton, runAnywayButton})
            .build();
    ackButton.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            missingParameterBubble.killBubble(true);
          }
        });
    bubbleDelegator.setBubbleWindow(missingParameterBubble);

    missingParameterBubble.setVisible(true);
    return missingParameterBubble;
  }