IDEADEV-40882 add static imports when needed
[idea/community.git] / platform / lang-impl / src / com / intellij / refactoring / move / moveFilesOrDirectories / MoveFilesOrDirectoriesDialog.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.intellij.refactoring.move.moveFilesOrDirectories;
18
19 import com.intellij.ide.util.DirectoryUtil;
20 import com.intellij.openapi.application.ApplicationManager;
21 import com.intellij.openapi.command.CommandProcessor;
22 import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
23 import com.intellij.openapi.help.HelpManager;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.ui.DialogWrapper;
26 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
27 import com.intellij.psi.PsiDirectory;
28 import com.intellij.psi.PsiElement;
29 import com.intellij.psi.PsiFile;
30 import com.intellij.psi.PsiManager;
31 import com.intellij.refactoring.RefactoringBundle;
32 import com.intellij.refactoring.util.CommonRefactoringUtil;
33 import com.intellij.ui.DocumentAdapter;
34 import com.intellij.ui.IdeBorderFactory;
35 import com.intellij.util.IncorrectOperationException;
36 import org.jetbrains.annotations.NonNls;
37
38 import javax.swing.*;
39 import javax.swing.event.DocumentEvent;
40 import java.awt.*;
41 import java.io.File;
42
43 public class MoveFilesOrDirectoriesDialog extends DialogWrapper{
44   public static interface Callback {
45     void run(MoveFilesOrDirectoriesDialog dialog);
46   }
47
48   private JLabel myNameLabel;
49   private TextFieldWithBrowseButton myTargetDirectoryField;
50   private String myHelpID;
51   private final Project myProject;
52   private final Callback myCallback;
53   private PsiDirectory myTargetDirectory;
54
55   public MoveFilesOrDirectoriesDialog(Project project, Callback callback) {
56     super(project, true);
57     myProject = project;
58     myCallback = callback;
59     setTitle(RefactoringBundle.message("move.title"));
60     init();
61   }
62
63   protected Action[] createActions(){
64     return new Action[]{getOKAction(),getCancelAction(),getHelpAction()};
65   }
66
67   public JComponent getPreferredFocusedComponent() {
68     return myTargetDirectoryField.getTextField();
69   }
70
71   protected JComponent createCenterPanel() {
72     return null;
73   }
74
75   protected JComponent createNorthPanel() {
76     JPanel panel = new JPanel();
77     panel.setLayout(new GridBagLayout());
78
79     panel.setBorder(IdeBorderFactory.createBorder());
80
81     myNameLabel = new JLabel();
82     panel.add(myNameLabel, new GridBagConstraints(0,0,2,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(4,8,4,8),0,0));
83
84     panel.add(new JLabel(RefactoringBundle.message("move.files.to.directory.label")),
85               new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(4,8,4,8),0,0));
86
87     myTargetDirectoryField = new TextFieldWithBrowseButton();
88     myTargetDirectoryField.addBrowseFolderListener(RefactoringBundle.message("select.target.directory"),
89                                                    RefactoringBundle.message("the.file.will.be.moved.to.this.directory"),
90                                                    null, FileChooserDescriptorFactory.createSingleFolderDescriptor());
91     myTargetDirectoryField.setTextFieldPreferredWidth(60);
92     panel.add(myTargetDirectoryField, new GridBagConstraints(1,1,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,new Insets(4,0,4,8),0,0));
93
94     myTargetDirectoryField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
95       public void textChanged(DocumentEvent event) {
96         validateOKButton();
97       }
98     });
99
100     return panel;
101   }
102
103   public void setData(PsiElement[] psiElements, PsiDirectory initialTargetDirectory, @NonNls String helpID) {
104     if (psiElements.length == 1) {
105       String text;
106       if (psiElements[0] instanceof PsiFile) {
107         text = RefactoringBundle.message("move.file.0",
108                                          ((PsiFile)psiElements[0]).getVirtualFile().getPresentableUrl());
109       }
110       else {
111         text = RefactoringBundle.message("move.directory.0",
112                                          ((PsiDirectory)psiElements[0]).getVirtualFile().getPresentableUrl());
113       }
114       myNameLabel.setText(text);
115     }
116     else {
117       boolean isFile = true;
118       boolean isDirectory = true;
119       for (PsiElement psiElement : psiElements) {
120         isFile &= psiElement instanceof PsiFile;
121         isDirectory &= psiElement instanceof PsiDirectory;
122       }
123       myNameLabel.setText(isFile ?
124                           RefactoringBundle.message("move.specified.files") :
125                           isDirectory ?
126                           RefactoringBundle.message("move.specified.directories") :
127                           RefactoringBundle.message("move.specified.elements"));
128     }
129     myTargetDirectoryField.setText(initialTargetDirectory == null ? "" : initialTargetDirectory.getVirtualFile().getPresentableUrl());
130
131     validateOKButton();
132     myHelpID = helpID;
133   }
134
135   protected void doHelpAction() {
136     HelpManager.getInstance().invokeHelp(myHelpID);
137   }
138
139   private void validateOKButton() {
140     setOKActionEnabled(myTargetDirectoryField.getText().length() > 0);
141   }
142
143   protected void doOKAction() {
144     CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
145       public void run() {
146         final Runnable action = new Runnable() {
147           public void run() {
148             String directoryName = myTargetDirectoryField.getText().replace(File.separatorChar, '/');
149             try {
150               myTargetDirectory = DirectoryUtil.mkdirs(PsiManager.getInstance(myProject), directoryName);
151             }
152             catch (IncorrectOperationException e) {
153             }
154           }
155         };
156         ApplicationManager.getApplication().runWriteAction(action);
157       }
158     }, RefactoringBundle.message("create.directory"), null);
159     if (myTargetDirectory == null){
160       CommonRefactoringUtil.showErrorMessage(getTitle(),
161                                               RefactoringBundle.message("cannot.create.directory"), myHelpID, myProject);
162       return;
163     }
164     myCallback.run(this);
165   }
166
167   public PsiDirectory getTargetDirectory() {
168     return myTargetDirectory;
169   }
170 }