2 * Copyright (C) 2008 Roger C. Soares
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.egit
.ui
.internal
.clone
;
20 import java
.lang
.reflect
.InvocationTargetException
;
21 import java
.net
.URISyntaxException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.Collections
;
25 import java
.util
.Comparator
;
26 import java
.util
.List
;
28 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
30 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
31 import org
.eclipse
.jface
.wizard
.WizardPage
;
32 import org
.eclipse
.osgi
.util
.NLS
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.events
.SelectionEvent
;
35 import org
.eclipse
.swt
.events
.SelectionListener
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.layout
.RowLayout
;
39 import org
.eclipse
.swt
.widgets
.Button
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Event
;
42 import org
.eclipse
.swt
.widgets
.Label
;
43 import org
.eclipse
.swt
.widgets
.Listener
;
44 import org
.eclipse
.swt
.widgets
.Table
;
45 import org
.eclipse
.swt
.widgets
.TableItem
;
46 import org
.spearce
.egit
.ui
.UIIcons
;
47 import org
.spearce
.egit
.ui
.UIText
;
48 import org
.spearce
.jgit
.lib
.Constants
;
49 import org
.spearce
.jgit
.lib
.Ref
;
50 import org
.spearce
.jgit
.lib
.Repository
;
51 import org
.spearce
.jgit
.transport
.FetchConnection
;
52 import org
.spearce
.jgit
.transport
.Transport
;
53 import org
.spearce
.jgit
.transport
.URIish
;
55 class SourceBranchPage
extends WizardPage
{
56 private final List
<BranchChangeListener
> branchChangeListeners
;
58 private final CloneSourcePage sourcePage
;
60 private URIish validated
;
64 private List
<Ref
> available
= Collections
.<Ref
> emptyList();
68 private Table availTable
;
70 private boolean allSelected
;
72 SourceBranchPage(final CloneSourcePage sp
) {
73 super(SourceBranchPage
.class.getName());
75 setTitle(UIText
.CloneSourcePage_title
);
76 setImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
77 sourcePage
.addURIishChangeListener(new URIishChangeListener() {
78 public void uriishChanged(final URIish newURI
) {
79 if (newURI
== null || !newURI
.equals(validated
))
80 setPageComplete(false);
83 branchChangeListeners
= new ArrayList
<BranchChangeListener
>(3);
86 void addBranchChangeListener(final BranchChangeListener l
) {
87 branchChangeListeners
.add(l
);
90 Collection
<Ref
> getSelectedBranches() {
92 final ArrayList
<Ref
> r
= new ArrayList
<Ref
>(available
.size());
93 for (int i
= 0; i
< available
.size(); i
++) {
94 if (availTable
.getItem(i
).getChecked())
95 r
.add(available
.get(i
));
106 boolean isAllSelected() {
110 public void createControl(final Composite parent
) {
111 final Composite panel
= new Composite(parent
, SWT
.NULL
);
112 final GridLayout layout
= new GridLayout();
113 layout
.numColumns
= 1;
114 panel
.setLayout(layout
);
116 label
= new Label(panel
, SWT
.NONE
);
117 label
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
119 availTable
= new Table(panel
, SWT
.CHECK
| SWT
.V_SCROLL
| SWT
.BORDER
);
120 availTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
121 availTable
.addListener(SWT
.Selection
, new Listener() {
122 public void handleEvent(final Event event
) {
123 if (event
.detail
== SWT
.CHECK
) {
125 setPageComplete(isPageComplete());
130 final Composite bPanel
= new Composite(panel
, SWT
.NONE
);
131 bPanel
.setLayout(new RowLayout());
133 b
= new Button(bPanel
, SWT
.PUSH
);
134 b
.setText(UIText
.SourceBranchPage_selectAll
);
135 b
.addSelectionListener(new SelectionListener() {
136 public void widgetDefaultSelected(SelectionEvent e
) {
140 public void widgetSelected(SelectionEvent e
) {
141 for (int i
= 0; i
< availTable
.getItemCount(); i
++)
142 availTable
.getItem(i
).setChecked(true);
144 setPageComplete(isPageComplete());
147 b
= new Button(bPanel
, SWT
.PUSH
);
148 b
.setText(UIText
.SourceBranchPage_selectNone
);
149 b
.addSelectionListener(new SelectionListener() {
150 public void widgetDefaultSelected(SelectionEvent e
) {
154 public void widgetSelected(SelectionEvent e
) {
155 for (int i
= 0; i
< availTable
.getItemCount(); i
++)
156 availTable
.getItem(i
).setChecked(false);
158 setPageComplete(isPageComplete());
161 bPanel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
164 setPageComplete(false);
168 public void setVisible(final boolean visible
) {
171 super.setVisible(visible
);
175 public boolean isPageComplete() {
176 if (getSelectedBranches().isEmpty()) {
177 setErrorMessage(UIText
.SourceBranchPage_errorBranchRequired
);
181 setErrorMessage(null);
185 private void revalidate() {
188 newURI
= sourcePage
.getURI();
189 } catch (URISyntaxException e
) {
190 setErrorMessage(e
.getReason());
191 setPageComplete(false);
195 label
.setText(NLS
.bind(UIText
.SourceBranchPage_branchList
, newURI
197 label
.getParent().layout();
199 if (newURI
.equals(validated
)) {
200 setErrorMessage(null);
201 setPageComplete(true);
205 setErrorMessage(null);
206 setPageComplete(false);
208 available
= new ArrayList
<Ref
>();
209 availTable
.removeAll();
211 label
.getDisplay().asyncExec(new Runnable() {
213 revalidateImpl(newURI
);
218 private void revalidateImpl(final URIish newURI
) {
219 if (label
.isDisposed() || !isCurrentPage())
222 getContainer().run(true, true, new IRunnableWithProgress() {
223 public void run(final IProgressMonitor pm
)
224 throws InvocationTargetException
, InterruptedException
{
225 final IProgressMonitor monitor
;
227 monitor
= new NullProgressMonitor();
231 final Repository db
= new Repository(new File("/tmp"));
232 final Transport tn
= Transport
.open(db
, newURI
);
233 final Collection
<Ref
> adv
;
234 final FetchConnection fn
= tn
.openFetch();
241 final Ref idHEAD
= fn
.getRef(Constants
.HEAD
);
243 for (final Ref r
: adv
) {
244 final String n
= r
.getName();
245 if (!n
.startsWith(Constants
.HEADS_PREFIX
+ "/"))
248 if (idHEAD
== null || head
!= null)
250 if (r
.getObjectId().equals(idHEAD
.getObjectId()))
253 Collections
.sort(available
, new Comparator
<Ref
>() {
254 public int compare(final Ref o1
, final Ref o2
) {
255 return o1
.getName().compareTo(o2
.getName());
258 if (idHEAD
!= null && head
== null) {
260 available
.add(0, idHEAD
);
262 } catch (Exception err
) {
263 throw new InvocationTargetException(err
);
268 } catch (InvocationTargetException e
) {
269 setErrorMessage(e
.getCause().getMessage());
270 setPageComplete(false);
272 } catch (InterruptedException e
) {
273 setErrorMessage("Connection attempt interrupted.");
274 setPageComplete(false);
280 for (final Ref r
: available
) {
281 String n
= r
.getName();
282 if (n
.startsWith(Constants
.HEADS_PREFIX
+ "/"))
283 n
= n
.substring((Constants
.HEADS_PREFIX
+ "/").length());
284 final TableItem ti
= new TableItem(availTable
, SWT
.NONE
);
289 setErrorMessage(null);
290 setPageComplete(isPageComplete());
293 private void notifyChanged() {
294 for (final BranchChangeListener l
: branchChangeListeners
)