PageRenderTime 48ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/mercurial/src/org/netbeans/modules/mercurial/ui/log/LogAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 189 lines | 123 code | 12 blank | 54 comment | 26 complexity | 9c1e189d9a87f582367044ee86fbe6c6 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-2006 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.log;
  45. import java.awt.EventQueue;
  46. import org.netbeans.modules.versioning.spi.VCSContext;
  47. import java.io.File;
  48. import java.util.ArrayList;
  49. import java.util.LinkedHashSet;
  50. import java.util.List;
  51. import java.util.Set;
  52. import org.netbeans.modules.mercurial.FileStatus;
  53. import org.netbeans.modules.mercurial.FileStatusCache;
  54. import org.netbeans.modules.mercurial.Mercurial;
  55. import org.netbeans.modules.mercurial.WorkingCopyInfo;
  56. import org.netbeans.modules.mercurial.ui.branch.HgBranch;
  57. import org.netbeans.modules.mercurial.util.HgUtils;
  58. import org.netbeans.modules.versioning.util.Utils;
  59. import org.openide.filesystems.FileObject;
  60. import org.openide.filesystems.FileUtil;
  61. import org.openide.loaders.DataObject;
  62. import org.openide.loaders.DataObjectNotFoundException;
  63. import org.openide.nodes.Node;
  64. import org.openide.util.Exceptions;
  65. import org.openide.util.NbBundle;
  66. /**
  67. * Log action for mercurial:
  68. * hg log - show revision history of entire repository or files
  69. *
  70. * @author John Rice
  71. */
  72. public class LogAction extends SearchHistoryAction {
  73. @Override
  74. protected String getBaseName(Node[] nodes) {
  75. return "CTL_MenuItem_Log"; //NOI18N
  76. }
  77. @Override
  78. protected void performContextAction(Node[] nodes) {
  79. VCSContext context = HgUtils.getCurrentContext(nodes);
  80. openHistory(context, NbBundle.getMessage(LogAction.class, "MSG_Log_TabTitle", org.netbeans.modules.versioning.util.Utils.getContextDisplayName(context)));
  81. }
  82. private void openHistory (final VCSContext context, final String title) {
  83. File repositoryRoot = getRepositoryRoot(context);
  84. final File[] files = replaceCopiedFiles(getFiles(context, repositoryRoot));
  85. openHistory(repositoryRoot, files, title);
  86. }
  87. public static void openHistory (File repositoryRoot, File[] files) {
  88. List<Node> nodes = new ArrayList<Node>(files.length);
  89. for (File file : files) {
  90. FileObject fo = FileUtil.toFileObject(file);
  91. if(fo == null) continue;
  92. DataObject dao;
  93. try {
  94. dao = DataObject.find(fo);
  95. } catch (DataObjectNotFoundException ex) {
  96. Exceptions.printStackTrace(ex);
  97. continue;
  98. }
  99. nodes.add(dao.getNodeDelegate());
  100. }
  101. if(nodes.isEmpty()) return;
  102. String title = NbBundle.getMessage(
  103. LogAction.class,
  104. "MSG_Log_TabTitle", // NOI18N
  105. Utils.getContextDisplayName(VCSContext.forNodes(nodes.toArray(new Node[nodes.size()]))));
  106. openHistory(repositoryRoot, files, title);
  107. }
  108. private static void openHistory (final File repositoryRoot, final File[] files, final String title) {
  109. Utils.postParallel(new Runnable() {
  110. @Override
  111. public void run () {
  112. if (files == null) {
  113. return;
  114. }
  115. outputSearchContextTab(repositoryRoot, files, "MSG_Log_Title");
  116. final boolean startSearch = files != null && (files.length == 1 && !files[0].isDirectory() || files.length > 1 && Utils.shareCommonDataObject(files));
  117. final String branchName;
  118. HgLogMessage[] parents = WorkingCopyInfo.getInstance(repositoryRoot).getWorkingCopyParents();
  119. if (parents.length == 1) {
  120. if (parents[0].getBranches().length > 0) {
  121. branchName = parents[0].getBranches()[0];
  122. } else {
  123. branchName = HgBranch.DEFAULT_NAME;
  124. }
  125. } else {
  126. branchName = ""; //NOI18N
  127. }
  128. EventQueue.invokeLater(new Runnable() {
  129. @Override
  130. public void run () {
  131. SearchHistoryTopComponent tc = new SearchHistoryTopComponent(files, branchName);
  132. tc.setDisplayName(title);
  133. tc.open();
  134. tc.requestActive();
  135. if (startSearch) {
  136. tc.search(false);
  137. }
  138. }
  139. });
  140. }
  141. }, 0);
  142. }
  143. private static File[] replaceCopiedFiles (File[] files) {
  144. if (files == null) {
  145. return null;
  146. }
  147. Set<File> originalFiles = new LinkedHashSet<File>(files.length);
  148. FileStatusCache cache = Mercurial.getInstance().getFileStatusCache();
  149. for (File file : files) {
  150. FileStatus st = cache.getStatus(file).getStatus(null);
  151. if (st != null && st.isCopied() && st.getOriginalFile() != null) {
  152. file = st.getOriginalFile();
  153. }
  154. originalFiles.add(file);
  155. }
  156. return originalFiles.toArray(new File[originalFiles.size()]);
  157. }
  158. /**
  159. * Opens search panel with a diff view fixed on a line
  160. * @param file file to search history for
  161. * @param lineNumber number of a line to fix on
  162. */
  163. public static void openSearch(final File file, final int lineNumber) {
  164. SearchHistoryTopComponent tc = new SearchHistoryTopComponent(file, new SearchHistoryTopComponent.DiffResultsViewFactory() {
  165. @Override
  166. DiffResultsView createDiffResultsView(SearchHistoryPanel panel, List<RepositoryRevision> results) {
  167. return new DiffResultsViewForLine(panel, results, lineNumber);
  168. }
  169. });
  170. String tcTitle = NbBundle.getMessage(SearchHistoryAction.class, "CTL_SearchHistory_Title", file.getName()); // NOI18N
  171. tc.setDisplayName(tcTitle);
  172. tc.open();
  173. tc.requestActive();
  174. tc.search(true);
  175. tc.activateDiffView(true);
  176. }
  177. }