psi-based attribute predefining
[idea/community.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / configurable / VcsGeneralConfigurationPanel.java
1 /*
2  * Copyright 2000-2009 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 com.intellij.openapi.vcs.configurable;
17
18 import com.intellij.openapi.application.ApplicationNamesInfo;
19 import com.intellij.openapi.options.Configurable;
20 import com.intellij.openapi.options.ConfigurationException;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.util.Comparing;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.openapi.vcs.*;
25 import com.intellij.openapi.vcs.ex.ProjectLevelVcsManagerEx;
26 import com.intellij.openapi.vcs.readOnlyHandler.ReadonlyStatusHandlerImpl;
27 import com.intellij.openapi.vfs.ReadonlyStatusHandler;
28 import com.intellij.util.ui.UIUtil;
29 import org.jetbrains.annotations.Nls;
30
31 import javax.swing.*;
32 import java.awt.*;
33 import java.util.*;
34 import java.util.List;
35
36 public class VcsGeneralConfigurationPanel implements Configurable {
37
38   private JCheckBox myForceNonEmptyComment;
39   private JCheckBox myShowReadOnlyStatusDialog;
40
41   private JRadioButton myShowDialogOnAddingFile;
42   private JRadioButton myPerformActionOnAddingFile;
43   private JRadioButton myDoNothingOnAddingFile;
44
45   private JRadioButton myShowDialogOnRemovingFile;
46   private JRadioButton myPerformActionOnRemovingFile;
47   private JRadioButton myDoNothingOnRemovingFile;
48
49   private JPanel myPanel;
50
51   private final JRadioButton[] myOnFileAddingGroup;
52   private final JRadioButton[] myOnFileRemovingGroup;
53
54   private final Project myProject;
55   private JPanel myPromptsPanel;
56
57
58   Map<VcsShowOptionsSettingImpl, JCheckBox> myPromptOptions = new LinkedHashMap<VcsShowOptionsSettingImpl, JCheckBox>();
59   private JPanel myRemoveConfirmationPanel;
60   private JPanel myAddConfirmationPanel;
61   private JCheckBox myCbOfferToMoveChanges;
62   private JComboBox myFailedCommitChangelistCombo;
63   private JLabel myRemoveConfirmationLabel;
64   private JLabel myAddConfirmationLabel;
65
66   public VcsGeneralConfigurationPanel(final Project project) {
67
68     myProject = project;
69
70     myOnFileAddingGroup = new JRadioButton[]{
71       myShowDialogOnAddingFile,
72       myPerformActionOnAddingFile,
73       myDoNothingOnAddingFile
74     };
75
76     myOnFileRemovingGroup = new JRadioButton[]{
77       myShowDialogOnRemovingFile,
78       myPerformActionOnRemovingFile,
79       myDoNothingOnRemovingFile
80     };
81
82     myPromptsPanel.setLayout(new GridLayout(3, 0));
83
84     List<VcsShowOptionsSettingImpl> options = ProjectLevelVcsManagerEx.getInstanceEx(project).getAllOptions();
85
86     for (VcsShowOptionsSettingImpl setting : options) {
87       if (!setting.getApplicableVcses().isEmpty() || project.isDefault()) {
88         final JCheckBox checkBox = new JCheckBox(setting.getDisplayName());
89         myPromptsPanel.add(checkBox);
90         myPromptOptions.put(setting, checkBox);
91       }
92     }
93
94     myPromptsPanel.setSize(myPromptsPanel.getPreferredSize());
95
96     myAddConfirmationLabel.setText(VcsBundle.message("add.confirmation.label.text", ApplicationNamesInfo.getInstance().getProductName()));
97     myRemoveConfirmationLabel.setText(VcsBundle.message("remove.confirmation.label.text", ApplicationNamesInfo.getInstance().getProductName()));
98   }
99
100   public void apply() throws ConfigurationException {
101
102     VcsConfiguration settings = VcsConfiguration.getInstance(myProject);
103
104     settings.FORCE_NON_EMPTY_COMMENT = myForceNonEmptyComment.isSelected();
105     settings.OFFER_MOVE_TO_ANOTHER_CHANGELIST_ON_PARTIAL_COMMIT = myCbOfferToMoveChanges.isSelected();
106     settings.MOVE_TO_FAILED_COMMIT_CHANGELIST = getFailedCommitConfirm();
107
108     for (VcsShowOptionsSettingImpl setting : myPromptOptions.keySet()) {
109       setting.setValue(myPromptOptions.get(setting).isSelected());
110     }
111
112     getAddConfirmation().setValue(getSelected(myOnFileAddingGroup));
113     getRemoveConfirmation().setValue(getSelected(myOnFileRemovingGroup));
114
115
116     getReadOnlyStatusHandler().getState().SHOW_DIALOG = myShowReadOnlyStatusDialog.isSelected();
117   }
118
119   private VcsShowConfirmationOption.Value getFailedCommitConfirm() {
120     switch(myFailedCommitChangelistCombo.getSelectedIndex()) {
121       case 0: return VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY;
122       case 1: return VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY;
123       default: return VcsShowConfirmationOption.Value.SHOW_CONFIRMATION;
124     }
125   }
126
127   private VcsShowConfirmationOption getAddConfirmation() {
128     return ProjectLevelVcsManagerEx.getInstanceEx(myProject)
129       .getConfirmation(VcsConfiguration.StandardConfirmation.ADD);
130   }
131
132   private VcsShowConfirmationOption getRemoveConfirmation() {
133     return ProjectLevelVcsManagerEx.getInstanceEx(myProject)
134       .getConfirmation(VcsConfiguration.StandardConfirmation.REMOVE);
135   }
136
137
138   private static VcsShowConfirmationOption.Value getSelected(JRadioButton[] group) {
139     if (group[0].isSelected()) return VcsShowConfirmationOption.Value.SHOW_CONFIRMATION;
140     if (group[1].isSelected()) return VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY;
141     return VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY;
142   }
143
144
145   private ReadonlyStatusHandlerImpl getReadOnlyStatusHandler() {
146     return ((ReadonlyStatusHandlerImpl)ReadonlyStatusHandler.getInstance(myProject));
147   }
148
149   public boolean isModified() {
150
151     VcsConfiguration settings = VcsConfiguration.getInstance(myProject);
152     if (settings.FORCE_NON_EMPTY_COMMENT != myForceNonEmptyComment.isSelected()){
153       return true;
154     }
155     if (settings.OFFER_MOVE_TO_ANOTHER_CHANGELIST_ON_PARTIAL_COMMIT != myCbOfferToMoveChanges.isSelected()){
156       return true;
157     }
158
159     if (!Comparing.equal(getFailedCommitConfirm(), settings.MOVE_TO_FAILED_COMMIT_CHANGELIST)) {
160       return true;
161     }
162
163     if (getReadOnlyStatusHandler().getState().SHOW_DIALOG != myShowReadOnlyStatusDialog.isSelected()) {
164       return true;
165     }
166
167     for (VcsShowOptionsSettingImpl setting : myPromptOptions.keySet()) {
168       if (setting.getValue() != myPromptOptions.get(setting).isSelected()) return true;
169     }
170
171     if (getSelected(myOnFileAddingGroup) != getAddConfirmation().getValue()) return true;
172     if (getSelected(myOnFileRemovingGroup) != getRemoveConfirmation().getValue()) return true;
173
174     return false;
175   }
176
177   public void reset() {
178     VcsConfiguration settings = VcsConfiguration.getInstance(myProject);
179     myForceNonEmptyComment.setSelected(settings.FORCE_NON_EMPTY_COMMENT);
180     myCbOfferToMoveChanges.setSelected(settings.OFFER_MOVE_TO_ANOTHER_CHANGELIST_ON_PARTIAL_COMMIT);
181     myShowReadOnlyStatusDialog.setSelected(getReadOnlyStatusHandler().getState().SHOW_DIALOG);
182     if (settings.MOVE_TO_FAILED_COMMIT_CHANGELIST == VcsShowConfirmationOption.Value.DO_ACTION_SILENTLY) {
183       myFailedCommitChangelistCombo.setSelectedIndex(0);
184     }
185     else if (settings.MOVE_TO_FAILED_COMMIT_CHANGELIST == VcsShowConfirmationOption.Value.DO_NOTHING_SILENTLY) {
186       myFailedCommitChangelistCombo.setSelectedIndex(1);
187     }
188     else {
189       myFailedCommitChangelistCombo.setSelectedIndex(2);
190     }
191
192     for (VcsShowOptionsSettingImpl setting : myPromptOptions.keySet()) {
193       myPromptOptions.get(setting).setSelected(setting.getValue());
194     }
195
196     selectInGroup(myOnFileAddingGroup, getAddConfirmation());
197     selectInGroup(myOnFileRemovingGroup, getRemoveConfirmation());
198   }
199
200   private static void selectInGroup(final JRadioButton[] group, final VcsShowConfirmationOption confirmation) {
201     final VcsShowConfirmationOption.Value value = confirmation.getValue();
202     final int index;
203     //noinspection EnumSwitchStatementWhichMissesCases
204     switch(value) {
205       case SHOW_CONFIRMATION: index = 0; break;
206       case DO_ACTION_SILENTLY: index = 1; break;
207       default: index = 2;
208     }
209     group[index].setSelected(true);
210   }
211
212
213   public JComponent getPanel() {
214     return myPanel;
215   }
216
217   public void updateAvailableOptions(final Collection<AbstractVcs> activeVcses) {
218     for (VcsShowOptionsSettingImpl setting : myPromptOptions.keySet()) {
219       final JCheckBox checkBox = myPromptOptions.get(setting);
220       checkBox.setEnabled(setting.isApplicableTo(activeVcses) || myProject.isDefault());
221       if (!myProject.isDefault()) {
222         checkBox.setToolTipText(VcsBundle.message("tooltip.text.action.applicable.to.vcses", composeText(setting.getApplicableVcses())));
223       }
224     }
225
226     if (!myProject.isDefault()) {
227       final ProjectLevelVcsManagerEx vcsManager = ProjectLevelVcsManagerEx.getInstanceEx(myProject);
228       final VcsShowConfirmationOptionImpl addConfirmation = vcsManager.getConfirmation(VcsConfiguration.StandardConfirmation.ADD);
229       UIUtil.setEnabled(myAddConfirmationPanel, addConfirmation.isApplicableTo(activeVcses), true);
230       myAddConfirmationPanel.setToolTipText(
231         VcsBundle.message("tooltip.text.action.applicable.to.vcses", composeText(addConfirmation.getApplicableVcses())));
232
233       final VcsShowConfirmationOptionImpl removeConfirmation = vcsManager.getConfirmation(VcsConfiguration.StandardConfirmation.REMOVE);
234       UIUtil.setEnabled(myRemoveConfirmationPanel, removeConfirmation.isApplicableTo(activeVcses), true);
235       myRemoveConfirmationPanel.setToolTipText(
236         VcsBundle.message("tooltip.text.action.applicable.to.vcses", composeText(removeConfirmation.getApplicableVcses())));
237     }
238   }
239
240   private static String composeText(final List<AbstractVcs> applicableVcses) {
241     final TreeSet<String> result = new TreeSet<String>();
242     for (AbstractVcs abstractVcs : applicableVcses) {
243       result.add(abstractVcs.getDisplayName());
244     }
245     return StringUtil.join(result, ", ");
246   }
247
248   @Nls
249   public String getDisplayName() {
250     return "Confirmation";
251   }
252
253   public Icon getIcon() {
254     return null;
255   }
256
257   public String getHelpTopic() {
258     return "project.propVCSSupport.Confirmation";
259   }
260
261   public JComponent createComponent() {
262     return getPanel();
263   }
264
265   public void disposeUIResources() {
266   }
267
268 }