2 * Copyright 2000-2009 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
.execution
.junit2
.configuration
;
18 import com
.intellij
.execution
.configurations
.JavaRunConfigurationModule
;
19 import com
.intellij
.execution
.configurations
.ModuleBasedConfiguration
;
20 import com
.intellij
.openapi
.module
.Module
;
21 import com
.intellij
.openapi
.module
.ModuleManager
;
22 import com
.intellij
.openapi
.module
.ModuleTypeManager
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.psi
.PsiClass
;
25 import com
.intellij
.ui
.ComboboxSpeedSearch
;
26 import com
.intellij
.ui
.SortedComboBoxModel
;
27 import org
.jetbrains
.annotations
.Nullable
;
31 import java
.util
.ArrayList
;
32 import java
.util
.Collection
;
33 import java
.util
.Comparator
;
34 import java
.util
.List
;
36 public class ConfigurationModuleSelector
{
37 private final Project myProject
;
38 private final JComboBox myModulesList
;
39 private final SortedComboBoxModel
<Object
> myModules
= new SortedComboBoxModel
<Object
>(new Comparator
<Object
>() {
40 public int compare(final Object module
, final Object module1
) {
41 if (module
instanceof Module
&& module1
instanceof Module
){
42 return ((Module
)module
).getName().compareToIgnoreCase(((Module
)module1
).getName());
47 private static final String NO_MODULE
= "<no module>";
49 public ConfigurationModuleSelector(final Project project
, final JComboBox modulesList
) {
51 myModulesList
= modulesList
;
52 new ComboboxSpeedSearch(modulesList
){
53 protected String
getElementText(Object element
) {
54 if (element
instanceof Module
){
55 return ((Module
)element
).getName();
56 } else if (element
== null) {
59 return super.getElementText(element
);
62 myModulesList
.setModel(myModules
);
63 myModulesList
.setRenderer(new DefaultListCellRenderer(){
64 public Component
getListCellRendererComponent(final JList list
, final Object value
, final int index
, final boolean isSelected
, final boolean cellHasFocus
) {
65 final Component component
= super.getListCellRendererComponent(list
, value
, index
, isSelected
, cellHasFocus
);
66 if (value
instanceof Module
) {
67 final Module module
= (Module
)value
;
68 setIcon(module
.getModuleType().getNodeIcon(true));
69 setText(module
.getName());
70 } else if (value
== null) {
78 public void applyTo(final ModuleBasedConfiguration configurationModule
) {
79 configurationModule
.setModule((Module
)myModulesList
.getSelectedItem());
82 public void reset(final ModuleBasedConfiguration configuration
) {
83 final Module
[] modules
= ModuleManager
.getInstance(getProject()).getModules();
84 final List
<Module
> list
= new ArrayList
<Module
>();
85 for (final Module module
: modules
) {
86 if (isModuleAccepted(module
)) list
.add(module
);
89 myModules
.setSelectedItem(configuration
.getConfigurationModule().getModule());
92 public static boolean isModuleAccepted(final Module module
) {
93 return ModuleTypeManager
.getInstance().isClasspathProvider(module
.getModuleType());
96 public Project
getProject() {
100 public JavaRunConfigurationModule
getConfigurationModule() {
101 final JavaRunConfigurationModule configurationModule
= new JavaRunConfigurationModule(getProject(), false);
102 configurationModule
.setModule((Module
)myModules
.getSelectedItem());
103 return configurationModule
;
106 private void setModules(final Collection
<Module
> modules
) {
109 for (Module module
: modules
) {
110 myModules
.add(module
);
114 public Module
getModule() {
115 return (Module
)myModules
.getSelectedItem();
119 public PsiClass
findClass(final String className
) {
120 return getConfigurationModule().findClass(className
);
123 public String
getModuleName() {
124 final Module module
= (Module
)myModules
.getSelectedItem();
125 return module
== null ?
"" : module
.getName();