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
.options
.ConfigurationException
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.project
.ProjectManager
;
22 import com
.intellij
.openapi
.project
.ex
.ProjectManagerEx
;
23 import com
.intellij
.openapi
.projectRoots
.Sdk
;
24 import com
.intellij
.openapi
.projectRoots
.SdkType
;
25 import com
.intellij
.openapi
.roots
.ex
.ProjectRootManagerEx
;
26 import com
.intellij
.openapi
.roots
.ui
.configuration
.ProjectStructureConfigurable
;
27 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.JdkListConfigurable
;
28 import com
.intellij
.openapi
.roots
.ui
.configuration
.projectRoot
.ProjectJdksModel
;
29 import com
.intellij
.openapi
.ui
.Messages
;
30 import com
.intellij
.openapi
.ui
.MultiLineLabelUI
;
31 import com
.intellij
.openapi
.util
.IconLoader
;
32 import com
.intellij
.util
.Consumer
;
33 import com
.intellij
.util
.ui
.UIUtil
;
34 import org
.jetbrains
.annotations
.Nullable
;
38 import java
.awt
.event
.ActionEvent
;
39 import java
.awt
.event
.ActionListener
;
42 * @author Eugene Zhuravlev
45 public class ProjectJdkForModuleStep
extends ModuleWizardStep
{
46 private static final Icon NEW_PROJECT_ICON
= IconLoader
.getIcon("/newprojectwizard.png");
47 private final JdkChooserPanel myJdkChooser
;
48 private final JPanel myPanel
;
49 private final WizardContext myContext
;
50 private final SdkType myType
;
51 private boolean myInitialized
= false;
53 public ProjectJdkForModuleStep(final WizardContext context
, final SdkType type
) {
56 myJdkChooser
= new JdkChooserPanel(getProject(context
, type
));
58 myPanel
= new JPanel(new GridBagLayout());
59 myPanel
.setBorder(BorderFactory
.createEtchedBorder());
61 final JLabel label
= new JLabel(IdeBundle
.message("prompt.please.select.module.jdk", type
.getPresentableName()));
62 label
.setUI(new MultiLineLabelUI());
63 myPanel
.add(label
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 2, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
,
64 GridBagConstraints
.HORIZONTAL
, new Insets(8, 10, 8, 10), 0, 0));
66 final JLabel jdklabel
= new JLabel(IdeBundle
.message("label.project.jdk"));
67 jdklabel
.setFont(UIUtil
.getLabelFont().deriveFont(Font
.BOLD
));
68 myPanel
.add(jdklabel
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 2, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
,
69 GridBagConstraints
.NONE
, new Insets(8, 10, 0, 10), 0, 0));
71 myPanel
.add(myJdkChooser
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 1.0, GridBagConstraints
.NORTHWEST
,
72 GridBagConstraints
.BOTH
, new Insets(2, 10, 10, 5), 0, 0));
73 JButton configureButton
= new JButton(IdeBundle
.message("button.configure"));
74 myPanel
.add(configureButton
, new GridBagConstraints(1, GridBagConstraints
.RELATIVE
, 1, 1, 0.0, 1.0, GridBagConstraints
.NORTHWEST
,
75 GridBagConstraints
.NONE
, new Insets(2, 0, 10, 5), 0, 0));
77 configureButton
.addActionListener(new ActionListener() {
78 public void actionPerformed(ActionEvent e
) {
80 final Project project
= getProject(context
, type
);
81 final ProjectStructureConfigurable projectConfig
= ProjectStructureConfigurable
.getInstance(project
);
82 final JdkListConfigurable jdkConfig
= JdkListConfigurable
.getInstance(project
);
83 final ProjectJdksModel projectJdksModel
= projectConfig
.getProjectJdksModel();
84 final boolean[] successfullyAdded
= new boolean[1];
85 projectJdksModel
.doAdd(type
, myPanel
, new Consumer
<Sdk
>() {
86 public void consume(final Sdk jdk
) {
87 successfullyAdded
[0] = jdkConfig
.addJdkNode(jdk
, false);
88 myJdkChooser
.updateList(jdk
, type
);
91 if (!successfullyAdded
[0]) {
93 projectJdksModel
.apply(jdkConfig
);
95 catch (ConfigurationException e1
) {
105 private static Project
getProject(final WizardContext context
, final SdkType type
) {
106 Project project
= context
.getProject();
107 if (type
!= null && project
== null) { //'module' step inside project creation
108 project
= ProjectManager
.getInstance().getDefaultProject();
113 public JComponent
getPreferredFocusedComponent() {
114 return myJdkChooser
.getPreferredFocusedComponent();
117 public String
getHelpId() {
118 return "project.new.page2";
121 public JComponent
getComponent() {
125 public void updateDataModel() {
126 myContext
.setProjectJdk(getJdk());
130 public void updateStep() {
131 if (!myInitialized
) { //lazy default project initialization
132 myJdkChooser
.fillList(myType
);
133 Sdk defaultJdk
= getDefaultJdk();
134 if (defaultJdk
!= null) {
135 myJdkChooser
.selectJdk(defaultJdk
);
137 myInitialized
= true;
141 public Sdk
getJdk() {
142 return myJdkChooser
.getChosenJdk();
145 public Icon
getIcon() {
146 return NEW_PROJECT_ICON
;
150 private static Sdk
getDefaultJdk() {
151 Project defaultProject
= ProjectManagerEx
.getInstanceEx().getDefaultProject();
152 return ProjectRootManagerEx
.getInstanceEx(defaultProject
).getProjectJdk();
156 public boolean validate() {
157 final Sdk jdk
= myJdkChooser
.getChosenJdk();
159 int result
= Messages
.showOkCancelDialog(IdeBundle
.message("prompt.confirm.project.no.jdk"),
160 IdeBundle
.message("title.no.jdk.specified"), Messages
.getWarningIcon());