Refactor/rewrite CloneSourcePage to universal RepositorySelectionPage
[egit/zawir.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / clone / CloneDestinationPage.java
blob249bfd3e59d7785a2d32b4dc4b5bb37ebeff4ec5
1 /*******************************************************************************
2 * Copyright (C) 2008, Roger C. Soares <rogersoares@intelinet.com.br>
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;
11 import java.io.File;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.jface.wizard.WizardPage;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.FileDialog;
27 import org.eclipse.swt.widgets.Group;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Text;
30 import org.spearce.egit.ui.UIText;
31 import org.spearce.egit.ui.internal.components.RepositorySelectionListener;
32 import org.spearce.egit.ui.internal.components.RepositorySelectionPage;
33 import org.spearce.jgit.lib.Constants;
34 import org.spearce.jgit.lib.Ref;
35 import org.spearce.jgit.transport.RemoteConfig;
36 import org.spearce.jgit.transport.URIish;
38 /**
39 * Wizard page that allows the user entering the location of a repository to be
40 * cloned.
42 class CloneDestinationPage extends WizardPage {
43 private final RepositorySelectionPage sourcePage;
45 private final SourceBranchPage branchPage;
47 private URIish validated;
49 private Combo initialBranch;
51 private Text directoryText;
53 private Text remoteText;
55 CloneDestinationPage(final RepositorySelectionPage sp,
56 final SourceBranchPage bp) {
57 super(CloneDestinationPage.class.getName());
58 sourcePage = sp;
59 branchPage = bp;
61 setTitle(UIText.CloneDestinationPage_title);
63 sourcePage.addRepositorySelectionListener(new RepositorySelectionListener() {
64 public void selectionChanged(final URIish newURI,
65 final RemoteConfig newConfig) {
66 if (newURI == null || !newURI.equals(validated))
67 setPageComplete(false);
69 });
70 branchPage.addBranchChangeListener(new BranchChangeListener() {
71 public void branchesChanged() {
72 setPageComplete(false);
74 });
77 public void createControl(final Composite parent) {
78 final Composite panel = new Composite(parent, SWT.NULL);
79 final GridLayout layout = new GridLayout();
80 layout.numColumns = 1;
81 panel.setLayout(layout);
83 createDestinationGroup(panel);
84 createConfigGroup(panel);
86 setControl(panel);
87 setPageComplete(isPageComplete());
90 @Override
91 public void setVisible(final boolean visible) {
92 if (visible)
93 revalidate();
94 super.setVisible(visible);
97 private void createDestinationGroup(final Composite parent) {
98 final Group g = createGroup(parent,
99 UIText.CloneDestinationPage_groupDestination);
101 newLabel(g, UIText.CloneDestinationPage_promptDirectory + ":");
102 final Composite p = new Composite(g, SWT.NONE);
103 final GridLayout grid = new GridLayout();
104 grid.numColumns = 2;
105 p.setLayout(grid);
106 p.setLayoutData(createFieldGridData());
107 directoryText = new Text(p, SWT.BORDER);
108 directoryText.setLayoutData(createFieldGridData());
109 directoryText.addModifyListener(new ModifyListener() {
110 public void modifyText(final ModifyEvent e) {
111 setPageComplete(isPageComplete());
114 final Button b = new Button(p, SWT.PUSH);
115 b.setText(UIText.CloneDestinationPage_browseButton);
116 b.addSelectionListener(new SelectionListener() {
117 public void widgetDefaultSelected(SelectionEvent e) {
118 // Do nothing.
121 public void widgetSelected(final SelectionEvent e) {
122 final FileDialog d;
124 d = new FileDialog(getShell(), SWT.APPLICATION_MODAL | SWT.SAVE);
125 if (directoryText.getText().length() > 0) {
126 final File f = new File(directoryText.getText());
127 d.setFilterPath(f.getAbsoluteFile().getAbsolutePath());
128 d.setFileName(f.getName());
130 final String r = d.open();
131 if (r != null)
132 directoryText.setText(r);
136 newLabel(g, UIText.CloneDestinationPage_promptInitialBranch + ":");
137 initialBranch = new Combo(g, SWT.DROP_DOWN | SWT.READ_ONLY);
138 initialBranch.setLayoutData(createFieldGridData());
141 private void createConfigGroup(final Composite parent) {
142 final Group g = createGroup(parent,
143 UIText.CloneDestinationPage_groupConfiguration);
145 newLabel(g, UIText.CloneDestinationPage_promptRemoteName + ":");
146 remoteText = new Text(g, SWT.BORDER);
147 remoteText.setText("origin");
148 remoteText.setLayoutData(createFieldGridData());
151 private static Group createGroup(final Composite parent, final String text) {
152 final Group g = new Group(parent, SWT.NONE);
153 final GridLayout layout = new GridLayout();
154 layout.numColumns = 2;
155 g.setLayout(layout);
156 g.setText(text);
157 final GridData gd = new GridData();
158 gd.grabExcessHorizontalSpace = true;
159 gd.horizontalAlignment = SWT.FILL;
160 g.setLayoutData(gd);
161 return g;
164 private static void newLabel(final Group g, final String text) {
165 new Label(g, SWT.NULL).setText(text);
168 private static GridData createFieldGridData() {
169 return new GridData(SWT.FILL, SWT.DEFAULT, true, false);
173 * @return location the user wants to store this repository.
175 public File getDestinationFile() {
176 return new File(directoryText.getText());
180 * @return initial branch selected (includes refs/heads prefix).
182 public String getInitialBranch() {
183 final int ix = initialBranch.getSelectionIndex();
184 if (ix < 0)
185 return Constants.HEADS_PREFIX + "/" + Constants.MASTER;
186 return Constants.HEADS_PREFIX + "/" + initialBranch.getItem(ix);
190 * @return remote name
192 public String getRemote() {
193 return remoteText.getText();
196 @Override
197 public boolean isPageComplete() {
198 final String dstpath = directoryText.getText();
199 if (dstpath.length() == 0) {
200 setErrorMessage(NLS.bind(UIText.CloneDestinationPage_fieldRequired,
201 UIText.CloneDestinationPage_promptDirectory));
202 return false;
204 if (new File(dstpath).exists()) {
205 setErrorMessage(NLS.bind(UIText.CloneDestinationPage_errorExists,
206 new File(dstpath).getName()));
207 return false;
209 if (initialBranch.getSelectionIndex() < 0) {
210 setErrorMessage(NLS.bind(UIText.CloneDestinationPage_fieldRequired,
211 UIText.CloneDestinationPage_promptInitialBranch));
212 return false;
214 if (remoteText.getText().length() == 0) {
215 setErrorMessage(NLS.bind(UIText.CloneDestinationPage_fieldRequired,
216 UIText.CloneDestinationPage_promptRemoteName));
217 return false;
220 setErrorMessage(null);
221 return true;
224 private void revalidate() {
225 URIish newURI = null;
226 newURI = sourcePage.getURI();
227 validated = newURI;
229 if (newURI == null || !newURI.equals(validated)) {
230 final String n = getSuggestedName();
231 setDescription(NLS.bind(UIText.CloneDestinationPage_description,
232 n != null ? n : "<unknown>"));
234 if (n != null) {
235 directoryText.setText(new File(ResourcesPlugin.getWorkspace()
236 .getRoot().getRawLocation().toFile(), n)
237 .getAbsolutePath());
241 initialBranch.removeAll();
242 final Ref head = branchPage.getHEAD();
243 int newix = 0;
244 for (final Ref r : branchPage.getSelectedBranches()) {
245 String name = r.getName();
246 if (name.startsWith(Constants.HEADS_PREFIX + "/"))
247 name = name.substring((Constants.HEADS_PREFIX + "/").length());
248 if (head != null && head.getName().equals(r.getName()))
249 newix = initialBranch.getItemCount();
250 initialBranch.add(name);
252 initialBranch.select(newix);
255 private String getSuggestedName() {
256 if (validated == null)
257 return null;
259 String path = validated.getPath();
260 int s = path.lastIndexOf('/');
261 if (s != -1)
262 path = path.substring(s + 1);
263 if (path.endsWith(".git"))
264 path = path.substring(0, path.length() - 4);
265 return path;