summaryrefslogtreecommitdiff
path: root/plugins/maven/src/main/java/org/jetbrains/idea/maven/project/actions/ToggleProfileAction.java
blob: 3e9bf7f92ddd997149356241db7908ac5e075520 (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
/*
 * Copyright 2000-2010 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 org.jetbrains.idea.maven.project.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.project.ProjectBundle;
import org.jetbrains.idea.maven.utils.MavenDataKeys;
import org.jetbrains.idea.maven.utils.actions.MavenAction;
import org.jetbrains.idea.maven.utils.actions.MavenActionUtil;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ToggleProfileAction extends MavenAction {
  public void update(AnActionEvent e) {
    super.update(e);
    if (!isAvailable(e)) return;

    MavenProjectsManager projectsManager = MavenActionUtil.getProjectsManager(e.getDataContext());
    List<String> profiles = e.getData(MavenDataKeys.MAVEN_PROFILES);

    e.getPresentation().setText(isActive(projectsManager, profiles)
                                ? ProjectBundle.message("maven.profile.deactivate")
                                : ProjectBundle.message("maven.profile.activate"));
  }

  @Override
  protected boolean isAvailable(AnActionEvent e) {
    if (!super.isAvailable(e)) return false;

    List<String> selectedProfiles = e.getData(MavenDataKeys.MAVEN_PROFILES);
    if (selectedProfiles == null || selectedProfiles.isEmpty()) return false;

    Collection<String> activeProfiles = MavenActionUtil.getProjectsManager(e.getDataContext()).getExplicitProfiles();
    int activeCount = 0;
    for (String profile : selectedProfiles) {
      if (activeProfiles.contains(profile)) {
        activeCount++;
      }
    }
    return activeCount == 0 || activeCount == selectedProfiles.size();
  }

  private static boolean isActive(MavenProjectsManager projectsManager, List<String> profiles) {
    return projectsManager.getExplicitProfiles().contains(profiles.get(0));
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    MavenProjectsManager manager = MavenActionUtil.getProjectsManager(e.getDataContext());
    List<String> selectedProfiles = e.getData(MavenDataKeys.MAVEN_PROFILES);

    Set<String> activeProfiles = new HashSet<String>(manager.getExplicitProfiles());
    if (isActive(manager, selectedProfiles)) {
      activeProfiles.removeAll(selectedProfiles);
    }
    else {
      activeProfiles.addAll(selectedProfiles);
    }
    manager.setExplicitProfiles(activeProfiles);
  }
}