summaryrefslogtreecommitdiff
path: root/plugins/hg4idea/src/org/zmlx/hg4idea/ui/HgConfigurationProjectPanel.java
blob: 619536de0f884f40b6b364f22964166efee9d4d2 (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
// Copyright 2008-2010 Victor Iacoban
//
// 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 org.zmlx.hg4idea.ui;

import com.intellij.dvcs.branch.DvcsBranchSync;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.components.JBCheckBox;
import org.jetbrains.annotations.NotNull;
import org.zmlx.hg4idea.HgProjectSettings;
import org.zmlx.hg4idea.HgVcs;
import org.zmlx.hg4idea.HgVcsMessages;
import org.zmlx.hg4idea.repo.HgRepositoryManager;
import org.zmlx.hg4idea.util.HgUtil;
import org.zmlx.hg4idea.util.HgVersion;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HgConfigurationProjectPanel {

  @NotNull private final HgProjectSettings myProjectSettings;

  private JPanel myMainPanel;
  private JCheckBox myCheckIncomingOutgoingCbx;
  private JCheckBox myIgnoredWhitespacesInAnnotationsCbx;
  private TextFieldWithBrowseButton myPathSelector;
  private JButton myTestButton;
  private JBCheckBox mySyncBranchControl;
  private JPanel myRepositorySettingsPanel;
  private final HgVcs myVcs;

  public HgConfigurationProjectPanel(@NotNull HgProjectSettings projectSettings, @NotNull Project project) {
    myProjectSettings = projectSettings;
    myVcs = HgVcs.getInstance(project);
    loadSettings();
    myTestButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String executable = getCurrentPath();
        HgVersion version;
        try {
          version = HgVersion.identifyVersion(executable);
        }
        catch (Exception exception) {
          Messages.showErrorDialog(myMainPanel, exception.getMessage(), HgVcsMessages.message("hg4idea.run.failed.title"));
          return;
        }
        Messages.showInfoMessage(myMainPanel, String.format("Mercurial version is %s", version.toString()),
                                 HgVcsMessages.message("hg4idea.run.success.title")
        );
      }
    });
    final HgRepositoryManager repositoryManager = ServiceManager.getService(project, HgRepositoryManager.class);
    myRepositorySettingsPanel.setVisible(repositoryManager != null && repositoryManager.moreThanOneRoot());
  }

  public boolean isModified() {
    boolean executableModified = !getCurrentPath().equals(myProjectSettings.getHgExecutable());
    return executableModified ||
           myCheckIncomingOutgoingCbx.isSelected() != myProjectSettings.isCheckIncomingOutgoing() ||
           ((myProjectSettings.getSyncSetting() == DvcsBranchSync.SYNC) != mySyncBranchControl.isSelected()) ||
           myIgnoredWhitespacesInAnnotationsCbx.isSelected() != myProjectSettings.isWhitespacesIgnoredInAnnotations();
  }

  public void saveSettings() {
    myProjectSettings.setCheckIncomingOutgoing(myCheckIncomingOutgoingCbx.isSelected());
    myProjectSettings.setIgnoreWhitespacesInAnnotations(myIgnoredWhitespacesInAnnotationsCbx.isSelected());
    myProjectSettings.setHgExecutable(getCurrentPath());
    myProjectSettings.setSyncSetting(mySyncBranchControl.isSelected() ? DvcsBranchSync.SYNC : DvcsBranchSync.DONT);
    myVcs.checkVersion();
  }

  private String getCurrentPath() {
    return myPathSelector.getText().trim();
  }

  public void loadSettings() {
    myCheckIncomingOutgoingCbx.setSelected(myProjectSettings.isCheckIncomingOutgoing());
    myIgnoredWhitespacesInAnnotationsCbx.setSelected(myProjectSettings.isWhitespacesIgnoredInAnnotations());
    myPathSelector.setText(myProjectSettings.getGlobalSettings().getHgExecutable());
    mySyncBranchControl.setSelected(myProjectSettings.getSyncSetting() == DvcsBranchSync.SYNC);
  }

  public JPanel getPanel() {
    return myMainPanel;
  }

  public void validate() throws ConfigurationException {
    String hgExecutable;
    hgExecutable = getCurrentPath();
    if (!HgUtil.isExecutableValid(hgExecutable)) {
      throw new ConfigurationException(
        HgVcsMessages.message("hg4idea.configuration.executable.error", hgExecutable)
      );
    }
  }

  private void createUIComponents() {
    myPathSelector = new HgSetExecutablePathPanel(myProjectSettings);
  }

  @NotNull
  public HgProjectSettings getProjectSettings() {
    return myProjectSettings;
  }
}