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
.openapi
.roots
.ui
.configuration
;
18 import com
.intellij
.ide
.DataManager
;
19 import com
.intellij
.ide
.util
.projectWizard
.ProjectJdkListRenderer
;
20 import com
.intellij
.openapi
.actionSystem
.DefaultActionGroup
;
21 import com
.intellij
.openapi
.application
.ApplicationBundle
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.project
.ProjectBundle
;
24 import com
.intellij
.openapi
.projectRoots
.Sdk
;
25 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.JdkListConfigurable
;
26 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.ProjectJdksModel
;
27 import com
.intellij
.openapi
.ui
.popup
.JBPopupFactory
;
28 import com
.intellij
.openapi
.util
.Computable
;
29 import com
.intellij
.openapi
.util
.Condition
;
30 import com
.intellij
.ui
.SimpleTextAttributes
;
31 import com
.intellij
.ui
.ScreenUtil
;
32 import com
.intellij
.util
.Consumer
;
36 import java
.awt
.event
.ActionEvent
;
37 import java
.awt
.event
.ActionListener
;
38 import java
.util
.ArrayList
;
39 import java
.util
.Arrays
;
40 import java
.util
.Collections
;
41 import java
.util
.Comparator
;
44 * @author Eugene Zhuravlev
47 class JdkComboBox
extends JComboBox
{
48 private final JButton myEditButton
= new JButton(ApplicationBundle
.message("button.edit"));
50 public JdkComboBox(final ProjectJdksModel jdksModel
) {
51 super(new JdkComboBoxModel(jdksModel
));
52 setRenderer(new ProjectJdkListRenderer() {
53 protected void customizeCellRenderer(JList list
, Object value
, int index
, boolean selected
, boolean hasFocus
) {
54 if (JdkComboBox
.this.isEnabled()) {
55 if (value
instanceof InvalidJdkComboBoxItem
) {
56 final String str
= value
.toString();
57 append(str
, SimpleTextAttributes
.ERROR_ATTRIBUTES
);
59 else if (value
instanceof ProjectJdkComboBoxItem
){
60 final ProjectJdkComboBoxItem item
= (ProjectJdkComboBoxItem
)value
;
61 final String str
= item
.toString();
62 final Sdk jdk
= jdksModel
.getProjectJdk();
64 setIcon(jdk
.getSdkType().getIcon());
65 append(ProjectBundle
.message("project.roots.project.jdk.inherited"), SimpleTextAttributes
.REGULAR_ATTRIBUTES
);
66 append(" (" + jdk
.getName() + ")", SimpleTextAttributes
.GRAYED_ATTRIBUTES
);
68 append(str
, SimpleTextAttributes
.ERROR_ATTRIBUTES
);
72 super.customizeCellRenderer(list
, value
!= null ?
((JdkComboBoxItem
)value
).getJdk() : new NoneJdkComboBoxItem(), index
, selected
, hasFocus
);
80 public Dimension
getPreferredSize() {
81 final Rectangle rec
= ScreenUtil
.getScreenRectangle(0, 0);
82 final Dimension size
= super.getPreferredSize();
83 final int maxWidth
= rec
.width
/ 4;
84 if (size
.width
> maxWidth
) {
85 size
.width
= maxWidth
;
91 public Dimension
getMinimumSize() {
92 final Dimension minSize
= super.getMinimumSize();
93 final Dimension prefSize
= getPreferredSize();
94 if (minSize
.width
> prefSize
.width
) {
95 minSize
.width
= prefSize
.width
;
100 public JButton
createSetupButton(final Project project
, final ProjectJdksModel jdksModel
, final JdkComboBoxItem firstItem
) {
101 return createSetupButton(project
, jdksModel
, firstItem
, null, false);
105 public JButton
createSetupButton(final Project project
,
106 final ProjectJdksModel jdksModel
,
107 final JdkComboBoxItem firstItem
,
108 final Condition
<Sdk
> additionalSetup
,
109 final boolean moduleJdkSetup
) {
110 final JButton setUpButton
= new JButton(ApplicationBundle
.message("button.new"));
111 setUpButton
.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e
) {
113 final JdkListConfigurable configurable
= JdkListConfigurable
.getInstance(project
);
114 DefaultActionGroup group
= new DefaultActionGroup();
115 jdksModel
.createAddActions(group
, JdkComboBox
.this, new Consumer
<Sdk
>() {
116 public void consume(final Sdk jdk
) {
117 configurable
.addJdkNode(jdk
, false);
118 reloadModel(firstItem
, project
);
119 setSelectedJdk(jdk
); //restore selection
120 if (additionalSetup
!= null) {
121 if (additionalSetup
.value(jdk
)) { //leave old selection
122 setSelectedJdk(firstItem
.getJdk());
127 JBPopupFactory
.getInstance()
128 .createActionGroupPopup(ProjectBundle
.message("project.roots.set.up.jdk.title", moduleJdkSetup ?
1 : 2), group
,
129 DataManager
.getInstance().getDataContext(), JBPopupFactory
.ActionSelectionAid
.MNEMONICS
, false)
130 .showUnderneathOf(setUpButton
);
136 public void appendEditButton(final Project project
, final JPanel panel
, GridBagConstraints gc
, final Computable
<Sdk
> retrieveJDK
){
137 myEditButton
.addActionListener(new ActionListener() {
138 public void actionPerformed(ActionEvent e
) {
139 final Sdk projectJdk
= retrieveJDK
.compute();
140 ProjectStructureConfigurable
.getInstance(project
).select(projectJdk
, true);
143 addActionListener(new ActionListener(){
144 public void actionPerformed(ActionEvent e
) {
145 final JdkComboBoxItem selectedItem
= getSelectedItem();
146 if (selectedItem
instanceof ProjectJdkComboBoxItem
) {
147 myEditButton
.setEnabled(ProjectStructureConfigurable
.getInstance(project
).getProjectJdksModel().getProjectJdk() != null);
149 myEditButton
.setEnabled(!(selectedItem
instanceof InvalidJdkComboBoxItem
) && selectedItem
!= null && selectedItem
.getJdk() != null);
153 panel
.add(myEditButton
, gc
);
156 public JdkComboBoxItem
getSelectedItem() {
157 return (JdkComboBoxItem
)super.getSelectedItem();
160 public Sdk
getSelectedJdk() {
161 final JdkComboBoxItem selectedItem
= (JdkComboBoxItem
)super.getSelectedItem();
162 return selectedItem
!= null? selectedItem
.getJdk() : null;
165 public void setSelectedJdk(Sdk jdk
) {
166 final int index
= indexOf(jdk
);
168 setSelectedIndex(index
);
172 public void setInvalidJdk(String name
) {
173 removeInvalidElement();
174 addItem(new InvalidJdkComboBoxItem(name
));
175 setSelectedIndex(getModel().getSize() - 1);
178 private int indexOf(Sdk jdk
) {
179 final JdkComboBoxModel model
= (JdkComboBoxModel
)getModel();
180 final int count
= model
.getSize();
181 for (int idx
= 0; idx
< count
; idx
++) {
182 final JdkComboBoxItem elementAt
= model
.getElementAt(idx
);
184 if (elementAt
instanceof NoneJdkComboBoxItem
) {
186 } else if (elementAt
instanceof ProjectJdkComboBoxItem
){
191 if (jdk
.equals(elementAt
.getJdk())) {
199 private void removeInvalidElement() {
200 final JdkComboBoxModel model
= (JdkComboBoxModel
)getModel();
201 final int count
= model
.getSize();
202 for (int idx
= 0; idx
< count
; idx
++) {
203 final JdkComboBoxItem elementAt
= model
.getElementAt(idx
);
204 if (elementAt
instanceof InvalidJdkComboBoxItem
) {
211 public void reloadModel(JdkComboBoxItem firstItem
, Project project
) {
212 final DefaultComboBoxModel model
= ((DefaultComboBoxModel
)getModel());
213 model
.removeAllElements();
214 model
.addElement(firstItem
);
215 final ArrayList
<Sdk
> projectJdks
= new ArrayList
<Sdk
>(ProjectJdksModel
.getInstance(project
).getProjectJdks().values());
216 Collections
.sort(projectJdks
, new Comparator
<Sdk
>() {
217 public int compare(final Sdk o1
, final Sdk o2
) {
218 return o1
.getName().compareToIgnoreCase(o2
.getName());
221 for (Sdk projectJdk
: projectJdks
) {
222 model
.addElement(new JdkComboBox
.JdkComboBoxItem(projectJdk
));
226 private static class JdkComboBoxModel
extends DefaultComboBoxModel
{
227 public JdkComboBoxModel(final ProjectJdksModel jdksModel
) {
229 final Sdk
[] jdks
= jdksModel
.getSdks();
230 Arrays
.sort(jdks
, new Comparator
<Sdk
>() {
231 public int compare(final Sdk s1
, final Sdk s2
) {
232 return s1
.getName().compareToIgnoreCase(s2
.getName());
235 for (Sdk jdk
: jdks
) {
236 addElement(new JdkComboBoxItem(jdk
));
240 // implements javax.swing.ListModel
241 public JdkComboBoxItem
getElementAt(int index
) {
242 return (JdkComboBoxItem
)super.getElementAt(index
);
246 public static class JdkComboBoxItem
{
247 private final Sdk myJdk
;
249 public JdkComboBoxItem(Sdk jdk
) {
253 public Sdk
getJdk() {
257 public String
toString() {
258 return myJdk
.getName();
262 public static class ProjectJdkComboBoxItem
extends JdkComboBoxItem
{
263 public ProjectJdkComboBoxItem() {
267 public String
toString() {
268 return ProjectBundle
.message("jdk.combo.box.project.item");
272 public static class NoneJdkComboBoxItem
extends JdkComboBoxItem
{
273 public NoneJdkComboBoxItem() {
277 public String
toString() {
278 return ProjectBundle
.message("jdk.combo.box.none.item");
282 private static class InvalidJdkComboBoxItem
extends JdkComboBoxItem
{
283 private final String myName
;
285 public InvalidJdkComboBoxItem(String name
) {
287 myName
= ProjectBundle
.message("jdk.combo.box.invalid.item", name
);
290 public String
toString() {