update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / LocateLibraryDialog.java
blob17d2534f583fb4334968f51e286e9273cbb522f2
1 /*
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.codeInsight.daemon.impl.quickfix;
18 import com.intellij.codeInsight.daemon.QuickFixBundle;
19 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
20 import com.intellij.openapi.module.Module;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.ui.DialogWrapper;
23 import com.intellij.openapi.ui.Messages;
24 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
25 import com.intellij.openapi.util.io.FileUtil;
26 import com.intellij.ui.DocumentAdapter;
27 import com.intellij.util.ui.UIUtil;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.Nullable;
31 import javax.swing.*;
32 import javax.swing.event.DocumentEvent;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.io.File;
36 import java.io.IOException;
38 public class LocateLibraryDialog extends DialogWrapper {
39 private JPanel contentPane;
40 private JTextPane myDescription;
41 private JLabel myFileLabel;
42 private TextFieldWithBrowseButton myLibraryFile;
43 private JRadioButton myAddThisFileRadioButton;
44 private JRadioButton myCopyToRadioButton;
45 private TextFieldWithBrowseButton myCopyToDir;
47 private final Project myProject;
48 private String myResultingLibraryPath;
50 public String getResultingLibraryPath() {
51 return myResultingLibraryPath;
54 public LocateLibraryDialog(Module module, String libraryPath, final @NonNls String libraryName, final String libraryDescription ) {
55 super (module.getProject(), true);
56 setTitle ( QuickFixBundle.message("add.library.title.dialog"));
58 myProject = module.getProject();
60 // copied from Messages.MessageDialog
61 JLabel label = new JLabel();
62 myDescription.setFont(label.getFont());
63 myDescription.setBackground(UIUtil.getOptionPaneBackground());
64 myDescription.setForeground(label.getForeground());
65 myDescription.setText(libraryDescription);
66 // end of copy
68 myFileLabel.setLabelFor(myLibraryFile.getTextField());
70 myLibraryFile.setText(new File ( libraryPath, libraryName).getPath() );
71 myLibraryFile.addBrowseFolderListener(QuickFixBundle.message("add.library.title.locate.library"),
72 QuickFixBundle.message("add.library.description.locate.library"), myProject,
73 new FileChooserDescriptor(false,false,true,false,false,false));
75 myCopyToDir.setText(new File (module.getModuleFilePath()).getParent());
76 myCopyToDir.addBrowseFolderListener(QuickFixBundle.message("add.library.title.choose.folder"),
77 QuickFixBundle.message("add.library.description.choose.folder"), myProject,
78 new FileChooserDescriptor(false,true,false,false,false,false));
80 final ActionListener listener = new ActionListener() {
81 public void actionPerformed(final ActionEvent e) {
82 updateButtons();
86 myAddThisFileRadioButton.addActionListener(listener);
87 myCopyToRadioButton.addActionListener(listener);
89 myAddThisFileRadioButton.setSelected(true);
91 myCopyToDir.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
92 protected void textChanged(DocumentEvent e) {
93 updateButtons();
95 });
97 updateButtons();
99 init ();
102 private void updateButtons() {
103 final boolean copyEnabled = myCopyToRadioButton.isSelected();
104 myCopyToDir.setEnabled(copyEnabled);
105 if ( copyEnabled ) {
106 myCopyToDir.getTextField().requestFocusInWindow();
108 setOKActionEnabled(! copyEnabled || myCopyToDir.getText().length() != 0 );
111 @NonNls
112 protected String getDimensionServiceKey() {
113 return "#org.jetbrains.codeInsight.daemon.impl.quickfix.LocateLibraryDialog";
116 @Nullable
117 protected JComponent createCenterPanel() {
118 return contentPane;
121 protected void doOKAction() {
122 if (getOKAction().isEnabled()) {
123 myResultingLibraryPath = getResultingPath();
124 if ( myResultingLibraryPath != null ) {
125 close(OK_EXIT_CODE);
130 @Nullable
131 private String getResultingPath() {
132 final File srcFile = new File(myLibraryFile.getText());
133 if (!srcFile.exists()) {
134 Messages.showErrorDialog(myProject, QuickFixBundle.message("add.library.error.not.found", srcFile.getPath()),
135 QuickFixBundle.message("add.library.title.error"));
136 return null;
139 if (!myCopyToRadioButton.isSelected()) {
140 return srcFile.getPath();
143 final String dstDir = myCopyToDir.getText();
144 if ( dstDir.length() == 0 ) {
145 return null;
148 File dstFile = new File(dstDir, srcFile.getName());
149 try {
150 FileUtil.copy(srcFile, dstFile);
151 return dstFile.getPath();
153 catch (IOException e) {
154 Messages.showErrorDialog(myProject,
155 QuickFixBundle.message("add.library.error.cannot.copy", srcFile.getPath(), dstFile.getPath(), e.getMessage()),
156 QuickFixBundle.message("add.library.title.error"));
157 return null;