Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / importing / GitScmUrlImportWizardPage.java
blobd8657a72cdeca1827d910dcbccf1bc870a0ae3bf
1 /*******************************************************************************
2 * Copyright (c) 2012 Tomasz Zarna and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 * Tomasz Zarna <Tomasz.Zarna@pl.ibm.com> - initial implementation
12 *******************************************************************************/
13 package org.eclipse.egit.ui.internal.importing;
15 import java.net.URI;
17 import org.eclipse.egit.core.internal.GitURI;
18 import org.eclipse.egit.ui.Activator;
19 import org.eclipse.egit.ui.internal.SWTUtils;
20 import org.eclipse.egit.ui.internal.UIText;
21 import org.eclipse.jface.dialogs.IDialogSettings;
22 import org.eclipse.jface.viewers.ArrayContentProvider;
23 import org.eclipse.jface.viewers.ILabelProvider;
24 import org.eclipse.jface.viewers.StyledCellLabelProvider;
25 import org.eclipse.jface.viewers.StyledString;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.viewers.ViewerCell;
28 import org.eclipse.jface.viewers.ViewerComparator;
29 import org.eclipse.jface.wizard.WizardPage;
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.events.SelectionListener;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
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.team.core.ScmUrlImportDescription;
42 import org.eclipse.team.ui.IScmUrlImportWizardPage;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.ide.IDE;
46 /**
47 * Wizard page that allows the user to import repositories with SCM URLs.
49 public class GitScmUrlImportWizardPage extends WizardPage implements
50 IScmUrlImportWizardPage {
52 private ScmUrlImportDescription[] descriptions;
53 private Label counterLabel;
54 private TableViewer bundlesViewer;
55 private Button useMaster;
57 private static final String GIT_PAGE_USE_MASTER = "org.eclipse.team.egit.ui.import.page.master"; //$NON-NLS-1$
59 static class GitLabelProvider extends StyledCellLabelProvider implements ILabelProvider {
61 /* (non-Javadoc)
62 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
64 @Override
65 public Image getImage(Object element) {
66 return PlatformUI.getWorkbench().getSharedImages().getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
69 /* (non-Javadoc)
70 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
72 @Override
73 public String getText(Object element) {
74 return getStyledText(element).getString();
77 /* (non-Javadoc)
78 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
80 @Override
81 public void update(ViewerCell cell) {
82 StyledString string = getStyledText(cell.getElement());
83 cell.setText(string.getString());
84 cell.setStyleRanges(string.getStyleRanges());
85 cell.setImage(getImage(cell.getElement()));
86 super.update(cell);
89 private StyledString getStyledText(Object element) {
90 StyledString styledString = new StyledString();
91 if (element instanceof ScmUrlImportDescription) {
92 ScmUrlImportDescription description = (ScmUrlImportDescription) element;
93 String project = description.getProject();
94 URI scmUrl = description.getUri();
95 try {
96 String version = getTag(scmUrl);
97 String host = getServer(scmUrl);
98 styledString.append(project);
99 if (version != null) {
100 styledString.append(' ');
101 styledString.append(version,
102 StyledString.DECORATIONS_STYLER);
104 styledString.append(' ');
105 styledString.append('[', StyledString.DECORATIONS_STYLER);
106 styledString.append(host, StyledString.DECORATIONS_STYLER);
107 styledString.append(']', StyledString.DECORATIONS_STYLER);
108 } catch (IllegalArgumentException e) {
109 styledString.append(e.getMessage());
110 Activator.logError(e.getMessage(), e);
112 return styledString;
114 styledString.append(element.toString());
115 return styledString;
120 * Constructs the page.
122 public GitScmUrlImportWizardPage() {
123 super("git", UIText.GitScmUrlImportWizardPage_title, null); //$NON-NLS-1$
124 setDescription(UIText.GitScmUrlImportWizardPage_description);
127 /* (non-Javadoc)
128 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
130 @Override
131 public void createControl(Composite parent) {
132 Composite comp = SWTUtils.createHVFillComposite(parent, SWTUtils.MARGINS_NONE, 1);
133 Composite group = SWTUtils.createHFillComposite(comp, SWTUtils.MARGINS_NONE, 1);
135 Button versions = SWTUtils.createRadioButton(group,
136 UIText.GitScmUrlImportWizardPage_importVersion);
137 useMaster = SWTUtils.createRadioButton(group,
138 UIText.GitScmUrlImportWizardPage_importMaster);
139 SelectionListener listener = new SelectionAdapter() {
140 @Override
141 public void widgetSelected(SelectionEvent e) {
142 bundlesViewer.refresh(true);
145 versions.addSelectionListener(listener);
146 useMaster.addSelectionListener(listener);
148 Table table = new Table(comp, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
149 GridData gd = new GridData(GridData.FILL_BOTH);
150 gd.heightHint = 200;
151 gd.widthHint = 225;
152 table.setLayoutData(gd);
154 bundlesViewer = new TableViewer(table);
155 bundlesViewer.setLabelProvider(new GitLabelProvider());
156 bundlesViewer.setContentProvider(ArrayContentProvider.getInstance());
157 bundlesViewer.setComparator(new ViewerComparator());
158 counterLabel = new Label(comp, SWT.NONE);
159 counterLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
160 setControl(comp);
161 setPageComplete(true);
163 // Initialize versions versus master
164 IDialogSettings settings = getWizard().getDialogSettings();
165 boolean useMasterSetting = settings != null
166 && settings.getBoolean(GIT_PAGE_USE_MASTER);
167 useMaster.setSelection(useMasterSetting);
168 versions.setSelection(!useMasterSetting);
170 if (descriptions != null) {
171 bundlesViewer.setInput(descriptions);
172 updateCount();
177 @Override
178 public boolean finish() {
179 boolean head = false;
180 if (getControl() != null) {
181 head = useMaster.getSelection();
182 // store settings
183 IDialogSettings settings = getWizard().getDialogSettings();
184 if (settings != null)
185 settings.put(GIT_PAGE_USE_MASTER, head);
186 } else {
187 // use whatever was used last time
188 IDialogSettings settings = getWizard().getDialogSettings();
189 if (settings != null)
190 head = settings.getBoolean(GIT_PAGE_USE_MASTER);
193 if (head && descriptions != null)
194 // modify tags on bundle import descriptions
195 for (ScmUrlImportDescription description : descriptions) {
196 URI scmUri = description.getUri();
197 description.setUrl(removeTag(scmUri));
200 return true;
203 /* (non-Javadoc)
204 * @see org.eclipse.team.ui.IScmUrlImportWizardPage#getSelection()
206 @Override
207 public ScmUrlImportDescription[] getSelection() {
208 return descriptions;
211 @Override
212 public void setSelection(ScmUrlImportDescription[] descriptions) {
213 this.descriptions = descriptions;
214 // fill viewer
215 if (bundlesViewer != null) {
216 bundlesViewer.setInput(descriptions);
217 updateCount();
222 * Updates the count of bundles that will be imported
224 private void updateCount() {
225 counterLabel.setText(NLS.bind(UIText.GitScmUrlImportWizardPage_counter,
226 Integer.valueOf(descriptions.length)));
227 counterLabel.getParent().layout();
230 private static String getTag(URI scmUri) {
231 GitURI gitURI = new GitURI(scmUri);
232 return gitURI.getTag();
236 * Remove tag attributes from the given URI reference. Results in the URI
237 * pointing to HEAD.
239 * @param scmUri
240 * a SCM URI reference to modify
241 * @return Returns the content of the stripped URI as a string.
243 private static String removeTag(URI scmUri) {
244 StringBuilder sb = new StringBuilder();
245 sb.append(scmUri.getScheme()).append(':');
246 String ssp = scmUri.getSchemeSpecificPart();
247 int j = ssp.indexOf(';');
248 if (j != -1) {
249 sb.append(ssp.substring(0, j));
250 String[] params = ssp.substring(j).split(";"); //$NON-NLS-1$
251 for (String param : params) {
252 // PDE way of providing tags
253 if (param.startsWith("tag=")) {//$NON-NLS-1$
254 // ignore
255 } else if (param.startsWith("version=")) {//$NON-NLS-1$
256 // ignore
257 } else if (!param.isEmpty()) {
258 sb.append(";").append(param); //$NON-NLS-1$
261 } else
262 sb.append(ssp);
263 return sb.toString();
266 private static String getServer(URI scmUri) {
267 GitURI gitURI = new GitURI(scmUri);
268 return gitURI.getRepository().toString();