update copyright
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / actions / AddAction.java
blobc9cb2b633278dd844c84aca15cc51c89b645c335
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.jetbrains.idea.svn.actions;
20 import com.intellij.openapi.actionSystem.DataContext;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.vcs.AbstractVcs;
24 import com.intellij.openapi.vcs.VcsException;
25 import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.openapi.vfs.VirtualFileManager;
28 import org.jetbrains.idea.svn.SvnBundle;
29 import org.jetbrains.idea.svn.SvnStatusUtil;
30 import org.jetbrains.idea.svn.SvnVcs;
31 import org.jetbrains.idea.svn.checkin.SvnCheckinEnvironment;
32 import org.tmatesoft.svn.core.SVNCancelException;
33 import org.tmatesoft.svn.core.SVNException;
34 import org.tmatesoft.svn.core.wc.ISVNEventHandler;
35 import org.tmatesoft.svn.core.wc.SVNEvent;
36 import org.tmatesoft.svn.core.wc.SVNEventAction;
37 import org.tmatesoft.svn.core.wc.SVNWCClient;
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collection;
44 public class AddAction extends BasicAction {
45 static final Logger log = Logger.getInstance("org.jetbrains.idea.svn.action.AddAction");
47 protected String getActionName(AbstractVcs vcs) {
48 log.debug("enter: getActionName");
49 return SvnBundle.message("action.name.add.files", vcs.getName());
52 protected boolean isEnabled(Project project, SvnVcs vcs, VirtualFile file) {
53 return SvnStatusUtil.fileCanBeAdded(project, file);
56 protected boolean needsFiles() {
57 return true;
60 protected void batchPerform(final Project project, SvnVcs activeVcs, VirtualFile[] files, DataContext context)
61 throws VcsException {
62 log.debug("enter: batchPerform");
64 SvnVcs vcs = SvnVcs.getInstance(project);
65 SVNWCClient wcClient = vcs.createWCClient();
66 wcClient.setEventHandler(new AddEventListener(project));
68 Collection<SVNException> exceptions = SvnCheckinEnvironment.scheduleUnversionedFilesForAddition(wcClient, Arrays.asList(files), true);
69 if (! exceptions.isEmpty()) {
70 final Collection<String> messages = new ArrayList<String>(exceptions.size());
71 for (SVNException exception : exceptions) {
72 messages.add(exception.getMessage());
74 throw new VcsException(messages);
78 protected boolean isBatchAction() {
79 log.debug("enter: isBatchAction");
80 return true;
83 protected void perform(Project project, SvnVcs activeVcs, VirtualFile file, DataContext context)
84 throws VcsException {
85 try {
86 SVNWCClient wcClient = activeVcs.createWCClient();
87 wcClient.setEventHandler(new AddEventListener(project));
88 wcClient.doAdd(new File(file.getPath()), false, false, true, true);
90 catch (SVNException e) {
91 VcsException ve = new VcsException(e);
92 ve.setVirtualFile(file);
93 throw ve;
97 private static class AddEventListener implements ISVNEventHandler {
98 private final Project myProject;
100 public AddEventListener(Project project) {
101 myProject = project;
104 public void handleEvent(SVNEvent event, double progress) {
105 if (event.getAction() == SVNEventAction.ADD && event.getFile() != null) {
106 VirtualFile vfile = VirtualFileManager.getInstance()
107 .findFileByUrl("file://" + event.getFile().getAbsolutePath().replace(File.separatorChar, '/'));
108 if (vfile != null) {
109 VcsDirtyScopeManager.getInstance(myProject).fileDirty(vfile);
114 public void checkCancelled() throws SVNCancelException {