1 /*******************************************************************************
2 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * See LICENSE for the full license text, also available.
9 *******************************************************************************/
10 package org
.eclipse
.egit
.ui
.internal
.clone
;
13 import java
.io
.IOException
;
14 import java
.lang
.reflect
.InvocationTargetException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.Comparator
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.runtime
.IStatus
;
21 import org
.eclipse
.core
.runtime
.Status
;
22 import org
.eclipse
.egit
.core
.op
.ListRemoteOperation
;
23 import org
.eclipse
.egit
.ui
.Activator
;
24 import org
.eclipse
.egit
.ui
.UIText
;
25 import org
.eclipse
.egit
.ui
.internal
.components
.BaseWizardPage
;
26 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelection
;
27 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelectionPage
;
28 import org
.eclipse
.egit
.ui
.internal
.components
.SelectionChangeListener
;
29 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
30 import org
.eclipse
.osgi
.util
.NLS
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.events
.SelectionAdapter
;
33 import org
.eclipse
.swt
.events
.SelectionEvent
;
34 import org
.eclipse
.swt
.layout
.GridData
;
35 import org
.eclipse
.swt
.layout
.GridLayout
;
36 import org
.eclipse
.swt
.layout
.RowLayout
;
37 import org
.eclipse
.swt
.widgets
.Button
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Label
;
40 import org
.eclipse
.swt
.widgets
.Table
;
41 import org
.eclipse
.swt
.widgets
.TableItem
;
42 import org
.spearce
.jgit
.lib
.Constants
;
43 import org
.spearce
.jgit
.lib
.Ref
;
44 import org
.spearce
.jgit
.lib
.Repository
;
45 import org
.spearce
.jgit
.transport
.URIish
;
47 class SourceBranchPage
extends BaseWizardPage
{
48 private final RepositorySelectionPage sourcePage
;
50 private RepositorySelection validatedRepoSelection
;
54 private List
<Ref
> availableRefs
= new ArrayList
<Ref
>();
56 private List
<Ref
> selectedRefs
= new ArrayList
<Ref
>();
60 private Table refsTable
;
62 private String transportError
;
64 SourceBranchPage(final RepositorySelectionPage sp
) {
65 super(SourceBranchPage
.class.getName());
67 setTitle(UIText
.SourceBranchPage_title
);
68 setDescription(UIText
.SourceBranchPage_description
);
70 sourcePage
.addSelectionListener(new SelectionChangeListener() {
71 public void selectionChanged() {
72 if (!sourcePage
.selectionEquals(validatedRepoSelection
))
73 setPageComplete(false);
80 List
<Ref
> getSelectedBranches() {
81 return new ArrayList
<Ref
>(selectedRefs
);
88 boolean isAllSelected() {
89 return availableRefs
.size() == selectedRefs
.size();
92 boolean selectionEquals(final List
<Ref
> selectedRefs
, final Ref head
) {
93 return this.selectedRefs
.equals(selectedRefs
) && this.head
== head
;
96 public void createControl(final Composite parent
) {
97 final Composite panel
= new Composite(parent
, SWT
.NULL
);
98 final GridLayout layout
= new GridLayout();
99 layout
.numColumns
= 1;
100 panel
.setLayout(layout
);
102 label
= new Label(panel
, SWT
.NONE
);
103 label
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
105 refsTable
= new Table(panel
, SWT
.CHECK
| SWT
.V_SCROLL
| SWT
.BORDER
);
106 refsTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
107 refsTable
.addSelectionListener(new SelectionAdapter() {
109 public void widgetSelected(final SelectionEvent e
) {
110 if (e
.detail
!= SWT
.CHECK
)
113 final TableItem tableItem
= (TableItem
) e
.item
;
114 final int i
= refsTable
.indexOf(tableItem
);
115 final Ref ref
= availableRefs
.get(i
);
117 if (tableItem
.getChecked()) {
118 int insertionPos
= 0;
119 for (int j
= 0; j
< i
; j
++) {
120 if (selectedRefs
.contains(availableRefs
.get(j
)))
123 selectedRefs
.add(insertionPos
, ref
);
125 selectedRefs
.remove(ref
);
127 notifySelectionChanged();
132 final Composite bPanel
= new Composite(panel
, SWT
.NONE
);
133 bPanel
.setLayout(new RowLayout());
134 final Button selectB
;
135 selectB
= new Button(bPanel
, SWT
.PUSH
);
136 selectB
.setText(UIText
.SourceBranchPage_selectAll
);
137 selectB
.addSelectionListener(new SelectionAdapter() {
138 public void widgetSelected(final SelectionEvent e
) {
139 for (int i
= 0; i
< refsTable
.getItemCount(); i
++)
140 refsTable
.getItem(i
).setChecked(true);
141 selectedRefs
.clear();
142 selectedRefs
.addAll(availableRefs
);
143 notifySelectionChanged();
147 final Button unselectB
= new Button(bPanel
, SWT
.PUSH
);
148 unselectB
.setText(UIText
.SourceBranchPage_selectNone
);
149 unselectB
.addSelectionListener(new SelectionAdapter() {
150 public void widgetSelected(final SelectionEvent e
) {
151 for (int i
= 0; i
< refsTable
.getItemCount(); i
++)
152 refsTable
.getItem(i
).setChecked(false);
153 selectedRefs
.clear();
154 notifySelectionChanged();
158 bPanel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, true, false));
160 addSelectionListener(new SelectionChangeListener() {
161 public void selectionChanged() {
162 selectB
.setEnabled(selectedRefs
.size() != availableRefs
.size());
163 unselectB
.setEnabled(selectedRefs
.size() != 0);
172 public void setVisible(final boolean visible
) {
175 super.setVisible(visible
);
179 * Check internal state for page completion status. This method should be
180 * called only when all necessary data from previous form is available.
182 private void checkPage() {
183 if (transportError
!= null) {
184 setErrorMessage(transportError
);
185 setPageComplete(false);
189 if (getSelectedBranches().isEmpty()) {
190 setErrorMessage(UIText
.SourceBranchPage_errorBranchRequired
);
191 setPageComplete(false);
195 setErrorMessage(null);
196 setPageComplete(true);
199 private void revalidate() {
200 if (sourcePage
.selectionEquals(validatedRepoSelection
)) {
201 // URI hasn't changed, no need to refill the page with new data
206 final RepositorySelection newRepoSelection
= sourcePage
.getSelection();
207 label
.setText(NLS
.bind(UIText
.SourceBranchPage_branchList
,
208 newRepoSelection
.getURI().toString()));
209 label
.getParent().layout();
211 validatedRepoSelection
= null;
212 transportError
= null;
214 availableRefs
.clear();
215 selectedRefs
.clear();
216 refsTable
.removeAll();
217 setPageComplete(false);
218 setErrorMessage(null);
219 label
.getDisplay().asyncExec(new Runnable() {
221 revalidateImpl(newRepoSelection
);
226 private void revalidateImpl(final RepositorySelection newRepoSelection
) {
227 if (label
.isDisposed() || !isCurrentPage())
230 final ListRemoteOperation listRemoteOp
;
232 final URIish uri
= newRepoSelection
.getURI();
233 final Repository db
= new Repository(new File("/tmp"));
234 listRemoteOp
= new ListRemoteOperation(db
, uri
);
235 getContainer().run(true, true, listRemoteOp
);
236 } catch (InvocationTargetException e
) {
237 Throwable why
= e
.getCause();
238 transportError(why
.getMessage());
239 ErrorDialog
.openError(getShell(),
240 UIText
.SourceBranchPage_transportError
,
241 UIText
.SourceBranchPage_cannotListBranches
, new Status(
242 IStatus
.ERROR
, Activator
.getPluginId(), 0, why
243 .getMessage(), why
.getCause()));
245 } catch (IOException e
) {
246 transportError(UIText
.SourceBranchPage_cannotCreateTemp
);
248 } catch (InterruptedException e
) {
249 transportError(UIText
.SourceBranchPage_remoteListingCancelled
);
253 final Ref idHEAD
= listRemoteOp
.getRemoteRef(Constants
.HEAD
);
255 for (final Ref r
: listRemoteOp
.getRemoteRefs()) {
256 final String n
= r
.getName();
257 if (!n
.startsWith(Constants
.R_HEADS
))
259 availableRefs
.add(r
);
260 if (idHEAD
== null || head
!= null)
262 if (r
.getObjectId().equals(idHEAD
.getObjectId()))
265 Collections
.sort(availableRefs
, new Comparator
<Ref
>() {
266 public int compare(final Ref o1
, final Ref o2
) {
267 return o1
.getName().compareTo(o2
.getName());
270 if (idHEAD
!= null && head
== null) {
272 availableRefs
.add(0, idHEAD
);
275 validatedRepoSelection
= newRepoSelection
;
276 for (final Ref r
: availableRefs
) {
277 String n
= r
.getName();
278 if (n
.startsWith(Constants
.R_HEADS
))
279 n
= n
.substring(Constants
.R_HEADS
.length());
280 final TableItem ti
= new TableItem(refsTable
, SWT
.NONE
);
285 notifySelectionChanged();
289 private void transportError(final String msg
) {
290 transportError
= msg
;