PageRenderTime 44ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/webswing-app-toolkit/src/main/java/org/webswing/toolkit/extra/WebShellFolderManager.java

https://bitbucket.org/meszarv/webswing
Java | 197 lines | 182 code | 15 blank | 0 comment | 29 complexity | 7615c4cb6c6328a48131c09a2c6508f4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package org.webswing.toolkit.extra;
  2. import org.webswing.Constants;
  3. import sun.awt.shell.ShellFolder;
  4. import sun.awt.shell.ShellFolder.Invoker;
  5. import sun.awt.shell.Win32ShellFolderManager2;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.lang.reflect.Constructor;
  10. import java.lang.reflect.Method;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.List;
  15. @SuppressWarnings("restriction")
  16. public class WebShellFolderManager extends Win32ShellFolderManager2 {
  17. private boolean windows;
  18. private Object defaultManager;
  19. private File root;
  20. private List<File> roots = new ArrayList<File>();
  21. public WebShellFolderManager() {
  22. String path = System.getProperty(Constants.SWING_START_SYS_PROP_TRANSFER_DIR, System.getProperty("user.dir") + "/upload");
  23. String[] paths = path.split(File.pathSeparator);
  24. for (int i = 0; i < paths.length; i++) {
  25. File root = new IsolatedRootFile(paths[i]);
  26. if (!root.getAbsoluteFile().exists()) {
  27. root.mkdirs();
  28. }
  29. roots.add(root);
  30. if (i == 0) {
  31. this.root = root;
  32. System.setProperty("user.home", root.getAbsolutePath());
  33. }
  34. }
  35. windows = System.getProperty("os.name", "").startsWith("Windows");
  36. try {
  37. Class<?> managerClass = ClassLoader.getSystemClassLoader().loadClass("sun.awt.shell.ShellFolderManager");
  38. Constructor<?> c = managerClass.getDeclaredConstructor();
  39. c.setAccessible(true);
  40. defaultManager = c.newInstance();
  41. } catch (Exception e) {
  42. System.err.println("Error while instantiating default shell folder manager. " + e.getMessage());
  43. e.printStackTrace();
  44. }
  45. }
  46. @Override
  47. public Object get(String paramString) {
  48. if (paramString.equals("fileChooserDefaultFolder")) {
  49. return root;
  50. }
  51. if (paramString.equals("roots")) {
  52. return roots.toArray(new File[roots.size()]);
  53. }
  54. if (paramString.equals("fileChooserComboBoxFolders")) {
  55. return roots.toArray(new File[roots.size()]);
  56. }
  57. if (paramString.equals("fileChooserShortcutPanelFolders")) {
  58. return roots.toArray(new File[roots.size()]);
  59. }
  60. if (paramString.startsWith("fileChooserIcon ") || paramString.startsWith("optionPaneIcon ") || paramString.startsWith("shell32Icon ")) {
  61. return super.get(paramString);
  62. }
  63. return null;
  64. }
  65. @Override
  66. public ShellFolder createShellFolder(File paramFile) throws FileNotFoundException {
  67. try {
  68. if (isSubfolderOfRoots(paramFile)) {
  69. if (windows) {
  70. return super.createShellFolder(paramFile);
  71. } else {
  72. try {
  73. Method m = defaultManager.getClass().getDeclaredMethod("createShellFolder", File.class);
  74. m.setAccessible(true);
  75. return (ShellFolder) m.invoke(defaultManager, paramFile);
  76. } catch (Exception e) {
  77. System.err.println("Failed to invoke createShellFolder method on default shell folder manager: " + e.getMessage());
  78. e.printStackTrace();
  79. return null;
  80. }
  81. }
  82. } else {
  83. throw new FileNotFoundException("Path is outside the allowed Webswing Filesystem isolation folder. (" + paramFile.getCanonicalPath() + ")");
  84. }
  85. } catch (IOException e) {
  86. System.err.println("Error while creating ShellFolder. " + e.getMessage());
  87. if (e instanceof FileNotFoundException) {
  88. throw (FileNotFoundException) e;
  89. } else {
  90. throw new FileNotFoundException("Error while creating ShellFolder. " + e.getMessage());
  91. }
  92. }
  93. }
  94. private boolean isSubfolderOfRoots(File paramFile) throws IOException {
  95. String cp = paramFile.getCanonicalPath();
  96. for (File root : roots) {
  97. if (cp.startsWith(root.getCanonicalPath())) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. @Override
  104. protected Invoker createInvoker() {
  105. if (windows) {
  106. return super.createInvoker();
  107. } else {
  108. try {
  109. Method m = defaultManager.getClass().getDeclaredMethod("createInvoker");
  110. m.setAccessible(true);
  111. return (Invoker) m.invoke(defaultManager);
  112. } catch (Exception e) {
  113. System.err.println("Failed to invoke createInvoker method on default shell folder manager: " + e.getMessage());
  114. e.printStackTrace();
  115. return null;
  116. }
  117. }
  118. }
  119. @Override
  120. public boolean isComputerNode(File paramFile) {
  121. if (windows) {
  122. return super.isComputerNode(paramFile);
  123. } else {
  124. try {
  125. Method m = defaultManager.getClass().getDeclaredMethod("isComputerNode", File.class);
  126. m.setAccessible(true);
  127. return (Boolean) m.invoke(defaultManager, paramFile);
  128. } catch (Exception e) {
  129. System.err.println("Failed to invoke isComputerNode method on default shell folder manager: " + e.getMessage());
  130. e.printStackTrace();
  131. return false;
  132. }
  133. }
  134. }
  135. @Override
  136. public boolean isFileSystemRoot(File paramFile) {
  137. try {
  138. for (File root : roots) {
  139. if (root.getCanonicalPath().equals(paramFile.getCanonicalPath())) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. } catch (IOException e1) {
  145. if (windows) {
  146. return super.isFileSystemRoot(paramFile);
  147. } else {
  148. try {
  149. Method m = defaultManager.getClass().getDeclaredMethod("isFileSystemRoot", File.class);
  150. m.setAccessible(true);
  151. return (Boolean) m.invoke(defaultManager, paramFile);
  152. } catch (Exception e) {
  153. System.err.println("Failed to invoke isFileSystemRoot method on default shell folder manager: " + e.getMessage());
  154. e.printStackTrace();
  155. return false;
  156. }
  157. }
  158. }
  159. }
  160. @SuppressWarnings("rawtypes")
  161. public void sortFiles(List paramList) {
  162. if (windows) {
  163. try {
  164. Method m = super.getClass().getDeclaredMethod("sortFiles", List.class);
  165. m.setAccessible(true);
  166. m.invoke(defaultManager, paramList);
  167. } catch (Exception e) {
  168. System.err.println("Failed to invoke sortFiles method on default shell folder manager: " + e.getMessage());
  169. e.printStackTrace();
  170. }
  171. } else {
  172. try {
  173. Method m = defaultManager.getClass().getDeclaredMethod("sortFiles", List.class);
  174. m.setAccessible(true);
  175. m.invoke(defaultManager, paramList);
  176. } catch (Exception e) {
  177. System.err.println("Failed to invoke sortFiles method on default shell folder manager: " + e.getMessage());
  178. e.printStackTrace();
  179. }
  180. }
  181. }
  182. }