Honor workbench setting for Dialog font
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / repository / CreateBranchPage.java
blobd1370fc5dd7b757c777e87c6fe62991ad153aaf9
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.repository;
13 import java.io.IOException;
14 import java.util.Map.Entry;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.egit.core.op.BranchOperation;
19 import org.eclipse.egit.ui.UIText;
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.layout.GridDataFactory;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.jgit.lib.Constants;
24 import org.eclipse.jgit.lib.ObjectId;
25 import org.eclipse.jgit.lib.Ref;
26 import org.eclipse.jgit.lib.RefUpdate;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
41 /**
42 * Allows to create a new local branch based on another branch. The source
43 * branch can be selected using a drop down.
45 public class CreateBranchPage extends WizardPage {
47 private final Repository myRepository;
49 private final Ref myBaseBranch;
51 private Text nameText;
53 private Button checkout;
55 private Combo branchCombo;
57 /**
58 * Constructs this page.
59 * <p>
60 * If a base branch is provided, the drop down will be selected accordingly
62 * @param repo
63 * the repository
64 * @param baseBranch
65 * the branch to base the new branch on, may be null
67 public CreateBranchPage(Repository repo, Ref baseBranch) {
68 super(CreateBranchPage.class.getName());
69 this.myRepository = repo;
70 this.myBaseBranch = baseBranch;
71 setTitle(UIText.CreateBranchPage_Title);
74 public void createControl(Composite parent) {
75 Composite main = new Composite(parent, SWT.NONE);
76 main.setLayout(new GridLayout(3, false));
78 Label sourceLabel = new Label(main, SWT.NONE);
79 sourceLabel.setText(UIText.CreateBranchPage_SourceBranchLabel);
80 sourceLabel.setToolTipText(UIText.CreateBranchPage_SourceBranchTooltip);
81 this.branchCombo = new Combo(main, SWT.READ_ONLY | SWT.DROP_DOWN);
83 GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(
84 this.branchCombo);
86 try {
87 for (Entry<String, Ref> ref : myRepository.getRefDatabase()
88 .getRefs(Constants.R_HEADS).entrySet()) {
89 if (!ref.getValue().isSymbolic())
90 this.branchCombo.add(ref.getValue().getName());
92 for (Entry<String, Ref> ref : myRepository.getRefDatabase()
93 .getRefs(Constants.R_REMOTES).entrySet()) {
94 if (!ref.getValue().isSymbolic())
95 this.branchCombo.add(ref.getValue().getName());
98 } catch (IOException e1) {
99 // ignore here
102 this.branchCombo.addSelectionListener(new SelectionAdapter() {
103 @Override
104 public void widgetSelected(SelectionEvent e) {
105 checkPage();
108 // select the current branch in the drop down
109 if (myBaseBranch != null) {
110 this.branchCombo.setText(myBaseBranch.getName());
111 } else {
112 // TODO this only works for local branches; once we have a solution
113 // for accessing the correct information (we need this for the
114 // project label decoration, too), we should figure out if a remote
115 // branch is checked-out and set the text accordingly
116 String fullBranch;
117 try {
118 fullBranch = myRepository.getFullBranch();
119 this.branchCombo.setText(fullBranch);
120 } catch (IOException e1) {
121 // ignore
125 Label nameLabel = new Label(main, SWT.NONE);
126 nameLabel.setText(UIText.CreateBranchPage_BranchNameLabel);
128 // we visualize the prefix here
129 Text prefix = new Text(main, SWT.NONE);
130 prefix.setText(Constants.R_HEADS);
131 prefix.setEnabled(false);
133 nameText = new Text(main, SWT.BORDER);
134 GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);
136 nameText.addModifyListener(new ModifyListener() {
137 public void modifyText(ModifyEvent e) {
138 checkPage();
142 boolean isBare = myRepository.getConfig().getBoolean(
143 "core", "bare", false); //$NON-NLS-1$ //$NON-NLS-2$
144 checkout = new Button(main, SWT.CHECK);
145 checkout.setText(UIText.CreateBranchPage_CheckoutButton);
146 // most of the time, we probably will check this out
147 // unless we have a bare repository which doesn't allow
148 // check out at all
149 checkout.setSelection(!isBare);
150 checkout.setEnabled(!isBare);
151 checkout.setVisible(!isBare);
152 GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(
153 checkout);
154 checkout.addSelectionListener(new SelectionAdapter() {
156 @Override
157 public void widgetSelected(SelectionEvent e) {
158 checkPage();
163 Dialog.applyDialogFont(main);
164 setControl(main);
165 nameText.setFocus();
167 // in any case, we will have to enter the name
168 setPageComplete(false);
169 if (this.myBaseBranch != null)
170 setMessage(UIText.CreateBranchPage_ChosseNameMessage);
171 else
172 setMessage(UIText.CreateBranchPage_ChooseBranchAndNameMessage);
175 private void checkPage() {
176 setErrorMessage(null);
178 try {
180 if (branchCombo.getText().length() == 0) {
181 setErrorMessage(UIText.CreateBranchPage_MissingSourceMessage);
182 return;
184 if (nameText.getText().length() == 0) {
185 setErrorMessage(UIText.CreateBranchPage_MissingNameMessage);
186 return;
189 String fullName = getBranchName();
190 try {
191 if (myRepository.getRef(fullName) != null)
192 setErrorMessage(NLS.bind(
193 UIText.CreateBranchPage_BranchAlreadyExistsMessage,
194 fullName));
195 return;
196 } catch (IOException e) {
197 // ignore here
199 } finally {
200 setPageComplete(getErrorMessage() == null);
204 private String getBranchName() {
205 return Constants.R_HEADS + nameText.getText();
208 private String getSourceBranchName() {
209 if (myBaseBranch != null)
210 return myBaseBranch.getName();
211 else if (this.branchCombo != null)
212 return this.branchCombo.getText();
213 else
214 return null;
218 * @param monitor
219 * @throws CoreException
220 * @throws IOException
222 public void createBranch(IProgressMonitor monitor) throws CoreException,
223 IOException {
224 String newRefName = getBranchName();
225 RefUpdate updateRef;
227 monitor.beginTask(UIText.CreateBranchPage_CreatingBranchMessage,
228 IProgressMonitor.UNKNOWN);
229 updateRef = myRepository.updateRef(newRefName);
231 Ref sourceBranch;
232 if (myBaseBranch != null) {
233 sourceBranch = myBaseBranch;
234 } else {
235 sourceBranch = myRepository.getRef(getSourceBranchName());
237 ObjectId startAt = sourceBranch.getObjectId();
238 String startBranch = myRepository
239 .shortenRefName(sourceBranch.getName());
240 updateRef.setNewObjectId(startAt);
241 updateRef
242 .setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$
243 updateRef.update();
244 if (checkout.getSelection()) {
245 if (monitor.isCanceled())
246 return;
247 monitor.beginTask(UIText.CreateBranchPage_CheckingOutMessage,
248 IProgressMonitor.UNKNOWN);
249 new BranchOperation(myRepository, getBranchName()).execute(monitor);