From 1ba77ed2de0fc6eb2f00119c503deac0529c1705 Mon Sep 17 00:00:00 2001 From: Alexey Pegov Date: Thu, 14 Oct 2010 18:35:56 +0400 Subject: [PATCH] IDEA-57435 Use specified project name instead of directory name in case of directory-based project --- .../newProjectWizard/ProjectNameStep.java | 13 ------ .../ui/configuration/ProjectConfigurable.java | 44 +++++++++++++++++++ .../ide/RecentProjectsManagerBase.java | 41 ++++++++++++++++- .../impl/stores/ProjectStoreImpl.java | 33 ++++++++++++-- .../openapi/project/ex/ProjectEx.java | 2 + .../openapi/project/impl/ProjectImpl.java | 39 +++++++++++++++- .../src/com/intellij/mock/MockProject.java | 4 ++ 7 files changed, 156 insertions(+), 20 deletions(-) diff --git a/java/idea-ui/src/com/intellij/ide/util/newProjectWizard/ProjectNameStep.java b/java/idea-ui/src/com/intellij/ide/util/newProjectWizard/ProjectNameStep.java index 265099825f8e..7a6d9951771f 100644 --- a/java/idea-ui/src/com/intellij/ide/util/newProjectWizard/ProjectNameStep.java +++ b/java/idea-ui/src/com/intellij/ide/util/newProjectWizard/ProjectNameStep.java @@ -30,8 +30,6 @@ import com.intellij.projectImport.ProjectFormatPanel; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.io.File; /** @@ -75,17 +73,6 @@ public class ProjectNameStep extends ModuleWizardStep { myNamePathComponent.setVisible(myWizardContext.getProject() == null); myFormatPanel.setVisible(myWizardContext.getProject() == null); - - final Object selectedProjectFormat = myFormatPanel.getStorageFormatComboBox().getSelectedItem(); - myNamePathComponent.syncNameToPath(ProjectFormatPanel.DIR_BASED.equals(selectedProjectFormat)); - - myFormatPanel.getStorageFormatComboBox().addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - final Object o = myFormatPanel.getStorageFormatComboBox().getSelectedItem(); - myNamePathComponent.syncNameToPath(ProjectFormatPanel.DIR_BASED.equals(o)); - } - }); - myAdditionalContentPanel = new JPanel(new GridBagLayout()); 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)); } diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ProjectConfigurable.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ProjectConfigurable.java index 87c3740e0fad..bbd0bff46540 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ProjectConfigurable.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ProjectConfigurable.java @@ -19,11 +19,13 @@ package com.intellij.openapi.roots.ui.configuration; import com.intellij.compiler.ModuleCompilerUtil; import com.intellij.ide.util.BrowseFilesListener; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.StorageScheme; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.FileChooserFactory; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectBundle; +import com.intellij.openapi.project.ex.ProjectEx; import com.intellij.openapi.roots.CompilerProjectExtension; import com.intellij.openapi.roots.LanguageLevelProjectExtension; import com.intellij.openapi.roots.ModifiableRootModel; @@ -71,6 +73,8 @@ public class ProjectConfigurable extends NamedConfigurable implements D private FieldPanel myProjectCompilerOutput; + private JTextField myProjectName; + private MyJPanel myPanel; private final Alarm myUpdateWarningAlarm = new Alarm(Alarm.ThreadToUse.SHARED_THREAD); @@ -107,6 +111,30 @@ public class ProjectConfigurable extends NamedConfigurable implements D myPanel = new MyJPanel(); myPanel.setPreferredSize(new Dimension(700, 500)); + if (((ProjectEx)myProject).getStateStore().getStorageScheme().equals(StorageScheme.DIRECTORY_BASED)) { + final JPanel namePanel = new JPanel(new BorderLayout()); + final JLabel label = + new JLabel("Project name:", SwingConstants.LEFT); + label.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0)); + namePanel.add(label, BorderLayout.NORTH); + + myProjectName = new JTextField(); + myProjectName.setColumns(40); + + final JPanel nameFieldPanel = new JPanel(); + nameFieldPanel.setLayout(new BoxLayout(nameFieldPanel, BoxLayout.X_AXIS)); + nameFieldPanel.add(Box.createHorizontalStrut(10)); + nameFieldPanel.add(myProjectName); + + namePanel.add(nameFieldPanel, BorderLayout.CENTER); + final JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.LEFT)); + wrapper.add(namePanel); + wrapper.setAlignmentX(0); + myPanel.add(wrapper, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0.0, 0.0, + GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, + new Insets(4, 0, 10, 0), 0, 0)); + } + myProjectJdkConfigurable = new ProjectJdkConfigurable(myProject, model); myPanel.add(myProjectJdkConfigurable.createComponent(), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, @@ -144,6 +172,10 @@ public class ProjectConfigurable extends NamedConfigurable implements D } myLanguageLevelCombo.reset(myProject); updateCircularDependencyWarning(); + + if (myProjectName != null) { + myProjectName.setText(myProject.getName()); + } } finally { myFreeze = false; @@ -193,6 +225,10 @@ public class ProjectConfigurable extends NamedConfigurable implements D public void apply() throws ConfigurationException { final CompilerProjectExtension compilerProjectExtension = CompilerProjectExtension.getInstance(myProject); + if (myProjectName != null && myProjectName.getText().trim().length() == 0) { + throw new ConfigurationException("Please, specify project name!"); + } + ApplicationManager.getApplication().runWriteAction(new Runnable() { public void run() { // set the output path first so that handlers of RootsChanged event sent after JDK is set @@ -216,6 +252,10 @@ public class ProjectConfigurable extends NamedConfigurable implements D LanguageLevelProjectExtension.getInstance(myProject).setLanguageLevel(newLevel); try { myProjectJdkConfigurable.apply(); + + if (myProjectName != null) { + ((ProjectEx)myProject).setProjectName(myProjectName.getText().trim()); + } } catch (ConfigurationException e) { //cant't be @@ -262,6 +302,10 @@ public class ProjectConfigurable extends NamedConfigurable implements D if (!Comparing.strEqual(FileUtil.toSystemIndependentName(VfsUtil.urlToPath(compilerOutput)), FileUtil.toSystemIndependentName(myProjectCompilerOutput.getText()))) return true; if (myProjectJdkConfigurable.isModified()) return true; + if (myProjectName != null) { + if (!myProjectName.getText().trim().equals(myProject.getName())) return true; + } + return false; } diff --git a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java index cf234642c68a..b00d261411f9 100644 --- a/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java +++ b/platform/platform-impl/src/com/intellij/ide/RecentProjectsManagerBase.java @@ -34,7 +34,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.event.InputEvent; -import java.io.File; +import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -223,7 +223,44 @@ public abstract class RecentProjectsManagerBase implements PersistentStateCompon public ReopenProjectAction(String projectPath) { myProjectPath = projectPath; - getTemplatePresentation().setText(projectPath, false); + + String _text = projectPath; + final String projectName = getProjectName(projectPath); + if (projectName != null) { + _text = String.format("%s (%s)", projectPath, projectName); + } + + getTemplatePresentation().setText(_text, false); + } + + @Nullable + private String getProjectName(String path) { + if (new File(path).isDirectory()) { + final File nameFile = new File(new File(path, Project.DIRECTORY_STORE_FOLDER), ".name"); + if (nameFile.exists()) { + BufferedReader in = null; + try { + in = new BufferedReader(new InputStreamReader(new FileInputStream(nameFile))); + final String name = in.readLine(); + if (name != null && name.length() > 0) return name.trim(); + } + catch (IOException e) { + // ignore + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // ignore + } + } + } + } + } + + return null; } public void actionPerformed(AnActionEvent e) { diff --git a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java index adf58d793b63..06641703960a 100644 --- a/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/components/impl/stores/ProjectStoreImpl.java @@ -47,10 +47,7 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.List; @@ -272,6 +269,34 @@ class ProjectStoreImpl extends BaseFileConfigurableStoreImpl implements IProject if (myScheme == StorageScheme.DIRECTORY_BASED) { final VirtualFile baseDir = getProjectBaseDir(); assert baseDir != null : "project file: " + (getProjectFile() == null ? "[NULL]" : getProjectFile().getPath()); + + final VirtualFile ideaDir = baseDir.findChild(".idea"); + if (ideaDir != null && ideaDir.isValid()) { + final VirtualFile nameFile = ideaDir.findChild(".name"); + if (nameFile != null && nameFile.isValid()) { + BufferedReader in = null; + try { + in = new BufferedReader(new InputStreamReader(nameFile.getInputStream())); + final String name = in.readLine(); + if (name != null && name.length() > 0) return name.trim(); + } + catch (IOException e) { + // ignore + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException e) { + // ignore + } + } + } + } + } + + return baseDir.getName().replace(":", ""); } diff --git a/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectEx.java b/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectEx.java index 0787795beda1..2adaf0e140b1 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectEx.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/ex/ProjectEx.java @@ -36,4 +36,6 @@ public interface ProjectEx extends Project { void setOptimiseTestLoadSpeed(boolean optimiseTestLoadSpeed); void checkUnknownMacros(final boolean showDialog); + + void setProjectName(String name); } diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java index 6ae1eefe8a29..5c57957c8264 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java @@ -46,8 +46,11 @@ import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.ex.MessagesEx; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Key; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.wm.WindowManager; +import com.intellij.openapi.wm.impl.FrameTitleBuilder; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,6 +59,7 @@ import org.picocontainer.defaults.CachingComponentAdapter; import org.picocontainer.defaults.ConstructorInjectionComponentAdapter; import javax.swing.event.HyperlinkEvent; +import java.io.File; import java.io.IOException; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -87,7 +91,9 @@ public class ProjectImpl extends ComponentManagerImpl implements ProjectEx { return isDisposed(); } }; - private final String myName; + + private String myName; + private String myOldName; public static Key CREATION_TIME = Key.create("ProjectImpl.CREATION_TIME"); @@ -103,6 +109,15 @@ public class ProjectImpl extends ComponentManagerImpl implements ProjectEx { myManager = manager; myName = isDefault() ? TEMPLATE_PROJECT_NAME : projectName == null ? getStateStore().getProjectName() : projectName; + if (!isDefault() && projectName != null && getStateStore().getStorageScheme().equals(StorageScheme.DIRECTORY_BASED)) myOldName = ""; // new project + } + + public void setProjectName(final String projectName) { + if (!projectName.equals(myName)) { + myOldName = myName; + myName = projectName; + WindowManager.getInstance().getFrame(this).setTitle(FrameTitleBuilder.getInstance().getProjectTitle(this)); + } } protected void boostrapPicoContainer() { @@ -248,6 +263,28 @@ public class ProjectImpl extends ComponentManagerImpl implements ProjectEx { if (mySavingInProgress.compareAndSet(false, true)) { try { + if (!isDefault()) { + final IProjectStore stateStore = getStateStore(); + if (stateStore.getStorageScheme().equals(StorageScheme.DIRECTORY_BASED)) { + final VirtualFile baseDir = stateStore.getProjectBaseDir(); + if (baseDir != null && baseDir.isValid()) { + if (myOldName != null && !myOldName.equals(getName())) { + final VirtualFile ideaDir = baseDir.findChild(DIRECTORY_STORE_FOLDER); + if (ideaDir != null && ideaDir.isValid() && ideaDir.isDirectory()) { + final File nameFile = new File(ideaDir.getPath(), ".name"); + try { + FileUtil.writeToFile(nameFile, new String(getName()).getBytes(), false); + myOldName = null; + } + catch (IOException e) { + LOG.info("Unable to store project name to: " + nameFile.getPath()); + } + } + } + } + } + } + doSave(); } catch (IComponentStore.SaveCancelledException e) { diff --git a/platform/testFramework/src/com/intellij/mock/MockProject.java b/platform/testFramework/src/com/intellij/mock/MockProject.java index 13a3e9cb3720..3bc0fb8c32df 100644 --- a/platform/testFramework/src/com/intellij/mock/MockProject.java +++ b/platform/testFramework/src/com/intellij/mock/MockProject.java @@ -37,6 +37,10 @@ public class MockProject extends MockComponentManager implements ProjectEx { return false; } + @Override + public void setProjectName(String name) { + } + @Override public void checkUnknownMacros(final boolean showDialog) { }