42c2ec0d256826a45b220d9ade2ec5110fe4ed91
[idea/community.git] / plugins / terminal / src / org / jetbrains / plugins / terminal / TerminalSettingsPanel.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.jetbrains.plugins.terminal;
17
18 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
19 import com.intellij.openapi.ui.TextComponentAccessor;
20 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
21 import com.intellij.openapi.util.Comparing;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.ui.DocumentAdapter;
24 import com.intellij.ui.IdeBorderFactory;
25 import com.intellij.ui.components.JBCheckBox;
26 import org.jetbrains.annotations.NotNull;
27
28 import javax.swing.*;
29 import javax.swing.event.DocumentEvent;
30 import java.awt.*;
31 import java.util.Arrays;
32
33 /**
34  * @author traff
35  */
36 public class TerminalSettingsPanel {
37   private JPanel myWholePanel;
38   private TextFieldWithBrowseButton myShellPathField;
39   private JBCheckBox mySoundBellCheckBox;
40   private JBCheckBox myCloseSessionCheckBox;
41   private JBCheckBox myMouseReportCheckBox;
42   private JTextField myTabNameTextField;
43   private JBCheckBox myPasteOnMiddleButtonCheckBox;
44   private JBCheckBox myCopyOnSelectionCheckBox;
45   private JBCheckBox myOverrideIdeShortcuts;
46   private JBCheckBox myShellIntegration;
47   private TextFieldWithBrowseButton myStartDirectoryField;
48   private JPanel myProjectSettingsPanel;
49   private JPanel myGlobalSettingsPanel;
50   private TerminalOptionsProvider myOptionsProvider;
51   private TerminalProjectOptionsProvider myProjectOptionsProvider;
52
53   public JComponent createPanel(@NotNull TerminalOptionsProvider provider, @NotNull TerminalProjectOptionsProvider projectOptionsProvider) {
54     myOptionsProvider = provider;
55     myProjectOptionsProvider = projectOptionsProvider;
56
57     myProjectSettingsPanel.setBorder(IdeBorderFactory.createTitledBorder("Project settings"));
58     myGlobalSettingsPanel.setBorder(IdeBorderFactory.createTitledBorder("Application settings"));
59
60     FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false, false, false);
61
62     myShellPathField.addBrowseFolderListener(
63       "",
64       "Shell executable path",
65       null,
66       fileChooserDescriptor,
67       TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);
68
69     fileChooserDescriptor = new FileChooserDescriptor(false, true, false, false, false, false);
70
71     myStartDirectoryField.addBrowseFolderListener(
72       "",
73       "Starting directory",
74       null,
75       fileChooserDescriptor,
76       TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT);
77
78     myShellPathField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
79       @Override
80       protected void textChanged(DocumentEvent e) {
81         myShellPathField
82           .getTextField().setForeground(StringUtil.equals(myShellPathField.getText(), myProjectOptionsProvider.getDefaultShellPath()) ?
83                                         getDefaultValueColor() : getChangedValueColor());
84       }
85     });
86
87     myStartDirectoryField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
88       @Override
89       protected void textChanged(DocumentEvent e) {
90         myStartDirectoryField
91           .getTextField()
92           .setForeground(StringUtil.equals(myStartDirectoryField.getText(), myProjectOptionsProvider.getDefaultStartingDirectory()) ?
93                          getDefaultValueColor() : getChangedValueColor());
94       }
95     });
96
97     return myWholePanel;
98   }
99
100   public boolean isModified() {
101     return !Comparing.equal(myShellPathField.getText(), myProjectOptionsProvider.getShellPath())
102            || !Comparing.equal(myStartDirectoryField.getText(), myProjectOptionsProvider.getStartingDirectory())
103            || !Comparing.equal(myTabNameTextField.getText(), myOptionsProvider.getTabName())
104            || (myCloseSessionCheckBox.isSelected() != myOptionsProvider.closeSessionOnLogout())
105            || (myMouseReportCheckBox.isSelected() != myOptionsProvider.enableMouseReporting())
106            || (mySoundBellCheckBox.isSelected() != myOptionsProvider.audibleBell())
107            || (myCopyOnSelectionCheckBox.isSelected() != myOptionsProvider.copyOnSelection())
108            || (myPasteOnMiddleButtonCheckBox.isSelected() != myOptionsProvider.pasteOnMiddleMouseButton())
109            || (myOverrideIdeShortcuts.isSelected() != myOptionsProvider.overrideIdeShortcuts())
110            || (myShellIntegration.isSelected() != myOptionsProvider.shellIntegration())
111       ;
112   }
113
114   public void apply() {
115     myProjectOptionsProvider.setShellPath(myShellPathField.getText());
116     myProjectOptionsProvider.setStartingDirectory(myStartDirectoryField.getText());
117     myOptionsProvider.setTabName(myTabNameTextField.getText());
118     myOptionsProvider.setCloseSessionOnLogout(myCloseSessionCheckBox.isSelected());
119     myOptionsProvider.setReportMouse(myMouseReportCheckBox.isSelected());
120     myOptionsProvider.setSoundBell(mySoundBellCheckBox.isSelected());
121     myOptionsProvider.setCopyOnSelection(myCopyOnSelectionCheckBox.isSelected());
122     myOptionsProvider.setPasteOnMiddleMouseButton(myPasteOnMiddleButtonCheckBox.isSelected());
123     myOptionsProvider.setOverrideIdeShortcuts(myOverrideIdeShortcuts.isSelected());
124     myOptionsProvider.setShellIntegration(myShellIntegration.isSelected());
125   }
126
127   public void reset() {
128     myShellPathField.setText(myProjectOptionsProvider.getShellPath());
129     myStartDirectoryField.setText(myProjectOptionsProvider.getStartingDirectory());
130     myTabNameTextField.setText(myOptionsProvider.getTabName());
131     myCloseSessionCheckBox.setSelected(myOptionsProvider.closeSessionOnLogout());
132     myMouseReportCheckBox.setSelected(myOptionsProvider.enableMouseReporting());
133     mySoundBellCheckBox.setSelected(myOptionsProvider.audibleBell());
134     myCopyOnSelectionCheckBox.setSelected(myOptionsProvider.copyOnSelection());
135     myPasteOnMiddleButtonCheckBox.setSelected(myOptionsProvider.pasteOnMiddleMouseButton());
136     myOverrideIdeShortcuts.setSelected(myOptionsProvider.overrideIdeShortcuts());
137     myShellIntegration.setSelected(myOptionsProvider.shellIntegration());
138   }
139
140   public Color getDefaultValueColor() {
141     return findColorByKey("TextField.inactiveForeground", "nimbusDisabledText");
142   }
143
144   @NotNull
145   private static Color findColorByKey(String... colorKeys) {
146     Color c = null;
147     for (String key : colorKeys) {
148       c = UIManager.getColor(key);
149       if (c != null) {
150         break;
151       }
152     }
153
154     assert c != null : "Can't find color for keys " + Arrays.toString(colorKeys);
155     return c;
156   }
157
158   public Color getChangedValueColor() {
159     return findColorByKey("TextField.foreground");
160   }
161 }