FileSaverDialog
[fedora-idea.git] / platform-impl / src / com / intellij / openapi / fileChooser / ex / FileSaverDialogImpl.java
blob51894b019c7ecebdc6540fcabf82ebaeb73cfea3
1 package com.intellij.openapi.fileChooser.ex;
3 import com.intellij.openapi.fileChooser.FileSystemTree;
4 import com.intellij.openapi.fileChooser.FileSaverDescriptor;
5 import com.intellij.openapi.fileChooser.FileSaverDialog;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.vfs.VirtualFile;
8 import com.intellij.openapi.vfs.VirtualFileWrapper;
9 import com.intellij.ui.DocumentAdapter;
10 import com.intellij.ui.UIBundle;
11 import org.jetbrains.annotations.Nullable;
13 import javax.swing.*;
14 import javax.swing.event.DocumentEvent;
15 import java.awt.*;
16 import java.io.File;
17 import java.util.List;
19 /**
20 * @author Konstantin Bulenkov
22 public class FileSaverDialogImpl extends FileChooserDialogImpl implements FileSaverDialog {
23 protected final JTextField myFileName = new JTextField(20);
24 protected final FileSaverDescriptor myDescriptor;
26 public FileSaverDialogImpl(FileSaverDescriptor chooserDescriptor, Project project) {
27 super(chooserDescriptor, project);
28 myDescriptor = chooserDescriptor;
29 setTitle(UIBundle.message("file.chooser.save.dialog.default.title"));
32 @Nullable
33 public VirtualFileWrapper save(@Nullable VirtualFile baseDir, @Nullable String filename) {
34 init();
35 myFileSystemTree.addListener(new FileSystemTree.Listener() {
36 public void selectionChanged(final List<VirtualFile> selection) {
37 updateFileName(selection);
38 updateOkButton();
40 }, myDisposable);
42 if (filename != null) {
43 myFileName.setText(filename);
46 if (baseDir != null && baseDir.isValid() && baseDir.isDirectory()) {
47 myFileSystemTree.select(baseDir, null);
50 show();
52 if (getExitCode() == OK_EXIT_CODE) {
53 final File file = getFile();
54 return file == null ? null : new VirtualFileWrapper(file);
56 return null;
59 @Nullable
60 protected File getFile() {
61 final VirtualFile selected = myFileSystemTree.getSelectedFile();
62 if (selected != null && !selected.isDirectory()) {
63 return new File(selected.getPath());
66 String path = (selected == null) ? myPathTextField.getTextFieldText() : selected.getPath();
67 final File dir = new File(path);
68 if (! dir.exists() || path == null) return null;
69 if (dir.isDirectory()) {
70 path += File.separator + myFileName.getText();
73 boolean correctExt = true;
74 for (String ext : myDescriptor.getFileExtentions()) {
75 correctExt = path.endsWith("." + ext);
76 if (correctExt) break;
79 if (!correctExt) {
80 path += "." + myDescriptor.getFileExtentions()[0];
83 return new File(path);
86 private void updateFileName(List<VirtualFile> selection) {
87 for (VirtualFile file : selection) {
88 if (file.isDirectory()) {
89 myPathTextField.getField().setText(file.getPath());
90 } else {
91 myFileName.setText(file.getName());
92 final VirtualFile parent = file.getParent();
93 if (parent != null) {
94 myPathTextField.getField().setText(parent.getPath());
98 if (selection.size() == 0) {
99 myFileName.setText("");
101 updateOkButton();
104 @Override
105 protected JComponent createCenterPanel() {
106 JComponent component = super.createCenterPanel();
107 MyPanel panel = new MyPanel();
108 panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
109 panel.add(component, BorderLayout.CENTER);
110 panel.add(createFileNamePanel(), BorderLayout.SOUTH);
111 return panel;
114 protected JComponent createFileNamePanel() {
115 final JPanel panel = new JPanel(new BorderLayout());
116 panel.add(new JLabel(UIBundle.message("file.chooser.save.dialog.file.name")), BorderLayout.WEST);
117 myFileName.setText("");
118 myFileName.getDocument().addDocumentListener(new DocumentAdapter() {
119 protected void textChanged(DocumentEvent e) {
120 updateOkButton();
124 panel.add(myFileName, BorderLayout.CENTER);
125 return panel;
128 private boolean isFileNameExist() {
129 if (myPathTextField == null) return false;
130 final String path = myPathTextField.getTextFieldText();
131 return path != null && new File(path.trim()).exists() && myFileName.getText().trim().length() > 0;
134 protected void updateOkButton() {
135 setOKActionEnabled(true);
138 @Override
139 protected void setOKActionEnabled(boolean isEnabled) {
140 //double check. FileChooserFactoryImpl sets enable ok button
141 super.setOKActionEnabled(isFileNameExist());