summaryrefslogtreecommitdiff
path: root/plugins/cvs/cvs-plugin/src/com/intellij/cvsSupport2/config/ui/CvsConfigurationsListEditor.java
blob: 6182b816ef384fb82b56af0670e1c06058338271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.cvsSupport2.config.ui;

import com.intellij.CvsBundle;
import com.intellij.cvsSupport2.config.CvsApplicationLevelConfiguration;
import com.intellij.cvsSupport2.config.CvsRootConfiguration;
import com.intellij.cvsSupport2.ui.CvsRootChangeListener;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CustomShortcutSet;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.InputException;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.AnActionButtonRunnable;
import com.intellij.ui.DumbAwareActionButton;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBList;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * author: lesya
 */
public class CvsConfigurationsListEditor extends DialogWrapper implements DataProvider{
  private final Project myProject;
  private final JList myList = new JBList();
  private final DefaultListModel myModel = new DefaultListModel();
  private CvsRootConfiguration mySelection;

  private final Cvs2SettingsEditPanel myCvs2SettingsEditPanel;
  @NonNls private static final String SAMPLE_CVSROOT = ":pserver:user@host/server/home/user/cvs";
  private boolean myIsUpdating = false;

  public CvsConfigurationsListEditor(List<CvsRootConfiguration> configs, Project project) {
    this(configs, project, false);
  }

  public CvsConfigurationsListEditor(List<CvsRootConfiguration> configs, Project project, boolean readOnly) {
    super(true);
    myProject = project;
    myCvs2SettingsEditPanel = new Cvs2SettingsEditPanel(project, readOnly);
    setTitle(CvsBundle.message("operation.name.edit.configurations"));
    myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    selectNone();
    fillModel(configs);

    myCvs2SettingsEditPanel.addCvsRootChangeListener(new CvsRootChangeListener() {
      @Override
      public void onCvsRootChanged() {
        if (mySelection == null) return;
        try {
          myCvs2SettingsEditPanel.saveTo(mySelection);
        } catch (InputException ignore) {}
        myList.repaint();
      }
    });
    setTitle(CvsBundle.message("dialog.title.cvs.roots"));
    if (!configs.isEmpty()) {
      myList.setSelectedIndex(0);
    }
    if (readOnly) {
      myList.setEnabled(false);
    }
    init();
  }

  @Nullable
  public static CvsRootConfiguration reconfigureCvsRoot(String root, Project project){
    final CvsApplicationLevelConfiguration configuration = CvsApplicationLevelConfiguration.getInstance();
    final CvsRootConfiguration selectedConfig = configuration.getConfigurationForCvsRoot(root);
    final ArrayList<CvsRootConfiguration> modifiableList = new ArrayList<CvsRootConfiguration>(configuration.CONFIGURATIONS);
    final CvsConfigurationsListEditor editor = new CvsConfigurationsListEditor(modifiableList, project, true);
    editor.select(selectedConfig);
    editor.show();
    if (editor.isOK()){
      configuration.CONFIGURATIONS = modifiableList;
      return configuration.getConfigurationForCvsRoot(root);
    } else {
      return null;
    }
  }

  @NotNull
  @Override
  protected Action[] createLeftSideActions() {
    final AbstractAction globalSettingsAction = new AbstractAction(CvsBundle.message("button.text.global.settings")) {
      @Override
      public void actionPerformed(ActionEvent e) {
        new ConfigureCvsGlobalSettingsDialog(myProject).show();
      }
    };
    return new Action[]{globalSettingsAction};
  }

  @Override
  protected void doOKAction() {
    if (saveSelectedConfiguration()) {
      super.doOKAction();
    }
  }

  private void fillModel(List<CvsRootConfiguration> configurations) {
    for (final CvsRootConfiguration configuration : configurations) {
      myModel.addElement(configuration.getMyCopy());
    }
  }

