/**
   * Displays the best machting error bubble depending on the given {@link UserError}. If no
   * explicit match can be found, displays a generic error bubble.
   *
   * @param error the error in question, must not be {@code null}
   * @return the bubble instance, never {@code null}
   */
  public static BubbleWindow displayBubbleForUserError(final UserError error) {
    if (error == null) {
      throw new IllegalArgumentException("userError must not be null!");
    }

    if (error instanceof PortUserError) {
      PortUserError userError = (PortUserError) error;
      if (userError.getCode() == 149) {
        // for "no data" errors we display an error bubble instead of a dialog
        return displayInputPortNoDataInformation(userError.getPort());
      } else if (userError.getCode() == 156) {
        return displayInputPortWrongTypeInformation(
            userError.getPort(), userError.getExpectedType(), userError.getActualType());
      } else {
        return displayGenericPortError(userError);
      }
    } else if (error.getClass().equals(UndefinedParameterError.class)) {
      UndefinedParameterError userError = (UndefinedParameterError) error;
      if (userError.getCode() == 205 || userError.getCode() == 217) {
        // for "missing mandatory parameter" errors we display an error bubble
        // instead of a dialog
        Operator op = userError.getOperator();
        ParameterType param = op != null ? op.getParameterType(userError.getKey()) : null;
        if (op != null && param != null) {
          return displayMissingMandatoryParameterInformation(op, param);
        } else {
          return displayGenericUserError(error);
        }
      } else {
        Operator op = userError.getOperator();
        ParameterType param = op != null ? op.getParameterType(userError.getKey()) : null;
        if (op != null && param != null) {
          return displayMissingMandatoryParameterInformation(op, param);
        } else {
          return displayGenericUserError(error);
        }
      }
    } else if (error instanceof ParameterError) {
      ParameterError userError = (ParameterError) error;
      Operator op = userError.getOperator();
      ParameterType param = op != null ? op.getParameterType(userError.getKey()) : null;
      if (op != null && param != null) {
        return displayGenericParameterError(userError);
      } else {
        return displayGenericUserError(error);
      }
    } else if (error instanceof AttributeNotFoundError) {
      AttributeNotFoundError userError = (AttributeNotFoundError) error;
      Operator op = userError.getOperator();
      ParameterType param = op != null ? op.getParameterType(userError.getKey()) : null;
      if (op != null && param != null) {
        return displayAttributeNotFoundParameterInformation(userError);
      } else {
        return displayGenericUserError(error);
      }
    } else if (error instanceof ProcessExecutionUserErrorError) {
      ProcessExecutionUserErrorError userError = (ProcessExecutionUserErrorError) error;
      if (userError.getUserError() != null && userError.getUserError().getOperator() != null) {
        return displayUserErrorInExecutedProcess(userError);
      } else {
        return displayGenericUserError(error);
      }
    } else {
      return displayGenericUserError(error);
    }
  }
  /**
   * Displays an information bubble pointing to a port to indicate a {@link PortUserError} was
   * thrown.
   *
   * @param error the error instance
   * @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 PortInfoBubble} instance, never {@code null}
   */
  private static PortInfoBubble displayGenericPortError(
      final PortUserError error, 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);

    // input ports (located left) show the "hook" of the bubble on the left and vice versa
    AlignedSide prefSide =
        error.getPort() instanceof InputPort ? AlignedSide.LEFT : AlignedSide.RIGHT;
    PortBubbleBuilder builder =
        new PortBubbleBuilder(RapidMinerGUI.getMainFrame(), error.getPort(), i18nKey, message, "");
    final PortInfoBubble portErrorBubble =
        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) {
            portErrorBubble.killBubble(true);
          }
        });
    bubbleDelegator.setBubbleWindow(portErrorBubble);

    portErrorBubble.setVisible(true);
    return portErrorBubble;
  }