Upload/Download of multiple files now possible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / preferences / Util.java
index 417165e..9508175 100644 (file)
@@ -10,6 +10,7 @@ import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IWorkspaceRoot;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.QualifiedName;
@@ -24,12 +25,15 @@ public class Util {
   public static String titleToDB(String in) {
     return in.replaceAll(" ", "_");
   }
+
   public static String db2Title(String in) {
     return in.replaceAll("_", " ");
   }
+
   public static String db2TitleLink(String in) {
-    return "[["+in.replaceAll("_", " ")+"]]";
+    return "[[" + in.replaceAll("_", " ") + "]]";
   }
+
   public static Shell findShell() {
     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
     if (window != null) {
@@ -58,6 +62,12 @@ public class Util {
   }
 
   public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath) {
+    return getHTMLFileName(file,binBasePath,srcBasePath, ".html");
+  }
+  public static String getXMLFileName(IFile file, String binBasePath, String srcBasePath) {
+    return getHTMLFileName(file,binBasePath,srcBasePath, ".xml");
+  }
+  public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath, String fileExtension) {
     StringBuffer htmlBuffer = new StringBuffer();
     String htmlName = null;
     //    String srcBasePath = Util.getWikiTextsPath(file);
@@ -70,7 +80,7 @@ public class Util {
       }
       int index = fileName.lastIndexOf(".wp");
       if (index >= 0) {
-        htmlName = binBasePath + fileName.substring(0, index) + ".html";
+        htmlName = binBasePath + fileName.substring(0, index) + fileExtension;
       } else {
         htmlName = binBasePath + fileName;
       }
@@ -127,12 +137,12 @@ public class Util {
       store.setValue(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
       store.setValue(WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
       store.setValue(WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
-      store.setValue(WikiEditorPlugin.LOCAL_CSS_URL, "file://"+value+"/wpsrc/main.css");
-      store.setValue(WikiEditorPlugin.EXPORT_CSS_URL, "file://"+value+"/wpsrc/main.css");
+      store.setValue(WikiEditorPlugin.LOCAL_CSS_URL, "file://" + value + "/wpsrc/main.css");
+      store.setValue(WikiEditorPlugin.EXPORT_CSS_URL, "file://" + value + "/wpsrc/main.css");
     }
     String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH);
-    if (htmlFolder == null || htmlFolder.equals("")) {  
-      // set a global default 
+    if (htmlFolder == null || htmlFolder.equals("")) {
+      // set a global default
       store.setValue(WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
     }
     setProperty(project, WikiProjectPreferences.PREF_ID, FieldEditorOverlayPage.USEPROJECTSETTINGS, "true");
@@ -140,8 +150,8 @@ public class Util {
     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
-    setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_CSS_URL, "file://"+value+"/wpsrc/main.css");
-    setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_CSS_URL, "file://"+value+"/wpsrc/main.css");
+    setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_CSS_URL, "file://" + value + "/wpsrc/main.css");
+    setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_CSS_URL, "file://" + value + "/wpsrc/main.css");
     //    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
     //    IResource resource = root.findMember(project.getLocation());
     NullProgressMonitor _monitor = new NullProgressMonitor();
@@ -172,28 +182,49 @@ public class Util {
   public static String getLocalTemplate(IResource file) {
     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
   }
+
   public static String getLocalCssUrl(IResource file) {
     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_CSS_URL);
   }
+
   public static String getExportTemplate(IResource file) {
     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
   }
+
   public static String getExportCssUrl(IResource file) {
     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_CSS_URL);
   }
+
   public static String getProjectsWikiTextsPath(IProject project) {
     return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
   }
-  
+
   public static String getWikiFileName(String wikiLink, IFile currentFile, String key) {
     //    String basePath = currentFile.getProject().getLocation().toString();
     String basePath = getWikiTextsPath(currentFile);
     return basePath + "/" + FilterUtil.normalizeWikiLink(wikiLink) + ".wp";
   }
 
-  public static String getFileWikiName(IFile currentFile, String key) {
+  /**
+   * Extract the wiki name from the current filename, replaces '/' with ':' characters, but doesn't replace '_' characters
+   * 
+   * @param currentFile
+   * @return
+   */
+  public static String getFileWikiName(IFile currentFile) {
+    return getFileWikiName(currentFile, false);
+  }
+
+  /**
+   * Extract the wiki name from the current filename, replaces '/' with ':' characters, and '_' with ' ' characters if
+   * <code>replaceUnderscore==true</code>
+   * 
+   * @param currentFile
+   * @param replaceUnderscore
+   * @return
+   */
+  public static String getFileWikiName(IFile currentFile, boolean replaceUnderscore) {
     String filePath = currentFile.getLocation().toString();
-    //    String basePath = currentFile.getProject().getLocation().toString();
     String basePath = getWikiTextsPath(currentFile);
     StringBuffer result = new StringBuffer();
     int lastIndex = filePath.lastIndexOf(".wp");
@@ -208,12 +239,26 @@ public class Util {
         result.append(':');
         break;
       default:
-        result.append(ch);
+        if (ch == '_' && replaceUnderscore) {
+          result.append(' ');
+        } else {
+          result.append(ch);
+        }
       }
     }
     return result.toString();
   }
 
+  /**
+   * Extract the wiki name from the current filename, replaces '/' with ':' characters, and replaces all '_' with ' ' characters
+   * 
+   * @param currentFile
+   * @return
+   */
+  public static String getReadableWikiName(IFile currentFile) {
+    return getFileWikiName(currentFile, true);
+  }
+
   public static String getWikiTitle(IFile currentFile) {
     String fileName = currentFile.getName();
     String fileExt = currentFile.getFileExtension().toLowerCase();
@@ -222,4 +267,30 @@ public class Util {
     }
     return null;
   }
+
+  /**
+   * Create the folder resource in the workspace
+   * Recursive to create any folders which does not exist already
+   * 
+   * @param folderHandle
+   * @param monitor
+   */
+  public static void createFolder(IFolder folderHandle, IProgressMonitor monitor) {
+    try {
+      // Create the folder resource in the workspace
+      // Recursive to create any folders which does not exist already
+      if (!folderHandle.exists()) {
+        IContainer parent = folderHandle.getParent();
+        if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
+          createFolder((IFolder) parent, monitor);
+        }
+        //                 if (linkTargetPath != null)
+        //                             folderHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor);
+        //                 else
+        folderHandle.create(false, true, monitor);
+      }
+    } catch (CoreException e) {
+  
+    }
+  }
 }
\ No newline at end of file