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
.newProjectWizard
;
18 import com
.intellij
.ide
.IdeBundle
;
19 import com
.intellij
.ide
.highlighter
.ProjectFileType
;
20 import com
.intellij
.ide
.highlighter
.ModuleFileType
;
21 import com
.intellij
.ide
.util
.newProjectWizard
.modes
.WizardMode
;
22 import com
.intellij
.ide
.util
.projectWizard
.*;
23 import com
.intellij
.openapi
.application
.ApplicationInfo
;
24 import com
.intellij
.openapi
.application
.ApplicationManager
;
25 import com
.intellij
.openapi
.options
.ConfigurationException
;
26 import com
.intellij
.openapi
.ui
.Messages
;
27 import com
.intellij
.openapi
.util
.IconLoader
;
28 import com
.intellij
.openapi
.util
.text
.StringUtil
;
29 import com
.intellij
.openapi
.components
.StorageScheme
;
36 * @author Eugene Zhuravlev
39 public class ProjectNameStep
extends ModuleWizardStep
{
40 private static final Icon NEW_PROJECT_ICON
= IconLoader
.getIcon("/newprojectwizard.png");
42 private final JPanel myPanel
;
43 protected final JPanel myAdditionalContentPanel
;
44 protected NamePathComponent myNamePathComponent
;
45 protected final WizardContext myWizardContext
;
46 protected final StepSequence mySequence
;
47 protected final WizardMode myMode
;
48 private static final String DIR_BASED
= ".idea (directory based)";
49 private static final String FILE_BASED
= ".ipr (file based)";
50 private final JComboBox myStorageFormatCombo
= new JComboBox();
53 public ProjectNameStep(WizardContext wizardContext
, StepSequence sequence
, final WizardMode mode
) {
54 myWizardContext
= wizardContext
;
55 mySequence
= sequence
;
57 myNamePathComponent
= new NamePathComponent(
58 IdeBundle
.message("label.project.name"),
59 IdeBundle
.message("label.project.files.location"),
60 IdeBundle
.message("title.select.project.file.directory", IdeBundle
.message("project.new.wizard.project.identification")),
61 IdeBundle
.message("description.select.project.file.directory", StringUtil
.capitalize(IdeBundle
.message("project.new.wizard.project.identification"))),
64 final String baseDir
= myWizardContext
.getProjectFileDirectory();
65 final String projectName
= myWizardContext
.getProjectName();
66 final String initialProjectName
= projectName
!= null ? projectName
: ProjectWizardUtil
.findNonExistingFileName(baseDir
, "untitled", "");
67 myNamePathComponent
.setPath(projectName
== null ?
(baseDir
+ File
.separator
+ initialProjectName
) : baseDir
);
68 myNamePathComponent
.setNameValue(initialProjectName
);
69 myNamePathComponent
.getNameComponent().setSelectionStart(0);
70 myNamePathComponent
.getNameComponent().setSelectionEnd(initialProjectName
.length());
71 myPanel
= new JPanel(new GridBagLayout());
72 myPanel
.setBorder(BorderFactory
.createCompoundBorder(BorderFactory
.createEtchedBorder(), BorderFactory
.createEmptyBorder(10, 10, 10, 10)));
73 myPanel
.add(myNamePathComponent
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(0, 4, 0, 0), 0, 0));
74 JPanel projectFileFormatPanel
= new JPanel(new BorderLayout());
75 myPanel
.add(projectFileFormatPanel
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(2, 6, 0, 0), 0, 0));
77 myNamePathComponent
.setVisible(myWizardContext
.getProject() == null);
78 projectFileFormatPanel
.setVisible(myWizardContext
.getProject() == null);
80 final JLabel label
= new JLabel("Project storage format:");
81 label
.setBorder(BorderFactory
.createEmptyBorder(0, 0, 0, 4));
82 projectFileFormatPanel
.add(label
, BorderLayout
.WEST
);
83 projectFileFormatPanel
.add(myStorageFormatCombo
, BorderLayout
.CENTER
);
84 myStorageFormatCombo
.insertItemAt(DIR_BASED
, 0);
85 myStorageFormatCombo
.insertItemAt(FILE_BASED
, 1);
87 myStorageFormatCombo
.setSelectedItem(DIR_BASED
);
89 myAdditionalContentPanel
= new JPanel(new GridBagLayout());
90 myPanel
.add(myAdditionalContentPanel
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 1.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.BOTH
, new Insets(0, 0, 0, 0), 0, 0));
93 public JComponent
getComponent() {
97 public void updateDataModel() {
98 myWizardContext
.setProjectName(getProjectName());
99 final String projectFileDirectory
= getProjectFileDirectory();
100 myWizardContext
.setProjectFileDirectory(projectFileDirectory
);
101 final ProjectBuilder moduleBuilder
= myMode
.getModuleBuilder();
102 myWizardContext
.setProjectBuilder(moduleBuilder
);
103 myWizardContext
.setProjectStorageFormat(getSelectedProjectStorageFormat());
104 if (moduleBuilder
instanceof SourcePathsBuilder
) {
105 ((SourcePathsBuilder
)moduleBuilder
).setContentEntryPath(projectFileDirectory
);
109 private StorageScheme
getSelectedProjectStorageFormat() {
110 return FILE_BASED
.equals(myStorageFormatCombo
.getSelectedItem()) ? StorageScheme
.DEFAULT
: StorageScheme
.DIRECTORY_BASED
;
113 public Icon
getIcon() {
114 return myWizardContext
.getProject() == null ? NEW_PROJECT_ICON
: ICON
;
117 public JComponent
getPreferredFocusedComponent() {
118 return myNamePathComponent
.getNameComponent();
121 public String
getHelpId() {
122 return "reference.dialogs.new.project.fromCode.name";
125 public String
getProjectFilePath() {
126 if (myWizardContext
.getProject() == null) {
127 if (getSelectedProjectStorageFormat() == StorageScheme
.DEFAULT
) {
128 return getProjectFileDirectory() + "/" + myNamePathComponent
.getNameValue() + ProjectFileType
.DOT_DEFAULT_EXTENSION
;
131 return getProjectFileDirectory() + "/" + ".idea";
135 return getProjectFileDirectory() + "/" + myNamePathComponent
.getNameValue() + ModuleFileType
.DOT_DEFAULT_EXTENSION
;
139 public String
getProjectFileDirectory() {
140 return myNamePathComponent
.getPath();
143 public String
getProjectName() {
144 return myNamePathComponent
.getNameValue();
147 public boolean validate() throws ConfigurationException
{
148 final String name
= myNamePathComponent
.getNameValue();
149 if (name
.length() == 0) {
150 final ApplicationInfo info
= ApplicationManager
.getApplication().getComponent(ApplicationInfo
.class);
151 throw new ConfigurationException(IdeBundle
.message("prompt.new.project.file.name", info
.getVersionName(), myWizardContext
.getPresentationName()));
154 final String projectFileDirectory
= getProjectFileDirectory();
155 if (projectFileDirectory
.length() == 0) {
156 throw new ConfigurationException(IdeBundle
.message("prompt.enter.project.file.location", myWizardContext
.getPresentationName()));
159 final boolean shouldPromptCreation
= myNamePathComponent
.isPathChangedByUser();
160 if (!ProjectWizardUtil
161 .createDirectoryIfNotExists(IdeBundle
.message("directory.project.file.directory", myWizardContext
.getPresentationName()), projectFileDirectory
, shouldPromptCreation
)) {
165 boolean shouldContinue
= true;
166 final File projectFile
= new File(getProjectFilePath());
167 if (projectFile
.exists()) {
168 int answer
= Messages
.showYesNoDialog(IdeBundle
.message("prompt.overwrite.project.file", projectFile
.getAbsolutePath(), myWizardContext
.getPresentationName()),
169 IdeBundle
.message("title.file.already.exists"), Messages
.getQuestionIcon());
170 shouldContinue
= (answer
== 0);
173 return shouldContinue
;