focus stealing issue: isActive() is more accurate + removed direct call to requestFoc...
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / application / ImportOldConfigsPanel.java
blob9db1364884697d6ea6f169f28453e7d3bb0f9782
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.openapi.application;
18 import com.intellij.openapi.MnemonicHelper;
19 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
20 import com.intellij.openapi.util.SystemInfo;
21 import com.intellij.openapi.util.text.StringUtil;
22 import org.jetbrains.annotations.Nullable;
24 import javax.swing.*;
25 import javax.swing.event.ChangeEvent;
26 import javax.swing.event.ChangeListener;
27 import java.awt.*;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.io.File;
32 /**
33 * @author max
35 public class ImportOldConfigsPanel extends JDialog {
36 private TextFieldWithBrowseButton myPrevInstallation;
37 private JRadioButton myRbDoNotImport;
38 private JRadioButton myRbImport;
39 private JPanel myRootPanel;
40 private File myLastSelection = null;
41 private JButton myOkButton;
42 private JLabel mySuggestLabel;
43 private JLabel myHomeLabel;
45 public ImportOldConfigsPanel(final Frame owner) {
46 super(owner, true);
48 init();
51 public ImportOldConfigsPanel() {
52 super((Dialog) null, true);
54 init();
57 private void init() {
58 new MnemonicHelper().register(getContentPane());
60 ButtonGroup group = new ButtonGroup();
61 group.add(myRbDoNotImport);
62 group.add(myRbImport);
63 myRbDoNotImport.setSelected(true);
65 final ApplicationNamesInfo namesInfo = ApplicationNamesInfo.getInstance();
66 final String productName = namesInfo.getProductName().equals("IDEA") ? namesInfo.getFullProductName() : namesInfo.getProductName();
67 mySuggestLabel.setText(ApplicationBundle.message("label.you.can.import", productName));
68 myRbDoNotImport.setText(ApplicationBundle.message("radio.do.not.import", productName));
69 myHomeLabel.setText(ApplicationBundle.message("editbox.installation.home", productName));
71 myRbImport.addChangeListener(new ChangeListener() {
72 public void stateChanged(ChangeEvent e) {
73 update();
75 });
77 if (SystemInfo.isMac) {
78 myPrevInstallation.setText(findPreviousInstallationMac(productName));
80 else if (SystemInfo.isWindows) {
81 String prevInstall = findPreviousInstallationWindows(productName);
82 if (prevInstall != null) {
83 myPrevInstallation.setText(prevInstall);
87 myPrevInstallation.addActionListener(new ActionListener() {
88 public void actionPerformed(ActionEvent e) {
89 JFileChooser fc = new JFileChooser();
90 if (myLastSelection != null){
91 fc = new JFileChooser(myLastSelection);
94 if (!SystemInfo.isMac) {
95 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
98 int returnVal = fc.showOpenDialog(ImportOldConfigsPanel.this);
99 if (returnVal == JFileChooser.APPROVE_OPTION){
100 File file = fc.getSelectedFile();
101 if (file != null){
102 myLastSelection = file;
103 myPrevInstallation.setText(file.getAbsolutePath());
109 myOkButton.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e) {
111 close();
115 getContentPane().setLayout(new BorderLayout());
116 getContentPane().add(myRootPanel);
117 getRootPane().setDefaultButton(myOkButton);
119 setTitle(ApplicationBundle.message("title.complete.installation"));
121 update();
122 pack();
124 Dimension parentSize = Toolkit.getDefaultToolkit().getScreenSize();
125 Dimension ownSize = getPreferredSize();
127 setLocation((parentSize.width - ownSize.width) / 2, (parentSize.height - ownSize.height) / 2);
130 @Nullable
131 private static String findPreviousInstallationWindows(String productName) {
132 String programFiles = System.getenv("ProgramFiles");
133 if (programFiles != null) {
134 File jetbrainsHome = new File(programFiles, "JetBrains");
135 if (jetbrainsHome.isDirectory()) {
136 final File[] files = jetbrainsHome.listFiles();
137 String latestVersion = null;
138 File latestFile = null;
139 for (File file : files) {
140 if (file.isDirectory() && file.getName().startsWith(productName)) {
141 String versionName = file.getName().substring(productName.length()).trim();
142 // EAP builds don't have . in version number - ignore them
143 if (versionName.indexOf('.') > 0) {
144 if (latestVersion == null || StringUtil.compareVersionNumbers(latestVersion, versionName) > 0) {
145 latestVersion = versionName;
146 latestFile = file;
151 if (latestFile != null) {
152 return latestFile.getAbsolutePath();
156 return null;
159 private static String findPreviousInstallationMac(String productName) {
160 //noinspection HardCodedStringLiteral
161 final String mostProbable = "/Applications/" + productName;
162 if (new File(mostProbable).exists()) {
163 return mostProbable;
165 return "/Applications";
168 private void close() {
169 if (isImportEnabled()) {
170 final String productWithVendor = ApplicationNamesInfo.getInstance().getFullProductName();
171 String instHome = myPrevInstallation.getText();
172 if ("".equals(instHome)) {
173 JOptionPane.showMessageDialog(this,
174 ApplicationBundle.message("error.please.select.previous.installation.home", productWithVendor),
175 ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
176 return;
179 if (PathManager.getHomePath().equals(instHome)) {
180 JOptionPane.showMessageDialog(this,
181 ApplicationBundle.message("error.selected.current.installation.home",
182 productWithVendor, productWithVendor),
183 ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
184 return;
187 if (!ConfigImportHelper.isInstallationHome(instHome)) {
188 JOptionPane.showMessageDialog(this,
189 ApplicationBundle.message("error.does.not.appear.to.be.installation.home", instHome,
190 productWithVendor),
191 ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
192 return;
195 if (!new File(instHome).canRead()) {
196 JOptionPane.showMessageDialog(this,
197 ApplicationBundle.message("error.no.read.permissions", instHome),
198 ApplicationBundle.message("title.installation.home.required"), JOptionPane.ERROR_MESSAGE);
199 return;
203 dispose();
206 public boolean isImportEnabled() {
207 return myRbImport.isSelected();
210 public File getSelectedFile() {
211 return new File(myPrevInstallation.getText());
214 private void update() {
215 myPrevInstallation.setEnabled(myRbImport.isSelected());
218 public static void main(String[] args) {
219 ImportOldConfigsPanel dlg = new ImportOldConfigsPanel();
220 dlg.setVisible(true);