PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/ui/diff/ImportDiffAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 247 lines | 179 code | 16 blank | 52 comment | 23 complexity | a76bb1b3dde0265fe7d3b6b9c23219f7 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.diff;
  45. import org.netbeans.modules.versioning.spi.VCSContext;
  46. import java.awt.event.ActionEvent;
  47. import java.io.File;
  48. import java.util.List;
  49. import org.netbeans.modules.mercurial.HgException;
  50. import org.netbeans.modules.mercurial.HgProgressSupport;
  51. import org.netbeans.modules.mercurial.Mercurial;
  52. import org.netbeans.modules.mercurial.OutputLogger;
  53. import org.netbeans.modules.mercurial.HgModuleConfig;
  54. import org.netbeans.modules.mercurial.util.HgUtils;
  55. import org.netbeans.modules.mercurial.util.HgCommand;
  56. import org.netbeans.modules.mercurial.ui.actions.ContextAction;
  57. import org.openide.util.NbBundle;
  58. import org.openide.util.RequestProcessor;
  59. import org.openide.DialogDisplayer;
  60. import org.openide.DialogDescriptor;
  61. import org.openide.NotifyDescriptor;
  62. import java.awt.event.ActionListener;
  63. import java.awt.Dialog;
  64. import java.util.Collection;
  65. import java.util.Map;
  66. import javax.swing.BoxLayout;
  67. import javax.swing.ButtonGroup;
  68. import javax.swing.JFileChooser;
  69. import javax.swing.JPanel;
  70. import javax.swing.JRadioButton;
  71. import javax.swing.filechooser.FileFilter;
  72. import org.netbeans.modules.mercurial.ui.log.HgLogMessage;
  73. import org.netbeans.modules.mercurial.ui.merge.MergeAction;
  74. import org.netbeans.modules.versioning.util.AccessibleJFileChooser;
  75. import org.openide.nodes.Node;
  76. /**
  77. * ImportDiff action for mercurial:
  78. * hg export
  79. *
  80. * @author Padraig O'Briain
  81. */
  82. public class ImportDiffAction extends ContextAction {
  83. @Override
  84. protected boolean enable(Node[] nodes) {
  85. return HgUtils.isFromHgRepository(HgUtils.getCurrentContext(nodes));
  86. }
  87. @Override
  88. protected String getBaseName(Node[] nodes) {
  89. return "CTL_MenuItem_ImportDiff"; // NOI18N
  90. }
  91. @Override
  92. protected void performContextAction(Node[] nodes) {
  93. VCSContext context = HgUtils.getCurrentContext(nodes);
  94. importDiff(context);
  95. }
  96. private static void importDiff(VCSContext ctx) {
  97. final File roots[] = HgUtils.getActionRoots(ctx);
  98. if (roots == null || roots.length == 0) return;
  99. final File root = Mercurial.getInstance().getRepositoryRoot(roots[0]);
  100. final JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(ImportDiffAction.class, "ACSD_ImportBrowseFolder"), null); // NO I18N
  101. fileChooser.setDialogTitle(NbBundle.getMessage(ImportDiffAction.class, "ImportBrowse_title")); // NO I18N
  102. fileChooser.setMultiSelectionEnabled(false);
  103. fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
  104. fileChooser.setApproveButtonMnemonic(NbBundle.getMessage(ImportDiffAction.class, "Import").charAt(0)); // NO I18N
  105. fileChooser.setApproveButtonText(NbBundle.getMessage(ImportDiffAction.class, "Import")); // NO I18N
  106. fileChooser.setCurrentDirectory(new File(HgModuleConfig.getDefault().getImportFolder()));
  107. JPanel panel = new JPanel();
  108. final JRadioButton asPatch = new JRadioButton(NbBundle.getMessage(ImportDiffAction.class, "CTL_Import_PatchOption")); //NOI18N
  109. org.openide.awt.Mnemonics.setLocalizedText(asPatch, asPatch.getText()); // NOI18N
  110. final JRadioButton asBundle = new JRadioButton(NbBundle.getMessage(ImportDiffAction.class, "CTL_Import_BundleOption")); //NOI18N
  111. org.openide.awt.Mnemonics.setLocalizedText(asBundle, asBundle.getText()); // NOI18N
  112. ButtonGroup buttonGroup = new ButtonGroup();
  113. buttonGroup.add(asBundle);
  114. buttonGroup.add(asPatch);
  115. asPatch.setSelected(true);
  116. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  117. panel.add(asPatch);
  118. panel.add(asBundle);
  119. fileChooser.setAccessory(panel);
  120. DialogDescriptor dd = new DialogDescriptor(fileChooser, NbBundle.getMessage(ImportDiffAction.class, "ImportBrowse_title")); // NO I18N
  121. dd.setOptions(new Object[0]);
  122. final Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
  123. fileChooser.addActionListener(new ActionListener() {
  124. @Override
  125. public void actionPerformed(ActionEvent e) {
  126. String state = e.getActionCommand();
  127. if (state.equals(JFileChooser.APPROVE_SELECTION)) {
  128. final File patchFile = fileChooser.getSelectedFile();
  129. HgModuleConfig.getDefault().setImportFolder(patchFile.getParent());
  130. if (patchFile != null) {
  131. RequestProcessor rp = Mercurial.getInstance().getRequestProcessor(root);
  132. HgProgressSupport support = new HgProgressSupport() {
  133. @Override
  134. public void perform() {
  135. OutputLogger logger = getLogger();
  136. if (asBundle.isSelected()) {
  137. performUnbundle(root, patchFile);
  138. } else if (asPatch.isSelected()) {
  139. performImport(root, patchFile, logger);
  140. }
  141. }
  142. private void performUnbundle(File root, File bundleFile) {
  143. OutputLogger logger = getLogger();
  144. try {
  145. logger.outputInRed(NbBundle.getMessage(ImportDiffAction.class, "MSG_UNBUNDLE_TITLE")); // NOI18N
  146. logger.outputInRed(NbBundle.getMessage(ImportDiffAction.class, "MSG_UNBUNDLE_TITLE_SEP")); // NOI18N
  147. List<String> list = HgCommand.doUnbundle(root, bundleFile, logger);
  148. if (list != null && !list.isEmpty()) {
  149. List<String> updatedFilesList = list;
  150. logger.output(HgUtils.replaceHttpPassword(list));
  151. // Handle Merge - both automatic and merge with conflicts
  152. boolean bMergeNeededDueToPull = HgCommand.isMergeNeededMsg(list.get(list.size() - 1));
  153. boolean bConfirmMerge = false;
  154. boolean warnMoreHeads = true;
  155. if (bMergeNeededDueToPull) {
  156. bConfirmMerge = HgUtils.confirmDialog(
  157. ImportDiffAction.class, "MSG_UNBUNDLE_MERGE_CONFIRM_TITLE", "MSG_UNBUNDLE_MERGE_CONFIRM_QUERY"); // NOI18N
  158. warnMoreHeads = false;
  159. } else {
  160. boolean bOutStandingUncommittedMerges = HgCommand.isMergeAbortUncommittedMsg(list.get(list.size() - 1));
  161. if (bOutStandingUncommittedMerges) {
  162. bConfirmMerge = HgUtils.confirmDialog(
  163. ImportDiffAction.class, "MSG_UNBUNDLE_MERGE_CONFIRM_TITLE", "MSG_UNBUNDLE_MERGE_UNCOMMITTED_CONFIRM_QUERY"); // NOI18N
  164. }
  165. }
  166. if (bConfirmMerge) {
  167. logger.output(""); // NOI18N
  168. logger.outputInRed(NbBundle.getMessage(ImportDiffAction.class, "MSG_UNBUNDLE_MERGE_DO")); // NOI18N
  169. updatedFilesList = MergeAction.doMergeAction(root, null, logger);
  170. } else {
  171. HgLogMessage[] heads = HgCommand.getHeadRevisionsInfo(root, true, OutputLogger.getLogger(null));
  172. Map<String, Collection<HgLogMessage>> branchHeads = HgUtils.sortByBranch(heads);
  173. if (!branchHeads.isEmpty()) {
  174. MergeAction.displayMergeWarning(branchHeads, logger, warnMoreHeads);
  175. }
  176. }
  177. boolean fileUpdated = isUpdated(updatedFilesList);
  178. if (fileUpdated) {
  179. HgUtils.notifyUpdatedFiles(root, updatedFilesList);
  180. HgUtils.forceStatusRefresh(root);
  181. }
  182. }
  183. } catch (HgException.HgCommandCanceledException ex) {
  184. // canceled by user, do nothing
  185. } catch (HgException ex) {
  186. HgUtils.notifyException(ex);
  187. } finally {
  188. logger.outputInRed(NbBundle.getMessage(ImportDiffAction.class, "MSG_UNBUNDLE_DONE")); // NOI18N
  189. logger.output(""); // NOI18N
  190. }
  191. }
  192. };
  193. support.start(rp, root, org.openide.util.NbBundle.getMessage(ImportDiffAction.class, "LBL_ImportDiff_Progress")); // NOI18N
  194. }
  195. }
  196. dialog.dispose();
  197. }
  198. });
  199. dialog.setVisible(true);
  200. }
  201. private static boolean isUpdated (List<String> list) {
  202. boolean updated = false;
  203. for (String s : list) {
  204. if (s.contains("getting ") || s.startsWith("merging ")) { //NOI18N
  205. updated = true;
  206. break;
  207. }
  208. }
  209. return updated;
  210. }
  211. private static void performImport(final File repository, File patchFile, OutputLogger logger) {
  212. try {
  213. logger.outputInRed(
  214. NbBundle.getMessage(ImportDiffAction.class,
  215. "MSG_IMPORT_TITLE")); // NOI18N
  216. logger.outputInRed(
  217. NbBundle.getMessage(ImportDiffAction.class,
  218. "MSG_IMPORT_TITLE_SEP")); // NOI18N
  219. List<String> list = HgCommand.doImport(repository, patchFile, logger);
  220. Mercurial.getInstance().changesetChanged(repository);
  221. logger.output(list); // NOI18N
  222. } catch (HgException.HgCommandCanceledException ex) {
  223. // canceled by user, do nothing
  224. } catch (HgException ex) {
  225. HgUtils.notifyException(ex);
  226. } finally {
  227. logger.outputInRed(NbBundle.getMessage(ImportDiffAction.class, "MSG_IMPORT_DONE")); // NOI18N
  228. logger.output(""); // NOI18N
  229. }
  230. }
  231. }