PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/plugin/src/com/vectrace/MercurialEclipse/model/HgPath.java

https://bitbucket.org/mercurialeclipse/main/
Java | 211 lines | 141 code | 21 blank | 49 comment | 33 complexity | 3c9f5ca5628dae5cf4464bc1c1f82d96 MD5 | raw file
Possible License(s): EPL-1.0
  1. /*******************************************************************************
  2. * Copyright (c) 2010 Andrei Loskutov.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Andrei Loskutov - implementation
  10. *******************************************************************************/
  11. package com.vectrace.MercurialEclipse.model;
  12. import java.io.File;
  13. import java.io.FileFilter;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import org.eclipse.core.resources.IFile;
  18. import org.eclipse.core.resources.IProject;
  19. import org.eclipse.core.resources.IResource;
  20. import org.eclipse.core.runtime.IAdaptable;
  21. import org.eclipse.core.runtime.IPath;
  22. import org.eclipse.core.runtime.Path;
  23. import org.eclipse.jface.resource.ImageDescriptor;
  24. import org.eclipse.ui.ISharedImages;
  25. import org.eclipse.ui.PlatformUI;
  26. import org.eclipse.ui.model.IWorkbenchAdapter;
  27. import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
  28. import com.vectrace.MercurialEclipse.exception.HgException;
  29. import com.vectrace.MercurialEclipse.utils.ResourceUtils;
  30. /**
  31. * A virtual (but always canonical) path object which can be used to show plain files and
  32. * directories together with those known by Eclipse workspace.
  33. *
  34. * @author Andrei
  35. */
  36. public class HgPath extends File implements IWorkbenchAdapter, IAdaptable {
  37. private final Path path;
  38. public HgPath(String pathname) throws IOException {
  39. this(new File(pathname));
  40. }
  41. public HgPath(File file) throws IOException {
  42. super(file.getCanonicalPath());
  43. path = new Path(getAbsolutePath());
  44. }
  45. public Object[] getChildren(Object o) {
  46. if (isFile()) {
  47. return new Object[0];
  48. }
  49. File[] files = listFiles();
  50. if (files == null) {
  51. return new Object[0];
  52. }
  53. List<Object> children = new ArrayList<Object>();
  54. for (File file : files) {
  55. IResource workspaceHandle;
  56. try {
  57. workspaceHandle = ResourceUtils.convert(file);
  58. if (workspaceHandle != null && workspaceHandle.exists()) {
  59. children.add(workspaceHandle);
  60. } else {
  61. if (isHgRoot(file)) {
  62. children.add(HgRoot.get(file));
  63. } else {
  64. if (!".hg".equals(file.getName())) {
  65. children.add(new HgPath(file));
  66. }
  67. }
  68. }
  69. } catch (Exception e) {
  70. MercurialEclipsePlugin.logError(e);
  71. continue;
  72. }
  73. }
  74. return children.toArray();
  75. }
  76. public ImageDescriptor getImageDescriptor(Object object) {
  77. if (isFile()) {
  78. return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
  79. ISharedImages.IMG_OBJ_FILE);
  80. }
  81. return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
  82. ISharedImages.IMG_OBJ_FOLDER);
  83. }
  84. public String getLabel(Object o) {
  85. return getAbsolutePath();
  86. }
  87. public Object getParent(Object o) {
  88. try {
  89. File parentFile = getParentFile();
  90. if(parentFile != null) {
  91. return new HgPath(parentFile);
  92. }
  93. } catch (IOException e) {
  94. MercurialEclipsePlugin.logError(e);
  95. }
  96. return null;
  97. }
  98. public Object getAdapter(Class adapter) {
  99. if (adapter == IWorkbenchAdapter.class) {
  100. return this;
  101. }
  102. if (adapter == IResource.class || adapter == IFile.class || adapter == IProject.class) {
  103. try {
  104. IResource resource = ResourceUtils.convert(this);
  105. if (adapter == IFile.class && !(resource instanceof IFile)) {
  106. // do NOT return folder objects for iFile!
  107. return null;
  108. }
  109. return resource;
  110. } catch (HgException e) {
  111. MercurialEclipsePlugin.logError(e);
  112. }
  113. }
  114. return null;
  115. }
  116. /**
  117. * @return the {@link IPath} object corresponding to this root, never null
  118. */
  119. public IPath getIPath() {
  120. return path;
  121. }
  122. public IPath toAbsolute(IPath relative) {
  123. return path.append(relative);
  124. }
  125. public IPath toAbsolute(String relative) {
  126. return path.append(relative);
  127. }
  128. /**
  129. * Converts given path to the relative
  130. *
  131. * @param child
  132. * a possible child path, non null
  133. * @return a hg root relative path of a given file, if the given file is located under this
  134. * root, otherwise the path of a given file. If the given path matches the root,
  135. * returns an empty string
  136. */
  137. public String toRelative(File child) {
  138. return ResourceUtils.toRelative(this, child);
  139. }
  140. /**
  141. * Converts given path to the relative
  142. *
  143. * @param child
  144. * a possible child path, non null
  145. * @return a hg root relative path of a given file, if the given file is located under this
  146. * root, otherwise the path of a given file. If the given path matches the root,
  147. * returns an empty string
  148. */
  149. public IPath toRelative(IPath child) {
  150. return child.makeRelativeTo(getIPath());
  151. }
  152. /**
  153. * Converts given file to the relative path (if the file exists)
  154. *
  155. * @param child
  156. * a possible child path, non null
  157. * @return a hg root relative path of a given file, if the given file is located under this
  158. * root, otherwise the path of a given file. If the given path matches the root, returns
  159. * an empty path. If the file location can not be computed, returns null.
  160. * @see IResource#getLocation()
  161. * @see ResourceUtils#getPath(IResource)
  162. */
  163. public IPath toRelative(IFile file) {
  164. IPath location = ResourceUtils.getPath(file);
  165. if(location.isEmpty()) {
  166. return null;
  167. }
  168. return new Path(toRelative(location.toFile()));
  169. }
  170. public static boolean isHgRoot(File path) {
  171. if (path == null || !path.isDirectory()) {
  172. return false;
  173. }
  174. FileFilter hg = new FileFilter() {
  175. public boolean accept(File path1) {
  176. return path1.getName().equalsIgnoreCase(".hg") && path1.isDirectory(); //$NON-NLS-1$
  177. }
  178. };
  179. File[] rootContent = path.listFiles(hg);
  180. return rootContent != null && rootContent.length == 1;
  181. }
  182. @Override
  183. public boolean equals(Object obj) {
  184. return super.equals(obj);
  185. }
  186. @Override
  187. public int hashCode() {
  188. return super.hashCode();
  189. }
  190. }