summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/fileChooser/actions/FileDeleteAction.java
blob: b07102f62a0705f6be73a28be8b8913ea05b7fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.fileChooser.actions;

import com.intellij.CommonBundle;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.fileChooser.FileChooserPanel;
import com.intellij.openapi.fileChooser.FileSystemTree;
import com.intellij.openapi.fileChooser.ex.FileChooserKeys;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.NioFiles;
import com.intellij.ui.UIBundle;
import com.intellij.util.ui.IoErrorText;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public class FileDeleteAction extends FileChooserAction {
  @Override
  protected void update(@NotNull FileChooserPanel panel, @NotNull AnActionEvent e) {
    var visible = !isDisabled(e);
    e.getPresentation().setVisible(visible);
    e.getPresentation().setEnabled(visible && !panel.selectedPaths().isEmpty());
  }

  @Override
  protected void actionPerformed(@NotNull FileChooserPanel panel, @NotNull AnActionEvent e) {
    var paths = panel.selectedPaths();
    if (paths.isEmpty()) return;

    var project = e.getProject();
    var ok = MessageDialogBuilder.yesNo(UIBundle.message("file.chooser.delete.title"), UIBundle.message("file.chooser.delete.confirm"))
      .yesText(ApplicationBundle.message("button.delete")).noText(CommonBundle.getCancelButtonText())
      .icon(UIUtil.getWarningIcon())
      .ask(project);
    if (!ok) return;

    try {
      var progress = IdeBundle.message("progress.deleting");
      ProgressManager.getInstance().run(new Task.WithResult<Void, IOException>(e.getProject(), panel.getComponent(), progress, true) {
        @Override
        protected Void compute(@NotNull ProgressIndicator indicator) throws IOException {
          for (var path : paths) {
            if (indicator.isCanceled()) break;
            indicator.setText(path.toString());
            NioFiles.deleteRecursively(path, p -> {
              indicator.checkCanceled();
              indicator.setText2(path.relativize(p).toString());
            });
          }
          return null;
        }
      });
      panel.reload(null);
    }
    catch (IOException ex) {
      Messages.showErrorDialog(panel.getComponent(), IoErrorText.message(ex), CommonBundle.getErrorTitle());
    }
  }

  private static boolean isDisabled(AnActionEvent e) {
    return e.getData(FileChooserKeys.DELETE_ACTION_AVAILABLE) == Boolean.FALSE;
  }

  @Override
  protected void update(@NotNull FileSystemTree fileChooser, @NotNull AnActionEvent e) {
    boolean visible = !isDisabled(e);
    e.getPresentation().setVisible(visible);
    e.getPresentation().setEnabled(visible && new VirtualFileDeleteProvider().canDeleteElement(e.getDataContext()));
  }

  @Override
  protected void actionPerformed(@NotNull FileSystemTree fileChooser, @NotNull AnActionEvent e) {
    new VirtualFileDeleteProvider().deleteElement(e.getDataContext());
  }
}