Search in sources :

Example 1 with CheckoutDialog

use of org.eclipse.egit.ui.internal.dialogs.CheckoutDialog in project egit by eclipse.

the class SwitchToMenu method createDynamicMenu.

private void createDynamicMenu(Menu menu, final Repository repository) {
    MenuItem newBranch = new MenuItem(menu, SWT.PUSH);
    newBranch.setText(UIText.SwitchToMenu_NewBranchMenuLabel);
    newBranch.setImage(newBranchImage);
    newBranch.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String sourceRef = repository.getConfig().getString(ConfigConstants.CONFIG_WORKFLOW_SECTION, null, ConfigConstants.CONFIG_KEY_DEFBRANCHSTARTPOINT);
            CreateBranchWizard wiz = null;
            try {
                Ref ref = null;
                if (sourceRef != null) {
                    ref = repository.findRef(sourceRef);
                }
                if (ref != null) {
                    wiz = new CreateBranchWizard(repository, ref.getName());
                } else {
                    wiz = new CreateBranchWizard(repository, repository.getFullBranch());
                }
            } catch (IOException e1) {
            // Ignore
            }
            if (wiz == null) {
                wiz = new CreateBranchWizard(repository);
            }
            WizardDialog dlg = new WizardDialog(e.display.getActiveShell(), wiz);
            dlg.setHelpAvailable(false);
            dlg.open();
        }
    });
    createSeparator(menu);
    try {
        String currentBranch = repository.getFullBranch();
        Map<String, Ref> localBranches = repository.getRefDatabase().getRefs(Constants.R_HEADS);
        TreeMap<String, Ref> sortedRefs = new TreeMap<>(CommonUtils.STRING_ASCENDING_COMPARATOR);
        // Add the MAX_NUM_MENU_ENTRIES most recently used branches first
        ReflogReader reflogReader = repository.getReflogReader(Constants.HEAD);
        List<ReflogEntry> reflogEntries;
        if (reflogReader == null) {
            reflogEntries = Collections.emptyList();
        } else {
            reflogEntries = reflogReader.getReverseEntries();
        }
        for (ReflogEntry entry : reflogEntries) {
            CheckoutEntry checkout = entry.parseCheckout();
            if (checkout != null) {
                Ref ref = localBranches.get(checkout.getFromBranch());
                if (ref != null)
                    if (sortedRefs.size() < MAX_NUM_MENU_ENTRIES)
                        sortedRefs.put(checkout.getFromBranch(), ref);
                ref = localBranches.get(checkout.getToBranch());
                if (ref != null)
                    if (sortedRefs.size() < MAX_NUM_MENU_ENTRIES)
                        sortedRefs.put(checkout.getToBranch(), ref);
            }
        }
        // Add the recently used branches to the menu, in alphabetical order
        int itemCount = 0;
        for (final Entry<String, Ref> entry : sortedRefs.entrySet()) {
            itemCount++;
            final String shortName = entry.getKey();
            final String fullName = entry.getValue().getName();
            createMenuItem(menu, repository, currentBranch, fullName, shortName);
            // Do not duplicate branch names
            localBranches.remove(shortName);
        }
        if (itemCount < MAX_NUM_MENU_ENTRIES) {
            // local branches
            if (itemCount > 0 && localBranches.size() > 0)
                createSeparator(menu);
            // Now add more other branches if we have only a few branch switches
            // Sort the remaining local branches
            sortedRefs.clear();
            sortedRefs.putAll(localBranches);
            for (final Entry<String, Ref> entry : sortedRefs.entrySet()) {
                itemCount++;
                // protect ourselves against a huge sub-menu
                if (itemCount > MAX_NUM_MENU_ENTRIES)
                    break;
                final String fullName = entry.getValue().getName();
                final String shortName = entry.getKey();
                createMenuItem(menu, repository, currentBranch, fullName, shortName);
            }
        }
        if (itemCount > 0)
            createSeparator(menu);
        MenuItem others = new MenuItem(menu, SWT.PUSH);
        others.setText(UIText.SwitchToMenu_OtherMenuLabel);
        others.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                CheckoutDialog dialog = new CheckoutDialog(e.display.getActiveShell(), repository);
                if (dialog.open() == Window.OK) {
                    BranchOperationUI.checkout(repository, dialog.getRefName()).start();
                }
            }
        });
    } catch (IOException e) {
        Activator.handleError(e.getMessage(), e, true);
    }
}
Also used : ReflogReader(org.eclipse.jgit.lib.ReflogReader) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) CheckoutDialog(org.eclipse.egit.ui.internal.dialogs.CheckoutDialog) MenuItem(org.eclipse.swt.widgets.MenuItem) IOException(java.io.IOException) TreeMap(java.util.TreeMap) CreateBranchWizard(org.eclipse.egit.ui.internal.repository.CreateBranchWizard) Ref(org.eclipse.jgit.lib.Ref) CheckoutEntry(org.eclipse.jgit.lib.CheckoutEntry) ReflogEntry(org.eclipse.jgit.lib.ReflogEntry) SelectionEvent(org.eclipse.swt.events.SelectionEvent) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 2 with CheckoutDialog

use of org.eclipse.egit.ui.internal.dialogs.CheckoutDialog in project egit by eclipse.

the class BranchActionHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Repository repository = getRepository(true, event);
    if (repository == null) {
        return null;
    }
    CheckoutDialog dialog = new CheckoutDialog(getShell(event), repository);
    if (dialog.open() == Window.OK) {
        BranchOperationUI.checkout(repository, dialog.getRefName()).start();
    }
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CheckoutDialog(org.eclipse.egit.ui.internal.dialogs.CheckoutDialog)

Aggregations

CheckoutDialog (org.eclipse.egit.ui.internal.dialogs.CheckoutDialog)2 IOException (java.io.IOException)1 TreeMap (java.util.TreeMap)1 CreateBranchWizard (org.eclipse.egit.ui.internal.repository.CreateBranchWizard)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 CheckoutEntry (org.eclipse.jgit.lib.CheckoutEntry)1 Ref (org.eclipse.jgit.lib.Ref)1 ReflogEntry (org.eclipse.jgit.lib.ReflogEntry)1 ReflogReader (org.eclipse.jgit.lib.ReflogReader)1 Repository (org.eclipse.jgit.lib.Repository)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 MenuItem (org.eclipse.swt.widgets.MenuItem)1