diff options
author | Mathias Kinzler | 2010-04-26 14:07:43 +0000 |
---|---|---|
committer | Chris Aniszczyk | 2010-04-26 20:34:27 +0000 |
commit | 974c5c47687dccbed6b0fe88b7025c8e1363bc42 (patch) | |
tree | 14b517da4ce703ea22655da87da48b38bfb96600 | |
parent | 2a37cbe8457669294d7bdd2e930f5f7cb0fc88bb (diff) | |
download | egit-974c5c47687dccbed6b0fe88b7025c8e1363bc42.tar.gz egit-974c5c47687dccbed6b0fe88b7025c8e1363bc42.tar.xz egit-974c5c47687dccbed6b0fe88b7025c8e1363bc42.zip |
BranchSelectionDialog fixes
Currently, hitting "Rename" on a tag
will do some renaming inappropriately: the result
is a new local branch.
The same goes when renaming a remote
branch using this dialog: the result is a local
branch.
This fix will disable renaming of tags
altogether and use the appropriate prefixes
when renaming branches.
In addition, the different buttons will be
enabled/disabled depending on the selection
of the tag/branch.
Change-Id: I38b2f04f71ba7784506891fb22bc2e72bfaec4ec
Bug: 308457
Signed-off-by: Mathias Kinzler <mathias.kinzler@sap.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
4 files changed, 111 insertions, 79 deletions
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/BranchAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/BranchAction.java index e2d5965268..f7d619947d 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/BranchAction.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/BranchAction.java @@ -20,13 +20,13 @@ import org.eclipse.egit.ui.internal.decorators.GitLightweightDecorator; import org.eclipse.egit.ui.internal.dialogs.BranchSelectionDialog; import org.eclipse.egit.ui.internal.trace.GitTraceLocation; import org.eclipse.jface.action.IAction; -import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.window.Window; +import org.eclipse.jgit.lib.Repository; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.widgets.Display; import org.eclipse.team.core.TeamException; -import org.eclipse.jgit.lib.Repository; /** * Action for selecting a branch and checking it out. @@ -48,9 +48,8 @@ public class BranchAction extends RepositoryAction { return; } - BranchSelectionDialog dialog = new BranchSelectionDialog(getShell(), repository); - dialog.setShowResetType(false); - if (dialog.open() != IDialogConstants.OK_ID) { + BranchSelectionDialog dialog = new BranchSelectionDialog(getShell(), repository, false); + if (dialog.open() != Window.OK) { return; } diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ResetAction.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ResetAction.java index e4d4e64d5f..d29727162a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ResetAction.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/actions/ResetAction.java @@ -46,7 +46,7 @@ public class ResetAction extends RepositoryAction { return; } - BranchSelectionDialog branchSelectionDialog = new BranchSelectionDialog(getShell(), repository); + BranchSelectionDialog branchSelectionDialog = new BranchSelectionDialog(getShell(), repository, true); if (branchSelectionDialog.open() == IDialogConstants.OK_ID) { final String refName = branchSelectionDialog.getRefName(); final ResetType type = branchSelectionDialog.getResetType(); diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/BranchSelectionDialog.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/BranchSelectionDialog.java index b5f00c5c0b..f8ad57263d 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/BranchSelectionDialog.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/dialogs/BranchSelectionDialog.java @@ -28,13 +28,19 @@ import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.window.Window; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefRename; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RefUpdate.Result; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridLayout; @@ -48,50 +54,44 @@ import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; -import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.RefRename; -import org.eclipse.jgit.lib.RefUpdate; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.RefUpdate.Result; /** * The branch and reset selection dialog * */ public class BranchSelectionDialog extends Dialog { + private final Repository repo; - private boolean showResetType = true; + private final boolean showResetType; + + private Tree branchTree; + + private Button confirmationBtn; + + private Button renameButton; + + private Button newButton; + + private String selectedBranch; + + private ResetType resetType = ResetType.MIXED; /** * Construct a dialog to select a branch to reset to or check out * @param parentShell * @param repo + * @param showReset true if the "reset" part should be shown */ - public BranchSelectionDialog(Shell parentShell, Repository repo) { + public BranchSelectionDialog(Shell parentShell, Repository repo, boolean showReset) { super(parentShell); this.repo = repo; + this.showResetType = showReset; } - /** - * Pre-set whether or present a reset or checkout dialog - * @param show - */ - public void setShowResetType(boolean show) { - this.showResetType = show; - } - - private Composite parent; - - private Tree branchTree; - - private Button confirmationBtn; - @Override protected Composite createDialogArea(Composite base) { - parent = (Composite) super.createDialogArea(base); + Composite parent = (Composite) super.createDialogArea(base); parent.setLayout(GridLayoutFactory.swtDefaults().create()); new Label(parent, SWT.NONE).setText(UIText.BranchSelectionDialog_Refs); branchTree = new Tree(parent, SWT.BORDER); @@ -99,15 +99,31 @@ public class BranchSelectionDialog extends Dialog { branchTree.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - // calculate the user's selected ref - refNameFromDialog(); - // make sure it's a valid selection - confirmationBtn.setEnabled(refName != null); + // enable the buttons depending on the selection + boolean oneSelected = branchTree.getSelection().length == 1; + + String refName = refNameFromDialog(); + + boolean headSelected = Constants.HEAD.equals(refName); + + boolean tagSelected = refName!=null && refName.startsWith( + Constants.R_TAGS); + + boolean branchSelected = refName!=null && (refName.startsWith(Constants.R_HEADS) || refName.startsWith(Constants.R_REMOTES)); + + // TODO add support for checkout of tags + confirmationBtn.setEnabled(oneSelected && branchSelected && !headSelected + && !tagSelected); + // we don't support rename on tags + renameButton.setEnabled(oneSelected && branchSelected && !headSelected && !tagSelected); + + // new branch can not be based on a tag + newButton.setEnabled(oneSelected && branchSelected && !tagSelected); } }); if (showResetType) { - buildResetGroup(); + buildResetGroup(parent); } String rawTitle = showResetType ? UIText.BranchSelectionDialog_TitleReset @@ -124,7 +140,7 @@ public class BranchSelectionDialog extends Dialog { return parent; } - private void buildResetGroup() { + private void buildResetGroup(Composite parent) { Group g = new Group(parent, SWT.NONE); g.setText(UIText.BranchSelectionDialog_ResetType); g.setLayoutData(GridDataFactory.swtDefaults().align(SWT.CENTER, SWT.CENTER).create()); @@ -243,17 +259,13 @@ public class BranchSelectionDialog extends Dialog { } } - private String refName = null; - /** - * @return Selected ref + * @return the selected refName */ public String getRefName() { - return refName; + return this.selectedBranch; } - private ResetType resetType = ResetType.MIXED; - /** * @return Type of Reset */ @@ -263,7 +275,7 @@ public class BranchSelectionDialog extends Dialog { @Override protected void okPressed() { - refNameFromDialog(); + this.selectedBranch = refNameFromDialog(); if (showResetType) { if (resetType == ResetType.HARD) { if (!MessageDialog.openQuestion(getShell(), @@ -277,16 +289,16 @@ public class BranchSelectionDialog extends Dialog { super.okPressed(); } - private void refNameFromDialog() { + private String refNameFromDialog() { TreeItem[] selection = branchTree.getSelection(); - refName = null; - if (selection != null && selection.length > 0) { + if (selection.length > 0) { TreeItem item = selection[0]; - refName = (String) item.getData(); + return (String) item.getData(); } + return null; } - private InputDialog getRefNameInputDialog(String prompt) { + private InputDialog getRefNameInputDialog(String prompt, final String refPrefix) { InputDialog labelDialog = new InputDialog( getShell(), UIText.BranchSelectionDialog_QuestionNewBranchTitle, @@ -299,7 +311,7 @@ public class BranchSelectionDialog extends Dialog { return ""; //$NON-NLS-1$ } - String testFor = Constants.R_HEADS + newText; + String testFor = refPrefix + newText; try { if (repo.resolve(testFor) != null) return UIText.BranchSelectionDialog_ErrorAlreadyExists; @@ -320,34 +332,47 @@ public class BranchSelectionDialog extends Dialog { @Override protected void createButtonsForButtonBar(Composite parent) { if (!showResetType) { - Button newButton = new Button(parent, SWT.PUSH); + newButton = new Button(parent, SWT.PUSH); newButton.setFont(JFaceResources.getDialogFont()); newButton.setText(UIText.BranchSelectionDialog_NewBranch); setButtonLayoutData(newButton); ((GridLayout)parent.getLayout()).numColumns++; - Button renameButton = new Button(parent, SWT.PUSH); + renameButton = new Button(parent, SWT.PUSH); renameButton.setFont(JFaceResources.getDialogFont()); renameButton.setText(UIText.BranchSelectionDialog_Rename); setButtonLayoutData(renameButton); ((GridLayout)parent.getLayout()).numColumns++; - renameButton.addSelectionListener(new SelectionListener() { + renameButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { - // check what ref name the user selected, if any. - refNameFromDialog(); - String branchName; + String refName = refNameFromDialog(); + String refPrefix; + + // the button should be disabled anyway, but we check again + if (refName.equals(Constants.HEAD)) + return; + if (refName.startsWith(Constants.R_HEADS)) - branchName = refName.substring(Constants.R_HEADS.length()); - else - branchName = refName; + refPrefix = Constants.R_HEADS; + else if (refName.startsWith(Constants.R_REMOTES)) + refPrefix = Constants.R_REMOTES; + else if (refName.startsWith(Constants.R_TAGS)) + refPrefix = Constants.R_TAGS; + else { + // the button should be disabled anyway, but we check again + return; + } + + String branchName = refName.substring(refPrefix.length()); + InputDialog labelDialog = getRefNameInputDialog(NLS .bind( UIText.BranchSelectionDialog_QuestionNewBranchNameMessage, - branchName)); + branchName, refPrefix), refPrefix); if (labelDialog.open() == Window.OK) { - String newRefName = Constants.R_HEADS + labelDialog.getValue(); + String newRefName = refPrefix + labelDialog.getValue(); try { RefRename renameRef = repo.renameRef(refName, newRefName); if (renameRef.rename() != Result.RENAMED) { @@ -376,28 +401,41 @@ public class BranchSelectionDialog extends Dialog { } } } - public void widgetDefaultSelected(SelectionEvent e) { - widgetSelected(e); - } }); - newButton.addSelectionListener(new SelectionListener() { + newButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { // check what ref name the user selected, if any. - refNameFromDialog(); + String refName = refNameFromDialog(); + + String refPrefix; + + // the button should be disabled anyway, but we check again + if (refName.equals(Constants.HEAD)) + return; + + if (refName.startsWith(Constants.R_HEADS)) + refPrefix = Constants.R_HEADS; + else if (refName.startsWith(Constants.R_REMOTES)) + refPrefix = Constants.R_REMOTES; + else { + // the button should be disabled anyway, but we check again + return; + } + + InputDialog labelDialog = getRefNameInputDialog( + NLS + .bind( + UIText.BranchSelectionDialog_QuestionNewBranchMessage, + refName, refPrefix), refPrefix); - InputDialog labelDialog = getRefNameInputDialog(UIText.BranchSelectionDialog_QuestionNewBranchMessage); if (labelDialog.open() == Window.OK) { String newRefName = Constants.R_HEADS + labelDialog.getValue(); RefUpdate updateRef; try { updateRef = repo.updateRef(newRefName); Ref startRef = repo.getRef(refName); - ObjectId startAt; - if (refName == null) - startAt = repo.resolve(Constants.HEAD); - else - startAt = repo.resolve(refName); + ObjectId startAt = repo.resolve(refName); String startBranch; if (startRef != null) startBranch = refName; @@ -424,11 +462,6 @@ public class BranchSelectionDialog extends Dialog { } } } - - - public void widgetDefaultSelected(SelectionEvent e) { - widgetSelected(e); - } }); } confirmationBtn = createButton(parent, IDialogConstants.OK_ID, diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties index 8e2c1fdc4e..55c19a959a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/uitext.properties @@ -470,8 +470,8 @@ BranchSelectionDialog_LocalBranches=Local Branches BranchSelectionDialog_NewBranch=&New branch BranchSelectionDialog_OkCheckout=&Checkout BranchSelectionDialog_OkReset=&Reset -BranchSelectionDialog_QuestionNewBranchMessage=Enter name of new branch. It will branch from the selected branch. refs/heads/ will be prepended to the name you type -BranchSelectionDialog_QuestionNewBranchNameMessage=Enter new name of the {0} branch. refs/heads/ will be prepended to the name you type +BranchSelectionDialog_QuestionNewBranchMessage=Enter name of the new branch. It will branch from the selected branch {0}. {1} will be prepended to the name you type +BranchSelectionDialog_QuestionNewBranchNameMessage=Enter new name of the {0} branch. {1} will be prepended to the name you type BranchSelectionDialog_QuestionNewBranchTitle=New branch BranchSelectionDialog_QuestionNewBranchNameTitle=Rename branch BranchSelectionDialog_ReallyResetMessage=Resetting will overwrite any changes in your working directory.\n\nDo you wish to continue? |