package net.sourceforge.phpeclipse.wiki.preferences; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; 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.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.QualifiedName; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; import org.plog4u.wiki.filter.FilterUtil; public class Util { public static Shell findShell() { IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window != null) { return window.getShell(); } Display display = Display.getCurrent(); if (display == null) { display = Display.getDefault(); return display.getActiveShell(); } // worst case, just create our own. return new Shell(WikiEditorPlugin.getStandardDisplay()); } public static String getProjectsWikiOutputPath(IResource resource, String key) { return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key); // int index = temp.indexOf(File.pathSeparatorChar); // if (index >= 0) { // temp = temp.substring(0, index); // } // return temp; } public static String getPreferenceValue(IResource resource, String key) { return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key); } public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath) { StringBuffer htmlBuffer = new StringBuffer(); String htmlName = null; // String srcBasePath = Util.getWikiTextsPath(file); // String fileName = file.getProjectRelativePath().toString(); String fileName = file.getLocation().toString(); if (fileName.startsWith(srcBasePath)) { fileName = fileName.substring(srcBasePath.length()); if (fileName.charAt(0) != '/') { fileName = "/" + fileName; } int index = fileName.lastIndexOf(".wp"); if (index >= 0) { htmlName = binBasePath + fileName.substring(0, index) + ".html"; } else { htmlName = binBasePath + fileName; } } return htmlName; } public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) { IProject project = resource.getProject(); String value = null; if (useProjectSettings(project, pageId)) { value = getProperty(resource, pageId, key); } if (value != null) return value; return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key); } public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) { String value = null; if (useProjectSettings(resource, pageId)) { value = getProperty(resource, pageId, key); } if (value != null) return value; return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key); } private static String getProperty(IResource resource, String pageId, String key) { try { return resource.getPersistentProperty(new QualifiedName(pageId, key)); } catch (CoreException e) { } return null; } private static void setProperty(IResource resource, String pageId, String key, String value) { try { resource.setPersistentProperty(new QualifiedName(pageId, key), value); } catch (CoreException e) { } } private static boolean useProjectSettings(IResource resource, String pageId) { String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS); return "true".equals(use); } public static void setWikiTextsPath(IProject project) { String value = project.getLocation().toString(); IPreferenceStore store = WikiEditorPlugin.getDefault().getPreferenceStore(); String globalBasePath = store.getString(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH); if (globalBasePath == null || globalBasePath.equals("")) { store.setValue(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc"); } String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH); 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"); setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc"); setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin"); // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); // IResource resource = root.findMember(project.getLocation()); NullProgressMonitor _monitor = new NullProgressMonitor(); // if (resource!=null && resource.exists() && (resource instanceof IContainer)) { // IContainer container = (IContainer) resource; final IFolder srcFolder = project.getFolder(new Path("wpsrc")); if (!srcFolder.exists()) { try { srcFolder.create(true, false, _monitor); } catch (CoreException e) { } } final IFolder binFolder = project.getFolder(new Path("wpbin")); if (!binFolder.exists()) { try { binFolder.create(true, false, _monitor); } catch (CoreException e) { } } // } } public static String getWikiTextsPath(IResource file) { return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH); } 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) { String filePath = currentFile.getLocation().toString(); // String basePath = currentFile.getProject().getLocation().toString(); String basePath = getWikiTextsPath(currentFile); StringBuffer result = new StringBuffer(); int lastIndex = filePath.lastIndexOf(".wp"); if (lastIndex < 0) { lastIndex = filePath.length(); } char ch; for (int i = basePath.length() + 1; i < lastIndex; i++) { ch = filePath.charAt(i); switch (ch) { case '/': result.append(':'); break; default: result.append(ch); } } return result.toString(); } public static String getWikiTitle(IFile currentFile) { String fileName = currentFile.getName(); String fileExt = currentFile.getFileExtension().toLowerCase(); if (fileExt.equals("wp")) { return fileName.substring(0, fileName.length() - 3); } return null; } }