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
11 * Tomasz Zarna <Tomasz.Zarna@pl.ibm.com> - initial implementation
12 *******************************************************************************/
13 package org
.eclipse
.egit
.ui
.internal
.importing
;
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
;
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
{
62 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
65 public Image
getImage(Object element
) {
66 return PlatformUI
.getWorkbench().getSharedImages().getImage(IDE
.SharedImages
.IMG_OBJ_PROJECT
);
70 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
73 public String
getText(Object element
) {
74 return getStyledText(element
).getString();
78 * @see org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.jface.viewers.ViewerCell)
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()));
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();
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
);
114 styledString
.append(element
.toString());
120 * Constructs the page.
122 public GitScmUrlImportWizardPage() {
123 super("git", UIText
.GitScmUrlImportWizardPage_title
, null); //$NON-NLS-1$
124 setDescription(UIText
.GitScmUrlImportWizardPage_description
);
128 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
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() {
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
);
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
));
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
);
178 public boolean finish() {
179 boolean head
= false;
180 if (getControl() != null) {
181 head
= useMaster
.getSelection();
183 IDialogSettings settings
= getWizard().getDialogSettings();
184 if (settings
!= null)
185 settings
.put(GIT_PAGE_USE_MASTER
, head
);
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
));
204 * @see org.eclipse.team.ui.IScmUrlImportWizardPage#getSelection()
207 public ScmUrlImportDescription
[] getSelection() {
212 public void setSelection(ScmUrlImportDescription
[] descriptions
) {
213 this.descriptions
= descriptions
;
215 if (bundlesViewer
!= null) {
216 bundlesViewer
.setInput(descriptions
);
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
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(';');
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$
255 } else if (param
.startsWith("version=")) {//$NON-NLS-1$
257 } else if (!param
.isEmpty()) {
258 sb
.append(";").append(param
); //$NON-NLS-1$
263 return sb
.toString();
266 private static String
getServer(URI scmUri
) {
267 GitURI gitURI
= new GitURI(scmUri
);
268 return gitURI
.getRepository().toString();