VCS: option to turn off background checks for "changed on server"
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / configurable / VcsDirectoryConfigurationPanel.java
blobf1369dc8afb8c8590d90c5d7c0bf8f875c076806
1 /*
2 * Copyright (c) 2000-2007 JetBrains s.r.o. All Rights Reserved.
3 */
5 package com.intellij.openapi.vcs.configurable;
7 import com.intellij.CommonBundle;
8 import com.intellij.openapi.options.Configurable;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.util.io.FileUtil;
11 import com.intellij.openapi.vcs.AbstractVcs;
12 import com.intellij.openapi.vcs.ProjectLevelVcsManager;
13 import com.intellij.openapi.vcs.VcsBundle;
14 import com.intellij.openapi.vcs.VcsDirectoryMapping;
15 import com.intellij.openapi.vfs.VirtualFile;
16 import com.intellij.ui.*;
17 import com.intellij.ui.table.TableView;
18 import com.intellij.util.ui.AbstractTableCellEditor;
19 import com.intellij.util.ui.ColumnInfo;
20 import com.intellij.util.ui.ListTableModel;
21 import org.jetbrains.annotations.Nls;
23 import javax.swing.*;
24 import javax.swing.event.ListSelectionEvent;
25 import javax.swing.event.ListSelectionListener;
26 import javax.swing.table.TableCellEditor;
27 import javax.swing.table.TableCellRenderer;
28 import java.awt.*;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.ItemListener;
32 import java.awt.event.ItemEvent;
33 import java.io.File;
34 import java.util.*;
35 import java.util.List;
37 /**
38 * @author yole
40 public class VcsDirectoryConfigurationPanel extends PanelWithButtons implements Configurable {
41 private final Project myProject;
42 private final ProjectLevelVcsManager myVcsManager;
43 private final TableView<VcsDirectoryMapping> myDirectoryMappingTable;
44 private final ComboboxWithBrowseButton myVcsComboBox = new ComboboxWithBrowseButton();
45 private final List<ModuleVcsListener> myListeners = new ArrayList<ModuleVcsListener>();
47 private final ColumnInfo<VcsDirectoryMapping, String> DIRECTORY = new ColumnInfo<VcsDirectoryMapping, String>(VcsBundle.message("column.info.configure.vcses.directory")) {
48 public String valueOf(final VcsDirectoryMapping mapping) {
49 String directory = mapping.getDirectory();
50 if (directory.length() == 0) {
51 return "<Project Root>";
53 VirtualFile baseDir = myProject.getBaseDir();
54 if (baseDir != null) {
55 return FileUtil.getRelativePath(new File(baseDir.getPath()), new File(directory));
57 return directory;
62 private final ColumnInfo<VcsDirectoryMapping, String> VCS_SETTING = new ColumnInfo<VcsDirectoryMapping, String>(VcsBundle.message("comumn.name.configure.vcses.vcs")) {
63 public String valueOf(final VcsDirectoryMapping object) {
64 return object.getVcs();
67 public boolean isCellEditable(final VcsDirectoryMapping o) {
68 return true;
71 public void setValue(final VcsDirectoryMapping o, final String aValue) {
72 Collection<AbstractVcs> activeVcses = getActiveVcses();
73 o.setVcs(aValue);
74 checkNotifyListeners(activeVcses);
77 public TableCellRenderer getRenderer(final VcsDirectoryMapping p0) {
78 return new ColoredTableCellRenderer() {
79 protected void customizeCellRenderer(JTable table, Object value, boolean selected, boolean hasFocus, int row, int column) {
80 final String vcsName = p0.getVcs();
81 String text;
82 if (vcsName.length() == 0) {
83 text = VcsBundle.message("none.vcs.presentation");
85 else {
86 final AbstractVcs vcs = myVcsManager.findVcsByName(vcsName);
87 if (vcs != null) {
88 text = vcs.getDisplayName();
90 else {
91 text = VcsBundle.message("unknown.vcs.presentation", vcsName);
94 append(text, new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, table.getForeground()));
99 @Override
100 public TableCellEditor getEditor(final VcsDirectoryMapping o) {
101 return new AbstractTableCellEditor() {
102 public Object getCellEditorValue() {
103 final VcsWrapper selectedVcs = (VcsWrapper) myVcsComboBox.getComboBox().getSelectedItem();
104 return ((selectedVcs == null) || (selectedVcs.getOriginal() == null)) ? "" : selectedVcs.getOriginal().getName();
107 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
108 String vcsName = (String) value;
109 myVcsComboBox.getComboBox().setSelectedItem(VcsWrapper.fromName(myProject, vcsName));
110 return myVcsComboBox;
115 private ListTableModel<VcsDirectoryMapping> myModel;
116 private JButton myAddButton;
117 private JButton myEditButton;
118 private JButton myRemoveButton;
120 public VcsDirectoryConfigurationPanel(final Project project) {
121 myProject = project;
122 myVcsManager = ProjectLevelVcsManager.getInstance(project);
124 myDirectoryMappingTable = new TableView<VcsDirectoryMapping>();
125 initializeModel();
127 myVcsComboBox.getComboBox().setModel(buildVcsWrappersModel(myProject));
128 myVcsComboBox.getComboBox().setRenderer(new EditorComboBoxRenderer(myVcsComboBox.getComboBox().getEditor()));
129 myVcsComboBox.getComboBox().addItemListener(new ItemListener() {
130 public void itemStateChanged(final ItemEvent e) {
131 if (myDirectoryMappingTable != null && myDirectoryMappingTable.isEditing()) {
132 myDirectoryMappingTable.stopEditing();
136 myVcsComboBox.addActionListener(new ActionListener() {
137 public void actionPerformed(ActionEvent e) {
138 final VcsWrapper vcsWrapper = ((VcsWrapper)myVcsComboBox.getComboBox().getSelectedItem());
139 AbstractVcs abstractVcs = null;
140 if (vcsWrapper != null){
141 abstractVcs = vcsWrapper.getOriginal();
143 new VcsConfigurationsDialog(project, myVcsComboBox.getComboBox(), abstractVcs).show();
147 myDirectoryMappingTable.setRowHeight(myVcsComboBox.getPreferredSize().height);
148 myDirectoryMappingTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
149 public void valueChanged(final ListSelectionEvent e) {
150 updateButtons();
153 initPanel();
154 updateButtons();
157 private void initializeModel() {
158 List<VcsDirectoryMapping> mappings = new ArrayList<VcsDirectoryMapping>();
159 for(VcsDirectoryMapping mapping: ProjectLevelVcsManager.getInstance(myProject).getDirectoryMappings()) {
160 mappings.add(new VcsDirectoryMapping(mapping.getDirectory(), mapping.getVcs(), mapping.getRootSettings()));
162 myModel = new ListTableModel<VcsDirectoryMapping>(new ColumnInfo[]{DIRECTORY, VCS_SETTING}, mappings, 0);
163 myDirectoryMappingTable.setModel(myModel);
166 private void updateButtons() {
167 final boolean hasSelection = myDirectoryMappingTable.getSelectedObject() != null;
168 myEditButton.setEnabled(hasSelection);
169 myRemoveButton.setEnabled(hasSelection);
172 public static DefaultComboBoxModel buildVcsWrappersModel(final Project project) {
173 final AbstractVcs[] vcss = ProjectLevelVcsManager.getInstance(project).getAllVcss();
174 VcsWrapper[] vcsWrappers = new VcsWrapper[vcss.length+1];
175 vcsWrappers [0] = new VcsWrapper(null);
176 for(int i=0; i<vcss.length; i++) {
177 vcsWrappers [i+1] = new VcsWrapper(vcss [i]);
179 return new DefaultComboBoxModel(vcsWrappers);
182 protected String getLabelText() {
183 return null;
186 protected JButton[] createButtons() {
187 myAddButton = new JButton(CommonBundle.message("button.add"));
188 myAddButton.addActionListener(new ActionListener() {
189 public void actionPerformed(final ActionEvent e) {
190 addMapping();
193 myEditButton = new JButton(CommonBundle.message("button.edit"));
194 myEditButton.addActionListener(new ActionListener() {
195 public void actionPerformed(final ActionEvent e) {
196 editMapping();
199 myRemoveButton = new JButton(CommonBundle.message("button.remove"));
200 myRemoveButton.addActionListener(new ActionListener() {
201 public void actionPerformed(final ActionEvent e) {
202 removeMapping();
205 return new JButton[] {myAddButton, myEditButton, myRemoveButton};
208 private void addMapping() {
209 Collection<AbstractVcs> activeVcses = getActiveVcses();
210 VcsMappingConfigurationDialog dlg = new VcsMappingConfigurationDialog(myProject, VcsBundle.message("directory.mapping.add.title"));
211 dlg.show();
212 if (dlg.isOK()) {
213 VcsDirectoryMapping mapping = new VcsDirectoryMapping();
214 dlg.saveToMapping(mapping);
215 List<VcsDirectoryMapping> items = new ArrayList<VcsDirectoryMapping>(myModel.getItems());
216 items.add(mapping);
217 myModel.setItems(items);
218 checkNotifyListeners(activeVcses);
222 private void editMapping() {
223 Collection<AbstractVcs> activeVcses = getActiveVcses();
224 VcsMappingConfigurationDialog dlg = new VcsMappingConfigurationDialog(myProject, VcsBundle.message("directory.mapping.remove.title"));
225 final VcsDirectoryMapping mapping = myDirectoryMappingTable.getSelectedObject();
226 dlg.setMapping(mapping);
227 dlg.show();
228 if (dlg.isOK()) {
229 dlg.saveToMapping(mapping);
230 myModel.fireTableDataChanged();
231 checkNotifyListeners(activeVcses);
235 private void removeMapping() {
236 Collection<AbstractVcs> activeVcses = getActiveVcses();
237 ArrayList<VcsDirectoryMapping> mappings = new ArrayList<VcsDirectoryMapping>(myModel.getItems());
238 int index = myDirectoryMappingTable.getSelectionModel().getMinSelectionIndex();
239 Collection<VcsDirectoryMapping> selection = myDirectoryMappingTable.getSelection();
240 mappings.removeAll(selection);
241 myModel.setItems(mappings);
242 if (mappings.size() > 0) {
243 if (index >= mappings.size()) {
244 index = mappings.size()-1;
246 myDirectoryMappingTable.getSelectionModel().setSelectionInterval(index, index);
248 checkNotifyListeners(activeVcses);
251 protected JComponent createMainComponent() {
252 return new JScrollPane(myDirectoryMappingTable);
255 public void reset() {
256 initializeModel();
259 public void apply() {
260 myVcsManager.setDirectoryMappings(myModel.getItems());
261 initializeModel();
264 public boolean isModified() {
265 return !myModel.getItems().equals(myVcsManager.getDirectoryMappings());
268 public void addVcsListener(final ModuleVcsListener moduleVcsListener) {
269 myListeners.add(moduleVcsListener);
272 public void removeVcsListener(final ModuleVcsListener moduleVcsListener) {
273 myListeners.remove(moduleVcsListener);
276 private void checkNotifyListeners(Collection<AbstractVcs> oldVcses) {
277 Collection<AbstractVcs> vcses = getActiveVcses();
278 if (!vcses.equals(oldVcses)) {
279 for(ModuleVcsListener listener: myListeners) {
280 listener.activeVcsSetChanged(vcses);
285 public Collection<AbstractVcs> getActiveVcses() {
286 Set<AbstractVcs> vcses = new HashSet<AbstractVcs>();
287 for(VcsDirectoryMapping mapping: myModel.getItems()) {
288 if (mapping.getVcs().length() > 0) {
289 vcses.add(myVcsManager.findVcsByName(mapping.getVcs()));
292 return vcses;
295 @Nls
296 public String getDisplayName() {
297 return "Mappings";
300 public Icon getIcon() {
301 return null;
304 public String getHelpTopic() {
305 return null;
308 public JComponent createComponent() {
309 return this;
312 public void disposeUIResources() {