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
.spearce
.jgit
.errors
.NotSupportedException
;
35 import org
.spearce
.jgit
.errors
.TransportException
;
36 import org
.spearce
.jgit
.lib
.Repository
;
37 import org
.spearce
.jgit
.lib
.RepositoryConfig
;
38 import org
.spearce
.jgit
.transport
.FetchResult
;
39 import org
.spearce
.jgit
.transport
.RefSpec
;
40 import org
.spearce
.jgit
.transport
.RemoteConfig
;
41 import org
.spearce
.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
);
70 refSpecPage
= new RefSpecPage(localDb
, false, repoPage
);
71 // TODO use/create another cool icon
72 setDefaultPageImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
73 setNeedsProgressMonitor(true);
77 public void addPages() {
83 public boolean performFinish() {
84 if (repoPage
.getSelection().isConfigSelected()
85 && refSpecPage
.isSaveRequested())
88 final Transport transport
;
89 final RepositorySelection repoSelection
= repoPage
.getSelection();
91 if (repoSelection
.isConfigSelected())
92 transport
= Transport
.open(localDb
, repoSelection
.getConfig());
94 transport
= Transport
.open(localDb
, repoSelection
.getURI());
95 } catch (final NotSupportedException e
) {
96 ErrorDialog
.openError(getShell(),
97 UIText
.FetchWizard_transportNotSupportedTitle
,
98 UIText
.FetchWizard_transportNotSupportedMessage
,
99 new Status(IStatus
.ERROR
, org
.eclipse
.egit
.ui
.Activator
100 .getPluginId(), e
.getMessage(), e
));
103 transport
.setTagOpt(refSpecPage
.getTagOpt());
105 final Job fetchJob
= new FetchJob(transport
, refSpecPage
.getRefSpecs(),
107 fetchJob
.setUser(true);
113 public String
getWindowTitle() {
114 final IWizardPage currentPage
= getContainer().getCurrentPage();
115 if (currentPage
== repoPage
|| currentPage
== null)
116 return UIText
.FetchWizard_windowTitleDefault
;
117 return NLS
.bind(UIText
.FetchWizard_windowTitleWithSource
,
121 private void saveConfig() {
122 final RemoteConfig rc
= repoPage
.getSelection().getConfig();
123 rc
.setFetchRefSpecs(refSpecPage
.getRefSpecs());
124 rc
.setTagOpt(refSpecPage
.getTagOpt());
125 final RepositoryConfig config
= localDb
.getConfig();
129 } catch (final IOException e
) {
130 ErrorDialog
.openError(getShell(), UIText
.FetchWizard_cantSaveTitle
,
131 UIText
.FetchWizard_cantSaveMessage
, new Status(
132 IStatus
.WARNING
, Activator
.getPluginId(), e
134 // Continue, it's not critical.
138 private String
getSourceString() {
139 final RepositorySelection repoSelection
= repoPage
.getSelection();
140 if (repoSelection
.isConfigSelected())
141 return repoSelection
.getConfigName();
142 return repoSelection
.getURI().toString();
145 private class FetchJob
extends Job
{
146 private final Transport transport
;
148 private final List
<RefSpec
> refSpecs
;
150 private final String sourceString
;
152 public FetchJob(final Transport transport
,
153 final List
<RefSpec
> refSpecs
, final String sourceString
) {
154 super(NLS
.bind(UIText
.FetchWizard_jobName
, sourceString
));
155 this.transport
= transport
;
156 this.refSpecs
= refSpecs
;
157 this.sourceString
= sourceString
;
161 protected IStatus
run(IProgressMonitor monitor
) {
163 monitor
= new NullProgressMonitor();
164 final FetchResult result
;
166 result
= transport
.fetch(new EclipseGitProgressTransformer(
168 } catch (final NotSupportedException e
) {
169 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
170 UIText
.FetchWizard_fetchNotSupported
, e
);
171 } catch (final TransportException e
) {
172 if (monitor
.isCanceled())
173 return Status
.CANCEL_STATUS
;
174 return new Status(IStatus
.ERROR
, Activator
.getPluginId(),
175 UIText
.FetchWizard_transportError
, e
);
178 PlatformUI
.getWorkbench().getDisplay().asyncExec(new Runnable() {
180 final Shell shell
= PlatformUI
.getWorkbench()
181 .getActiveWorkbenchWindow().getShell();
182 final Dialog dialog
= new FetchResultDialog(shell
, localDb
,
183 result
, sourceString
);
187 return Status
.OK_STATUS
;