ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / ui / RollbackProgressModifier.java
bloba936682535dc87b60b657b717d7a876f9efdc2d2
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.
16 package com.intellij.openapi.vcs.changes.ui;
18 import com.intellij.openapi.progress.ProgressIndicator;
19 import com.intellij.openapi.vcs.FilePath;
20 import com.intellij.openapi.vcs.VcsBundle;
21 import com.intellij.openapi.vcs.changes.Change;
22 import com.intellij.openapi.vcs.changes.ChangesUtil;
23 import com.intellij.openapi.vcs.rollback.RollbackProgressListener;
24 import com.intellij.openapi.vfs.VirtualFile;
25 import org.jetbrains.annotations.NotNull;
27 import java.io.File;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
32 public class RollbackProgressModifier implements RollbackProgressListener {
33 private final Set<String> myTakenPaths;
34 private final double myTotal;
35 private final ProgressIndicator myIndicator;
36 private int myCnt;
38 public RollbackProgressModifier(final double total, final ProgressIndicator indicator) {
39 myTotal = total;
40 myIndicator = indicator;
41 myTakenPaths = new HashSet<String>();
42 myCnt = 0;
45 private void acceptImpl(final String name) {
46 if (myIndicator != null) {
47 myIndicator.setText2(VcsBundle.message("rolling.back.file", name));
48 checkName(name);
49 if (! myIndicator.isIndeterminate()) {
50 myIndicator.setFraction(myCnt / myTotal);
52 myIndicator.checkCanceled();
56 private void checkName(final String name) {
57 if (! myTakenPaths.contains(name)) {
58 myTakenPaths.add(name);
59 if (myTotal >= (myCnt + 1)) {
60 ++ myCnt;
65 public void determinate() {
66 if (myIndicator != null) {
67 myIndicator.setIndeterminate(false);
71 public void indeterminate() {
72 if (myIndicator != null) {
73 myIndicator.setIndeterminate(true);
77 public void accept(@NotNull final Change change) {
78 acceptImpl(ChangesUtil.getFilePath(change).getIOFile().getAbsolutePath());
81 public void accept(@NotNull final FilePath filePath) {
82 acceptImpl(filePath.getIOFile().getAbsolutePath());
85 public void accept(final List<FilePath> paths) {
86 if (myIndicator != null) {
87 if (paths != null && (! paths.isEmpty())) {
88 for (int i = 0; i < paths.size(); i++) {
89 final FilePath path = paths.get(i);
90 final String name = path.getIOFile().getAbsolutePath();
91 checkName(name);
93 myIndicator.setFraction(myCnt / myTotal);
94 myIndicator.setText2(VcsBundle.message("rolling.back.file", paths.get(0).getIOFile().getAbsolutePath()));
99 public void accept(final File file) {
100 acceptImpl(file.getAbsolutePath());
103 public void accept(final VirtualFile file) {
104 acceptImpl(new File(file.getPath()).getAbsolutePath());
107 public void checkCanceled() {
108 if (myIndicator != null) {
109 myIndicator.checkCanceled();