summaryrefslogtreecommitdiff
path: root/java/idea-ui/src/com/intellij/ide/util/newProjectWizard/ProjectTypesList.java
blob: 2872e0deff50c8537249f6e354c2efb77332973e (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright 2000-2012 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 com.intellij.ide.util.newProjectWizard;

import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.ui.popup.ListItemDescriptor;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.platform.ProjectTemplate;
import com.intellij.platform.templates.RemoteTemplatesFactory;
import com.intellij.psi.codeStyle.MinusculeMatcher;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.ListSpeedSearch;
import com.intellij.ui.SpeedSearchComparator;
import com.intellij.ui.components.JBList;
import com.intellij.ui.popup.list.GroupedItemsListRenderer;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Dmitry Avdeev
 *         Date: 11/21/12
 */
public class ProjectTypesList implements Disposable {

  private final JBList myList;
  private final CollectionListModel<TemplateItem> myModel;
  private MinusculeMatcher myMatcher;
  private Pair<TemplateItem, Integer> myBestMatch;

  private TemplateItem myLoadingItem;

  public ProjectTypesList(JBList list, MultiMap<TemplatesGroup, ProjectTemplate> map, final WizardContext context) {
    myList = list;

    new ListSpeedSearch(myList) {
      @Override
      protected String getElementText(Object element) {
        return super.getElementText(element);
      }
    }.setComparator(new SpeedSearchComparator(false));
    List<TemplateItem> items = buildItems(map);
    final TemplatesGroup samplesGroup = new TemplatesGroup("Loading Templates...", "", null, 0, null, null, null);
    myLoadingItem = new TemplateItem(new LoadingProjectTemplate(), samplesGroup) {
      @Override
      Icon getIcon() {
        return null;
      }

      @Override
      String getDescription() {
        return "";
      }
    };
    items.add(myLoadingItem);
    myModel = new CollectionListModel<TemplateItem>(items);

    final RemoteTemplatesFactory factory = new RemoteTemplatesFactory();
    ProgressManager.getInstance().run(new Task.Backgroundable(context.getProject(), "Loading Templates") {
      @Override
      public void run(@NotNull ProgressIndicator indicator) {
        try {
          myList.setPaintBusy(true);
          String[] groups = factory.getGroups();
          final List<TemplateItem> items = new ArrayList<TemplateItem>();
          for (String group : groups) {
            TemplatesGroup templatesGroup = new TemplatesGroup(group, "", factory.getGroupIcon(group), 0, null, null, null);
            ProjectTemplate[] templates = factory.createTemplates(group, context);
            for (ProjectTemplate template : templates) {
              items.add(new TemplateItem(template, templatesGroup));
            }
          }
          //noinspection SSBasedInspection
          SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              int index = myList.getSelectedIndex();
              myModel.remove(myLoadingItem);
              myModel.add(items);
              myList.setSelectedIndex(index);
            }
          });
        }
        finally {
          myList.setPaintBusy(false);
        }
      }
    });

    myList.setCellRenderer(new GroupedItemsListRenderer(new ListItemDescriptor() {
      @Nullable
      @Override
      public String getTextFor(Object value) {
        return ((TemplateItem)value).getName();
      }

      @Nullable
      @Override
      public String getTooltipFor(Object value) {
        return null;
      }

      @Nullable
      @Override
      public Icon getIconFor(Object value) {
        return ((TemplateItem)value).getIcon();
      }

      @Override
      public boolean hasSeparatorAboveOf(Object value) {
        TemplateItem item = (TemplateItem)value;
        int index = myModel.getElementIndex(item);
        return index == 0 || !myModel.getElementAt(index - 1).getGroupName().equals(item.getGroupName());
      }

      @Nullable
      @Override
      public String getCaptionAboveOf(Object value) {
        return ((TemplateItem)value).getGroupName();
      }
    }));

    myList.setModel(myModel);
  }

  void installKeyAction(JComponent component) {
    new AnAction() {
      @Override
      public void actionPerformed(AnActionEvent e) {
        InputEvent event = e.getInputEvent();
        if (event instanceof KeyEvent) {
          int row = myList.getSelectedIndex();
           int toSelect;
           switch (((KeyEvent)event).getKeyCode()) {
             case KeyEvent.VK_UP:
               toSelect = row == 0 ? myList.getItemsCount() - 1 : row - 1;
               myList.setSelectedIndex(toSelect);
               myList.ensureIndexIsVisible(toSelect);
               break;
             case KeyEvent.VK_DOWN:
               toSelect = row < myList.getItemsCount() - 1 ? row + 1 : 0;
               myList.setSelectedIndex(toSelect);
               myList.ensureIndexIsVisible(toSelect);
               break;
           }
        }
      }
    }.registerCustomShortcutSet(new CustomShortcutSet(KeyEvent.VK_UP, KeyEvent.VK_DOWN), component);
  }

  void resetSelection() {
    if (myList.getSelectedIndex() != -1) return;
    SelectTemplateSettings settings = SelectTemplateSettings.getInstance();
    if (settings.getLastGroup() == null || !setSelectedTemplate(settings.getLastGroup(), settings.getLastTemplate())) {
      myList.setSelectedIndex(0);
    }
  }

  void saveSelection() {
    TemplateItem item = (TemplateItem)myList.getSelectedValue();
    if (item != null) {
      SelectTemplateSettings.getInstance().setLastTemplate(item.getGroupName(), item.getName());
    }
  }

  private List<TemplateItem> buildItems(MultiMap<TemplatesGroup, ProjectTemplate> map) {
    List<TemplateItem> items = new ArrayList<TemplateItem>();
    List<TemplatesGroup> groups = new ArrayList<TemplatesGroup>(map.keySet());
    Collections.sort(groups);
    for (TemplatesGroup group : groups) {
      for (ProjectTemplate template : map.get(group)) {
        TemplateItem templateItem = new TemplateItem(template, group);
        items.add(templateItem);
      }
    }
    return items;
  }

  @Nullable
  public ProjectTemplate getSelectedTemplate() {
    Object value = myList.getSelectedValue();
    return value instanceof TemplateItem ? ((TemplateItem)value).myTemplate : null;
  }

  public boolean setSelectedTemplate(@Nullable String group, @Nullable String name) {
    for (int i = 0; i < myList.getModel().getSize(); i++) {
      Object o = myList.getModel().getElementAt(i);
      if (o instanceof TemplateItem && ((TemplateItem)o).myGroup.getName().equals(group) && ((TemplateItem)o).getName().equals(name)) {
        myList.setSelectedIndex(i);
        myList.ensureIndexIsVisible(i);
        return true;
      }
    }

    return false;
  }

  @Override
  public void dispose() {
  }

  class TemplateItem {

    private final ProjectTemplate myTemplate;
    private final TemplatesGroup myGroup;

    TemplateItem(ProjectTemplate template, TemplatesGroup group) {
      myTemplate = template;
      myGroup = group;
    }

    String getName() {
      return myTemplate.getName();
    }

    public String getGroupName() {
      return myGroup.getName();
    }

    Icon getIcon() {
      return myTemplate.createModuleBuilder().getNodeIcon();
    }

    protected int getMatchingDegree() {
      if (myMatcher == null) return Integer.MAX_VALUE;
      String text = getName() + " " + getGroupName();
      String description = getDescription();
      if (description != null) {
        text += " " + StringUtil.stripHtml(description, false);
      }
      int i = myMatcher.matchingDegree(text);
      if (myBestMatch == null || i > myBestMatch.second) {
        myBestMatch = Pair.create(this, i);
      }
      return i;
    }

    @Nullable
    String getDescription() {
      return myTemplate.getDescription();
    }

    @Override
    public String toString() {
      return getName() + " " + getGroupName();
    }
  }
}