Fetch GUI
[egit/zawir.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / fetch / FetchWizard.java
blob2fa6828de406129063a75afc76530abafb324d8e
1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org.spearce.egit.ui.internal.fetch;
10 import java.io.IOException;
11 import java.net.URISyntaxException;
12 import java.util.List;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.ErrorDialog;
21 import org.eclipse.jface.wizard.IWizardPage;
22 import org.eclipse.jface.wizard.Wizard;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.PlatformUI;
26 import org.spearce.egit.core.EclipseGitProgressTransformer;
27 import org.spearce.egit.ui.Activator;
28 import org.spearce.egit.ui.UIIcons;
29 import org.spearce.egit.ui.UIText;
30 import org.spearce.egit.ui.internal.components.RefSpecPage;
31 import org.spearce.egit.ui.internal.components.RepositorySelection;
32 import org.spearce.egit.ui.internal.components.RepositorySelectionPage;
33 import org.spearce.jgit.errors.NotSupportedException;
34 import org.spearce.jgit.errors.TransportException;
35 import org.spearce.jgit.lib.Repository;
36 import org.spearce.jgit.lib.RepositoryConfig;
37 import org.spearce.jgit.transport.FetchResult;
38 import org.spearce.jgit.transport.RefSpec;
39 import org.spearce.jgit.transport.RemoteConfig;
40 import org.spearce.jgit.transport.Transport;
42 /**
43 * Wizard allowing user to specify all needed data to fetch from another
44 * repository - including selection of remote repository, ref specifications,
45 * annotated tags fetching strategy.
46 * <p>
47 * Fetch operation is performed upon successful completion of this wizard.
49 public class FetchWizard extends Wizard {
50 private final Repository localDb;
52 private final RepositorySelectionPage repoPage;
54 private final RefSpecPage refSpecPage;
56 /**
57 * Create wizard for provided local repository.
59 * @param localDb
60 * local repository to fetch to.
61 * @throws URISyntaxException
62 * when configuration of this repository contains illegal URIs.
64 public FetchWizard(final Repository localDb) throws URISyntaxException {
65 this.localDb = localDb;
66 final List<RemoteConfig> remotes = RemoteConfig
67 .getAllRemoteConfigs(localDb.getConfig());
68 repoPage = new RepositorySelectionPage(true, remotes);
69 refSpecPage = new RefSpecPage(localDb, false, repoPage);
70 // TODO use/create another cool icon
71 setDefaultPageImageDescriptor(UIIcons.WIZBAN_IMPORT_REPO);
72 setNeedsProgressMonitor(true);
75 @Override
76 public void addPages() {
77 addPage(repoPage);
78 addPage(refSpecPage);
81 @Override
82 public boolean performFinish() {
83 if (repoPage.getSelection().isConfigSelected()
84 && refSpecPage.isSaveRequested())
85 saveConfig();
87 final Transport transport;
88 final RepositorySelection repoSelection = repoPage.getSelection();
89 try {
90 if (repoSelection.isConfigSelected())
91 transport = Transport.open(localDb, repoSelection.getConfig());
92 else
93 transport = Transport.open(localDb, repoSelection.getURI());
94 } catch (final NotSupportedException e) {
95 ErrorDialog.openError(getShell(),
96 UIText.FetchWizard_transportNotSupportedTitle,
97 UIText.FetchWizard_transportNotSupportedMessage,
98 new Status(IStatus.ERROR, org.spearce.egit.ui.Activator
99 .getPluginId(), e.getMessage(), e));
100 return false;
102 transport.setTagOpt(refSpecPage.getTagOpt());
104 final Job fetchJob = new FetchJob(transport, refSpecPage.getRefSpecs(),
105 getSourceString());
106 fetchJob.setUser(true);
107 fetchJob.schedule();
108 return true;
111 @Override
112 public String getWindowTitle() {
113 final IWizardPage currentPage = getContainer().getCurrentPage();
114 if (currentPage == repoPage || currentPage == null)
115 return UIText.FetchWizard_windowTitleDefault;
116 return NLS.bind(UIText.FetchWizard_windowTitleWithSource,
117 getSourceString());
120 private void saveConfig() {
121 final RemoteConfig rc = repoPage.getSelection().getConfig();
122 rc.setFetchRefSpecs(refSpecPage.getRefSpecs());
123 rc.setTagOpt(refSpecPage.getTagOpt());
124 final RepositoryConfig config = localDb.getConfig();
125 rc.update(config);
126 try {
127 config.save();
128 } catch (final IOException e) {
129 ErrorDialog.openError(getShell(), UIText.FetchWizard_cantSaveTitle,
130 UIText.FetchWizard_cantSaveMessage, new Status(
131 IStatus.WARNING, Activator.getPluginId(), e
132 .getMessage(), e));
133 // Continue, it's not critical.
137 private String getSourceString() {
138 final RepositorySelection repoSelection = repoPage.getSelection();
139 if (repoSelection.isConfigSelected())
140 return repoSelection.getConfigName();
141 return repoSelection.getURI().toString();
144 private class FetchJob extends Job {
145 private final Transport transport;
147 private final List<RefSpec> refSpecs;
149 private final String sourceString;
151 public FetchJob(final Transport transport,
152 final List<RefSpec> refSpecs, final String sourceString) {
153 super(NLS.bind(UIText.FetchWizard_jobName, sourceString));
154 this.transport = transport;
155 this.refSpecs = refSpecs;
156 this.sourceString = sourceString;
159 @Override
160 protected IStatus run(IProgressMonitor monitor) {
161 if (monitor == null)
162 monitor = new NullProgressMonitor();
163 final FetchResult result;
164 try {
165 result = transport.fetch(new EclipseGitProgressTransformer(
166 monitor), refSpecs);
167 } catch (final NotSupportedException e) {
168 return new Status(IStatus.ERROR, Activator.getPluginId(),
169 UIText.FetchWizard_fetchNotSupported, e);
170 } catch (final TransportException e) {
171 if (monitor.isCanceled())
172 return Status.CANCEL_STATUS;
173 return new Status(IStatus.ERROR, Activator.getPluginId(),
174 UIText.FetchWizard_transportError, e);
177 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
178 public void run() {
179 final Shell shell = PlatformUI.getWorkbench()
180 .getActiveWorkbenchWindow().getShell();
181 final Dialog dialog = new FetchResultDialog(shell, localDb,
182 result, sourceString);
183 dialog.open();
186 return Status.OK_STATUS;