/plugins/ProjectViewer/tags/bp_projectviewer_1_0_3/projectviewer/ProjectFile.java

# · Java · 225 lines · 98 code · 33 blank · 94 comment · 9 complexity · 0638f331fcd66dc2a26ac62ce782742d MD5 · raw file

  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. package projectviewer;
  17. import java.awt.datatransfer.*;
  18. import java.io.File;
  19. import java.util.*;
  20. import org.gjt.sp.jedit.*;
  21. /**
  22. * A project file.
  23. */
  24. public class ProjectFile
  25. implements Transferable
  26. {
  27. private final static int KEY_UNSET = -1;
  28. private static Comparator comparator;
  29. private String fullPath;
  30. private String name;
  31. private boolean opened;
  32. private int key;
  33. /**
  34. * Create a new <code>ProjectFile</code>.
  35. */
  36. public ProjectFile( String fullFilePath ) {
  37. this( KEY_UNSET, fullFilePath );
  38. }
  39. /**
  40. * Create a new <code>ProjectFile</code>.
  41. *
  42. * <p>Note: This is a constructor intended to be used internally.</p>
  43. */
  44. ProjectFile( int aKey, String fullFilePath ) {
  45. key = aKey;
  46. fullPath = fullFilePath;
  47. }
  48. /**
  49. * Returns <code>true</code> if project file exists.
  50. */
  51. public boolean exists() {
  52. return toFile().exists();
  53. }
  54. /**
  55. * Returns underlying <code>java.io.File</code>.
  56. */
  57. public File toFile() {
  58. return new File( fullPath );
  59. }
  60. /**
  61. * Returns <code>true</code> if this file is opened.
  62. */
  63. public boolean isOpened() {
  64. return isInBuffer();
  65. }
  66. /**
  67. * Returns the path identifying this file.
  68. */
  69. public String getPath() {
  70. return fullPath;
  71. }
  72. /**
  73. * Returns the name of the file, with the path structure.
  74. */
  75. public String getName() {
  76. if ( name == null )
  77. name = getPath().substring( getPath().lastIndexOf( File.separatorChar ) + 1 );
  78. return name;
  79. }
  80. /**
  81. * Returns the buffer for the given file, or <code>null</code> if
  82. * this file isn't currently in a buffer.
  83. */
  84. public Buffer getBuffer() {
  85. return jEdit.getBuffer( getPath() );
  86. }
  87. /**
  88. * Returns <code>true</code> if this file is currently in a JEdit buffer.
  89. */
  90. public boolean isInBuffer() {
  91. return getBuffer() != null;
  92. }
  93. /**
  94. * Returns <code>true</code> if the given path equals the path of
  95. * this file.
  96. */
  97. public boolean pathEquals( String aPath ) {
  98. return getPath().equals( aPath );
  99. }
  100. /**
  101. * Returns the name of the file.
  102. */
  103. public String toString() {
  104. return getName();
  105. }
  106. /**
  107. * Returns <code>true</code> if the specified this object
  108. * equals <code>obj</code>.
  109. */
  110. public boolean equals( Object obj ) {
  111. if ( super.equals( obj ) ) return true;
  112. if ( !( obj instanceof ProjectFile ) ) return false;
  113. return pathEquals( ((ProjectFile) obj).getPath() );
  114. }
  115. /**
  116. * Returns the hash code.
  117. */
  118. public int hashCode() {
  119. return getPath().hashCode();
  120. }
  121. /**
  122. * Returns a comparator for project files.
  123. */
  124. public static Comparator getComparator() {
  125. if ( comparator == null )
  126. comparator = new FileComparator();
  127. return comparator;
  128. }
  129. /**
  130. * Returns an array of DataFlavor objects indicating the flavors the data
  131. * can be provided in.
  132. */
  133. public DataFlavor[] getTransferDataFlavors() {
  134. return new DataFlavor[] { DataFlavor.javaFileListFlavor };
  135. }
  136. /**
  137. * Returns whether or not the specified data flavor is supported for
  138. * this object.
  139. */
  140. public boolean isDataFlavorSupported(DataFlavor flavor) {
  141. return flavor.equals( DataFlavor.javaFileListFlavor );
  142. }
  143. /**
  144. * Returns an object which represents the data to be transferred.
  145. */
  146. public Object getTransferData( DataFlavor flavor )
  147. throws UnsupportedFlavorException
  148. {
  149. if ( !isDataFlavorSupported( flavor ) )
  150. throw new UnsupportedFlavorException( flavor );
  151. List fileList = new ArrayList(1);
  152. fileList.add( toFile() );
  153. return fileList;
  154. }
  155. /**
  156. * Returns <code>true</code> if the project's key is unset.
  157. */
  158. boolean isKeyUnset() {
  159. return key == KEY_UNSET;
  160. }
  161. /**
  162. * Set the project's key.
  163. */
  164. void setKey( int aKey ) {
  165. key = aKey;
  166. }
  167. /**
  168. * Returns the key.
  169. */
  170. int getKey() {
  171. return key;
  172. }
  173. /**
  174. * Performs a simple cast.
  175. */
  176. private static ProjectFile toProjectFile( Object obj ) {
  177. return (ProjectFile) obj;
  178. }
  179. /**
  180. * A compare class.
  181. */
  182. private static class FileComparator
  183. implements Comparator
  184. {
  185. /**
  186. * Compare the two objects.
  187. */
  188. public int compare( Object obj1, Object obj2 ) {
  189. return toProjectFile( obj1 ).getName().compareTo( toProjectFile( obj2 ).getName() );
  190. }
  191. }
  192. }