diff --git a/platform/lang-impl/src/com/intellij/execution/ui/CommonProgramParametersPanel.java b/platform/lang-impl/src/com/intellij/execution/ui/CommonProgramParametersPanel.java index 7a52248840ad..1f8350651577 100644 --- a/platform/lang-impl/src/com/intellij/execution/ui/CommonProgramParametersPanel.java +++ b/platform/lang-impl/src/com/intellij/execution/ui/CommonProgramParametersPanel.java @@ -21,7 +21,6 @@ import com.intellij.execution.configuration.EnvironmentVariablesComponent; import com.intellij.icons.AllIcons; import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.application.PathMacros; -import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.module.Module; @@ -30,21 +29,21 @@ import com.intellij.openapi.ui.FixedSizeButton; import com.intellij.openapi.ui.LabeledComponent; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.ui.VerticalFlowLayout; -import com.intellij.openapi.ui.popup.JBPopup; import com.intellij.openapi.ui.popup.JBPopupFactory; -import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.MacroAwareTextBrowseFolderListener; import com.intellij.ui.PanelWithAnchor; import com.intellij.ui.RawCommandLineEditor; import com.intellij.ui.components.JBList; import com.intellij.util.ArrayUtil; -import com.intellij.util.Consumer; import com.intellij.util.PathUtil; +import com.intellij.util.SmartList; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jps.model.serialization.PathMacroUtil; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.ArrayList; import java.util.List; public class CommonProgramParametersPanel extends JPanel implements PanelWithAnchor { @@ -55,7 +54,6 @@ public class CommonProgramParametersPanel extends JPanel implements PanelWithAnc protected JComponent myAnchor; private Module myModuleContext = null; - private boolean myHaveModuleContext = false; public CommonProgramParametersPanel() { super(); @@ -66,29 +64,27 @@ public class CommonProgramParametersPanel extends JPanel implements PanelWithAnc updateUI(); } + @Nullable + protected Project getProject() { + return myModuleContext != null ? myModuleContext.getProject() : null; + } + protected void initComponents() { myProgramParametersComponent = LabeledComponent.create(new RawCommandLineEditor(), ExecutionBundle.message("run.configuration.program.parameters")); - myWorkingDirectoryField = new TextFieldWithBrowseButton(new ActionListener() { + + FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); + //noinspection DialogTitleCapitalization + fileChooserDescriptor.setTitle(ExecutionBundle.message("select.working.directory.message")); + myWorkingDirectoryField = new TextFieldWithBrowseButton(); + myWorkingDirectoryField.addBrowseFolderListener(new MacroAwareTextBrowseFolderListener(fileChooserDescriptor, getProject()) { @Override public void actionPerformed(ActionEvent e) { - FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); - fileChooserDescriptor.setTitle(ExecutionBundle.message("select.working.directory.message")); - fileChooserDescriptor.putUserData(LangDataKeys.MODULE_CONTEXT, myModuleContext); - Project project = myModuleContext != null ? myModuleContext.getProject() : null; - FileChooser.chooseFile(fileChooserDescriptor, project, myWorkingDirectoryComponent, null, new Consumer() { - @Override - public void consume(VirtualFile file) { - setWorkingDirectory(file.getPresentableUrl()); - } - }); + myFileChooserDescriptor.putUserData(LangDataKeys.MODULE_CONTEXT, myModuleContext); + setProject(getProject()); + super.actionPerformed(e); } - }) { - @Override - protected void installPathCompletion(FileChooserDescriptor fileChooserDescriptor) { - super.installPathCompletion(FileChooserDescriptorFactory.createSingleFolderDescriptor()); - } - }; + }); final JPanel panel = new JPanel(new BorderLayout()); panel.add(myWorkingDirectoryField, BorderLayout.CENTER); @@ -98,11 +94,13 @@ public class CommonProgramParametersPanel extends JPanel implements PanelWithAnc button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - final List macros = new ArrayList(PathMacros.getInstance().getUserMacroNames()); - if (myHaveModuleContext) macros.add("MODULE_DIR"); + final List macros = new SmartList(PathMacros.getInstance().getUserMacroNames()); + if (myModuleContext != null) { + macros.add(PathMacroUtil.MODULE_DIR_MACRO_NAME); + } final JList list = new JBList(ArrayUtil.toStringArray(macros)); - final JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(list).setItemChoosenCallback(new Runnable() { + JBPopupFactory.getInstance().createListPopupBuilder(list).setItemChoosenCallback(new Runnable() { @Override public void run() { final Object value = list.getSelectedValue(); @@ -110,8 +108,7 @@ public class CommonProgramParametersPanel extends JPanel implements PanelWithAnc setWorkingDirectory("$" + value + "$"); } } - }).setMovable(false).setResizable(false).createPopup(); - popup.showUnderneathOf(button); + }).setMovable(false).setResizable(false).createPopup().showUnderneathOf(button); } }); panel.add(button, BorderLayout.EAST); @@ -157,7 +154,6 @@ public class CommonProgramParametersPanel extends JPanel implements PanelWithAnc public void setModuleContext(Module moduleContext) { myModuleContext = moduleContext; - myHaveModuleContext = true; } public LabeledComponent getProgramParametersComponent() { diff --git a/platform/lang-impl/src/com/intellij/ui/MacroAwareTextBrowseFolderListener.java b/platform/lang-impl/src/com/intellij/ui/MacroAwareTextBrowseFolderListener.java new file mode 100644 index 000000000000..3e30d74bcfd2 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/ui/MacroAwareTextBrowseFolderListener.java @@ -0,0 +1,36 @@ +package com.intellij.ui; + +import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.components.PathMacroManager; +import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.TextBrowseFolderListener; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class MacroAwareTextBrowseFolderListener extends TextBrowseFolderListener { + public MacroAwareTextBrowseFolderListener(@NotNull FileChooserDescriptor fileChooserDescriptor, + @Nullable Project project) { + super(fileChooserDescriptor, project); + } + + @NotNull + @Override + protected String expandPath(@NotNull String path) { + Project project = getProject(); + if (project != null) { + path = PathMacroManager.getInstance(project).expandPath(path); + } + + Module module = myFileChooserDescriptor.getUserData(LangDataKeys.MODULE_CONTEXT); + if (module == null) { + module = myFileChooserDescriptor.getUserData(LangDataKeys.MODULE); + } + if (module != null) { + path = PathMacroManager.getInstance(module).expandPath(path); + } + + return super.expandPath(path); + } +} \ No newline at end of file diff --git a/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java b/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java index 23c916ee276b..176ffd90c17f 100644 --- a/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java +++ b/platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java @@ -45,7 +45,6 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; -import java.util.List; public class ComponentWithBrowseButton extends JPanel implements Disposable { private final Comp myComponent; @@ -191,7 +190,7 @@ public class ComponentWithBrowseButton extends JPanel i private final String myDescription; protected ComponentWithBrowseButton myTextComponent; private final TextComponentAccessor myAccessor; - private final Project myProject; + private Project myProject; protected final FileChooserDescriptor myFileChooserDescriptor; public BrowseFolderActionListener(@Nullable String title, @Nullable String description, ComponentWithBrowseButton textField, @Nullable Project project, FileChooserDescriptor fileChooserDescriptor, TextComponentAccessor accessor) { @@ -203,20 +202,32 @@ public class ComponentWithBrowseButton extends JPanel i myAccessor = accessor; } + @Nullable + protected Project getProject() { + return myProject; + } + + protected void setProject(@Nullable Project project) { + myProject = project; + } + @Override - public void actionPerformed(ActionEvent e){ - FileChooserDescriptor fileChooserDescriptor = (FileChooserDescriptor)myFileChooserDescriptor.clone(); - if (myTitle != null) { - fileChooserDescriptor.setTitle(myTitle); - } - if (myDescription != null) { - fileChooserDescriptor.setDescription(myDescription); + public void actionPerformed(ActionEvent e) { + FileChooserDescriptor fileChooserDescriptor = myFileChooserDescriptor; + if (myTitle != null || myDescription != null) { + fileChooserDescriptor = (FileChooserDescriptor)myFileChooserDescriptor.clone(); + if (myTitle != null) { + fileChooserDescriptor.setTitle(myTitle); + } + if (myDescription != null) { + fileChooserDescriptor.setDescription(myDescription); + } } - FileChooser.chooseFiles(fileChooserDescriptor, myProject, getInitialFile(), new Consumer>() { + FileChooser.chooseFile(fileChooserDescriptor, getProject(), getInitialFile(), new Consumer() { @Override - public void consume(List files) { - onFileChoosen(files.get(0)); + public void consume(VirtualFile file) { + onFileChoosen(file); } }); } @@ -229,7 +240,7 @@ public class ComponentWithBrowseButton extends JPanel i } directoryName = FileUtil.toSystemIndependentName(directoryName); - VirtualFile path = LocalFileSystem.getInstance().findFileByPath(directoryName); + VirtualFile path = LocalFileSystem.getInstance().findFileByPath(expandPath(directoryName)); while (path == null && directoryName.length() > 0) { int pos = directoryName.lastIndexOf('/'); if (pos <= 0) break; @@ -239,6 +250,11 @@ public class ComponentWithBrowseButton extends JPanel i return path; } + @NotNull + protected String expandPath(@NotNull String path) { + return path; + } + protected String getComponentText() { return myAccessor.getText(myTextComponent.getChildComponent()).trim(); }