2 * Copyright 2000-2009 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
.roots
.ui
.configuration
.libraryEditor
;
18 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptor
;
19 import com
.intellij
.openapi
.fileChooser
.ex
.FileChooserDialogImpl
;
20 import com
.intellij
.openapi
.project
.ProjectBundle
;
21 import com
.intellij
.openapi
.ui
.Messages
;
22 import com
.intellij
.openapi
.util
.Pair
;
23 import com
.intellij
.openapi
.vfs
.VirtualFile
;
24 import com
.intellij
.ui
.FieldPanel
;
25 import com
.intellij
.util
.ui
.UIUtil
;
26 import org
.jetbrains
.annotations
.Nullable
;
29 import javax
.swing
.event
.DocumentEvent
;
30 import javax
.swing
.event
.DocumentListener
;
31 import javax
.swing
.event
.TreeSelectionEvent
;
32 import javax
.swing
.event
.TreeSelectionListener
;
35 public class LibraryFileChooser
extends FileChooserDialogImpl
{
36 private JTextField myNameField
;
37 private final boolean myInputName
;
38 private final LibraryTableEditor myParentEditor
;
39 private boolean myNameChangedByUser
= false;
41 public LibraryFileChooser(FileChooserDescriptor chooserDescriptor
,
44 LibraryTableEditor parentEditor
) {
45 super(chooserDescriptor
, parent
);
46 myInputName
= inputName
;
47 myParentEditor
= parentEditor
;
50 public String
getName() {
51 if (myNameField
!= null) {
52 final String name
= myNameField
.getText().trim();
53 return name
.length() > 0 ? name
: null;
58 private void setName(String name
) {
59 if (myNameField
!= null) {
60 final boolean savedValue
= myNameChangedByUser
;
62 myNameField
.setText(name
);
65 myNameChangedByUser
= savedValue
;
70 public JComponent
getPreferredFocusedComponent() {
71 return myInputName ? myNameField
: super.getPreferredFocusedComponent();
74 protected JComponent
createCenterPanel() {
75 final JComponent centerPanel
= super.createCenterPanel();
80 final JPanel panel
= new JPanel(new BorderLayout());
81 panel
.add(centerPanel
, BorderLayout
.CENTER
);
83 final FieldPanel fieldPanel
= FieldPanel
.create(ProjectBundle
.message("library.name.prompt"), null);
84 fieldPanel
.getFieldLabel().setFont(UIUtil
.getLabelFont().deriveFont(Font
.BOLD
));
85 myNameField
= fieldPanel
.getTextField();
86 myNameField
.getDocument().addDocumentListener(new DocumentListener() {
87 public void changedUpdate(DocumentEvent e
) {
88 myNameChangedByUser
= true;
91 public void insertUpdate(DocumentEvent e
) {
92 myNameChangedByUser
= true;
95 public void removeUpdate(DocumentEvent e
) {
96 myNameChangedByUser
= true;
99 panel
.add(fieldPanel
, BorderLayout
.NORTH
);
101 myFileSystemTree
.getTree().addTreeSelectionListener(new TreeSelectionListener() {
102 public void valueChanged(TreeSelectionEvent e
) {
103 if (myNameField
== null || myNameChangedByUser
) {
106 final VirtualFile
[] selectedFiles
= getSelectedFiles();
107 setName(selectedFiles
.length
== 1 ? selectedFiles
[0].getNameWithoutExtension() : "");
113 protected void doOKAction() {
114 if (!validateData()) {
121 private boolean validateData() {
122 JComponent componentToFocus
= null;
124 final VirtualFile
[] chosenFiles
= getSelectedFiles();
125 if (chosenFiles
!= null && chosenFiles
.length
> 0) {
127 final String name
= getName();
129 Messages
.showErrorDialog(myNameField
, ProjectBundle
.message("library.name.not.specified.error"),
130 ProjectBundle
.message("library.name.not.specified.title"));
131 componentToFocus
= myNameField
;
134 if (myParentEditor
.libraryAlreadyExists(name
)) {
135 Messages
.showErrorDialog(myNameField
, ProjectBundle
.message("library.name.already.exists.error", name
),
136 ProjectBundle
.message("library.name.already.exists.title"));
137 componentToFocus
= myNameField
;
143 Messages
.showErrorDialog(ProjectBundle
.message("library.files.not.selected.error"),
144 ProjectBundle
.message("library.files.not.selected.title"));
145 componentToFocus
= myFileSystemTree
.getTree();
151 if (componentToFocus
!= null) {
152 final JComponent _componentToFocus
= componentToFocus
;
153 SwingUtilities
.invokeLater(new Runnable() {
155 _componentToFocus
.requestFocus();
162 public Pair
<String
, VirtualFile
[]> chooseNameAndFiles(@Nullable VirtualFile toSelect
) {
163 VirtualFile
[] chosenFiles
= choose(toSelect
, null);
164 return new Pair
<String
, VirtualFile
[]>(getName(), chosenFiles
);
167 public Pair
<String
, VirtualFile
[]> chooseNameAndFiles() {
168 return chooseNameAndFiles(null);