ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / changes / committed / RepositoryChangesBrowser.java
blob00fe4b0da655dd856dbfe2a6c50de77dd25ecca6
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.committed;
18 import com.intellij.ide.actions.EditSourceAction;
19 import com.intellij.ide.impl.TypeSafeDataProviderAdapter;
20 import com.intellij.openapi.actionSystem.*;
21 import com.intellij.openapi.application.ModalityState;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.IconLoader;
24 import com.intellij.openapi.vcs.VcsDataKeys;
25 import com.intellij.openapi.vcs.changes.Change;
26 import com.intellij.openapi.vcs.changes.ChangeList;
27 import com.intellij.openapi.vcs.changes.ChangesUtil;
28 import com.intellij.openapi.vcs.changes.actions.OpenRepositoryVersionAction;
29 import com.intellij.openapi.vcs.changes.actions.RevertSelectedChangesAction;
30 import com.intellij.openapi.vcs.changes.actions.ShowDiffWithLocalAction;
31 import com.intellij.openapi.vcs.changes.ui.ChangesBrowser;
32 import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
33 import com.intellij.pom.Navigatable;
34 import org.jetbrains.annotations.NonNls;
36 import javax.swing.*;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.List;
42 /**
43 * @author yole
45 public class RepositoryChangesBrowser extends ChangesBrowser implements DataProvider {
46 private CommittedChangesBrowserUseCase myUseCase;
48 public RepositoryChangesBrowser(final Project project, final List<CommittedChangeList> changeLists) {
49 super(project, changeLists, Collections.<Change>emptyList(), null, false, false, null, MyUseCase.COMMITTED_CHANGES);
52 public RepositoryChangesBrowser(final Project project, final List<? extends ChangeList> changeLists, final List<Change> changes,
53 final ChangeList initialListSelection) {
54 super(project, changeLists, changes, initialListSelection, false, false, null, MyUseCase.COMMITTED_CHANGES);
57 protected void buildToolBar(final DefaultActionGroup toolBarGroup) {
58 super.buildToolBar(toolBarGroup);
60 toolBarGroup.add(new ShowDiffWithLocalAction());
61 final MyEditSourceAction editSourceAction = new MyEditSourceAction();
62 editSourceAction.registerCustomShortcutSet(CommonShortcuts.getEditSource(), this);
63 toolBarGroup.add(editSourceAction);
64 OpenRepositoryVersionAction action = new OpenRepositoryVersionAction();
65 toolBarGroup.add(action);
66 final RevertSelectedChangesAction revertSelectedChangesAction = new RevertSelectedChangesAction();
67 toolBarGroup.add(revertSelectedChangesAction);
69 ActionGroup group = (ActionGroup) ActionManager.getInstance().getAction("RepositoryChangesBrowserToolbar");
70 final AnAction[] actions = group.getChildren(null);
71 for (AnAction anAction : actions) {
72 toolBarGroup.add(anAction);
76 public void setUseCase(final CommittedChangesBrowserUseCase useCase) {
77 myUseCase = useCase;
80 public Object getData(@NonNls final String dataId) {
81 if (CommittedChangesBrowserUseCase.DATA_KEY.is(dataId)) {
82 return myUseCase;
85 else if (VcsDataKeys.SELECTED_CHANGES.is(dataId)) {
86 final List<Change> list = myViewer.getSelectedChanges();
87 return list.toArray(new Change [list.size()]);
88 } else if (VcsDataKeys.CHANGE_LEAD_SELECTION.is(dataId)) {
89 final Change highestSelection = myViewer.getHighestLeadSelection();
90 return (highestSelection == null) ? new Change[]{} : new Change[] {highestSelection};
91 } else {
92 final TypeSafeDataProviderAdapter adapter = new TypeSafeDataProviderAdapter(this);
93 return adapter.getData(dataId);
97 private class MyEditSourceAction extends EditSourceAction {
98 private final Icon myEditSourceIcon;
100 public MyEditSourceAction() {
101 myEditSourceIcon = IconLoader.getIcon("/actions/editSource.png");
104 public void update(final AnActionEvent event) {
105 super.update(event);
106 event.getPresentation().setIcon(myEditSourceIcon);
107 event.getPresentation().setText("Edit Source");
108 if ((! ModalityState.NON_MODAL.equals(ModalityState.current())) ||
109 CommittedChangesBrowserUseCase.IN_AIR.equals(CommittedChangesBrowserUseCase.DATA_KEY.getData(event.getDataContext()))) {
110 event.getPresentation().setEnabled(false);
114 protected Navigatable[] getNavigatables(final DataContext dataContext) {
115 Change[] changes = VcsDataKeys.SELECTED_CHANGES.getData(dataContext);
116 if (changes != null) {
117 Collection<Change> changeCollection = Arrays.asList(changes);
118 return ChangesUtil.getNavigatableArray(myProject, ChangesUtil.getFilesFromChanges(changeCollection));
120 return null;