2 * Copyright 2000-2012 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.application.options;
18 import com.intellij.openapi.module.Module;
19 import com.intellij.openapi.module.ModuleManager;
20 import com.intellij.openapi.module.ModuleType;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.roots.ui.configuration.ModulesAlphaComparator;
23 import com.intellij.openapi.ui.ComboBox;
24 import com.intellij.ui.ComboboxSpeedSearch;
25 import com.intellij.ui.SortedComboBoxModel;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.List;
37 public class ModulesComboBox extends ComboBox<Module> {
38 private final SortedComboBoxModel<Module> myModel;
39 private boolean myAllowEmptySelection;
41 public ModulesComboBox() {
42 this(new SortedComboBoxModel<>(ModulesAlphaComparator.INSTANCE));
45 private ModulesComboBox(final SortedComboBoxModel<Module> model) {
48 new ComboboxSpeedSearch(this){
50 protected String getElementText(Object element) {
51 if (element instanceof Module) {
52 return ((Module)element).getName();
53 } else if (element == null) {
56 return super.getElementText(element);
59 setRenderer(new ModuleListCellRenderer());
62 public void allowEmptySelection(@NotNull String emptySelectionText) {
63 myAllowEmptySelection = true;
65 setRenderer(new ModuleListCellRenderer(emptySelectionText));
68 public void setModules(@NotNull Collection<Module> modules) {
69 myModel.setAll(modules);
70 if (myAllowEmptySelection) {
75 public void fillModules(@NotNull Project project) {
76 fillModules(project, null);
79 public void fillModules(@NotNull Project project, final @Nullable ModuleType moduleType) {
80 Module[] allModules = ModuleManager.getInstance(project).getModules();
81 if (moduleType == null) {
82 setModules(Arrays.asList(allModules));
85 List<Module> modules = new ArrayList<>();
86 for (Module module : allModules) {
87 if (moduleType.equals(ModuleType.get(module))) {
95 public void setSelectedModule(@Nullable Module module) {
96 myModel.setSelectedItem(module);
100 public Module getSelectedModule() {
101 return myModel.getSelectedItem();