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
;
14 import javax
.swing
.event
.DocumentEvent
;
17 import java
.util
.List
;
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"));
33 public VirtualFileWrapper
save(@Nullable VirtualFile baseDir
, @Nullable String filename
) {
35 myFileSystemTree
.addListener(new FileSystemTree
.Listener() {
36 public void selectionChanged(final List
<VirtualFile
> selection
) {
37 updateFileName(selection
);
42 if (filename
!= null) {
43 myFileName
.setText(filename
);
46 if (baseDir
!= null && baseDir
.isValid() && baseDir
.isDirectory()) {
47 myFileSystemTree
.select(baseDir
, null);
52 if (getExitCode() == OK_EXIT_CODE
) {
53 final File file
= getFile();
54 return file
== null ?
null : new VirtualFileWrapper(file
);
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;
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());
91 myFileName
.setText(file
.getName());
92 final VirtualFile parent
= file
.getParent();
94 myPathTextField
.getField().setText(parent
.getPath());
98 if (selection
.size() == 0) {
99 myFileName
.setText("");
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
);
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
) {
124 panel
.add(myFileName
, BorderLayout
.CENTER
);
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);
139 protected void setOKActionEnabled(boolean isEnabled
) {
140 //double check. FileChooserFactoryImpl sets enable ok button
141 super.setOKActionEnabled(isFileNameExist());