summaryrefslogtreecommitdiff
path: root/plugins/git4idea/src/git4idea/util/UntrackedFilesNotifier.java
blob: 382d51b42032504800610f13b89965c163733e03 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.util;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsNotifier;
import com.intellij.openapi.vcs.changes.ui.SelectFilesDialog;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import git4idea.GitUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class UntrackedFilesNotifier {

  private UntrackedFilesNotifier() {
  }

  /**
   * Displays notification about {@code untracked files would be overwritten by checkout} error.
   * Clicking on the link in the notification opens a simple dialog with the list of these files.
   * @param root
   * @param relativePaths
   * @param operation   the name of the Git operation that caused the error: {@code rebase, merge, checkout}.
   * @param description the content of the notification or null if the deafult content is to be used.
   */
  public static void notifyUntrackedFilesOverwrittenBy(@NotNull final Project project,
                                                       @NotNull final VirtualFile root, @NotNull Collection<String> relativePaths,
                                                       @NotNull final String operation, @Nullable String description) {
    final String notificationTitle = StringUtil.capitalize(operation) + " failed";
    final String notificationDesc = description == null ? createUntrackedFilesOverwrittenDescription(operation, true) : description;

    final Collection<String> absolutePaths = GitUtil.toAbsolute(root, relativePaths);
    final List<VirtualFile> untrackedFiles = ContainerUtil.mapNotNull(absolutePaths, new Function<String, VirtualFile>() {
      @Override
      public VirtualFile fun(String absolutePath) {
        return GitUtil.findRefreshFileOrLog(absolutePath);
      }
    });

    VcsNotifier.getInstance(project).notifyError(notificationTitle, notificationDesc, new NotificationListener() {
      @Override
      public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
        if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
          final String dialogDesc = createUntrackedFilesOverwrittenDescription(operation, false);
          String title = "Untracked Files Preventing " + StringUtil.capitalize(operation);
          if (untrackedFiles.isEmpty()) {
            GitUtil.showPathsInDialog(project, absolutePaths, title, dialogDesc);
          }
          else {
            DialogWrapper dialog;
            dialog = new UntrackedFilesDialog(project, untrackedFiles, dialogDesc);
            dialog.setTitle(title);
            dialog.show();
          }
        }
      }
    });
  }
  
  public static String createUntrackedFilesOverwrittenDescription(@NotNull final String operation, boolean addLinkToViewFiles) {
    final String description1 = " untracked working tree files would be overwritten by " + operation + ".";
    final String description2 = "Please move or remove them before you can " + operation + ".";
    final String notificationDesc;
    if (addLinkToViewFiles) {
      notificationDesc = "Some" + description1 + "<br/>" + description2 + " <a href='view'>View them</a>";
    }
    else {
      notificationDesc = "These" + description1 + "<br/>" + description2;
    }
    return notificationDesc;
  }

  private static class UntrackedFilesDialog extends SelectFilesDialog {

    public UntrackedFilesDialog(Project project, Collection<VirtualFile> untrackedFiles, String dialogDesc) {
      super(project, new ArrayList<VirtualFile>(untrackedFiles), StringUtil.stripHtml(dialogDesc, true), null, false, false, true);
      init();
    }

    @NotNull
    @Override
    protected Action[] createActions() {
      return new Action[]{getOKAction()};
    }

  }
}