2 * Copyright 2000-2010 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.fileChooser.ex;
18 import com.intellij.openapi.fileChooser.FileSaverDescriptor;
19 import com.intellij.openapi.fileChooser.FileSaverDialog;
20 import com.intellij.openapi.fileChooser.FileSystemTree;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.ui.Messages;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.openapi.vfs.VirtualFileWrapper;
25 import com.intellij.ui.DocumentAdapter;
26 import com.intellij.ui.UIBundle;
27 import org.jetbrains.annotations.Nullable;
30 import javax.swing.event.DocumentEvent;
33 import java.util.List;
36 * @author Konstantin Bulenkov
38 public class FileSaverDialogImpl extends FileChooserDialogImpl implements FileSaverDialog {
39 protected final JTextField myFileName = new JTextField(20);
40 protected final JComboBox myExtentions = new JComboBox();
41 protected final FileSaverDescriptor myDescriptor;
43 public FileSaverDialogImpl(FileSaverDescriptor chooserDescriptor, Project project) {
44 super(chooserDescriptor, project);
45 myDescriptor = chooserDescriptor;
46 setTitle(UIBundle.message("file.chooser.save.dialog.default.title"));
47 for (String ext : chooserDescriptor.getFileExtensions()) {
48 myExtentions.addItem(ext);
53 public VirtualFileWrapper save(@Nullable VirtualFile baseDir, @Nullable String filename) {
55 myFileSystemTree.addListener(new FileSystemTree.Listener() {
56 public void selectionChanged(final List<VirtualFile> selection) {
57 updateFileName(selection);
62 if (filename != null) {
63 myFileName.setText(filename);
66 if (baseDir != null && baseDir.isValid() && baseDir.isDirectory()) {
67 myFileSystemTree.select(baseDir, null);
72 if (getExitCode() == OK_EXIT_CODE) {
73 final File file = getFile();
74 return file == null ? null : new VirtualFileWrapper(file);
80 protected File getFile() {
81 final VirtualFile selected = myFileSystemTree.getSelectedFile();
82 if (selected != null && !selected.isDirectory()) {
83 return new File(selected.getPath());
86 String path = (selected == null) ? myPathTextField.getTextFieldText() : selected.getPath();
87 final File dir = new File(path);
88 if (! dir.exists() || path == null) return null;
89 if (dir.isDirectory()) {
90 path += File.separator + myFileName.getText();
93 boolean correctExt = true;
94 for (String ext : myDescriptor.getFileExtensions()) {
95 correctExt = path.endsWith("." + ext);
96 if (correctExt) break;
100 path += "." + myExtentions.getSelectedItem();
103 return new File(path);
106 private void updateFileName(List<VirtualFile> selection) {
107 for (VirtualFile file : selection) {
108 if (file.isDirectory()) {
109 myPathTextField.getField().setText(file.getPath());
111 myFileName.setText(file.getName());
112 final VirtualFile parent = file.getParent();
113 if (parent != null) {
114 myPathTextField.getField().setText(parent.getPath());
122 protected JComponent createCenterPanel() {
123 JComponent component = super.createCenterPanel();
124 MyPanel panel = new MyPanel();
125 panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
126 panel.add(component, BorderLayout.CENTER);
127 panel.add(createFileNamePanel(), BorderLayout.SOUTH);
131 protected JComponent createFileNamePanel() {
132 final JPanel panel = new JPanel(new BorderLayout());
133 panel.add(new JLabel(UIBundle.message("file.chooser.save.dialog.file.name")), BorderLayout.WEST);
134 myFileName.setText("");
135 myFileName.getDocument().addDocumentListener(new DocumentAdapter() {
136 protected void textChanged(DocumentEvent e) {
141 panel.add(myFileName, BorderLayout.CENTER);
142 if (myExtentions.getModel().getSize() > 0) {
143 myExtentions.setSelectedIndex(0);
144 panel.add(myExtentions, BorderLayout.EAST);
149 private boolean isFileNameExist() {
150 if (myPathTextField == null) return false;
151 final String path = myPathTextField.getTextFieldText();
152 return path != null && new File(path.trim()).exists() && myFileName.getText().trim().length() > 0;
155 protected void updateOkButton() {
156 setOKActionEnabled(true);
160 protected void setOKActionEnabled(boolean isEnabled) {
161 //double check. FileChooserFactoryImpl sets enable ok button
162 super.setOKActionEnabled(isFileNameExist());
166 protected void doOKAction() {
167 final File file = getFile();
168 if (file != null && file.exists()) {
169 if (OK_EXIT_CODE != Messages.showYesNoDialog(this.getRootPane(),
170 UIBundle.message("file.chooser.save.dialog.confirmation", file.getName()),
171 UIBundle.message("file.chooser.save.dialog.confirmation.title"),
172 Messages.getWarningIcon())) {