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
;
18 import com
.intellij
.ide
.IdeBundle
;
19 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptor
;
20 import com
.intellij
.openapi
.fileChooser
.FileChooserDescriptorFactory
;
21 import com
.intellij
.openapi
.ui
.TextFieldWithBrowseButton
;
22 import com
.intellij
.openapi
.util
.Pair
;
23 import com
.intellij
.openapi
.util
.SystemInfo
;
24 import com
.intellij
.openapi
.options
.ShowSettingsUtil
;
25 import com
.intellij
.util
.containers
.HashMap
;
26 import com
.intellij
.xml
.XmlBundle
;
27 import org
.jetbrains
.annotations
.NotNull
;
31 import java
.awt
.event
.ActionEvent
;
32 import java
.awt
.event
.ActionListener
;
38 public class WebBrowsersPanel
extends JPanel
{
39 private final JPanel mySettingsPanel
;
40 private Map
<BrowsersConfiguration
.BrowserFamily
, Pair
<JCheckBox
, TextFieldWithBrowseButton
>> myBrowserSettingsMap
= new HashMap
<BrowsersConfiguration
.BrowserFamily
, Pair
<JCheckBox
, TextFieldWithBrowseButton
>>();
41 private final BrowsersConfiguration myConfiguration
;
43 public WebBrowsersPanel(final BrowsersConfiguration configuration
) {
44 setLayout(new BorderLayout());
46 myConfiguration
= configuration
;
48 mySettingsPanel
= new JPanel();
49 mySettingsPanel
.setLayout(new BoxLayout(mySettingsPanel
, BoxLayout
.Y_AXIS
));
51 add(mySettingsPanel
, BorderLayout
.NORTH
);
53 createIndividualSettings(BrowsersConfiguration
.BrowserFamily
.FIREFOX
, mySettingsPanel
);
54 createIndividualSettings(BrowsersConfiguration
.BrowserFamily
.EXPLORER
, mySettingsPanel
);
55 createIndividualSettings(BrowsersConfiguration
.BrowserFamily
.SAFARI
, mySettingsPanel
);
56 createIndividualSettings(BrowsersConfiguration
.BrowserFamily
.CHROME
, mySettingsPanel
);
57 createIndividualSettings(BrowsersConfiguration
.BrowserFamily
.OPERA
, mySettingsPanel
);
60 private void createIndividualSettings(@NotNull final BrowsersConfiguration
.BrowserFamily family
, final JPanel container
) {
61 final JPanel result
= new JPanel();
63 result
.setBorder(BorderFactory
.createTitledBorder(family
.getName()));
65 result
.setLayout(new BoxLayout(result
, BoxLayout
.Y_AXIS
));
67 final TextFieldWithBrowseButton field
= new TextFieldWithBrowseButton();
68 FileChooserDescriptor descriptor
= SystemInfo
.isMac
69 ? FileChooserDescriptorFactory
.createSingleFolderDescriptor()
70 : FileChooserDescriptorFactory
.createSingleFileNoJarsDescriptor();
71 field
.addBrowseFolderListener(IdeBundle
.message("title.select.path.to.browser"), null, null, descriptor
);
75 final JPanel bottomPanel
= new JPanel(new BorderLayout());
77 final JPanel activePanel
= new JPanel();
78 activePanel
.setLayout(new BoxLayout(activePanel
, BoxLayout
.X_AXIS
));
80 final JCheckBox checkBox
= new JCheckBox();
81 activePanel
.add(checkBox
);
82 final JLabel label
= new JLabel(XmlBundle
.message("browser.active"));
83 label
.setLabelFor(checkBox
);
84 activePanel
.add(label
);
85 bottomPanel
.add(activePanel
, BorderLayout
.WEST
);
87 final JButton resetButton
= new JButton(XmlBundle
.message("browser.default.settings"));
88 resetButton
.addActionListener(new ActionListener() {
89 public void actionPerformed(final ActionEvent e
) {
90 field
.getTextField().setText(family
.getExecutionPath());
94 JPanel buttonsPanel
= new JPanel(new BorderLayout());
95 if (family
.createBrowserSpecificSettings() != null) {
96 final JButton editSettingsButton
= new JButton(XmlBundle
.message("button.text.settings"));
97 editSettingsButton
.addActionListener(new ActionListener() {
98 public void actionPerformed(ActionEvent e
) {
102 buttonsPanel
.add(editSettingsButton
, BorderLayout
.CENTER
);
104 buttonsPanel
.add(resetButton
, BorderLayout
.EAST
);
106 bottomPanel
.add(buttonsPanel
, BorderLayout
.EAST
);
108 result
.add(bottomPanel
);
109 container
.add(result
);
111 final WebBrowserSettings settings
= myConfiguration
.getBrowserSettings(family
);
112 field
.getTextField().setText(settings
.getPath());
113 checkBox
.setSelected(settings
.isActive());
115 myBrowserSettingsMap
.put(family
, Pair
.create(checkBox
, field
));
118 private void editSettings(BrowsersConfiguration
.BrowserFamily family
) {
119 BrowserSpecificSettings settings
= myConfiguration
.getBrowserSettings(family
).getBrowserSpecificSettings();
120 if (settings
== null) {
121 settings
= family
.createBrowserSpecificSettings();
124 if (ShowSettingsUtil
.getInstance().editConfigurable(mySettingsPanel
, settings
.createConfigurable())) {
125 myConfiguration
.updateBrowserSpecificSettings(family
, settings
);
129 public void dispose() {
130 myBrowserSettingsMap
= null;
133 public boolean isModified() {
134 for (BrowsersConfiguration
.BrowserFamily family
: BrowsersConfiguration
.BrowserFamily
.values()) {
135 final WebBrowserSettings old
= myConfiguration
.getBrowserSettings(family
);
136 final Pair
<JCheckBox
, TextFieldWithBrowseButton
> settings
= myBrowserSettingsMap
.get(family
);
138 if (old
.isActive() != settings
.first
.isSelected() || !old
.getPath().equals(settings
.second
.getText())) {
146 public void apply() {
147 for (BrowsersConfiguration
.BrowserFamily family
: myBrowserSettingsMap
.keySet()) {
148 final Pair
<JCheckBox
, TextFieldWithBrowseButton
> buttonPair
= myBrowserSettingsMap
.get(family
);
149 myConfiguration
.updateBrowserValue(family
, buttonPair
.second
.getText(), buttonPair
.first
.isSelected());
153 public void reset() {
154 for (BrowsersConfiguration
.BrowserFamily family
: myBrowserSettingsMap
.keySet()) {
155 final Pair
<JCheckBox
, TextFieldWithBrowseButton
> buttonPair
= myBrowserSettingsMap
.get(family
);
156 final WebBrowserSettings settings
= myConfiguration
.getBrowserSettings(family
);
157 buttonPair
.first
.setSelected(settings
.isActive());
158 buttonPair
.second
.getTextField().setText(settings
.getPath());