Generalize UIUtils.addContentProposalToText a bit more
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / gerrit / GerritConfigurationPage.java
blobc5dce0991985def319d8836c53e5ad6d58859f48
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;
16 import java.util.Set;
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;
46 /**
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;
56 private Text branch;
58 private Group pushConfigurationGroup;
60 private Group fetchConfigurationGroup;
62 private Group uriGroup;
64 private Combo scheme;
66 private Text uriText;
68 private URIish pushURI;
70 private Text user;
72 private int eventDepth;
74 private final Repository repository;
76 private final String remoteName;
78 /**
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,
91 repositoryName));
95 @Override
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);
107 setControl(panel);
108 uriText.setFocus();
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() {
122 @Override
123 public void modifyText(final ModifyEvent e) {
124 eventDepth++;
125 try {
126 if (eventDepth == 1) {
127 if (pushURI != null) {
128 pushURI = pushURI.setUser(user.getText());
129 uriText.setText(pushURI.toString());
130 checkPage();
133 } finally {
134 eventDepth--;
139 uriText.addModifyListener(new ModifyListener() {
140 @Override
141 public void modifyText(final ModifyEvent e) {
142 eventDepth++;
143 try {
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) {
150 // empty
151 } finally {
152 eventDepth--;
154 checkPage();
158 for (Protocol p : Protocol.values()) {
159 scheme.add(p.getDefaultScheme());
161 scheme.addSelectionListener(new SelectionAdapter() {
162 @Override
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);
169 else
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);
183 branchLabel
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() {
192 @Override
193 public void modifyText(final ModifyEvent e) {
194 checkPage();
198 // give focus to the branch if label is activated using the mnemonic
199 branchLabel.addTraverseListener(new TraverseListener() {
200 @Override
201 public void keyTraversed(TraverseEvent e) {
202 branch.setFocus();
203 branch.selectAll();
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() {
221 return pushURI;
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
234 * @param id
235 * help context
237 public void setHelpContext(String id) {
238 helpContext = id;
241 @Override
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);
254 * @param uri
255 * the URI of the source repository
256 * @param targetBranch
258 public void setSelection(URIish uri, String targetBranch) {
260 setDefaults(uri, targetBranch);
261 checkPage();
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() {
280 try {
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);
288 return;
290 String branchName = branch.getText();
291 if (branchName.length() == 0) {
292 setErrorMessage(UIText.GerritConfigurationPage_errorBranchName);
293 setPageComplete(false);
294 return;
297 setErrorMessage(null);
298 setPageComplete(true);
301 private void addRefContentProposalToText(final Text textField) {
302 UIUtils.<String> addContentProposalToText(textField,
303 () -> {
304 try {
305 Set<String> sortedSet = new TreeSet<>(
306 String.CASE_INSENSITIVE_ORDER);
307 sortedSet.addAll(repository.getRefDatabase()
308 .getRefs(Constants.R_REMOTES + remoteName + '/')
309 .keySet());
310 return sortedSet;
311 } catch (IOException e) {
312 return Collections.emptyList();
314 }, (pattern, refName) -> {
315 if (pattern != null
316 && !pattern.matcher(refName).matches()) {
317 return null;
319 return new ContentProposal(refName);
320 }, null,
321 UIText.GerritConfigurationPage_BranchTooltipStartTyping,
322 UIText.GerritConfigurationPage_BranchTooltipHover);