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 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org
.eclipse
.egit
.ui
.internal
.fetch
;
11 import java
.io
.IOException
;
12 import java
.net
.URISyntaxException
;
13 import java
.util
.List
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.egit
.core
.EclipseGitProgressTransformer
;
21 import org
.eclipse
.egit
.ui
.Activator
;
22 import org
.eclipse
.egit
.ui
.UIIcons
;
23 import org
.eclipse
.egit
.ui
.UIText
;
24 import org
.eclipse
.egit
.ui
.internal
.components
.RefSpecPage
;
25 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelection
;
26 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelectionPage
;
27 import org
.eclipse
.jface
.dialogs
.Dialog
;
28 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
29 import org
.eclipse
.jface
.wizard
.IWizardPage
;
30 import org
.eclipse
.jface
.wizard
.Wizard
;
31 import org
.eclipse
.osgi
.util
.NLS
;
32 import org
.eclipse
.swt
.widgets
.Shell
;
33 import org
.eclipse
.ui
.PlatformUI
;
34 import org
.eclipse
.jgit
.errors
.NotSupportedException
;
35 import org
.eclipse
.jgit
.errors
.TransportException
;
36 import org
.eclipse
.jgit
.lib
.Repository
;
37 import org
.eclipse
.jgit
.lib
.RepositoryConfig
;
38 import org
.eclipse
.jgit
.transport
.FetchResult
;
39 import org
.eclipse
.jgit
.transport
.RefSpec
;
40 import org
.eclipse
.jgit
.transport
.RemoteConfig
;
41 import org
.eclipse
.jgit
.transport
.Transport
;
44 * Wizard allowing user to specify all needed data to fetch from another
45 * repository - including selection of remote repository, ref specifications,
46 * annotated tags fetching strategy.
48 * Fetch operation is performed upon successful completion of this wizard.
50 public class FetchWizard
extends Wizard
{
51 private final Repository localDb
;
53 private final RepositorySelectionPage repoPage
;
55 private final RefSpecPage refSpecPage
;
58 * Create wizard for provided local repository.
61 * local repository to fetch to.
62 * @throws URISyntaxException
63 * when configuration of this repository contains illegal URIs.
65 public FetchWizard(final Repository localDb
) throws URISyntaxException
{
66 this.localDb
= localDb
;
67 final List
<RemoteConfig
> remotes
= RemoteConfig
68 .getAllRemoteConfigs(localDb
.getConfig());
69 repoPage
= new RepositorySelectionPage(true, remotes
, null);
70 // TODO notify refSpec page about repoPage changes
71 refSpecPage
= new RefSpecPage(localDb
, false);
72 // TODO use/create another cool icon
73 setDefaultPageImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
74 setNeedsProgressMonitor(true);
78 public void addPages() {
86 public IWizardPage
getNextPage(IWizardPage page
) {
87 if (page
== getPages()[0]){
88 refSpecPage
.setSelection(repoPage
.getSelection());
90 return super.getNextPage(page
);
94 public boolean performFinish() {
95 if (repoPage
.getSelection().isConfigSelected()
96 && refSpecPage
.isSaveRequested())
99 final Transport transport
;
100 final RepositorySelection repoSelection
= repoPage
.getSelection();
102 if (repoSelection
.isConfigSelected())
103 transport
= Transport
.open(localDb
, repoSelection
.getConfig());
105 transport
= Transport
.open(localDb
, repoSelection
.getURI());
106 } catch (final NotSupportedException e
) {
107 ErrorDialog
.openError(getShell(),
108 UIText
.FetchWizard_transportNotSupportedTitle
,
109 UIText
.FetchWizard_transportNotSupportedMessage
,
110 new Status(IStatus
.ERROR
, org
.eclipse
.egit
.ui
.Activator
111 .getPluginId(), e
.getMessage(), e
));
114 transport
.setTagOpt(refSpecPage
.getTagOpt());
116 final Job fetchJob
= new FetchJob(transport
, refSpecPage
.getRefSpecs(),
118 fetchJob
.setUser(true);
121 RepositorySelectionPage
.saveUriInPrefs(repoPage
.getSelection().getURI().toString());
127 public String
getWindowTitle() {
128 final IWizardPage currentPage
= getContainer().getCurrentPage();
129 if (currentPage
== repoPage
|| currentPage
== null)
130 return UIText
.FetchWizard_windowTitleDefault
;
131 return NLS
.bind(UIText
.FetchWizard_windowTitleWithSource
,
135 private void saveConfig() {
136 final RemoteConfig rc
= repoPage
.getSelection().getConfig();
137 rc
.setFetchRefSpecs(refSpecPage
.getRefSpecs());
138 rc
.setTagOpt(refSpecPage
.getTagOpt());
139 final RepositoryConfig config
= localDb
.getConfig();
143 } catch (final IOException e
) {
144 ErrorDialog
.openError(getShell(), UIText
.FetchWizard_cantSaveTitle
,
145 UIText
.FetchWizard_cantSaveMessage
, new Status(
146 IStatus
.WARNING
, Activator
.getPluginId(), e
148 // Continue, it's not critical.
152 private String
getSourceString() {
153 final RepositorySelection repoSelection
= repoPage
.getSelection();
154 if (repoSelection
.isConfigSelected())
155 return repoSelection
.getConfigName();
156 return repoSelection
.getURI().toString();
159 private class FetchJob
extends Job
{
160 private final Transport transport
;
162 private final List
<RefSpec
> refSpecs
;
164 private final String sourceString
;
166 public FetchJob(final Transport transport
,
167 final List
<RefSpec
> refSpecs
, final String sourceString
) {
168 super(NLS
.bind(UIText
.FetchWizard_jobName
, sourceString
));
169 this.transport
= transport
;
170 this.refSpecs
= refSpecs
;
171 this.sourceString
= sourceString
;
175 protected IStatus
run(IProgressMonitor monitor
) {
177 monitor
= new NullProgressMonitor();
178 final FetchResult result
;
180 result
= transport
.fetch(new EclipseGitProgressTransformer(
182 } catch (final NotSupportedException e
) {
183 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
184 UIText
.FetchWizard_fetchNotSupported
, e
);
185 } catch (final TransportException e
) {
186 if (monitor
.isCanceled())
187 return Status
.CANCEL_STATUS
;
188 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
189 UIText
.FetchWizard_transportError
, e
);
192 PlatformUI
.getWorkbench().getDisplay().asyncExec(new Runnable() {
194 final Shell shell
= PlatformUI
.getWorkbench()
195 .getActiveWorkbenchWindow().getShell();
196 final Dialog dialog
= new FetchResultDialog(shell
, localDb
,
197 result
, sourceString
);
201 return Status
.OK_STATUS
;