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
;
25 import javax
.swing
.event
.ChangeEvent
;
26 import javax
.swing
.event
.ChangeListener
;
28 import java
.awt
.event
.ActionEvent
;
29 import java
.awt
.event
.ActionListener
;
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
) {
51 public ImportOldConfigsPanel() {
52 super((Dialog
) null, true);
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
) {
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();
102 myLastSelection
= file
;
103 myPrevInstallation
.setText(file
.getAbsolutePath());
109 myOkButton
.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e
) {
115 getContentPane().setLayout(new BorderLayout());
116 getContentPane().add(myRootPanel
);
117 getRootPane().setDefaultButton(myOkButton
);
119 setTitle(ApplicationBundle
.message("title.complete.installation"));
124 Dimension parentSize
= Toolkit
.getDefaultToolkit().getScreenSize();
125 Dimension ownSize
= getPreferredSize();
127 setLocation((parentSize
.width
- ownSize
.width
) / 2, (parentSize
.height
- ownSize
.height
) / 2);
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
;
151 if (latestFile
!= null) {
152 return latestFile
.getAbsolutePath();
159 private static String
findPreviousInstallationMac(String productName
) {
160 //noinspection HardCodedStringLiteral
161 final String mostProbable
= "/Applications/" + productName
;
162 if (new File(mostProbable
).exists()) {
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
);
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
);
187 if (!ConfigImportHelper
.isInstallationHome(instHome
)) {
188 JOptionPane
.showMessageDialog(this,
189 ApplicationBundle
.message("error.does.not.appear.to.be.installation.home", instHome
,
191 ApplicationBundle
.message("title.installation.home.required"), JOptionPane
.ERROR_MESSAGE
);
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
);
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);