Search in sources :

Example 1 with ReplaceWithOursTheirsMenu

use of org.eclipse.egit.ui.internal.actions.ReplaceWithOursTheirsMenu in project egit by eclipse.

the class StagingView method createPopupMenu.

private void createPopupMenu(final TreeViewer treeViewer) {
    final MenuManager menuMgr = new MenuManager();
    menuMgr.setRemoveAllWhenShown(true);
    Control control = treeViewer.getControl();
    control.setMenu(menuMgr.createContextMenu(control));
    menuMgr.addMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(IMenuManager manager) {
            control.setFocus();
            final IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
            if (selection.isEmpty())
                return;
            Set<StagingEntry> stagingEntrySet = new LinkedHashSet<>();
            Set<StagingFolderEntry> stagingFolderSet = new LinkedHashSet<>();
            boolean submoduleSelected = false;
            boolean folderSelected = false;
            boolean onlyFoldersSelected = true;
            for (Object element : selection.toArray()) {
                if (element instanceof StagingFolderEntry) {
                    StagingFolderEntry folder = (StagingFolderEntry) element;
                    folderSelected = true;
                    if (onlyFoldersSelected) {
                        stagingFolderSet.add(folder);
                    }
                    StagingViewContentProvider contentProvider = getContentProvider(treeViewer);
                    stagingEntrySet.addAll(contentProvider.getStagingEntriesFiltered(folder));
                } else if (element instanceof StagingEntry) {
                    if (onlyFoldersSelected) {
                        stagingFolderSet.clear();
                    }
                    onlyFoldersSelected = false;
                    StagingEntry entry = (StagingEntry) element;
                    if (entry.isSubmodule()) {
                        submoduleSelected = true;
                    }
                    stagingEntrySet.add(entry);
                }
            }
            List<StagingEntry> stagingEntryList = new ArrayList<>(stagingEntrySet);
            final IStructuredSelection fileSelection = new StructuredSelection(stagingEntryList);
            stagingEntrySet = null;
            if (!folderSelected) {
                Action openWorkingTreeVersion = new Action(UIText.CommitFileDiffViewer_OpenWorkingTreeVersionInEditorMenuLabel) {

                    @Override
                    public void run() {
                        openSelectionInEditor(fileSelection);
                    }
                };
                openWorkingTreeVersion.setEnabled(!submoduleSelected && anyElementIsExistingFile(fileSelection));
                menuMgr.add(openWorkingTreeVersion);
                String label = stagingEntryList.get(0).isStaged() ? UIText.CommitFileDiffViewer_CompareWorkingDirectoryMenuLabel : UIText.StagingView_CompareWithIndexMenuLabel;
                Action openCompareWithIndex = new Action(label) {

                    @Override
                    public void run() {
                        runCommand(ActionCommands.COMPARE_WITH_INDEX_ACTION, fileSelection);
                    }
                };
                menuMgr.add(openCompareWithIndex);
            }
            menuMgr.add(createSelectionPathCopyAction(treeViewer));
            Set<StagingEntry.Action> availableActions = getAvailableActions(fileSelection);
            boolean addReplaceWithFileInGitIndex = availableActions.contains(StagingEntry.Action.REPLACE_WITH_FILE_IN_GIT_INDEX);
            boolean addReplaceWithHeadRevision = availableActions.contains(StagingEntry.Action.REPLACE_WITH_HEAD_REVISION);
            boolean addStage = availableActions.contains(StagingEntry.Action.STAGE);
            boolean addUnstage = availableActions.contains(StagingEntry.Action.UNSTAGE);
            boolean addDelete = availableActions.contains(StagingEntry.Action.DELETE);
            boolean addIgnore = availableActions.contains(StagingEntry.Action.IGNORE);
            boolean addLaunchMergeTool = availableActions.contains(StagingEntry.Action.LAUNCH_MERGE_TOOL);
            boolean addReplaceWithOursTheirsMenu = availableActions.contains(StagingEntry.Action.REPLACE_WITH_OURS_THEIRS_MENU);
            boolean addAssumeUnchanged = availableActions.contains(StagingEntry.Action.ASSUME_UNCHANGED);
            boolean addUntrack = availableActions.contains(StagingEntry.Action.UNTRACK);
            if (addStage) {
                menuMgr.add(new Action(UIText.StagingView_StageItemMenuLabel, UIIcons.ELCL16_ADD) {

                    @Override
                    public void run() {
                        stage(selection);
                    }
                });
            }
            if (addUnstage) {
                menuMgr.add(new Action(UIText.StagingView_UnstageItemMenuLabel, UIIcons.UNSTAGE) {

                    @Override
                    public void run() {
                        unstage(selection);
                    }
                });
            }
            boolean selectionIncludesNonWorkspaceResources = selectionIncludesNonWorkspaceResources(fileSelection);
            if (addReplaceWithFileInGitIndex) {
                if (selectionIncludesNonWorkspaceResources) {
                    menuMgr.add(new ReplaceAction(UIText.StagingView_replaceWithFileInGitIndex, fileSelection, false));
                } else {
                    menuMgr.add(createItem(UIText.StagingView_replaceWithFileInGitIndex, ActionCommands.DISCARD_CHANGES_ACTION, // replace with index
                    fileSelection));
                }
            }
            if (addReplaceWithHeadRevision) {
                if (selectionIncludesNonWorkspaceResources) {
                    menuMgr.add(new ReplaceAction(UIText.StagingView_replaceWithHeadRevision, fileSelection, true));
                } else {
                    menuMgr.add(createItem(UIText.StagingView_replaceWithHeadRevision, ActionCommands.REPLACE_WITH_HEAD_ACTION, fileSelection));
                }
            }
            if (addIgnore) {
                if (!stagingFolderSet.isEmpty()) {
                    menuMgr.add(new IgnoreFoldersAction(stagingFolderSet));
                }
                menuMgr.add(new IgnoreAction(fileSelection));
            }
            if (addDelete) {
                menuMgr.add(new DeleteAction(fileSelection));
            }
            if (addLaunchMergeTool) {
                menuMgr.add(createItem(UIText.StagingView_MergeTool, ActionCommands.MERGE_TOOL_ACTION, fileSelection));
            }
            if (addReplaceWithOursTheirsMenu) {
                MenuManager replaceWithMenu = new MenuManager(UIText.StagingView_ReplaceWith);
                ReplaceWithOursTheirsMenu oursTheirsMenu = new ReplaceWithOursTheirsMenu();
                oursTheirsMenu.initialize(getSite());
                replaceWithMenu.add(oursTheirsMenu);
                menuMgr.add(replaceWithMenu);
            }
            if (addAssumeUnchanged) {
                menuMgr.add(new Action(UIText.StagingView_Assume_Unchanged, UIIcons.ASSUME_UNCHANGED) {

                    @Override
                    public void run() {
                        assumeUnchanged(selection);
                    }
                });
            }
            if (addUntrack) {
                menuMgr.add(new Action(UIText.StagingView_Untrack, UIIcons.UNTRACK) {

                    @Override
                    public void run() {
                        untrack(selection);
                    }
                });
            }
            menuMgr.add(new Separator());
            menuMgr.add(createShowInMenu());
        }
    });
}
Also used : IAction(org.eclipse.jface.action.IAction) Action(org.eclipse.jface.action.Action) BooleanPrefAction(org.eclipse.egit.ui.internal.actions.BooleanPrefAction) RepositoryToolbarAction(org.eclipse.egit.ui.internal.components.RepositoryMenuUtil.RepositoryToolbarAction) IWorkbenchAction(org.eclipse.ui.actions.ActionFactory.IWorkbenchAction) EnumSet(java.util.EnumSet) HashSet(java.util.HashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IMenuListener(org.eclipse.jface.action.IMenuListener) Control(org.eclipse.swt.widgets.Control) ReplaceWithOursTheirsMenu(org.eclipse.egit.ui.internal.actions.ReplaceWithOursTheirsMenu) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) ArrayList(java.util.ArrayList) List(java.util.List) IMenuManager(org.eclipse.jface.action.IMenuManager) Separator(org.eclipse.jface.action.Separator)

Aggregations

ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Set (java.util.Set)1 BooleanPrefAction (org.eclipse.egit.ui.internal.actions.BooleanPrefAction)1 ReplaceWithOursTheirsMenu (org.eclipse.egit.ui.internal.actions.ReplaceWithOursTheirsMenu)1 RepositoryToolbarAction (org.eclipse.egit.ui.internal.components.RepositoryMenuUtil.RepositoryToolbarAction)1 Action (org.eclipse.jface.action.Action)1 IAction (org.eclipse.jface.action.IAction)1 IMenuListener (org.eclipse.jface.action.IMenuListener)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 MenuManager (org.eclipse.jface.action.MenuManager)1 Separator (org.eclipse.jface.action.Separator)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 Control (org.eclipse.swt.widgets.Control)1 IWorkbenchAction (org.eclipse.ui.actions.ActionFactory.IWorkbenchAction)1