1 /*******************************************************************************
2 * Copyright (C) 2011, Stefan Lay <stefan.lay@sap.com>
3 * Copyright (C) 2011, 2013, Matthias Sohn <matthias.sohn@sap.com>
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 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *******************************************************************************/
10 package org
.eclipse
.egit
.ui
.internal
.gerrit
;
12 import java
.io
.IOException
;
13 import java
.net
.URISyntaxException
;
14 import java
.text
.MessageFormat
;
15 import java
.util
.Collections
;
17 import java
.util
.TreeSet
;
19 import org
.eclipse
.egit
.core
.internal
.gerrit
.GerritUtil
;
20 import org
.eclipse
.egit
.ui
.Activator
;
21 import org
.eclipse
.egit
.ui
.UIUtils
;
22 import org
.eclipse
.egit
.ui
.internal
.SWTUtils
;
23 import org
.eclipse
.egit
.ui
.internal
.UIText
;
24 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelectionPage
.Protocol
;
25 import org
.eclipse
.jface
.dialogs
.Dialog
;
26 import org
.eclipse
.jface
.fieldassist
.ContentProposal
;
27 import org
.eclipse
.jface
.wizard
.WizardPage
;
28 import org
.eclipse
.jgit
.lib
.Constants
;
29 import org
.eclipse
.jgit
.lib
.Repository
;
30 import org
.eclipse
.jgit
.transport
.URIish
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.events
.ModifyEvent
;
33 import org
.eclipse
.swt
.events
.ModifyListener
;
34 import org
.eclipse
.swt
.events
.SelectionAdapter
;
35 import org
.eclipse
.swt
.events
.SelectionEvent
;
36 import org
.eclipse
.swt
.events
.TraverseEvent
;
37 import org
.eclipse
.swt
.events
.TraverseListener
;
38 import org
.eclipse
.swt
.layout
.GridLayout
;
39 import org
.eclipse
.swt
.widgets
.Combo
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Group
;
42 import org
.eclipse
.swt
.widgets
.Label
;
43 import org
.eclipse
.swt
.widgets
.Text
;
44 import org
.eclipse
.ui
.PlatformUI
;
47 * Wizard page that offers simplified configuration if upstream repository
48 * is hosted by Gerrit.
50 class GerritConfigurationPage
extends WizardPage
{
52 private final static int GERRIT_DEFAULT_SSH_PORT
= 29418;
54 private String helpContext
= null;
58 private Group pushConfigurationGroup
;
60 private Group fetchConfigurationGroup
;
62 private Group uriGroup
;
68 private URIish pushURI
;
72 private int eventDepth
;
74 private final Repository repository
;
76 private final String remoteName
;
79 * @param repository the respository
80 * @param remoteName the remote name
83 public GerritConfigurationPage(Repository repository
, String remoteName
) {
84 super(GerritConfigurationPage
.class.getName());
85 this.repository
= repository
;
86 this.remoteName
= remoteName
;
87 setTitle(UIText
.GerritConfigurationPage_title
);
88 String repositoryName
= Activator
.getDefault().getRepositoryUtil().getRepositoryName(repository
);
89 setDescription(MessageFormat
.format(
90 UIText
.GerritConfigurationPage_PageDescription
, remoteName
,
96 public void createControl(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 createURIGroup(panel
);
103 createPushConfigurationGroup(panel
);
104 createFetchConfigurationGroup(panel
);
106 Dialog
.applyDialogFont(panel
);
111 private void createURIGroup(Composite panel
) {
112 uriGroup
= SWTUtils
.createHFillGroup(panel
,
113 UIText
.GerritConfigurationPage_pushUri
,
114 SWTUtils
.MARGINS_DEFAULT
, 2);
116 scheme
= new Combo(uriGroup
, SWT
.DROP_DOWN
| SWT
.READ_ONLY
);
117 uriText
= SWTUtils
.createText(uriGroup
);
119 new Label(uriGroup
, SWT
.NULL
).setText(UIText
.GerritConfigurationPage_UserLabel
);
120 user
= SWTUtils
.createText(uriGroup
);
121 user
.addModifyListener(new ModifyListener() {
123 public void modifyText(final ModifyEvent e
) {
126 if (eventDepth
== 1) {
127 if (pushURI
!= null) {
128 pushURI
= pushURI
.setUser(user
.getText());
129 uriText
.setText(pushURI
.toString());
139 uriText
.addModifyListener(new ModifyListener() {
141 public void modifyText(final ModifyEvent e
) {
144 if (eventDepth
== 1) {
145 URIish u
= new URIish(uriText
.getText());
146 String newUser
= u
.getUser();
147 user
.setText(newUser
!= null ? newUser
: ""); //$NON-NLS-1$
149 } catch (URISyntaxException e1
) {
158 for (Protocol p
: Protocol
.values()) {
159 scheme
.add(p
.getDefaultScheme());
161 scheme
.addSelectionListener(new SelectionAdapter() {
163 public void widgetSelected(final SelectionEvent e
) {
164 final int idx
= scheme
.getSelectionIndex();
165 pushURI
= pushURI
.setScheme(scheme
.getItem(idx
));
167 if (Protocol
.SSH
.handles(pushURI
))
168 pushURI
= pushURI
.setPort(GERRIT_DEFAULT_SSH_PORT
);
170 pushURI
= pushURI
.setPort(-1);
172 uriText
.setText(pushURI
.toString());
173 scheme
.setToolTipText(Protocol
.values()[idx
].getTooltip());
178 private void createPushConfigurationGroup(Composite panel
) {
179 pushConfigurationGroup
= SWTUtils
.createHFillGroup(panel
,
180 UIText
.GerritConfigurationPage_groupPush
,
181 SWTUtils
.MARGINS_DEFAULT
, 3);
182 Label branchLabel
= new Label(pushConfigurationGroup
, SWT
.NULL
);
184 .setText(UIText
.GerritConfigurationPage_labelDestinationBranch
);
185 // we visualize the prefix here
186 Text prefix
= new Text(pushConfigurationGroup
, SWT
.READ_ONLY
);
187 prefix
.setText(GerritUtil
.REFS_FOR
);
188 prefix
.setEnabled(false);
190 branch
= SWTUtils
.createText(pushConfigurationGroup
);
191 branch
.addModifyListener(new ModifyListener() {
193 public void modifyText(final ModifyEvent e
) {
198 // give focus to the branch if label is activated using the mnemonic
199 branchLabel
.addTraverseListener(new TraverseListener() {
201 public void keyTraversed(TraverseEvent e
) {
206 addRefContentProposalToText(branch
);
209 private void createFetchConfigurationGroup(Composite panel
) {
210 fetchConfigurationGroup
= SWTUtils
.createHFillGroup(panel
,
211 UIText
.GerritConfigurationPage_groupFetch
,
212 SWTUtils
.MARGINS_DEFAULT
, 2);
213 new Label(fetchConfigurationGroup
, SWT
.NULL
)
214 .setText(UIText
.GerritConfigurationPage_ConfigureFetchReviewNotes
);
218 * @return the push URI for Gerrit code review
220 public URIish
getURI() {
225 * @return the branch used in the gerrit push refspec: refs/for/branch
227 public String
getBranch() {
228 return branch
.getText();
232 * Set the ID for context sensitive help
237 public void setHelpContext(String id
) {
242 public void performHelp() {
243 PlatformUI
.getWorkbench().getHelpSystem().displayHelp(helpContext
);
247 * @param uri the URI of the source repository
249 public void setSelection(URIish uri
) {
250 setSelection(uri
, null);
255 * the URI of the source repository
256 * @param targetBranch
258 public void setSelection(URIish uri
, String targetBranch
) {
260 setDefaults(uri
, targetBranch
);
264 private void setDefaults(URIish uri
, String targetBranch
) {
265 URIish newPushURI
= uri
;
266 if (Protocol
.SSH
.handles(uri
) && uri
.getPort() < 0) {
267 newPushURI
= newPushURI
.setPort(GERRIT_DEFAULT_SSH_PORT
);
268 } else if (Protocol
.GIT
.handles(uri
)) {
269 newPushURI
= newPushURI
.setScheme(Protocol
.SSH
.getDefaultScheme());
270 newPushURI
= newPushURI
.setPort(GERRIT_DEFAULT_SSH_PORT
);
272 uriText
.setText(newPushURI
.toString());
273 final String uriScheme
= newPushURI
.getScheme();
274 if (uriScheme
!= null)
275 scheme
.select(scheme
.indexOf(uriScheme
));
276 branch
.setText(targetBranch
!= null ? targetBranch
: Constants
.MASTER
);
279 private void checkPage() {
281 pushURI
= new URIish(uriText
.getText());
282 String uriScheme
= pushURI
.getScheme();
283 if (uriScheme
!= null)
284 scheme
.select(scheme
.indexOf(uriScheme
));
285 } catch (URISyntaxException e
) {
286 setErrorMessage(e
.getLocalizedMessage());
287 setPageComplete(false);
290 String branchName
= branch
.getText();
291 if (branchName
.length() == 0) {
292 setErrorMessage(UIText
.GerritConfigurationPage_errorBranchName
);
293 setPageComplete(false);
297 setErrorMessage(null);
298 setPageComplete(true);
301 private void addRefContentProposalToText(final Text textField
) {
302 UIUtils
.<String
> addContentProposalToText(textField
,
305 Set
<String
> sortedSet
= new TreeSet
<>(
306 String
.CASE_INSENSITIVE_ORDER
);
307 sortedSet
.addAll(repository
.getRefDatabase()
308 .getRefs(Constants
.R_REMOTES
+ remoteName
+ '/')
311 } catch (IOException e
) {
312 return Collections
.emptyList();
314 }, (pattern
, refName
) -> {
316 && !pattern
.matcher(refName
).matches()) {
319 return new ContentProposal(refName
);
321 UIText
.GerritConfigurationPage_BranchTooltipStartTyping
,
322 UIText
.GerritConfigurationPage_BranchTooltipHover
);