1 /*******************************************************************************
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * See LICENSE for the full license text, also available.
8 *******************************************************************************/
9 package org
.spearce
.egit
.ui
.internal
.clone
;
12 import java
.lang
.reflect
.InvocationTargetException
;
13 import java
.net
.URISyntaxException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collection
;
16 import java
.util
.Collections
;
17 import java
.util
.Comparator
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
26 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
27 import org
.eclipse
.jface
.wizard
.WizardPage
;
28 import org
.eclipse
.osgi
.util
.NLS
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.events
.SelectionEvent
;
31 import org
.eclipse
.swt
.events
.SelectionListener
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.layout
.RowLayout
;
35 import org
.eclipse
.swt
.widgets
.Button
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Event
;
38 import org
.eclipse
.swt
.widgets
.Label
;
39 import org
.eclipse
.swt
.widgets
.Listener
;
40 import org
.eclipse
.swt
.widgets
.Table
;
41 import org
.eclipse
.swt
.widgets
.TableItem
;
42 import org
.spearce
.egit
.ui
.Activator
;
43 import org
.spearce
.egit
.ui
.UIIcons
;
44 import org
.spearce
.egit
.ui
.UIText
;
45 import org
.spearce
.jgit
.lib
.Constants
;
46 import org
.spearce
.jgit
.lib
.Ref
;
47 import org
.spearce
.jgit
.lib
.Repository
;
48 import org
.spearce
.jgit
.transport
.FetchConnection
;
49 import org
.spearce
.jgit
.transport
.Transport
;
50 import org
.spearce
.jgit
.transport
.URIish
;
52 class SourceBranchPage
extends WizardPage
{
53 private final List
<BranchChangeListener
> branchChangeListeners
;
55 private final CloneSourcePage sourcePage
;
57 private URIish validated
;
61 private List
<Ref
> available
= Collections
.<Ref
> emptyList();
65 private Table availTable
;
67 private boolean allSelected
;
69 private String transportError
;
71 SourceBranchPage(final CloneSourcePage sp
) {
72 super(SourceBranchPage
.class.getName());
74 setTitle(UIText
.CloneSourcePage_title
);
75 setImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
76 sourcePage
.addURIishChangeListener(new URIishChangeListener() {
77 public void uriishChanged(final URIish newURI
) {
78 if (newURI
== null || !newURI
.equals(validated
))
79 setPageComplete(false);
82 branchChangeListeners
= new ArrayList
<BranchChangeListener
>(3);
85 void addBranchChangeListener(final BranchChangeListener l
) {
86 branchChangeListeners
.add(l
);
89 Collection
<Ref
> getSelectedBranches() {
91 final ArrayList
<Ref
> r
= new ArrayList
<Ref
>(available
.size());
92 for (int i
= 0; i
< available
.size(); i
++) {
93 if (availTable
.getItem(i
).getChecked())
94 r
.add(available
.get(i
));
105 boolean isAllSelected() {
109 public void createControl(final Composite parent
) {
110 final Composite panel
= new Composite(parent
, SWT
.NULL
);
111 final GridLayout layout
= new GridLayout();
112 layout
.numColumns
= 1;
113 panel
.setLayout(layout
);
115 label
= new Label(panel
, SWT
.NONE
);
116 label
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
118 availTable
= new Table(panel
, SWT
.CHECK
| SWT
.V_SCROLL
| SWT
.BORDER
);
119 availTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
120 availTable
.addListener(SWT
.Selection
, new Listener() {
121 public void handleEvent(final Event event
) {
122 if (event
.detail
== SWT
.CHECK
) {
124 setPageComplete(isPageComplete());
129 final Composite bPanel
= new Composite(panel
, SWT
.NONE
);
130 bPanel
.setLayout(new RowLayout());
132 b
= new Button(bPanel
, SWT
.PUSH
);
133 b
.setText(UIText
.SourceBranchPage_selectAll
);
134 b
.addSelectionListener(new SelectionListener() {
135 public void widgetDefaultSelected(SelectionEvent e
) {
139 public void widgetSelected(SelectionEvent e
) {
140 for (int i
= 0; i
< availTable
.getItemCount(); i
++)
141 availTable
.getItem(i
).setChecked(true);
143 setPageComplete(isPageComplete());
146 b
= new Button(bPanel
, SWT
.PUSH
);
147 b
.setText(UIText
.SourceBranchPage_selectNone
);
148 b
.addSelectionListener(new SelectionListener() {
149 public void widgetDefaultSelected(SelectionEvent e
) {
153 public void widgetSelected(SelectionEvent e
) {
154 for (int i
= 0; i
< availTable
.getItemCount(); i
++)
155 availTable
.getItem(i
).setChecked(false);
157 setPageComplete(isPageComplete());
160 bPanel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
163 setPageComplete(false);
167 public void setVisible(final boolean visible
) {
170 super.setVisible(visible
);
174 public boolean isPageComplete() {
175 if (transportError
!= null) {
176 setErrorMessage(transportError
);
180 if (getSelectedBranches().isEmpty()) {
181 setErrorMessage(UIText
.SourceBranchPage_errorBranchRequired
);
185 setErrorMessage(null);
189 private void revalidate() {
192 newURI
= sourcePage
.getURI();
193 } catch (URISyntaxException e
) {
194 transportError(e
.getReason());
198 label
.setText(NLS
.bind(UIText
.SourceBranchPage_branchList
, newURI
200 label
.getParent().layout();
202 if (newURI
.equals(validated
)) {
203 setPageComplete(isPageComplete());
207 setErrorMessage(null);
208 setPageComplete(false);
209 transportError
= null;
211 available
= new ArrayList
<Ref
>();
212 availTable
.removeAll();
214 label
.getDisplay().asyncExec(new Runnable() {
216 revalidateImpl(newURI
);
221 private void revalidateImpl(final URIish newURI
) {
222 if (label
.isDisposed() || !isCurrentPage())
225 getContainer().run(true, true, new IRunnableWithProgress() {
226 public void run(final IProgressMonitor pm
)
227 throws InvocationTargetException
, InterruptedException
{
228 final IProgressMonitor monitor
;
230 monitor
= new NullProgressMonitor();
234 final Repository db
= new Repository(new File("/tmp"));
235 final Transport tn
= Transport
.open(db
, newURI
);
236 final Collection
<Ref
> adv
;
237 final FetchConnection fn
= tn
.openFetch();
244 final Ref idHEAD
= fn
.getRef(Constants
.HEAD
);
246 for (final Ref r
: adv
) {
247 final String n
= r
.getName();
248 if (!n
.startsWith(Constants
.HEADS_PREFIX
+ "/"))
251 if (idHEAD
== null || head
!= null)
253 if (r
.getObjectId().equals(idHEAD
.getObjectId()))
256 Collections
.sort(available
, new Comparator
<Ref
>() {
257 public int compare(final Ref o1
, final Ref o2
) {
258 return o1
.getName().compareTo(o2
.getName());
261 if (idHEAD
!= null && head
== null) {
263 available
.add(0, idHEAD
);
265 } catch (Exception err
) {
266 throw new InvocationTargetException(err
);
271 } catch (InvocationTargetException e
) {
272 Throwable why
= e
.getCause();
273 if ((why
instanceof OperationCanceledException
)) {
274 transportError(UIText
.SourceBranchPage_remoteListingCancelled
);
277 ErrorDialog
.openError(getShell(),
278 UIText
.SourceBranchPage_transportError
,
279 UIText
.SourceBranchPage_cannotListBranches
, new Status(
280 IStatus
.ERROR
, Activator
.getPluginId(), 0, why
281 .getMessage(), why
.getCause()));
282 transportError(why
.getMessage());
285 } catch (InterruptedException e
) {
286 transportError(UIText
.SourceBranchPage_interrupted
);
292 for (final Ref r
: available
) {
293 String n
= r
.getName();
294 if (n
.startsWith(Constants
.HEADS_PREFIX
+ "/"))
295 n
= n
.substring((Constants
.HEADS_PREFIX
+ "/").length());
296 final TableItem ti
= new TableItem(availTable
, SWT
.NONE
);
301 setErrorMessage(null);
302 setPageComplete(isPageComplete());
305 private void transportError(final String msg
) {
306 transportError
= msg
;
307 setErrorMessage(msg
);
308 setPageComplete(false);
311 private void notifyChanged() {
312 for (final BranchChangeListener l
: branchChangeListeners
)