update copyright
[fedora-idea.git] / xml / impl / src / com / intellij / ide / browsers / firefox / FirefoxSettingsConfigurable.java
blob4fe14efb30d213cdd3fc2a81cf34fbb624a178b9
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.ide.browsers.firefox;
18 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
19 import com.intellij.openapi.options.Configurable;
20 import com.intellij.openapi.options.ConfigurationException;
21 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
22 import com.intellij.openapi.util.Comparing;
23 import com.intellij.openapi.util.io.FileUtil;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import com.intellij.ui.DocumentAdapter;
26 import com.intellij.xml.XmlBundle;
27 import org.jetbrains.annotations.Nullable;
28 import org.jetbrains.annotations.Nls;
30 import javax.swing.*;
31 import javax.swing.event.DocumentEvent;
32 import java.io.File;
33 import java.util.List;
35 /**
36 * @author nik
38 public class FirefoxSettingsConfigurable implements Configurable {
39 private static final FileChooserDescriptor PROFILES_INI_CHOOSER_DESCRIPTOR = createProfilesIniChooserDescriptor();
41 private JPanel myMainPanel;
42 private JComboBox myProfileCombobox;
43 private TextFieldWithBrowseButton myProfilesIniPathField;
44 private final FirefoxSettings mySettings;
45 private String myLastProfilesIniPath;
46 private String myDefaultProfilesIniPath;
47 private String myDefaultProfile;
49 public FirefoxSettingsConfigurable(FirefoxSettings settings) {
50 mySettings = settings;
51 myProfilesIniPathField.addBrowseFolderListener(XmlBundle.message("chooser.title.select.profiles.ini.file"), null, null, PROFILES_INI_CHOOSER_DESCRIPTOR);
52 myProfilesIniPathField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
53 @Override
54 protected void textChanged(DocumentEvent e) {
55 updateProfilesList();
57 });
60 public static FileChooserDescriptor createProfilesIniChooserDescriptor() {
61 return new FileChooserDescriptor(true, false, false, false, false, false) {
62 @Override
63 public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
64 if (!file.isDirectory() && !file.getName().equals(FirefoxUtil.PROFILES_INI_FILE)) {
65 return false;
67 return super.isFileVisible(file, showHiddenFiles);
72 public JComponent createComponent() {
73 return myMainPanel;
76 public boolean isModified() {
77 return !Comparing.equal(mySettings.getProfile(), getConfiguredProfileName()) ||
78 !Comparing.equal(mySettings.getProfilesIniPath(), getConfiguredProfileIniPath());
81 @Nullable
82 private String getConfiguredProfileIniPath() {
83 final String path = myProfilesIniPathField.getText();
84 if (myDefaultProfilesIniPath.equals(path)) {
85 return null;
87 return FileUtil.toSystemIndependentName(path);
90 @Nullable
91 private String getConfiguredProfileName() {
92 final String selected = (String)myProfileCombobox.getSelectedItem();
93 if (Comparing.equal(myDefaultProfile, selected)) {
94 return null;
96 return selected;
99 public void apply() throws ConfigurationException {
100 mySettings.setProfile(getConfiguredProfileName());
101 mySettings.setProfilesIniPath(getConfiguredProfileIniPath());
104 public void reset() {
105 final File defaultFile = FirefoxUtil.getDefaultProfileIniPath();
106 myDefaultProfilesIniPath = defaultFile != null ? defaultFile.getAbsolutePath() : "";
108 final String path = mySettings.getProfilesIniPath();
109 myProfilesIniPathField.setText(path != null ? FileUtil.toSystemDependentName(path) : myDefaultProfilesIniPath);
110 updateProfilesList();
111 final String profileName = mySettings.getProfile();
112 myProfileCombobox.setSelectedItem(profileName != null ? profileName : myDefaultProfile);
115 private void updateProfilesList() {
116 final String profilesIniPath = myProfilesIniPathField.getText();
117 if (myLastProfilesIniPath != null && myLastProfilesIniPath.equals(profilesIniPath)) return;
119 myProfileCombobox.removeAllItems();
120 final List<FirefoxProfile> profiles = FirefoxUtil.computeProfiles(new File(profilesIniPath));
121 final FirefoxProfile defaultProfile = FirefoxUtil.getDefaultProfile(profiles);
122 myDefaultProfile = defaultProfile != null ? defaultProfile.getName() : null;
123 for (FirefoxProfile profile : profiles) {
124 myProfileCombobox.addItem(profile.getName());
126 if (!profiles.isEmpty()) {
127 myProfileCombobox.setSelectedIndex(0);
129 myLastProfilesIniPath = profilesIniPath;
132 public void disposeUIResources() {
135 @Nls
136 public String getDisplayName() {
137 return XmlBundle.message("display.name.firefox.settings");
140 public Icon getIcon() {
141 return null;
144 public String getHelpTopic() {
145 return null;