update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / projectRoot / JdkListConfigurable.java
blob531d5a90ee66afb1ed8ddca3ce187cd071ddfbdc
1 /*
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.projectRoot;
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.DefaultActionGroup;
21 import com.intellij.openapi.components.State;
22 import com.intellij.openapi.components.Storage;
23 import com.intellij.openapi.options.ConfigurationException;
24 import com.intellij.openapi.options.ShowSettingsUtil;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.project.ProjectBundle;
27 import com.intellij.openapi.projectRoots.Sdk;
28 import com.intellij.openapi.projectRoots.SdkModel;
29 import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl;
30 import com.intellij.openapi.roots.ProjectRootManager;
31 import com.intellij.openapi.roots.ui.configuration.ProjectStructureConfigurable;
32 import com.intellij.openapi.ui.MasterDetailsComponent;
33 import com.intellij.openapi.ui.NamedConfigurable;
34 import com.intellij.util.Consumer;
35 import org.jetbrains.annotations.Nls;
36 import org.jetbrains.annotations.NonNls;
37 import org.jetbrains.annotations.Nullable;
39 import javax.swing.*;
40 import javax.swing.tree.TreePath;
41 import java.util.HashMap;
43 @State(
44 name = "JdkListConfigurable.UI",
45 storages = {
46 @Storage(
47 id ="other",
48 file = "$WORKSPACE_FILE$"
51 public class JdkListConfigurable extends BaseStructureConfigurable {
53 private final ProjectJdksModel myJdksTreeModel;
56 SdkModel.Listener myListener = new SdkModel.Listener() {
57 public void sdkAdded(Sdk sdk) {
60 public void beforeSdkRemove(Sdk sdk) {
63 public void sdkChanged(Sdk sdk, String previousName) {
64 updateName();
67 public void sdkHomeSelected(Sdk sdk, String newSdkHome) {
68 updateName();
71 private void updateName() {
72 final TreePath path = myTree.getSelectionPath();
73 if (path != null) {
74 final NamedConfigurable configurable = ((MyNode)path.getLastPathComponent()).getConfigurable();
75 if (configurable != null && configurable instanceof JdkConfigurable) {
76 configurable.updateName();
82 public JdkListConfigurable(final Project project, ProjectStructureConfigurable root) {
83 super(project);
84 myJdksTreeModel = root.getProjectJdksModel();
85 myJdksTreeModel.addListener(myListener);
87 protected void processRemovedItems() {
90 protected boolean wasObjectStored(final Object editableObject) {
91 return false;
94 @Nls
95 public String getDisplayName() {
96 return "SDKs";
99 @Nullable
100 public Icon getIcon() {
101 return null;
104 @Nullable
105 @NonNls
106 public String getHelpTopic() {
107 return "reference.settingsdialog.project.structure.jdk";
110 @NonNls
111 public String getId() {
112 return "jdk.list";
115 @Nullable
116 public Runnable enableSearch(final String option) {
117 return null;
120 protected void loadTree() {
121 final HashMap<Sdk,Sdk> sdks = myJdksTreeModel.getProjectJdks();
122 for (Sdk sdk : sdks.keySet()) {
123 final JdkConfigurable configurable = new JdkConfigurable((ProjectJdkImpl)sdks.get(sdk), myJdksTreeModel, TREE_UPDATER, myHistory);
124 addNode(new MyNode(configurable), myRoot);
128 public boolean addJdkNode(final Sdk jdk, final boolean selectInTree) {
129 if (!myUiDisposed) {
130 addNode(new MyNode(new JdkConfigurable((ProjectJdkImpl)jdk, myJdksTreeModel, TREE_UPDATER, myHistory)), myRoot);
131 if (selectInTree) {
132 selectNodeInTree(MasterDetailsComponent.findNodeByObject(myRoot, jdk));
134 return true;
136 return false;
139 public void dispose() {
140 myJdksTreeModel.removeListener(myListener);
141 myJdksTreeModel.disposeUIResources();
144 public ProjectJdksModel getJdksTreeModel() {
145 return myJdksTreeModel;
148 public void reset() {
149 super.reset();
150 myTree.setRootVisible(false);
153 public void apply() throws ConfigurationException {
154 boolean modifiedJdks = false;
155 for (int i = 0; i < myRoot.getChildCount(); i++) {
156 final NamedConfigurable configurable = ((MyNode)myRoot.getChildAt(i)).getConfigurable();
157 if (configurable.isModified()) {
158 configurable.apply();
159 modifiedJdks = true;
163 if (myJdksTreeModel.isModified() || modifiedJdks) myJdksTreeModel.apply(this);
164 myJdksTreeModel.setProjectJdk(ProjectRootManager.getInstance(myProject).getProjectJdk());
167 public boolean isModified() {
168 return super.isModified() || myJdksTreeModel.isModified();
171 public static JdkListConfigurable getInstance(Project project) {
172 return ShowSettingsUtil.getInstance().findProjectConfigurable(project, JdkListConfigurable.class);
175 public AbstractAddGroup createAddAction() {
176 return new AbstractAddGroup(ProjectBundle.message("add.new.jdk.text")) {
177 public AnAction[] getChildren(@Nullable final AnActionEvent e) {
178 DefaultActionGroup group = new DefaultActionGroup(ProjectBundle.message("add.new.jdk.text"), true);
179 myJdksTreeModel.createAddActions(group, myTree, new Consumer<Sdk>() {
180 public void consume(final Sdk projectJdk) {
181 addJdkNode(projectJdk, true);
184 return group.getChildren(null);
189 protected void removeJdk(final Sdk jdk) {
190 myJdksTreeModel.removeJdk(jdk);
191 myContext.myJdkDependencyCache.remove(jdk);
192 myContext.myValidityCache.clear();
195 protected
196 @Nullable
197 String getEmptySelectionString() {
198 return "Select a JDK to view or edit its details here";