/*
   * (non-Javadoc)
   *
   * @see
   * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent
   * )
   */
  @Override
  public void actionPerformed(ActionEvent actionEvent) {

    try {

      // Asks the the file to the user
      String absolutePath =
          AcideFileManager.getInstance()
              .askForFile(
                  AcideFileOperation.SAVE,
                  AcideFileTarget.FILES,
                  AcideFileType.FILE,
                  "./configuration/menu/",
                  new AcideFileExtensionFilterManager(
                      new String[] {"menuConfig"},
                      AcideLanguageManager.getInstance().getLabels().getString("s287")));

      if (absolutePath != null) {

        // If it does not contain the extension
        if (!absolutePath.endsWith(".menuConfig"))

          // Adds it
          absolutePath += ".menuConfig";

        // Gets the ACIDE - A Configurable IDE current menu configuration
        String currentMenuConfiguration =
            AcideResourceManager.getInstance().getProperty("currentMenuConfiguration");

        // Copies the files
        AcideByteFileManager.getInstance().copy(currentMenuConfiguration, absolutePath);

        // Updates the ACIDE - A Configurable IDE current menu
        // configuration
        AcideResourceManager.getInstance().setProperty("currentMenuConfiguration", absolutePath);

        // Disables the save menu item
        AcideMainWindow.getInstance()
            .getMenu()
            .getConfigurationMenu()
            .getMenuMenu()
            .getSaveMenuMenuItem()
            .setEnabled(false);

        // The changes are saved
        AcideMenuConfigurationWindow.setChangesAreSaved(true);

        // Updates the log
        AcideLog.getLog()
            .info(
                AcideLanguageManager.getInstance().getLabels().getString("s528")
                    + absolutePath
                    + AcideLanguageManager.getInstance().getLabels().getString("s529"));
      }
    } catch (Exception exception) {

      // Displays an error message
      JOptionPane.showMessageDialog(
          null,
          exception.getMessage(),
          AcideLanguageManager.getInstance().getLabels().getString("s291"),
          JOptionPane.ERROR_MESSAGE);

      // Updates the log
      AcideLog.getLog().error(exception.getMessage());
    }
  }
  /**
   * Searches the wanted string in the text a string.
   *
   * @param currentCaretPosition current caret position.
   * @param wantedString wanted string.
   * @param text text in which the wanted string is searched for.
   * @param isCaseSensitive is case sensitive flag.
   * @param isRegularExpresion is regular expression flag.
   * @param isCompleted is completed flag.
   * @param direction search direction.
   * @return the position of the found coincidence.
   */
  public int search(
      int currentCaretPosition,
      String wantedString,
      String text,
      boolean isCaseSensitive,
      boolean isRegularExpresion,
      boolean isCompleted,
      AcideSearchDirection direction) {

    // If regular expressions are selected
    if (isRegularExpresion) {
      try {
        // If it is not case sensitive
        if (!isCaseSensitive)

          // Compiles the pattern with case insensitive
          _pattern = Pattern.compile(wantedString, Pattern.CASE_INSENSITIVE);
        else

          // Compile the pattern with case sensitive
          _pattern = Pattern.compile(wantedString);

        // Gets the matcher
        _matcher = _pattern.matcher(text);
        _regularExpresion = " ";
      } catch (PatternSyntaxException e) {
        // Updates the log
        AcideLog.getLog().info(AcideLanguageManager.getInstance().getLabels().getString("s2148"));

        AcideSearchWindow.getInstance().setOnTop(false);

        // Displays a message
        JOptionPane.showMessageDialog(
            null, AcideLanguageManager.getInstance().getLabels().getString("s2148"));

        AcideSearchWindow.getInstance().setOnTop(true);

        // Updates the status message in the status bar in the ACIDE
        // - A Configurable IDE main window
        AcideMainWindow.getInstance()
            .getStatusBar()
            .setStatusMessage(AcideLanguageManager.getInstance().getLabels().getString("s2148"));
        return -2;
      }
      switch (direction) {
        case FORWARD:
        case BOTH:
          if (_matcher.find(currentCaretPosition)) {
            _regularExpresion = " ";
            _regularExpresion = _matcher.group(0).toString();
          }
          break;
        case BACKWARD:
        case BOTHBACK:
          int index = 0;
          int limit = currentCaretPosition;
          boolean end = false;
          _regularExpresionList.clear();

          while ((_matcher.find()) && (!end)) {
            _regularExpresionList.add(_matcher.group(0).toString());
            index =
                text.indexOf(_matcher.group(0).toString(), index)
                    + _matcher.group(0).toString().length();

            if (index > limit) {
              end = true;
              _regularExpresionList.remove(_regularExpresionList.size() - 1);
            }
          }

          break;
      }
    }

    switch (direction) {
      case FORWARD:
        return forwardSearch(
            currentCaretPosition,
            wantedString,
            text,
            isCaseSensitive,
            isRegularExpresion,
            isCompleted,
            direction);

      case BACKWARD:
        return backwardSearch(
            currentCaretPosition,
            wantedString,
            text,
            isCaseSensitive,
            isRegularExpresion,
            isCompleted,
            direction);

      case BOTH:
        return bothSearch(
            currentCaretPosition,
            wantedString,
            text,
            isCaseSensitive,
            isRegularExpresion,
            isCompleted,
            direction);

      case BOTHBACK:
        return bothBackSearch(
            currentCaretPosition,
            wantedString,
            text,
            isCaseSensitive,
            isRegularExpresion,
            isCompleted,
            direction);
    }

    return -1;
  }
  /**
   * Updates the ACIDE - A Configurable IDE console menu components visibility with the menu
   * configuration.
   */
  public void updateComponentsVisibility() {

    AcideMenuItemConfiguration nameConfiguration;
    AcideMenuItemConfiguration nameFieldsConfiguration;
    AcideMenuItemConfiguration nameFieldsTypesConfiguration;

    _showDetailsSubmenuConfiguration =
        AcideMenuItemsConfiguration.getInstance()
            .getSubmenu(AcideConfigurationMenu.CONFIGURATION_MENU_NAME)
            .getSubmenu(AcideDatabasePanelMenu.DATABASE_MENU_NAME)
            .getSubmenu(SHOW_DETAILS_NAME);

    // Sets the name menu item to visible or not visible
    nameConfiguration = _showDetailsSubmenuConfiguration.getItem(SHOW_NAME_NAME);
    _nameMenuItem.setVisible(nameConfiguration.isVisible());

    // Sets the name fields menu item to visible or not visible
    nameFieldsConfiguration = _showDetailsSubmenuConfiguration.getItem(SHOW_NAME_FIELDS_NAME);
    _nameFieldsMenuItem.setVisible(nameFieldsConfiguration.isVisible());

    // Sets the name fields types menu item to visible or not visible
    nameFieldsTypesConfiguration =
        _showDetailsSubmenuConfiguration.getItem(SHOW_NAME_FIELDS_TYPES_NAME);
    _nameFieldsTypesMenuItem.setVisible(nameFieldsTypesConfiguration.isVisible());

    Iterator<AcideMenuObjectConfiguration> it = _insertedObjects.iterator();
    while (it.hasNext()) {
      AcideMenuObjectConfiguration ob = it.next();
      if (ob.isSubmenu()) {
        _insertedMenus.get(ob.getName()).updateComponentsVisibility();
        _insertedMenus.get(ob.getName()).setVisible(ob.isVisible());
      } else {
        _insertedItems.get(ob.getName()).setVisible(ob.isVisible());
      }
    }

    // Sets the console menu to visible or not visible
    _showDetailsSubmenuConfiguration.setVisible(
        _nameMenuItem.isVisible()
            || _nameFieldsMenuItem.isVisible()
            || _nameFieldsTypesMenuItem.isVisible());
    _showDetailsSubmenuConfiguration.setErasable(false);

    try {
      // Save the configuration for the menu that could have been modified
      AcideMenuConfiguration.getInstance()
          .saveMenuConfigurationFile("./configuration/menu/lastModified.menuConfig");

      // Gets the the ACIDE - A Configurable IDE current menu
      // configuration
      String currentMenuConfiguration =
          AcideResourceManager.getInstance().getProperty("currentMenuConfiguration");

      if (!currentMenuConfiguration.endsWith("lastModified.menuConfig")
          && !currentMenuConfiguration.endsWith("newMenu.menuConfig")) {

        // Updates the the ACIDE - A Configurable IDE previous
        // menu
        // configuration
        AcideResourceManager.getInstance()
            .setProperty("previousMenuConfiguration", currentMenuConfiguration);
      }

      // Updates the the ACIDE - A Configurable IDE current menu
      // configuration
      AcideResourceManager.getInstance()
          .setProperty("currentMenuConfiguration", "./configuration/menu/lastModified.menuConfig");
    } catch (Exception exception2) {

      // Updates the log
      AcideLog.getLog().error(exception2.getMessage());
      exception2.printStackTrace();
    }
  }