PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/org.cfeclipse.cfml/src/org/cfeclipse/cfml/images/AbstractImageEntry.java

https://github.com/lcamilo15/cfeclipse
Java | 161 lines | 142 code | 18 blank | 1 comment | 29 complexity | 037da6263dca5be02deebae78614a62e MD5 | raw file
  1. package org.cfeclipse.cfml.images;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.Enumeration;
  7. import java.util.HashSet;
  8. import java.util.jar.JarEntry;
  9. import java.util.jar.JarFile;
  10. import org.cfeclipse.cfml.views.images.IImageStore;
  11. public abstract class AbstractImageEntry implements IImageStore {
  12. HashSet<IStoreImageListener> images = new HashSet<IStoreImageListener>();
  13. protected ArrayList<Object> imageList = new ArrayList<Object>();
  14. public AbstractImageEntry() {
  15. super();
  16. }
  17. public synchronized Collection<?> getContents() {
  18. return new ArrayList<Object>(this.imageList);
  19. }
  20. public void init() {
  21. final Thread s = new Thread() {
  22. public void run() {
  23. AbstractImageEntry.this.initPlatform();
  24. }
  25. };
  26. s.start();
  27. }
  28. synchronized void reprocess(HashSet<String> str) {
  29. final File file = this.getFile();
  30. final HashSet<ItemGroup> toRemove = new HashSet<ItemGroup>();
  31. final HashSet<ItemGroup> toAdd = new HashSet<ItemGroup>();
  32. for (final Object s : this.imageList) {
  33. final ItemGroup z = (ItemGroup) s;
  34. if (str.contains(z.getName())) {
  35. toRemove.add(z);
  36. str.remove(z.getName());
  37. final ArrayList<IImageEntry> za = new ArrayList<IImageEntry>();
  38. final ItemGroup newF = new ItemGroup(z.getName(), za);
  39. final File file2 = new File(file, z.getName());
  40. this.parse(za, file2, file2);
  41. if (!za.isEmpty()) {
  42. toAdd.add(newF);
  43. }
  44. }
  45. }
  46. for (final String s : str) {
  47. final ArrayList<IImageEntry> za = new ArrayList<IImageEntry>();
  48. final ItemGroup newF = new ItemGroup(s, za);
  49. final File file2 = new File(file, s);
  50. this.parse(za, file2, file2);
  51. if (!za.isEmpty()) {
  52. toAdd.add(newF);
  53. }
  54. }
  55. this.imageList.removeAll(toRemove);
  56. this.imageList.addAll(toAdd);
  57. this.fireChanged();
  58. }
  59. public abstract File getFile();
  60. protected void initPlatform() {
  61. this.imageList.clear();
  62. this.fireChanged();
  63. final File file = this.getFile();
  64. if ((file != null) && file.exists()) {
  65. final File[] listFiles = file.listFiles();
  66. for (final File f : listFiles) {
  67. if (f.getName().charAt(0) == '.') {
  68. continue;
  69. }
  70. // System.out.println(f);
  71. final ArrayList<IImageEntry> z = new ArrayList<IImageEntry>();
  72. final ItemGroup group = new ItemGroup(f.getName(), z);
  73. this.parse(z, f, f);
  74. if (group.getChildCount() > 0) {
  75. this.fetched(group);
  76. }
  77. }
  78. }
  79. this.fireChanged();
  80. }
  81. protected synchronized void fetched(ItemGroup group) {
  82. this.imageList.add(group);
  83. this.fireChanged();
  84. }
  85. protected synchronized void fireChanged() {
  86. for (final IStoreImageListener l : this.images) {
  87. l.platformChanged();
  88. }
  89. }
  90. @SuppressWarnings("unchecked")
  91. protected void parse(ArrayList ls, File f, File root) {
  92. if (f.getName().endsWith(".jar")) {
  93. try {
  94. final JarFile jar = new JarFile(f);
  95. try {
  96. this.process(ls, jar, f);
  97. } finally {
  98. jar.close();
  99. }
  100. } catch (final IOException e) {
  101. }
  102. } else if (f.isDirectory()) {
  103. if (f.getName().charAt(0) != '.') {
  104. final File[] listFiles = f.listFiles();
  105. for (final File fa : listFiles) {
  106. this.parse(ls, fa, root);
  107. }
  108. }
  109. } else if (AbstractImageEntry.isImage(f.getName())) {
  110. final int length = root.getAbsolutePath().length() + 1;
  111. String absolutePath = f.getAbsolutePath();
  112. absolutePath = absolutePath.replace('\\', '/');
  113. ls.add(new FileImageEntry(absolutePath, f.getName(), absolutePath.substring(length)));
  114. }
  115. }
  116. private void process(ArrayList<Object> ls, JarFile jar, File f) {
  117. final Enumeration<JarEntry> entries = jar.entries();
  118. while (entries.hasMoreElements()) {
  119. final JarEntry e = entries.nextElement();
  120. final String name2 = e.getName();
  121. final String name = name2;
  122. if (AbstractImageEntry.isImage(name)) {
  123. String nm = name2;
  124. final int lastIndexOf = nm.lastIndexOf('/');
  125. if (lastIndexOf > -1) {
  126. nm = nm.substring(lastIndexOf + 1);
  127. }
  128. final String substring = nm;
  129. ls.add(new JarImageEntry(f.getAbsolutePath(), name2, substring));
  130. }
  131. }
  132. }
  133. public static boolean isImage(String name) {
  134. return name.endsWith(".gif") || name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".bmp")
  135. || name.endsWith(".jpeg") || name.endsWith(".tiff");
  136. }
  137. public synchronized void addListener(IStoreImageListener e) {
  138. this.images.add(e);
  139. }
  140. public synchronized void removeListener(IStoreImageListener e) {
  141. this.images.remove(e);
  142. }
  143. }