cleanup: remove useless '@author nik' from javadoc comments
[idea/community.git] / java / idea-ui / src / com / intellij / openapi / roots / ui / configuration / libraryEditor / ChangeLibraryLevelDialog.java
1 // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.openapi.roots.ui.configuration.libraryEditor;
3
4 import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
5 import com.intellij.openapi.project.Project;
6 import com.intellij.openapi.roots.libraries.LibraryTable;
7 import com.intellij.openapi.roots.ui.configuration.LibraryTableModifiableModelProvider;
8 import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
9 import com.intellij.openapi.ui.DialogWrapper;
10 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
11 import com.intellij.openapi.util.io.FileUtil;
12 import com.intellij.ui.DocumentAdapter;
13 import org.jetbrains.annotations.NotNull;
14 import org.jetbrains.annotations.Nullable;
15
16 import javax.swing.*;
17 import javax.swing.event.DocumentEvent;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20
21 public class ChangeLibraryLevelDialog extends DialogWrapper {
22   private JTextField myNameField;
23   private JCheckBox myCopyFilesCheckBox;
24   private TextFieldWithBrowseButton myDirectoryForFilesField;
25   private JPanel myMainPanel;
26   private final boolean myAllowEmptyName;
27   private final LibraryTable.ModifiableModel myModifiableModel;
28
29   public ChangeLibraryLevelDialog(JComponent parent,
30                                   Project project,
31                                   boolean copy,
32                                   String libraryName,
33                                   String path,
34                                   boolean allowEmptyName,
35                                   LibraryTableModifiableModelProvider provider) {
36     super(parent, true);
37     myAllowEmptyName = allowEmptyName;
38     final String actionName = copy ? "Copy" : "Move";
39     setTitle(actionName + " Library");
40     myCopyFilesCheckBox.setText(actionName + " library files to:");
41     myCopyFilesCheckBox.setMnemonic(copy ? 'C' : 'M');
42     myCopyFilesCheckBox.addActionListener(new ActionListener() {
43       @Override
44       public void actionPerformed(ActionEvent e) {
45         updateDirectoriesForFilesField();
46       }
47     });
48     myModifiableModel = provider.getModifiableModel();
49     myNameField.setText(libraryName);
50     myDirectoryForFilesField.addBrowseFolderListener("Directory for Library Files", null, project,
51                                                      FileChooserDescriptorFactory.createSingleFolderDescriptor());
52     myDirectoryForFilesField.setText(FileUtil.toSystemDependentName(path));
53     myNameField.selectAll();
54     init();
55     checkName();
56     updateDirectoriesForFilesField();
57     myNameField.getDocument().addDocumentListener(new DocumentAdapter() {
58       @Override
59       protected void textChanged(@NotNull DocumentEvent e) {
60         checkName();
61       }
62     });
63   }
64
65   private void checkName() {
66     final String name = getLibraryName();
67     if (name.isEmpty()) {
68       if (!myAllowEmptyName) {
69         setErrorText("Library name is not specified", myNameField);
70       }
71       return;
72     }
73     if (LibraryEditingUtil.libraryAlreadyExists(myModifiableModel, name)) {
74       setErrorText("Library '" + name + "' already exists", myNameField);
75       return;
76     }
77     setErrorText(null);
78   }
79
80   private void updateDirectoriesForFilesField() {
81     myDirectoryForFilesField.setEnabled(myCopyFilesCheckBox.isSelected());
82   }
83
84   @Override
85   public JComponent getPreferredFocusedComponent() {
86     return myNameField;
87   }
88
89   @Override
90   protected JComponent createCenterPanel() {
91     return myMainPanel;
92   }
93
94   public String getLibraryName() {
95     return myNameField.getText().trim();
96   }
97
98   @Nullable
99   public String getDirectoryForFilesPath() {
100     if (!myCopyFilesCheckBox.isSelected()) {
101       return null;
102     }
103     return FileUtil.toSystemIndependentName(myDirectoryForFilesField.getText());
104   }
105
106 }