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
.ide
.util
.projectWizard
;
18 import com
.intellij
.ide
.IdeBundle
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.project
.ProjectBundle
;
22 import com
.intellij
.openapi
.project
.ProjectManager
;
23 import com
.intellij
.openapi
.projectRoots
.ProjectJdkTable
;
24 import com
.intellij
.openapi
.projectRoots
.Sdk
;
25 import com
.intellij
.openapi
.projectRoots
.SdkType
;
26 import com
.intellij
.openapi
.projectRoots
.ui
.ProjectJdksEditor
;
27 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
28 import com
.intellij
.openapi
.roots
.ui
.configuration
.ProjectStructureConfigurable
;
29 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.ProjectJdksModel
;
30 import com
.intellij
.openapi
.ui
.DialogWrapper
;
31 import com
.intellij
.openapi
.util
.Comparing
;
32 import com
.intellij
.openapi
.wm
.ex
.WindowManagerEx
;
33 import com
.intellij
.util
.ArrayUtil
;
34 import gnu
.trove
.TIntArrayList
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 import javax
.swing
.event
.ListSelectionEvent
;
39 import javax
.swing
.event
.ListSelectionListener
;
41 import java
.awt
.event
.ActionEvent
;
42 import java
.awt
.event
.MouseAdapter
;
43 import java
.awt
.event
.MouseEvent
;
46 public class JdkChooserPanel
extends JPanel
{
47 private JList myList
= null;
48 private DefaultListModel myListModel
= null;
49 private Sdk myCurrentJdk
;
50 private final Project myProject
;
51 private SdkType
[] myAllowedJdkTypes
= null;
53 public JdkChooserPanel(Project project
) {
54 super(new BorderLayout());
56 myListModel
= new DefaultListModel();
57 myList
= new JList(myListModel
);
58 myList
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
59 myList
.setCellRenderer(new ProjectJdkListRenderer());
60 //noinspection HardCodedStringLiteral
61 myList
.setPrototypeCellValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
63 myList
.addListSelectionListener(new ListSelectionListener() {
64 public void valueChanged(ListSelectionEvent e
) {
65 myCurrentJdk
= (Sdk
)myList
.getSelectedValue();
68 myList
.addMouseListener(new MouseAdapter() {
69 public void mouseClicked(MouseEvent e
) {
70 if (e
.getClickCount() == 2 && myProject
== null) {
76 JPanel panel
= new JPanel(new BorderLayout());
77 panel
.add(new JScrollPane(myList
), BorderLayout
.CENTER
);
78 add(panel
, BorderLayout
.CENTER
);
79 if (myListModel
.getSize() > 0) {
80 myList
.setSelectedIndex(0);
85 * Sets the JDK types which may be shown in the panel.
87 * @param allowedJdkTypes the array of JDK types which may be shown, or null if all JDK types are allowed.
90 public void setAllowedJdkTypes(@Nullable final SdkType
[] allowedJdkTypes
) {
91 myAllowedJdkTypes
= allowedJdkTypes
;
94 public Sdk
getChosenJdk() {
98 public void editJdkTable() {
99 ProjectJdksEditor editor
= new ProjectJdksEditor((Sdk
)myList
.getSelectedValue(),
100 myProject
!= null ? myProject
: ProjectManager
.getInstance().getDefaultProject(),
104 Sdk selectedJdk
= editor
.getSelectedJdk();
105 updateList(selectedJdk
, null);
109 public void updateList(final Sdk selectedJdk
, final SdkType type
) {
110 final int[] selectedIndices
= myList
.getSelectedIndices();
113 if (selectedJdk
!= null) {
114 TIntArrayList list
= new TIntArrayList();
115 for (int i
= 0; i
< myListModel
.size(); i
++) {
116 final Sdk jdk
= (Sdk
)myListModel
.getElementAt(i
);
117 if (Comparing
.strEqual(jdk
.getName(), selectedJdk
.getName())){
121 final int[] indicesToSelect
= list
.toNativeArray();
122 if (indicesToSelect
.length
> 0) {
123 myList
.setSelectedIndices(indicesToSelect
);
125 else if (myList
.getModel().getSize() > 0) {
126 myList
.setSelectedIndex(0);
130 if (selectedIndices
.length
> 0) {
131 myList
.setSelectedIndices(selectedIndices
);
134 myList
.setSelectedIndex(0);
138 myCurrentJdk
= (Sdk
)myList
.getSelectedValue();
141 public JList
getPreferredFocusedComponent() {
145 public void fillList(final SdkType type
) {
148 if (myProject
== null) {
149 final Sdk
[] allJdks
= ProjectJdkTable
.getInstance().getAllJdks();
150 jdks
= getCompatibleJdks(type
, Arrays
.asList(allJdks
));
153 final ProjectJdksModel projectJdksModel
= ProjectStructureConfigurable
.getInstance(myProject
).getProjectJdksModel();
154 if (!projectJdksModel
.isInitialized()){ //should be initialized
155 projectJdksModel
.reset(myProject
);
157 final Collection
<Sdk
> collection
= projectJdksModel
.getProjectJdks().values();
158 jdks
= getCompatibleJdks(type
, collection
);
160 Arrays
.sort(jdks
, new Comparator
<Sdk
>() {
161 public int compare(final Sdk o1
, final Sdk o2
) {
162 return o1
.getName().compareToIgnoreCase(o2
.getName());
165 for (Sdk jdk
: jdks
) {
166 myListModel
.addElement(jdk
);
170 private Sdk
[] getCompatibleJdks(final SdkType type
, final Collection
<Sdk
> collection
) {
171 final Set
<Sdk
> compatibleJdks
= new HashSet
<Sdk
>();
172 for (Sdk projectJdk
: collection
) {
173 if (isCompatibleJdk(projectJdk
, type
)) {
174 compatibleJdks
.add(projectJdk
);
177 return compatibleJdks
.toArray(new Sdk
[compatibleJdks
.size()]);
180 private boolean isCompatibleJdk(final Sdk projectJdk
, final SdkType type
) {
182 return projectJdk
.getSdkType() == type
;
184 if (myAllowedJdkTypes
!= null) {
185 return ArrayUtil
.indexOf(myAllowedJdkTypes
, projectJdk
.getSdkType()) >= 0;
190 public JComponent
getDefaultFocusedComponent() {
194 public void selectJdk(Sdk defaultJdk
) {
195 final int index
= myListModel
.indexOf(defaultJdk
);
197 myList
.setSelectedIndex(index
);
201 private static Sdk
showDialog(final Project project
, String title
, final Component parent
, Sdk jdkToSelect
) {
202 final JdkChooserPanel jdkChooserPanel
= new JdkChooserPanel(project
);
203 jdkChooserPanel
.fillList(null);
204 final MyDialog dialog
= jdkChooserPanel
.new MyDialog(parent
);
206 dialog
.setTitle(title
);
208 if (jdkToSelect
!= null) {
209 jdkChooserPanel
.selectJdk(jdkToSelect
);
211 jdkChooserPanel
.myList
.addMouseListener(new MouseAdapter() {
213 public void mouseClicked(final MouseEvent e
) {
214 if (e
.getClickCount() == 2 && e
.getButton() == MouseEvent
.BUTTON1
) {
215 dialog
.clickDefaultButton();
220 return dialog
.isOK() ? jdkChooserPanel
.getChosenJdk() : null;
223 public static Sdk
chooseAndSetJDK(final Project project
) {
224 final Sdk projectJdk
= ProjectRootManager
.getInstance(project
).getProjectJdk();
225 final Sdk jdk
= showDialog(project
, ProjectBundle
.message("module.libraries.target.jdk.select.title"), WindowManagerEx
.getInstanceEx().getFrame(project
), projectJdk
);
229 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
231 ProjectRootManager
.getInstance(project
).setProjectJdk(jdk
);
237 public class MyDialog
extends DialogWrapper
implements ListSelectionListener
{
239 public MyDialog(Component parent
) {
241 setTitle(IdeBundle
.message("title.select.jdk"));
243 myList
.addListSelectionListener(this);
247 protected String
getDimensionServiceKey() {
248 return "#com.intellij.ide.util.projectWizard.JdkChooserPanel.MyDialog";
251 public void valueChanged(ListSelectionEvent e
) {
255 private void updateOkButton() {
256 setOKActionEnabled(myList
.getSelectedValue() != null);
259 public void dispose() {
260 myList
.removeListSelectionListener(this);
264 protected JComponent
createCenterPanel() {
265 return JdkChooserPanel
.this;
268 protected Action
[] createActions() {
269 return new Action
[]{new ConfigureAction(), getOKAction(), getCancelAction()};
272 public JComponent
getPreferredFocusedComponent() {
276 private final class ConfigureAction
extends AbstractAction
{
277 public ConfigureAction() {
278 super(IdeBundle
.message("button.configure.e"));
279 putValue(Action
.MNEMONIC_KEY
, new Integer('E'));
282 public void actionPerformed(ActionEvent e
) {