PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/ui/branch/CloseBranchAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 154 lines | 103 code | 7 blank | 44 comment | 17 complexity | 779171ab88860e13b8c2e85a6c03caf7 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2009 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.mercurial.ui.branch;
  45. import java.awt.EventQueue;
  46. import java.io.File;
  47. import org.netbeans.modules.mercurial.HgException;
  48. import org.netbeans.modules.mercurial.HgProgressSupport;
  49. import org.netbeans.modules.mercurial.Mercurial;
  50. import org.netbeans.modules.mercurial.util.HgUtils;
  51. import org.netbeans.modules.versioning.spi.VCSContext;
  52. import org.netbeans.modules.mercurial.ui.actions.ContextAction;
  53. import org.netbeans.modules.mercurial.ui.commit.CommitAction;
  54. import org.netbeans.modules.mercurial.ui.log.HgLogMessage;
  55. import org.netbeans.modules.mercurial.util.HgCommand;
  56. import org.netbeans.modules.versioning.util.Utils;
  57. import org.openide.util.RequestProcessor;
  58. import org.openide.DialogDisplayer;
  59. import org.openide.NotifyDescriptor;
  60. import org.openide.awt.ActionID;
  61. import org.openide.awt.ActionRegistration;
  62. import org.openide.nodes.AbstractNode;
  63. import org.openide.nodes.Children;
  64. import org.openide.nodes.Node;
  65. import org.openide.util.NbBundle;
  66. import org.openide.util.actions.SystemAction;
  67. import org.openide.util.lookup.Lookups;
  68. @ActionID(id = "org.netbeans.modules.mercurial.ui.branch.CloseBranchAction", category = "Mercurial")
  69. @ActionRegistration(displayName = "#CTL_MenuItem_CloseBranch")
  70. public class CloseBranchAction extends ContextAction {
  71. @Override
  72. protected boolean enable(Node[] nodes) {
  73. return HgUtils.isFromHgRepository(HgUtils.getCurrentContext(nodes));
  74. }
  75. @Override
  76. protected String getBaseName(Node[] nodes) {
  77. return "CTL_MenuItem_CloseBranch"; //NOI18N
  78. }
  79. @Override
  80. protected void performContextAction(Node[] nodes) {
  81. VCSContext ctx = HgUtils.getCurrentContext(nodes);
  82. final File roots[] = HgUtils.getActionRoots(ctx);
  83. if (roots == null || roots.length == 0) return;
  84. final File root = Mercurial.getInstance().getRepositoryRoot(roots[0]);
  85. final RequestProcessor rp = Mercurial.getInstance().getRequestProcessor(root);
  86. HgProgressSupport support = new HgProgressSupport() {
  87. @Override
  88. public void perform() {
  89. setDisplayName(NbBundle.getMessage(CloseBranchAction.class, "MSG_CloseBranch.Progress.preparing")); //NOI18N
  90. try {
  91. final String branchName = HgCommand.getBranch(root);
  92. if (branchName.equals(HgBranch.DEFAULT_NAME)) {
  93. NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(CloseBranchAction.class, "MSG_CloseBranch.error.defaultBranch"), NotifyDescriptor.ERROR_MESSAGE); //NOI18N
  94. DialogDisplayer.getDefault().notifyLater(nd);
  95. return;
  96. }
  97. int numberOfHeads = getBranchHeadCount(branchName);
  98. if (numberOfHeads == 0) {
  99. NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(CloseBranchAction.class, "MSG_CloseBranch.error.branchClosed", //NOI18N
  100. new Object[] { branchName }), NotifyDescriptor.ERROR_MESSAGE);
  101. DialogDisplayer.getDefault().notifyLater(nd);
  102. return;
  103. } else if (numberOfHeads > 1) {
  104. NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(CloseBranchAction.class, "MSG_CloseBranch.error.moreHeads", //NOI18N
  105. new Object[] { branchName, numberOfHeads }), NotifyDescriptor.ERROR_MESSAGE);
  106. DialogDisplayer.getDefault().notifyLater(nd);
  107. return;
  108. } else if (!isCanceled()) {
  109. final VCSContext ctx = VCSContext.forNodes(new Node[] { new AbstractNode(Children.LEAF, Lookups.fixed((Object[]) roots)) {
  110. @Override
  111. public String getDisplayName () {
  112. return root.getName();
  113. }
  114. }});
  115. EventQueue.invokeLater(new Runnable() {
  116. @Override
  117. public void run () {
  118. SystemAction.get(CommitAction.class).closeBranch(branchName, ctx,
  119. NbBundle.getMessage(CloseBranchAction.class, "MSG_CloseBranch.commit.title", new Object[] { branchName, Utils.getContextDisplayName(ctx) })); //NOI18N
  120. }
  121. });
  122. }
  123. } catch (HgException.HgCommandCanceledException ex) {
  124. // canceled by user, do nothing
  125. } catch (HgException ex) {
  126. HgUtils.notifyException(ex);
  127. }
  128. }
  129. private int getBranchHeadCount (String branchName) throws HgException {
  130. HgLogMessage[] messages = HgCommand.getHeadRevisionsInfo(root, false, getLogger());
  131. int headsInBranch = 0;
  132. for (HgLogMessage message : messages) {
  133. if (message.getBranches().length == 0 && branchName.equals(HgBranch.DEFAULT_NAME)) {
  134. ++headsInBranch;
  135. }
  136. for (String b : message.getBranches()) {
  137. if (b.equals(branchName)) {
  138. ++headsInBranch;
  139. }
  140. }
  141. }
  142. return headsInBranch;
  143. }
  144. };
  145. support.start(rp, root, NbBundle.getMessage(CloseBranchAction.class, "MSG_CloseBranch.Progress")); //NOI18N
  146. }
  147. }