Search in sources :

Example 1 with GitSynchronizeWizard

use of org.eclipse.egit.ui.internal.synchronize.GitSynchronizeWizard in project egit by eclipse.

the class SynchronizeWithMenu method fill.

@Override
public void fill(final Menu menu, int index) {
    if (srv == null)
        return;
    final IResource selectedResource = getSelection();
    if (selectedResource == null || selectedResource.isLinked(IResource.CHECK_ANCESTORS))
        return;
    RepositoryMapping mapping = RepositoryMapping.getMapping(selectedResource.getProject());
    if (mapping == null)
        return;
    final Repository repo = mapping.getRepository();
    if (repo == null)
        return;
    List<Ref> refs = new LinkedList<>();
    RefDatabase refDatabase = repo.getRefDatabase();
    try {
        refs.addAll(refDatabase.getAdditionalRefs());
    } catch (IOException e) {
    // do nothing
    }
    try {
        refs.addAll(refDatabase.getRefs(RefDatabase.ALL).values());
    } catch (IOException e) {
    // do nothing
    }
    Collections.sort(refs, CommonUtils.REF_ASCENDING_COMPARATOR);
    String currentBranch;
    try {
        currentBranch = repo.getFullBranch();
    } catch (IOException e) {
        // $NON-NLS-1$
        currentBranch = "";
    }
    int count = 0;
    String oldName = null;
    int refsLength = R_REFS.length();
    int tagsLength = R_TAGS.substring(refsLength).length();
    for (Ref ref : refs) {
        final String name = ref.getName();
        if (name.equals(Constants.HEAD) || name.equals(currentBranch) || excludeTag(ref, repo))
            continue;
        if (name.startsWith(R_REFS) && oldName != null && !oldName.regionMatches(refsLength, name, refsLength, tagsLength))
            new MenuItem(menu, SWT.SEPARATOR);
        MenuItem item = new MenuItem(menu, SWT.PUSH);
        item.setText(name);
        if (name.startsWith(Constants.R_TAGS))
            item.setImage(tagImage);
        else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_REMOTES))
            item.setImage(branchImage);
        item.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                GitSynchronizeData data;
                try {
                    data = new GitSynchronizeData(repo, HEAD, name, true);
                    if (!(selectedResource instanceof IProject)) {
                        HashSet<IResource> resources = new HashSet<>();
                        resources.add(selectedResource);
                        data.setIncludedResources(resources);
                    }
                    GitModelSynchronize.launch(data, new IResource[] { selectedResource });
                } catch (IOException e) {
                    Activator.logError(e.getMessage(), e);
                }
            }
        });
        if (++count == MAX_NUM_MENU_ENTRIES)
            break;
        oldName = name;
    }
    if (count > 1)
        new MenuItem(menu, SWT.SEPARATOR);
    MenuItem custom = new MenuItem(menu, SWT.PUSH);
    custom.setText(UIText.SynchronizeWithMenu_custom);
    custom.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            GitSynchronizeWizard gitWizard = new GitSynchronizeWizard();
            WizardDialog wizard = new WizardDialog(menu.getShell(), gitWizard);
            wizard.create();
            wizard.open();
        }
    });
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) GitSynchronizeWizard(org.eclipse.egit.ui.internal.synchronize.GitSynchronizeWizard) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) IOException(java.io.IOException) RefDatabase(org.eclipse.jgit.lib.RefDatabase) LinkedList(java.util.LinkedList) IProject(org.eclipse.core.resources.IProject) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) SelectionEvent(org.eclipse.swt.events.SelectionEvent) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)1 GitSynchronizeData (org.eclipse.egit.core.synchronize.dto.GitSynchronizeData)1 GitSynchronizeWizard (org.eclipse.egit.ui.internal.synchronize.GitSynchronizeWizard)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 Ref (org.eclipse.jgit.lib.Ref)1 RefDatabase (org.eclipse.jgit.lib.RefDatabase)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