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.
17 package com
.intellij
.openapi
.roots
.ui
.configuration
;
19 import com
.intellij
.openapi
.module
.Module
;
20 import com
.intellij
.openapi
.options
.ConfigurationException
;
21 import com
.intellij
.openapi
.options
.UnnamedConfigurable
;
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
.projectRoots
.SdkModel
;
26 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
27 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.ModuleStructureConfigurable
;
28 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.ProjectJdksModel
;
29 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.StructureConfigurableContext
;
30 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.daemon
.ModuleProjectStructureElement
;
31 import com
.intellij
.openapi
.util
.Comparing
;
32 import com
.intellij
.openapi
.util
.Computable
;
33 import org
.jetbrains
.annotations
.Nullable
;
37 import java
.awt
.event
.ActionEvent
;
38 import java
.awt
.event
.ActionListener
;
44 public class ProjectJdkConfigurable
implements UnnamedConfigurable
{
45 private JdkComboBox myCbProjectJdk
;
46 private JPanel myJdkPanel
;
47 private final Project myProject
;
48 private final ProjectJdksModel myJdksModel
;
49 private final SdkModel
.Listener myListener
= new SdkModel
.Listener() {
50 public void sdkAdded(Sdk sdk
) {
54 public void beforeSdkRemove(Sdk sdk
) {
58 public void sdkChanged(Sdk sdk
, String previousName
) {
62 public void sdkHomeSelected(Sdk sdk
, String newSdkHome
) {
67 private boolean myFreeze
= false;
69 public ProjectJdkConfigurable(Project project
, final ProjectJdksModel jdksModel
) {
71 myJdksModel
= jdksModel
;
72 myJdksModel
.addListener(myListener
);
77 public Sdk
getSelectedProjectJdk() {
78 return myJdksModel
.findSdk(myCbProjectJdk
.getSelectedJdk());
81 public JComponent
createComponent() {
85 private void reloadModel() {
87 final Sdk projectJdk
= myJdksModel
.getProjectJdk();
88 myCbProjectJdk
.reloadModel(new JdkComboBox
.NoneJdkComboBoxItem(), myProject
);
89 final String sdkName
= projectJdk
== null ? ProjectRootManager
.getInstance(myProject
).getProjectJdkName() : projectJdk
.getName();
90 if (sdkName
!= null) {
91 final Sdk jdk
= myJdksModel
.findSdk(sdkName
);
93 myCbProjectJdk
.setSelectedJdk(jdk
);
95 myCbProjectJdk
.setInvalidJdk(sdkName
);
99 myCbProjectJdk
.setSelectedJdk(null);
104 private void init() {
105 myJdkPanel
= new JPanel(new GridBagLayout());
106 myCbProjectJdk
= new JdkComboBox(myJdksModel
);
107 myCbProjectJdk
.insertItemAt(new JdkComboBox
.NoneJdkComboBoxItem(), 0);
108 myCbProjectJdk
.addActionListener(new ActionListener() {
109 public void actionPerformed(ActionEvent e
) {
110 if (myFreeze
) return;
111 final Sdk oldJdk
= myJdksModel
.getProjectJdk();
112 myJdksModel
.setProjectJdk(myCbProjectJdk
.getSelectedJdk());
116 myJdkPanel
.add(new JLabel(ProjectBundle
.message("module.libraries.target.jdk.project.radio")), new GridBagConstraints(0, 0, 3, 1, 0, 0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(0, 0, 4, 0), 0, 0));
117 myJdkPanel
.add(myCbProjectJdk
, new GridBagConstraints(0, 1, 1, 1, 0, 1.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(0, 4, 0, 0), 0, 0));
118 final JButton setUpButton
= myCbProjectJdk
.createSetupButton(myProject
, myJdksModel
, new JdkComboBox
.NoneJdkComboBoxItem());
119 myJdkPanel
.add(setUpButton
, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints
.WEST
, GridBagConstraints
.NONE
, new Insets(0, 4, 0, 0), 0, 0));
120 myCbProjectJdk
.appendEditButton(myProject
, myJdkPanel
, new GridBagConstraints(GridBagConstraints
.RELATIVE
, 1, 1, 1, 1.0, 0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(0, 4, 0, 0), 0, 0), new Computable
<Sdk
>() {
122 public Sdk
compute() {
123 return myJdksModel
.getProjectJdk();
128 private void clearCaches() {
129 final ModuleStructureConfigurable rootConfigurable
= ModuleStructureConfigurable
.getInstance(myProject
);
130 Module
[] modules
= rootConfigurable
.getModules();
131 for (Module module
: modules
) {
132 final StructureConfigurableContext context
= rootConfigurable
.getContext();
133 context
.getDaemonAnalyzer().queueUpdate(new ModuleProjectStructureElement(context
, module
));
137 public boolean isModified() {
138 final Sdk projectJdk
= ProjectRootManager
.getInstance(myProject
).getProjectJdk();
139 return !Comparing
.equal(projectJdk
, getSelectedProjectJdk());
142 public void apply() throws ConfigurationException
{
143 ProjectRootManager
.getInstance(myProject
).setProjectJdk(getSelectedProjectJdk());
146 public void reset() {
149 final String sdkName
= ProjectRootManager
.getInstance(myProject
).getProjectJdkName();
150 if (sdkName
!= null) {
151 final Sdk jdk
= myJdksModel
.findSdk(sdkName
);
153 myCbProjectJdk
.setSelectedJdk(jdk
);
155 myCbProjectJdk
.setInvalidJdk(sdkName
);
158 myCbProjectJdk
.setSelectedJdk(null);
162 public void disposeUIResources() {
163 myJdksModel
.removeListener(myListener
);
165 myCbProjectJdk
= null;