summaryrefslogtreecommitdiff
path: root/plugins/maven/src/main/java/org/jetbrains/idea/maven/wizards/SelectProfilesStep.java
blob: 3fc7fb47b202d2fa6978651a910d3670ddd2c3ee (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright 2000-2009 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.wizards;

import com.intellij.ide.util.MultiStateElementsChooser;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.projectImport.ProjectImportWizardStep;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.model.MavenExplicitProfiles;
import org.jetbrains.idea.maven.model.MavenProfileKind;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * @author Vladislav.Kaznacheev
 */
public class SelectProfilesStep extends ProjectImportWizardStep {
  private JPanel panel;
  private MultiStateElementsChooser<String, MavenProfileKind> profileChooser;
  private MavenProfileKindMarkStateDescriptor myMarkStateDescriptor;

  public SelectProfilesStep(final WizardContext context) {
    super(context);
  }

  public boolean isStepVisible() {
    if (!super.isStepVisible()) {
      return false;
    }
    final MavenProjectBuilder importBuilder = getBuilder();
    if (importBuilder != null) {
      return !importBuilder.getProfiles().isEmpty();
    }
    return false;
  }

  protected MavenProjectBuilder getBuilder() {
    return (MavenProjectBuilder)super.getBuilder();
  }

  public void createUIComponents() {
    myMarkStateDescriptor = new MavenProfileKindMarkStateDescriptor();
    profileChooser = new MultiStateElementsChooser<String, MavenProfileKind>(true, myMarkStateDescriptor);
  }

  public JComponent getComponent() {
    return panel;
  }

  public void updateStep() {
    List<String> allProfiles = getBuilder().getProfiles();
    List<String> activatedProfiles = getBuilder().getActivatedProfiles();
    MavenExplicitProfiles selectedProfiles = getBuilder().getSelectedProfiles();
    List<String> enabledProfiles = new ArrayList<String>(selectedProfiles.getEnabledProfiles());
    List<String> disabledProfiles = new ArrayList<String>(selectedProfiles.getDisabledProfiles());
    enabledProfiles.retainAll(allProfiles); // mark only existing profiles
    disabledProfiles.retainAll(allProfiles); // mark only existing profiles

    myMarkStateDescriptor.setActivatedProfiles(activatedProfiles);
    profileChooser.setElements(allProfiles, null);
    profileChooser.markElements(enabledProfiles, MavenProfileKind.EXPLICIT);
    profileChooser.markElements(disabledProfiles, MavenProfileKind.NONE);
  }

  public boolean validate() throws ConfigurationException {
    Collection<String> activatedProfiles = myMarkStateDescriptor.getActivatedProfiles();
    MavenExplicitProfiles newSelectedProfiles = MavenExplicitProfiles.NONE.clone();
    for (Map.Entry<String, MavenProfileKind> entry : profileChooser.getElementMarkStates().entrySet()) {
      String profile = entry.getKey();
      MavenProfileKind profileKind = entry.getValue();
      switch (profileKind) {
        case NONE:
          if (activatedProfiles.contains(profile)) {
            newSelectedProfiles.getDisabledProfiles().add(profile);
          }
          break;
        case EXPLICIT:
          newSelectedProfiles.getEnabledProfiles().add(profile);
          break;
        case IMPLICIT:
          break;
      }
    }
    return getBuilder().setSelectedProfiles(newSelectedProfiles);
  }

  public void updateDataModel() {
  }

  @NonNls
  public String getHelpId() {
    return "reference.dialogs.new.project.import.maven.page2";
  }

  private static class MavenProfileKindMarkStateDescriptor
    implements MultiStateElementsChooser.MarkStateDescriptor<String, MavenProfileKind> {
    private Collection<String> myActivatedProfiles = Collections.emptySet();

    public Collection<String> getActivatedProfiles() {
      return myActivatedProfiles;
    }

    public void setActivatedProfiles(Collection<String> activatedProfiles) {
      myActivatedProfiles = new THashSet<String>(activatedProfiles);
    }

    @NotNull
    @Override
    public MavenProfileKind getDefaultState(@NotNull String element) {
      return myActivatedProfiles.contains(element) ? MavenProfileKind.IMPLICIT : MavenProfileKind.NONE;
    }

    @NotNull
    @Override
    public MavenProfileKind getNextState(@NotNull String element, @NotNull MavenProfileKind state) {
      MavenProfileKind nextState;
      switch (state) {
        case NONE:
          nextState = MavenProfileKind.EXPLICIT;
          break;
        case EXPLICIT:
          nextState = getDefaultState(element);
          break;
        case IMPLICIT:
        default:
          nextState = MavenProfileKind.NONE;
          break;
      }
      return nextState;
    }

    @Nullable
    @Override
    public MavenProfileKind getNextState(@NotNull Map<String, MavenProfileKind> elementsWithStates) {
      MavenProfileKind nextState = null;
      for (Map.Entry<String, MavenProfileKind> entry : elementsWithStates.entrySet()) {
        MavenProfileKind nextElementState = getNextState(entry.getKey(), entry.getValue());
        if (nextState == null) {
          nextState = nextElementState;
        }
        else if (!nextState.equals(nextElementState)) {
          nextState = null;
          break;
        }
      }
      return nextState;
    }

    @Override
    public boolean isMarked(@NotNull MavenProfileKind state) {
      return state != MavenProfileKind.NONE;
    }

    @Nullable
    @Override
    public MavenProfileKind getMarkState(@Nullable Object value) {
      return value instanceof MavenProfileKind ? (MavenProfileKind)value : null;
    }

    @Nullable
    @Override
    public TableCellRenderer getMarkRenderer() {
      return new CheckboxTableCellRenderer();
    }
  }

  private static class CheckboxTableCellRenderer extends JCheckBox implements TableCellRenderer {
    public CheckboxTableCellRenderer() {
      setHorizontalAlignment(SwingConstants.CENTER);
      setBorder(null);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      if (isSelected) {
        setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
      }
      else {
        setForeground(table.getForeground());
        setBackground(table.getBackground());
      }

      MavenProfileKind state = (MavenProfileKind)value;
      setSelected(state != MavenProfileKind.NONE);
      setEnabled(state != MavenProfileKind.IMPLICIT);

      return this;
    }
  }
}