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
;
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.
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
;
57 * Create wizard for provided local repository.
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);
76 public void addPages() {
82 public boolean performFinish() {
83 if (repoPage
.getSelection().isConfigSelected()
84 && refSpecPage
.isSaveRequested())
87 final Transport transport
;
88 final RepositorySelection repoSelection
= repoPage
.getSelection();
90 if (repoSelection
.isConfigSelected())
91 transport
= Transport
.open(localDb
, repoSelection
.getConfig());
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
));
102 transport
.setTagOpt(refSpecPage
.getTagOpt());
104 final Job fetchJob
= new FetchJob(transport
, refSpecPage
.getRefSpecs(),
106 fetchJob
.setUser(true);
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
,
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();
128 } catch (final IOException e
) {
129 ErrorDialog
.openError(getShell(), UIText
.FetchWizard_cantSaveTitle
,
130 UIText
.FetchWizard_cantSaveMessage
, new Status(
131 IStatus
.WARNING
, Activator
.getPluginId(), 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
;
160 protected IStatus
run(IProgressMonitor monitor
) {
162 monitor
= new NullProgressMonitor();
163 final FetchResult result
;
165 result
= transport
.fetch(new EclipseGitProgressTransformer(
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() {
179 final Shell shell
= PlatformUI
.getWorkbench()
180 .getActiveWorkbenchWindow().getShell();
181 final Dialog dialog
= new FetchResultDialog(shell
, localDb
,
182 result
, sourceString
);
186 return Status
.OK_STATUS
;