This commit is contained in:
Roman Shevchenko
2012-04-16 22:39:25 +02:00
parent 6b1e898711
commit 24f40ef0ad
2 changed files with 39 additions and 101 deletions

View File

@@ -21,7 +21,6 @@ import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
@@ -31,19 +30,18 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Producer;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.idea.maven.dom.MavenDomBundle;
import org.jetbrains.idea.maven.dom.MavenDomUtil;
import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
public class ChooseFileIntentionAction implements IntentionAction {
private FileChooserFactory myTestFileChooserFactory;
public void setTestFileChooserFactory(FileChooserFactory factory) {
myTestFileChooserFactory = factory;
}
private Producer<VirtualFile[]> myFileChooser = null;
@NotNull
public String getFamilyName() {
@@ -67,27 +65,38 @@ public class ChooseFileIntentionAction implements IntentionAction {
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final MavenDomDependency dep = getDependency(file, editor);
PsiFile currentValue = dep.getSystemPath().getValue();
final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, true, true, false, false);
VirtualFile[] files = FileChooser.chooseFiles(descriptor, project, currentValue == null ? null : currentValue.getVirtualFile());
if (files.length == 0) return;
final VirtualFile[] files;
if (myFileChooser == null) {
final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, true, true, false, false);
final PsiFile currentValue = dep != null ? dep.getSystemPath().getValue() : null;
final VirtualFile toSelect = currentValue == null ? null : currentValue.getVirtualFile();
files = FileChooser.chooseFiles(descriptor, project, toSelect);
}
else {
files = myFileChooser.produce();
}
if (files == null || files.length == 0) return;
final PsiFile selectedFile = PsiManager.getInstance(project).findFile(files[0]);
if (selectedFile == null) return;
new WriteCommandAction(project) {
protected void run(Result result) throws Throwable {
dep.getSystemPath().setValue(selectedFile);
}
}.execute();
if (dep != null) {
new WriteCommandAction(project) {
protected void run(Result result) throws Throwable {
dep.getSystemPath().setValue(selectedFile);
}
}.execute();
}
}
private FileChooserFactory getFileChooserFactory() {
if (myTestFileChooserFactory != null) return myTestFileChooserFactory;
return FileChooserFactory.getInstance();
@TestOnly
public void setFileChooser(@Nullable final Producer<VirtualFile[]> fileChooser) {
myFileChooser = fileChooser;
}
private MavenDomDependency getDependency(PsiFile file, Editor editor) {
@Nullable
private static MavenDomDependency getDependency(PsiFile file, Editor editor) {
PsiElement el = PsiUtilCore.getElementAtOffset(file, editor.getCaretModel().getOffset());
XmlTag tag = PsiTreeUtil.getParentOfType(el, XmlTag.class, false);

View File

@@ -17,25 +17,18 @@ package org.jetbrains.idea.maven.dom;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.impl.config.IntentionActionWrapper;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.fileChooser.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.formatter.xml.XmlCodeStyleSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.util.Producer;
import org.jetbrains.idea.maven.dom.intentions.ChooseFileIntentionAction;
import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
import javax.swing.*;
import java.awt.*;
public class MavenDependencyCompletionAndResolutionTest extends MavenDomWithIndicesTestCase {
@Override
protected void setUpInWriteAction() throws Exception {
@@ -586,23 +579,26 @@ public class MavenDependencyCompletionAndResolutionTest extends MavenDomWithIndi
assertNotNull(action);
String libPath = myIndicesFixture.getRepositoryHelper().getTestDataPath("local1/junit/junit/4.0/junit-4.0.jar");
VirtualFile libFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(libPath);
final VirtualFile libFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(libPath);
MyFileChooserFactory factory = new MyFileChooserFactory();
factory.setFiles(new VirtualFile[]{libFile});
((ChooseFileIntentionAction)((IntentionActionWrapper)action).getDelegate()).setTestFileChooserFactory(factory);
((ChooseFileIntentionAction)((IntentionActionWrapper)action).getDelegate()).setFileChooser(new Producer<VirtualFile[]>() {
@Override
public VirtualFile[] produce() {
return new VirtualFile[]{libFile};
}
});
XmlCodeStyleSettings xmlSettings =
CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings().getCustomSettings(XmlCodeStyleSettings.class);
int prevValue = xmlSettings.XML_TEXT_WRAP;
try {
// prevent file path from wrapping.
xmlSettings.XML_TEXT_WRAP = CodeStyleSettings.DO_NOT_WRAP;
xmlSettings.XML_TEXT_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
myFixture.launchAction(action);
}
finally {
xmlSettings.XML_TEXT_WRAP = prevValue;
((ChooseFileIntentionAction)((IntentionActionWrapper)action).getDelegate()).setTestFileChooserFactory(null);
((ChooseFileIntentionAction)((IntentionActionWrapper)action).getDelegate()).setFileChooser(null);
}
MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, myProjectPom);
@@ -1005,71 +1001,4 @@ public class MavenDependencyCompletionAndResolutionTest extends MavenDomWithIndi
checkHighlighting();
}
private static class MyFileChooserFactory extends FileChooserFactory {
private VirtualFile[] myFiles;
public void setFiles(VirtualFile[] files) {
myFiles = files;
}
@NotNull
@Override
public FileChooserDialog createFileChooser(@NotNull FileChooserDescriptor descriptor,
@Nullable Project project,
@Nullable Component parent) {
return new MyFileChooserDialog(myFiles);
}
@NotNull
@Override
public PathChooserDialog createPathChooser(@NotNull FileChooserDescriptor descriptor,
@Nullable Project project,
@Nullable Component parent) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public FileTextField createFileTextField(@NotNull FileChooserDescriptor descriptor, boolean showHidden, Disposable parent) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public FileTextField createFileTextField(@NotNull FileChooserDescriptor descriptor, Disposable parent) {
throw new UnsupportedOperationException();
}
@Override
public void installFileCompletion(@NotNull JTextField field, @NotNull FileChooserDescriptor descriptor, boolean showHidden, Disposable parent) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public FileSaverDialog createSaveFileDialog(@NotNull FileSaverDescriptor descriptor, Project project) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
public FileSaverDialog createSaveFileDialog(@NotNull FileSaverDescriptor descriptor, @NotNull Component parent) {
throw new UnsupportedOperationException();
}
}
private static class MyFileChooserDialog implements FileChooserDialog {
private final VirtualFile[] myFiles;
public MyFileChooserDialog(VirtualFile[] files) {
myFiles = files;
}
@Override
@NotNull
public VirtualFile[] choose(@Nullable VirtualFile toSelect, @Nullable Project project) {
return myFiles;
}
}
}