import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vcs.VcsDirectoryMapping;
+import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.zmlx.hg4idea.HgUtil;
import org.zmlx.hg4idea.HgVcs;
import java.util.List;
/**
- * Action for initializing Mercurial repository. Command "hg init".
+ * Action for initializing a Mercurial repository.
+ * Command "hg init".
* @author Kirill Likhodedov
*/
public class HgInit extends DumbAwareAction {
- private static final Logger LOG = Logger.getInstance(HgInit.class.getName());
private Project myProject;
- public HgInit() {
- }
-
@Override
public void actionPerformed(AnActionEvent e) {
myProject = e.getData(PlatformDataKeys.PROJECT);
- if (myProject == null) {
- LOG.warn("[actionPerformed] project is null");
- return;
- }
// provide window to select the root directory
final HgInitDialog hgInitDialog = new HgInitDialog(myProject);
return;
}
- // check if it the project is not yet under mercurial and provide some options in that case
+ // check if the selected folder is not yet under mercurial and provide some options in that case
final VirtualFile vcsRoot = HgUtil.getNearestHgRoot(selectedRoot);
VirtualFile mapRoot = selectedRoot;
if (vcsRoot != null) {
if (dialog.getAnswer() == HgInitAlreadyUnderHgDialog.Answer.USE_PARENT_REPO) {
mapRoot = vcsRoot;
} else if (dialog.getAnswer() == HgInitAlreadyUnderHgDialog.Answer.CREATE_REPO_HERE) {
- createRepository(selectedRoot);
+ if (!createRepository(selectedRoot)) {
+ return;
+ }
}
} else { // no parent repository => creating the repository here.
- createRepository(selectedRoot);
+ if (!createRepository(selectedRoot)){
+ return;
+ }
}
- // update vcs directory mappings
- mapRoot.refresh(false, false);
- final String path = mapRoot.equals(myProject.getBaseDir()) ? "" : mapRoot.getPath();
- final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
- final List<VcsDirectoryMapping> vcsDirectoryMappings = new ArrayList<VcsDirectoryMapping>(vcsManager.getDirectoryMappings());
- VcsDirectoryMapping mapping = new VcsDirectoryMapping(path, HgVcs.VCS_NAME);
- for (int i = 0; i < vcsDirectoryMappings.size(); i++) {
- final VcsDirectoryMapping m = vcsDirectoryMappings.get(i);
- if (m.getDirectory().equals(path)) {
- if (m.getVcs().length() == 0) {
- vcsDirectoryMappings.set(i, mapping);
- mapping = null;
- break;
- }
- else if (m.getVcs().equals(mapping.getVcs())) {
- mapping = null;
- break;
+ // update vcs directory mappings if new repository was created inside the current project directory
+ if (myProject != null && myProject.getBaseDir() != null && VfsUtil.isAncestor(myProject.getBaseDir(), mapRoot, false)) {
+ mapRoot.refresh(false, false);
+ final String path = mapRoot.equals(myProject.getBaseDir()) ? "" : mapRoot.getPath();
+ final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
+ final List<VcsDirectoryMapping> vcsDirectoryMappings = new ArrayList<VcsDirectoryMapping>(vcsManager.getDirectoryMappings());
+ VcsDirectoryMapping mapping = new VcsDirectoryMapping(path, HgVcs.VCS_NAME);
+ for (int i = 0; i < vcsDirectoryMappings.size(); i++) {
+ final VcsDirectoryMapping m = vcsDirectoryMappings.get(i);
+ if (m.getDirectory().equals(path)) {
+ if (m.getVcs().length() == 0) {
+ vcsDirectoryMappings.set(i, mapping);
+ mapping = null;
+ break;
+ }
+ else if (m.getVcs().equals(mapping.getVcs())) {
+ mapping = null;
+ break;
+ }
}
}
+ if (mapping != null) {
+ vcsDirectoryMappings.add(mapping);
+ }
+ vcsManager.setDirectoryMappings(vcsDirectoryMappings);
+ vcsManager.updateActiveVcss();
}
- if (mapping != null) {
- vcsDirectoryMappings.add(mapping);
- }
- vcsManager.setDirectoryMappings(vcsDirectoryMappings);
- vcsManager.updateActiveVcss();
}
@Override
presentation.setVisible(project != null);
}
- private void createRepository(VirtualFile selectedRoot) {
- if ((new HgInitCommand(myProject)).execute(selectedRoot)) {
- Notifications.Bus.notify(new Notification(HgVcs.NOTIFICATION_GROUP_ID,
- HgVcsMessages.message("hg4idea.init.created.notification.title"),
- HgVcsMessages.message("hg4idea.init.created.notification.description", selectedRoot.getPresentableUrl()),
- NotificationType.INFORMATION), myProject);
- }
+ private boolean createRepository(VirtualFile selectedRoot) {
+ final boolean succeeded = (new HgInitCommand(myProject)).execute(selectedRoot);
+ Notifications.Bus.notify(new Notification(HgVcs.NOTIFICATION_GROUP_ID,
+ HgVcsMessages.message(succeeded ? "hg4idea.init.created.notification.title" : "hg4idea.init.error.title"),
+ HgVcsMessages.message(succeeded ? "hg4idea.init.created.notification.description" : "hg4idea.init.error.description",
+ selectedRoot.getPresentableUrl()),
+ succeeded ? NotificationType.INFORMATION : NotificationType.ERROR), myProject);
+ return succeeded;
}
}
private JRadioButton myCreateRepositoryForTheRadioButton;
private JRadioButton mySelectWhereToCreateRadioButton;
private TextFieldWithBrowseButton myTextFieldBrowser;
- private final Project myProject;
+
+ @Nullable private final Project myProject;
+ private final boolean myShowDialog; // basing on this field, show options or invoke file chooser at once
+ private final FileChooserDescriptor myFileDescriptor;
private VirtualFile mySelectedDir;
- private FileChooserDescriptor myFileDescriptor;
- private boolean myIsProjectBaseDirHgRoot; // basing on this field, show options or invoke file chooser at once
- public HgInitDialog(Project project) {
+ public HgInitDialog(@Nullable Project project) {
super(project);
myProject = project;
- myIsProjectBaseDirHgRoot = HgUtil.isHgRoot(myProject.getBaseDir());
+ // a file chooser instead of dialog will be shown immediately if there is no current project or if current project is already an hg root
+ myShowDialog = (myProject != null && !HgUtil.isHgRoot(myProject.getBaseDir()));
+
+ myFileDescriptor = new FileChooserDescriptor(false, true, false, false, false, false) {
+ public void validateSelectedFiles(VirtualFile[] files) throws Exception {
+ if (HgUtil.isHgRoot(files[0])) {
+ throw new ConfigurationException(HgVcsMessages.message("hg4idea.init.this.is.hg.root", files[0].getPresentableUrl()));
+ }
+ updateEverything();
+ }
+ };
+ myFileDescriptor.setHideIgnored(false);
+
init();
}
protected void init() {
super.init();
setTitle(HgVcsMessages.message("hg4idea.init.dialog.title"));
- mySelectedDir = myProject.getBaseDir();
+ if (myProject != null) {
+ mySelectedDir = myProject.getBaseDir();
+ }
mySelectWhereToCreateRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
- myFileDescriptor = new FileChooserDescriptor(false, true, false, false, false, false) {
- public void validateSelectedFiles(VirtualFile[] files) throws Exception {
- if (HgUtil.isHgRoot(files[0])) {
- throw new ConfigurationException(HgVcsMessages.message("hg4idea.init.this.is.hg.root", files[0].getPresentableUrl()));
- }
- updateEverything();
- }
- };
- myFileDescriptor.setHideIgnored(false);
myTextFieldBrowser.addBrowseFolderListener(HgVcsMessages.message("hg4idea.init.destination.directory.title"),
HgVcsMessages.message("hg4idea.init.destination.directory.description"),
myProject, myFileDescriptor);
*/
@Override
public void show() {
- if (myIsProjectBaseDirHgRoot) {
- final VirtualFile[] files = FileChooser.chooseFiles(myProject, myFileDescriptor, myProject.getBaseDir());
- mySelectedDir = (files.length == 0 ? null : files[0]);
- } else {
+ if (myShowDialog) {
super.show();
+ } else {
+ final VirtualFile[] files = FileChooser.chooseFiles(myProject, myFileDescriptor);
+ mySelectedDir = (files.length == 0 ? null : files[0]);
}
}
@Override
public boolean isOK() {
- if (myIsProjectBaseDirHgRoot) {
- return mySelectedDir != null;
- }
- return super.isOK();
+ return myShowDialog ? super.isOK() : mySelectedDir != null;
}
@Nullable
* enable/disable the 'OK' button, show error text and update mySelectedDir.
*/
private void updateEverything() {
- if (myCreateRepositoryForTheRadioButton.isSelected()) {
+ if (myShowDialog && myCreateRepositoryForTheRadioButton.isSelected()) {
enableOKAction();
mySelectedDir = myProject.getBaseDir();
} else {