public void deleteElement(@NotNull DataContext dataContext) {
   final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
   List<PropertiesFile> propertiesFiles = myResourceBundle.getPropertiesFiles(project);
   assert project != null;
   new SafeDeleteHandler()
       .invoke(
           project,
           ContainerUtil.map2Array(propertiesFiles, PsiElement.class, MAPPER),
           dataContext);
 }
 private static boolean isResourceBundleAlphaSortedExceptOneFile(
     final @NotNull ResourceBundle resourceBundle, final @NotNull PropertiesFile exceptedFile) {
   for (PropertiesFile file : resourceBundle.getPropertiesFiles()) {
     if (!(file instanceof PropertiesFileImpl)) {
       return true;
     }
     if (!file.equals(exceptedFile) && !file.isAlphaSorted()) {
       return false;
     }
   }
   return true;
 }
 @Nullable
 static PsiDirectory getResourceBundlePlacementDirectory(ResourceBundle resourceBundle) {
   PsiDirectory containingDirectory = null;
   for (PropertiesFile propertiesFile : resourceBundle.getPropertiesFiles()) {
     if (containingDirectory == null) {
       containingDirectory = propertiesFile.getContainingFile().getContainingDirectory();
     } else if (!containingDirectory.isEquivalentTo(
         propertiesFile.getContainingFile().getContainingDirectory())) {
       return null;
     }
   }
   LOG.assertTrue(containingDirectory != null);
   return containingDirectory;
 }
  private void combineToResourceBundleIfNeed(Collection<PsiFile> files) {
    Collection<PropertiesFile> createdFiles =
        ContainerUtil.map(
            files,
            (NotNullFunction<PsiFile, PropertiesFile>)
                dom -> {
                  final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(dom);
                  LOG.assertTrue(file != null, dom.getName());
                  return file;
                });

    ResourceBundle mainBundle = myResourceBundle;
    final Set<ResourceBundle> allBundles = new HashSet<>();
    if (mainBundle != null) {
      allBundles.add(mainBundle);
    }
    boolean needCombining = false;
    for (PropertiesFile file : createdFiles) {
      final ResourceBundle rb = file.getResourceBundle();
      if (mainBundle == null) {
        mainBundle = rb;
      } else if (!mainBundle.equals(rb)) {
        needCombining = true;
      }
      allBundles.add(rb);
    }

    if (needCombining) {
      final List<PropertiesFile> toCombine = new ArrayList<>(createdFiles);
      final String baseName = getBaseName();
      if (myResourceBundle != null) {
        toCombine.addAll(myResourceBundle.getPropertiesFiles());
      }
      ResourceBundleManager manager = ResourceBundleManager.getInstance(mainBundle.getProject());
      for (ResourceBundle bundle : allBundles) {
        manager.dissociateResourceBundle(bundle);
      }
      manager.combineToResourceBundle(toCombine, baseName);
    }
  }
  private void updateEditorsFromProperties() {
    String propertyName = getSelectedPropertyName();
    ((CardLayout) myValuesPanel.getLayout())
        .show(myValuesPanel, propertyName == null ? NO_PROPERTY_SELECTED : VALUES);
    if (propertyName == null) return;

    for (final PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles(myProject)) {
      final EditorEx editor = (EditorEx) myEditors.get(propertiesFile);
      if (editor == null) continue;
      final IProperty property = propertiesFile.findPropertyByKey(propertyName);
      final Document document = editor.getDocument();
      CommandProcessor.getInstance()
          .executeCommand(
              null,
              new Runnable() {
                @Override
                public void run() {
                  ApplicationManager.getApplication()
                      .runWriteAction(
                          new Runnable() {
                            @Override
                            public void run() {
                              updateDocumentFromPropertyValue(
                                  getPropertyEditorValue(property), document, propertiesFile);
                            }
                          });
                }
              },
              "",
              this);

      JPanel titledPanel = myTitledPanels.get(propertiesFile);
      ((TitledBorder) titledPanel.getBorder())
          .setTitleColor(property == null ? JBColor.RED : UIUtil.getLabelTextForeground());
      titledPanel.repaint();
    }
  }
 public void deletePropertyIfExist(String key, PropertiesFile file) {
   final IProperty property = file.findPropertyByKey(key);
   if (property != null && myKeysOrder != null) {
     boolean keyExistInOtherPropertiesFiles = false;
     for (PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles()) {
       if (!propertiesFile.equals(file) && propertiesFile.findPropertyByKey(key) != null) {
         keyExistInOtherPropertiesFiles = true;
         break;
       }
     }
     if (!keyExistInOtherPropertiesFiles) {
       myKeysOrder.remove(key);
     }
   }
   if (property != null) {
     PsiElement anElement = property.getPsiElement();
     if (anElement instanceof PomTargetPsiElement) {
       final PomTarget xmlProperty = ((PomTargetPsiElement) anElement).getTarget();
       LOG.assertTrue(xmlProperty instanceof XmlProperty);
       anElement = ((XmlProperty) xmlProperty).getNavigationElement();
     }
     anElement.delete();
   }
 }
  private void recreateEditorsPanel() {
    myValuesPanel.removeAll();
    myValuesPanel.setLayout(new CardLayout());

    if (!myProject.isOpen()) return;
    JPanel valuesPanelComponent = new MyJPanel(new GridBagLayout());
    myValuesPanel.add(
        new JBScrollPane(valuesPanelComponent) {
          @Override
          public void updateUI() {
            super.updateUI();
            getViewport().setBackground(UIUtil.getPanelBackground());
          }
        },
        VALUES);
    myValuesPanel.add(myNoPropertySelectedPanel, NO_PROPERTY_SELECTED);

    List<PropertiesFile> propertiesFiles = myResourceBundle.getPropertiesFiles();

    GridBagConstraints gc =
        new GridBagConstraints(
            0,
            0,
            0,
            0,
            0,
            0,
            GridBagConstraints.NORTHWEST,
            GridBagConstraints.BOTH,
            new Insets(5, 5, 5, 5),
            0,
            0);
    releaseAllEditors();
    myTitledPanels.clear();
    int y = 0;
    Editor previousEditor = null;
    Editor firstEditor = null;
    for (final PropertiesFile propertiesFile : propertiesFiles) {
      final Editor editor = createEditor();
      final Editor oldEditor = myEditors.put(propertiesFile, editor);
      if (firstEditor == null) {
        firstEditor = editor;
      }
      if (previousEditor != null) {
        editor.putUserData(
            ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor);
        previousEditor.putUserData(
            ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, editor);
      }
      previousEditor = editor;
      if (oldEditor != null) {
        EditorFactory.getInstance().releaseEditor(oldEditor);
      }
      ((EditorEx) editor)
          .addFocusListener(
              new FocusChangeListener() {
                @Override
                public void focusGained(final Editor editor) {
                  mySelectedEditor = editor;
                }

                @Override
                public void focusLost(final Editor eventEditor) {
                  writeEditorPropertyValue(editor, propertiesFile, null);
                }
              });
      gc.gridx = 0;
      gc.gridy = y++;
      gc.gridheight = 1;
      gc.gridwidth = GridBagConstraints.REMAINDER;
      gc.weightx = 1;
      gc.weighty = 1;
      gc.anchor = GridBagConstraints.CENTER;

      Locale locale = propertiesFile.getLocale();
      List<String> names = new ArrayList<String>();
      if (!Comparing.strEqual(locale.getDisplayLanguage(), null)) {
        names.add(locale.getDisplayLanguage());
      }
      if (!Comparing.strEqual(locale.getDisplayCountry(), null)) {
        names.add(locale.getDisplayCountry());
      }
      if (!Comparing.strEqual(locale.getDisplayVariant(), null)) {
        names.add(locale.getDisplayVariant());
      }

      String title = propertiesFile.getName();
      if (!names.isEmpty()) {
        title += " (" + StringUtil.join(names, "/") + ")";
      }
      JComponent comp =
          new JPanel(new BorderLayout()) {
            @Override
            public Dimension getPreferredSize() {
              Insets insets = getBorder().getBorderInsets(this);
              return new Dimension(100, editor.getLineHeight() * 4 + insets.top + insets.bottom);
            }
          };
      comp.add(editor.getComponent(), BorderLayout.CENTER);
      comp.setBorder(IdeBorderFactory.createTitledBorder(title, true));
      myTitledPanels.put(propertiesFile, (JPanel) comp);

      valuesPanelComponent.add(comp, gc);
    }
    if (previousEditor != null) {
      previousEditor.putUserData(
          ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, firstEditor);
      firstEditor.putUserData(
          ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor);
    }

    gc.gridx = 0;
    gc.gridy = y;
    gc.gridheight = GridBagConstraints.REMAINDER;
    gc.gridwidth = GridBagConstraints.REMAINDER;
    gc.weightx = 10;
    gc.weighty = 1;

    valuesPanelComponent.add(new JPanel(), gc);
    selectionChanged();
    myValuesPanel.repaint();
    UIUtil.invokeAndWaitIfNeeded(
        new Runnable() {
          @Override
          public void run() {
            updateEditorsFromProperties();
          }
        });
  }
  @SuppressWarnings("unchecked")
  private void createUIComponents() {
    final JBList projectExistLocalesList = new JBList();
    final MyExistLocalesListModel existLocalesListModel = new MyExistLocalesListModel();
    projectExistLocalesList.setModel(existLocalesListModel);
    projectExistLocalesList.setCellRenderer(getLocaleRenderer());
    myProjectExistLocalesPanel =
        ToolbarDecorator.createDecorator(projectExistLocalesList)
            .disableRemoveAction()
            .disableUpDownActions()
            .createPanel();
    myProjectExistLocalesPanel.setBorder(
        IdeBorderFactory.createTitledBorder("Project locales", false));

    final JBList localesToAddList = new JBList();

    final List<Locale> locales;
    final List<Locale> restrictedLocales;
    if (myResourceBundle == null) {
      locales = Collections.singletonList(PropertiesUtil.DEFAULT_LOCALE);
      restrictedLocales = Collections.emptyList();
    } else {
      locales = Collections.emptyList();
      restrictedLocales =
          ContainerUtil.map(myResourceBundle.getPropertiesFiles(), PropertiesFile::getLocale);
    }
    myLocalesModel =
        new CollectionListModel<Locale>(locales) {
          @Override
          public void add(@NotNull List<? extends Locale> elements) {
            final List<Locale> currentItems = getItems();
            elements =
                ContainerUtil.filter(
                    elements,
                    locale ->
                        !restrictedLocales.contains(locale) && !currentItems.contains(locale));
            super.add(elements);
          }
        };
    localesToAddList.setModel(myLocalesModel);
    localesToAddList.setCellRenderer(getLocaleRenderer());
    localesToAddList.addFocusListener(
        new FocusAdapter() {
          @Override
          public void focusGained(FocusEvent e) {
            projectExistLocalesList.clearSelection();
          }
        });
    projectExistLocalesList.addFocusListener(
        new FocusAdapter() {
          @Override
          public void focusGained(FocusEvent e) {
            localesToAddList.clearSelection();
          }
        });

    myNewBundleLocalesPanel =
        ToolbarDecorator.createDecorator(localesToAddList)
            .setAddAction(
                new AnActionButtonRunnable() {
                  @Override
                  public void run(AnActionButton button) {
                    final String rawAddedLocales =
                        Messages.showInputDialog(
                            myProject,
                            PropertiesBundle.message(
                                "create.resource.bundle.dialog.add.locales.validator.message"),
                            PropertiesBundle.message(
                                "create.resource.bundle.dialog.add.locales.validator.title"),
                            null,
                            null,
                            new InputValidatorEx() {
                              @Nullable
                              @Override
                              public String getErrorText(String inputString) {
                                return checkInput(inputString) ? null : "Invalid locales";
                              }

                              @Override
                              public boolean checkInput(String inputString) {
                                return extractLocalesFromString(inputString) != null;
                              }

                              @Override
                              public boolean canClose(String inputString) {
                                return checkInput(inputString);
                              }
                            });
                    if (rawAddedLocales != null) {
                      final List<Locale> locales = extractLocalesFromString(rawAddedLocales);
                      LOG.assertTrue(locales != null);
                      myLocalesModel.add(locales);
                    }
                  }
                })
            .setAddActionName("Add locales by suffix")
            .disableUpDownActions()
            .createPanel();
    myNewBundleLocalesPanel.setBorder(IdeBorderFactory.createTitledBorder("Locales to add", false));

    myAddLocaleFromExistButton = new JButton(AllIcons.Actions.Forward);
    new ClickListener() {
      @Override
      public boolean onClick(@NotNull MouseEvent event, int clickCount) {
        if (clickCount == 1) {
          myLocalesModel.add(
              ContainerUtil.map(projectExistLocalesList.getSelectedValues(), o -> (Locale) o));
          return true;
        }
        return false;
      }
    }.installOn(myAddLocaleFromExistButton);

    projectExistLocalesList.addListSelectionListener(
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            final List<Locale> currentItems = myLocalesModel.getItems();
            for (Object o : projectExistLocalesList.getSelectedValues()) {
              Locale l = (Locale) o;
              if (!restrictedLocales.contains(l) && !currentItems.contains(l)) {
                myAddLocaleFromExistButton.setEnabled(true);
                return;
              }
            }
            myAddLocaleFromExistButton.setEnabled(false);
          }
        });
    myAddLocaleFromExistButton.setEnabled(false);

    myAddAllButton = new JButton("Add All");
    new ClickListener() {
      @Override
      public boolean onClick(@NotNull MouseEvent event, int clickCount) {
        if (clickCount == 1) {
          myLocalesModel.add(existLocalesListModel.getLocales());
        }
        return false;
      }
    }.installOn(myAddAllButton);
  }