1616 * specific language governing permissions and limitations
1717 * under the License.
1818 */
19-
2019package org .netbeans .modules .git .ui .branch ;
2120
2221import org .netbeans .libs .git .GitException .NotMergedException ;
2322import org .netbeans .modules .git .client .GitClientExceptionHandler ;
2423import java .io .File ;
24+ import java .util .HashMap ;
25+ import java .util .Map ;
2526import java .util .logging .Level ;
2627import java .util .logging .Logger ;
28+ import org .netbeans .libs .git .GitBranch ;
2729import org .netbeans .modules .git .client .GitClient ;
2830import org .netbeans .libs .git .GitException ;
2931import org .netbeans .modules .git .Git ;
3032import org .netbeans .modules .git .client .GitProgressSupport ;
3133import org .netbeans .modules .git .ui .actions .SingleRepositoryAction ;
34+ import org .netbeans .modules .git .ui .history .BranchSelector ;
35+ import org .netbeans .modules .git .ui .repository .RepositoryInfo ;
3236import org .netbeans .modules .versioning .spi .VCSContext ;
3337import org .openide .DialogDisplayer ;
3438import org .openide .NotifyDescriptor ;
3539import org .openide .awt .ActionID ;
3640import org .openide .awt .ActionRegistration ;
41+ import org .openide .nodes .Node ;
3742import org .openide .util .NbBundle ;
3843
3944/**
@@ -47,26 +52,54 @@ public class DeleteBranchAction extends SingleRepositoryAction {
4752private static final Logger LOG =Logger .getLogger (DeleteBranchAction .class .getName ());
4853
4954@ Override
50- protected void performAction (File repository ,File []roots ,VCSContext context ) {
51- throw new UnsupportedOperationException ();
55+ protected void performAction (File repository ,File []roots ,VCSContext context ) {
56+ RepositoryInfo info =RepositoryInfo .getInstance (repository );
57+ HashMap <String ,GitBranch >branches =new HashMap <>(info .getBranches ());
58+ branches .remove (info .getActiveBranch ().getName ());
59+
60+ if (branches .isEmpty ()) {
61+ return ;
62+ }
63+
64+ BranchSelector selector =new BranchSelector (repository ,branches );
65+ if (selector .open ()) {
66+ deleteBranch (repository ,selector .getSelectedBranch ());
67+ }
68+ }
69+
70+ @ Override
71+ protected boolean enable (Node []activatedNodes ) {
72+ if (!super .enable (activatedNodes )) {
73+ return false ;
74+ }
75+
76+ // require 2+ branches
77+ Map .Entry <File ,File []>actionRoots =getActionRoots (getCurrentContext (activatedNodes ));
78+ if (actionRoots !=null ) {
79+ RepositoryInfo info =RepositoryInfo .getInstance (actionRoots .getKey ());
80+
81+ return info !=null &&info .getBranches ().size () >1 ;
82+ }
83+
84+ return false ;
5285 }
5386
54- public void deleteBranch (final File repository ,final String branchName ) {
87+ public void deleteBranch (final File repository ,final String branchName ) {
5588NotifyDescriptor nd =new NotifyDescriptor .Confirmation (NbBundle .getMessage (DeleteBranchAction .class ,"MSG_DeleteBranchAction.confirmation" ,branchName ),//NOI18N
56- NbBundle .getMessage (DeleteBranchAction .class ,"LBL_DeleteBranchAction.confirmation" ),//NOI18N
57- NotifyDescriptor .OK_CANCEL_OPTION ,NotifyDescriptor .QUESTION_MESSAGE );
89+ NbBundle .getMessage (DeleteBranchAction .class ,"LBL_DeleteBranchAction.confirmation" ),//NOI18N
90+ NotifyDescriptor .OK_CANCEL_OPTION ,NotifyDescriptor .QUESTION_MESSAGE );
5891
5992if (NotifyDescriptor .OK_OPTION ==DialogDisplayer .getDefault ().notify (nd )) {
6093GitProgressSupport supp =new GitProgressSupport () {
6194@ Override
62- protected void perform () {
95+ protected void perform () {
6396try {
6497GitClient client =getClient ();
6598boolean forceDelete =false ;
6699boolean cont ;
67100do {
68101try {
69- LOG .log (Level .FINE ,"Deleting a branch: {0}/{1}" ,new Object [] { branchName ,forceDelete });//NOI18N
102+ LOG .log (Level .FINE ,"Deleting a branch: {0}/{1}" ,new Object []{ branchName ,forceDelete });//NOI18N
70103client .deleteBranch (branchName ,forceDelete ,getProgressMonitor ());
71104cont =false ;
72105 }catch (GitException .NotMergedException ex ) {
@@ -78,10 +111,10 @@ protected void perform () {
78111 }
79112 }
80113
81- private boolean handleException (NotMergedException ex ) {
114+ private boolean handleException (NotMergedException ex ) {
82115NotifyDescriptor nd =new NotifyDescriptor .Confirmation (NbBundle .getMessage (DeleteBranchAction .class ,"MSG_DeleteBranchAction.notMerged" ,ex .getUnmergedRevision ()),//NOI18N
83- NbBundle .getMessage (DeleteBranchAction .class ,"LBL_DeleteBranchAction.notMerged" ),//NOI18N
84- NotifyDescriptor .YES_NO_OPTION ,NotifyDescriptor .QUESTION_MESSAGE );
116+ NbBundle .getMessage (DeleteBranchAction .class ,"LBL_DeleteBranchAction.notMerged" ),//NOI18N
117+ NotifyDescriptor .YES_NO_OPTION ,NotifyDescriptor .QUESTION_MESSAGE );
85118return NotifyDescriptor .YES_OPTION ==DialogDisplayer .getDefault ().notify (nd );
86119 }
87120 };