  private JComponent createListPanel() {
    final AnActionButton duplicateButton =
      new DumbAwareActionButton(CvsBundle.message("action.name.copy"), PlatformIcons.COPY_ICON) {

        @Override
        public void updateButton(AnActionEvent e) {
          e.getPresentation().setEnabled(getSelectedConfiguration() != null);
        }

        @Override
        public void actionPerformed(AnActionEvent e) {
          copySelectedConfiguration();
        }
      };
    duplicateButton.setShortcut(new CustomShortcutSet(
      KeyStroke.getKeyStroke(KeyEvent.VK_D, SystemInfo.isMac ? KeyEvent.META_MASK : KeyEvent.CTRL_MASK)));
    final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myList).setAddAction(new AnActionButtonRunnable() {
      @Override
      public void run(AnActionButton anActionButton) {
        createNewConfiguration();
      }
    }).addExtraAction(duplicateButton);
    return decorator.createPanel();
  }

  @Override
  protected JComponent createCenterPanel() {
    myList.setCellRenderer(new CvsListCellRenderer());
    final BorderLayout layout = new BorderLayout();
    layout.setHgap(6);

    final JPanel centerPanel = new JPanel(layout);
    final JComponent listPanel = createListPanel();
    centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
    centerPanel.add(listPanel, BorderLayout.CENTER);
    centerPanel.add(createCvsConfigurationPanel(), BorderLayout.EAST);

    myList.setModel(myModel);
    addSelectionListener();
    final int minWidth = myList.getFontMetrics(myList.getFont()).stringWidth(SAMPLE_CVSROOT) + 40;
    final Dimension minSize = new Dimension(minWidth, myList.getMaximumSize().height);
    listPanel.setMinimumSize(minSize);
    listPanel.setPreferredSize(minSize);
    return centerPanel;
  }

  private JComponent createCvsConfigurationPanel() {
    return myCvs2SettingsEditPanel.getPanel();
  }

  private boolean saveSelectedConfiguration() {
    if (getSelectedConfiguration() == null) return true;
    try {
      myCvs2SettingsEditPanel.saveTo(getSelectedConfiguration());
      return true;
    } catch (InputException e) {
      e.show();
      return false;
    }
  }

  private void copySelectedConfiguration() {
    if (!saveSelectedConfiguration()) return;
    final CvsRootConfiguration newConfig = mySelection.getMyCopy();
    myModel.addElement(newConfig);
    myList.setSelectedIndex(myModel.getSize() - 1);
  }

  private void editSelectedConfiguration() {
    editConfiguration(mySelection);
    myList.repaint();
  }

  private void createNewConfiguration() {
    if (!saveSelectedConfiguration()) return;
    myList.setSelectedValue(null, false);
    final CvsRootConfiguration newConfig =
      CvsApplicationLevelConfiguration.createNewConfiguration(CvsApplicationLevelConfiguration.getInstance());
    myModel.addElement(newConfig);
    myList.setSelectedValue(newConfig, true);
  }

  private void editConfiguration(CvsRootConfiguration newConfig) {
    myCvs2SettingsEditPanel.updateFrom(newConfig);
  }

  private void addSelectionListener() {
    myList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
        if (myIsUpdating) {
          return;
        }
        final int selectedIndex = myList.getSelectedIndex();
        if (selectedIndex < 0 || selectedIndex >= myModel.getSize()) {
          selectNone();
        }
        else {
          final CvsRootConfiguration newSelection = (CvsRootConfiguration)myModel.getElementAt(selectedIndex);
          if (newSelection == mySelection) return;
          if (!select(newSelection)) {
            myIsUpdating = true;
            myList.setSelectedValue(mySelection, true);
            myIsUpdating = false;
          }
        }
      }
    });
  }

  private boolean select(CvsRootConfiguration cvsConfiguration) {
    if (mySelection != null) {
      try {
        myCvs2SettingsEditPanel.saveTo(mySelection);
      } catch (final InputException e) {
        SwingUtilities.invokeLater(new Runnable() {

          @Override
          public void run() {
            e.show();
          }
        });
        return false;
      }
    }
    mySelection = cvsConfiguration;
    editSelectedConfiguration();
    return true;
  }

  private void selectNone() {
    mySelection = null;
    myCvs2SettingsEditPanel.disable();
  }

  public List<CvsRootConfiguration> getConfigurations() {
    final ArrayList<CvsRootConfiguration> result = new ArrayList<CvsRootConfiguration>();
    final Enumeration each = myModel.elements();
    while (each.hasMoreElements()) result.add((CvsRootConfiguration)each.nextElement());
    return result;
  }

  public CvsRootConfiguration getSelectedConfiguration() {
    return mySelection;
  }

  public void selectConfiguration(CvsRootConfiguration selectedConfiguration) {
    myList.setSelectedValue(selectedConfiguration, true);
  }

  @Override
  @NonNls
  public Object getData(String dataId) {
    if (PlatformDataKeys.HELP_ID.is(dataId)){
      return "reference.versioncontrol.cvs.roots";
    }
    return null;
  }

  @Override
  protected void doHelpAction() {
    HelpManager.getInstance().invokeHelp("reference.versioncontrol.cvs.roots");
  }

  @NotNull
  @Override
  protected Action[] createActions() {
    return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    if (myModel.isEmpty()) return null;
    return myCvs2SettingsEditPanel.getPreferredFocusedComponent();
  }
}