PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 297 lines | 157 code | 41 blank | 99 comment | 40 complexity | 749eab556302fe5a08838f32bd3d2f52 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.api.diff.StreamSource;
  46. import org.netbeans.api.diff.DiffController;
  47. import org.netbeans.modules.mercurial.Mercurial;
  48. import org.netbeans.modules.mercurial.FileInformation;
  49. import org.openide.util.NbBundle;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.util.*;
  53. import java.text.MessageFormat;
  54. import org.netbeans.modules.mercurial.FileStatus;
  55. import org.netbeans.modules.mercurial.ui.log.HgLogMessage.HgRevision;
  56. import org.netbeans.modules.versioning.diff.AbstractDiffSetup;
  57. /**
  58. * Represents on DIFF setup.
  59. *
  60. * @author Maros Sandor
  61. */
  62. public final class Setup extends AbstractDiffSetup {
  63. /**
  64. * What was locally changed? The right pane contains local file.
  65. *
  66. * <p>Local addition, removal or change is displayed in
  67. * the right pane as addition, removal or change respectively
  68. * (i.e. not reversed as removal, addition or change).
  69. *
  70. * <pre>
  71. * diff from-BASE to-LOCAL
  72. * </pre>
  73. */
  74. public static final int DIFFTYPE_LOCAL = 0;
  75. /**
  76. * What was remotely changed? The right pane contains remote file.
  77. *
  78. * <p>Remote addition, removal or change is displayed in
  79. * the right pane as addition, removal or change respectively
  80. * (i.e. not reversed as removal, addition or change).
  81. *
  82. * <pre>
  83. * diff from-BASE to-HEAD
  84. * </pre>
  85. */
  86. public static final int DIFFTYPE_REMOTE = 1;
  87. /**
  88. * What was locally changed comparing to recent head?
  89. * The Right pane contains local file.
  90. *
  91. * <p> Local addition, removal or change is displayed in
  92. * the right pane as addition, removal or change respectively
  93. * (i.e. not reversed as removal, addition or change).
  94. *
  95. * <pre>
  96. * diff from-HEAD to-LOCAL
  97. * </pre>
  98. */
  99. public static final int DIFFTYPE_ALL = 2;
  100. private final File baseFile;
  101. /**
  102. * Name of the file's property if the setup represents a property diff setup, null otherwise.
  103. */
  104. private final String propertyName;
  105. private final HgRevision firstRevision;
  106. private final HgRevision secondRevision;
  107. private final FileInformation info;
  108. private DiffStreamSource firstSource;
  109. private DiffStreamSource secondSource;
  110. private DiffController view;
  111. private DiffNode node;
  112. private String title;
  113. public Setup(File baseFile, String propertyName, int type) {
  114. this.baseFile = baseFile;
  115. this.propertyName = propertyName;
  116. info = Mercurial.getInstance().getFileStatusCache().getStatus(baseFile);
  117. int status = info.getStatus();
  118. FileStatus fileStatus = info.getStatus(null);
  119. ResourceBundle loc = NbBundle.getBundle(Setup.class);
  120. String firstTitle;
  121. String secondTitle;
  122. // the first source
  123. switch (type) {
  124. case DIFFTYPE_LOCAL:
  125. // from-BASE
  126. if (match(status, FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY
  127. | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY)) {
  128. firstRevision = HgRevision.BASE;
  129. File originalFile = null;
  130. if (fileStatus != null && fileStatus.isCopied()) {
  131. originalFile = fileStatus.getOriginalFile();
  132. }
  133. firstTitle = MessageFormat.format(loc.getString("MSG_DiffPanel_BaseRevision"), new Object [] { //NOI18N
  134. originalFile == null ? firstRevision.getRevisionNumber() : originalFile.getName() });
  135. } else if (match (status, FileInformation.STATUS_VERSIONED_NEWINREPOSITORY)) {
  136. firstRevision = null;
  137. firstTitle = NbBundle.getMessage(Setup.class, "LBL_Diff_NoLocalFile"); // NOI18N
  138. } else if (match(status, FileInformation.STATUS_VERSIONED_DELETEDLOCALLY
  139. | FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY)) {
  140. firstRevision = HgRevision.BASE;
  141. firstTitle = MessageFormat.format(loc.getString("MSG_DiffPanel_BaseRevision"), new Object [] { firstRevision.getRevisionNumber() }); // NOI18N
  142. } else {
  143. firstRevision = HgRevision.BASE;
  144. firstTitle = MessageFormat.format(loc.getString("MSG_DiffPanel_BaseRevision"), new Object [] { firstRevision.getRevisionNumber() }); // NOI18N
  145. }
  146. break;
  147. default:
  148. throw new IllegalArgumentException("Unknow diff type: " + type); // NOI18N
  149. }
  150. // the second source
  151. switch (type) {
  152. case DIFFTYPE_LOCAL:
  153. // to-LOCAL
  154. if (match(status, FileInformation.STATUS_VERSIONED_CONFLICT)) {
  155. secondRevision = HgRevision.CURRENT;
  156. secondTitle = MessageFormat.format(loc.getString("MSG_DiffPanel_LocalConflict"), new Object [] { secondRevision.getRevisionNumber() }); // NOI18N
  157. } else if (match(status, FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY
  158. | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY)) {
  159. secondRevision = HgRevision.CURRENT;
  160. if (fileStatus != null && fileStatus.isCopied()) {
  161. if (fileStatus.getOriginalFile() != null && !fileStatus.getOriginalFile().exists()) {
  162. secondTitle = loc.getString("MSG_DiffPanel_LocalRenamed"); // NOI18N
  163. } else {
  164. secondTitle = loc.getString("MSG_DiffPanel_LocalCopied"); // NOI18N
  165. }
  166. } else {
  167. secondTitle = loc.getString("MSG_DiffPanel_LocalNew"); // NOI18N
  168. }
  169. } else if (match (status, FileInformation.STATUS_VERSIONED_NEWINREPOSITORY)) {
  170. secondRevision = null;
  171. secondTitle = NbBundle.getMessage(Setup.class, "LBL_Diff_NoLocalFile"); // NOI18N
  172. } else if (match(status, FileInformation.STATUS_VERSIONED_DELETEDLOCALLY
  173. | FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY)) {
  174. secondRevision = null;
  175. secondTitle = loc.getString("MSG_DiffPanel_LocalDeleted"); // NOI18N
  176. } else {
  177. secondRevision = HgRevision.CURRENT;
  178. secondTitle = MessageFormat.format(loc.getString("MSG_DiffPanel_LocalModified"), new Object [] { secondRevision.getRevisionNumber() }); // NOI18N
  179. }
  180. break;
  181. default:
  182. throw new IllegalArgumentException("Unknow diff type: " + type); // NOI18N
  183. }
  184. firstSource = new DiffStreamSource(baseFile, firstRevision, firstTitle);
  185. secondSource = new DiffStreamSource(baseFile, secondRevision, secondTitle);
  186. title = "<html>" + Mercurial.getInstance().getMercurialAnnotator().annotateNameHtml(baseFile, info); // NOI18N
  187. }
  188. /**
  189. * Text file setup for arbitrary revisions.
  190. * @param firstRevision first revision or <code>null</code> for inital.
  191. * @param secondRevision second revision
  192. */
  193. public Setup(File baseFile, HgRevision firstRevision, HgRevision secondRevision, FileInformation info, final boolean forceNonEditable) {
  194. this.baseFile = baseFile;
  195. this.propertyName = null;
  196. this.firstRevision = firstRevision;
  197. this.secondRevision = secondRevision;
  198. this.info = info;
  199. File firstSourceBaseFile = baseFile;
  200. if (info != null && info.getStatus(null) != null && info.getStatus(null).getOriginalFile() != null) {
  201. firstSourceBaseFile = info.getStatus(null).getOriginalFile();
  202. }
  203. firstSource = new DiffStreamSource(firstSourceBaseFile, firstRevision, firstRevision.getRevisionNumber());
  204. // XXX delete when UndoAction works correctly
  205. secondSource = new DiffStreamSource(baseFile, secondRevision, secondRevision.getRevisionNumber()) {
  206. @Override
  207. public boolean isEditable() {
  208. return !forceNonEditable && super.isEditable();
  209. }
  210. };
  211. }
  212. public String getPropertyName() {
  213. return propertyName;
  214. }
  215. public File getBaseFile() {
  216. return baseFile;
  217. }
  218. public FileInformation getInfo() {
  219. return info;
  220. }
  221. public void setView(DiffController view) {
  222. this.view = view;
  223. }
  224. public DiffController getView() {
  225. return view;
  226. }
  227. @Override
  228. public StreamSource getFirstSource() {
  229. return firstSource;
  230. }
  231. @Override
  232. public StreamSource getSecondSource() {
  233. return secondSource;
  234. }
  235. void setNode(DiffNode node) {
  236. this.node = node;
  237. }
  238. DiffNode getNode() {
  239. return node;
  240. }
  241. @Override
  242. public String toString() {
  243. return title;
  244. }
  245. /**
  246. * Loads data
  247. * @param group that carries shared state. Note that this group must not be executed later on.
  248. */
  249. void initSources() throws IOException {
  250. if (firstSource != null) firstSource.init();
  251. if (secondSource != null) secondSource.init();
  252. }
  253. private static boolean match(int status, int mask) {
  254. return (status & mask) != 0;
  255. }
  256. }