1 package net.sourceforge.phpeclipse.wiki.preferences;
3 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
5 import org.eclipse.core.resources.IContainer;
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IFolder;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IResource;
10 import org.eclipse.core.resources.IWorkspaceRoot;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.NullProgressMonitor;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.core.runtime.QualifiedName;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21 import org.plog4u.wiki.filter.FilterUtil;
24 public static Shell findShell() {
25 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
27 return window.getShell();
29 Display display = Display.getCurrent();
30 if (display == null) {
31 display = Display.getDefault();
32 return display.getActiveShell();
34 // worst case, just create our own.
35 return new Shell(WikiEditorPlugin.getStandardDisplay());
38 public static String getProjectsWikiOutputPath(IResource resource, String key) {
39 return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
40 // int index = temp.indexOf(File.pathSeparatorChar);
42 // temp = temp.substring(0, index);
47 public static String getPreferenceValue(IResource resource, String key) {
48 return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
51 public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath) {
52 StringBuffer htmlBuffer = new StringBuffer();
53 String htmlName = null;
54 // String srcBasePath = Util.getWikiTextsPath(file);
55 // String fileName = file.getProjectRelativePath().toString();
56 String fileName = file.getLocation().toString();
57 if (fileName.startsWith(srcBasePath)) {
58 fileName = fileName.substring(srcBasePath.length());
59 if (fileName.charAt(0) != '/') {
60 fileName = "/" + fileName;
62 int index = fileName.lastIndexOf(".wp");
64 htmlName = binBasePath + fileName.substring(0, index) + ".html";
66 htmlName = binBasePath + fileName;
72 public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) {
73 IProject project = resource.getProject();
75 if (useProjectSettings(project, pageId)) {
76 value = getProperty(resource, pageId, key);
80 return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
83 public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) {
85 if (useProjectSettings(resource, pageId)) {
86 value = getProperty(resource, pageId, key);
90 return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
93 private static String getProperty(IResource resource, String pageId, String key) {
95 return resource.getPersistentProperty(new QualifiedName(pageId, key));
96 } catch (CoreException e) {
101 private static void setProperty(IResource resource, String pageId, String key, String value) {
103 resource.setPersistentProperty(new QualifiedName(pageId, key), value);
104 } catch (CoreException e) {
108 private static boolean useProjectSettings(IResource resource, String pageId) {
109 String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS);
110 return "true".equals(use);
113 public static void setWikiTextsPath(IProject project) {
114 String value = project.getLocation().toString();
115 IPreferenceStore store = WikiEditorPlugin.getDefault().getPreferenceStore();
116 String globalBasePath = store.getString(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
117 if (globalBasePath == null || globalBasePath.equals("")) {
118 store.setValue(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
120 String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH);
121 if (htmlFolder == null || htmlFolder.equals("")) {
122 // set a global default
123 store.setValue(WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
125 setProperty(project, WikiProjectPreferences.PREF_ID, FieldEditorOverlayPage.USEPROJECTSETTINGS, "true");
126 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
127 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
129 // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
130 // IResource resource = root.findMember(project.getLocation());
131 NullProgressMonitor _monitor = new NullProgressMonitor();
132 // if (resource!=null && resource.exists() && (resource instanceof IContainer)) {
133 // IContainer container = (IContainer) resource;
134 final IFolder srcFolder = project.getFolder(new Path("wpsrc"));
135 if (!srcFolder.exists()) {
137 srcFolder.create(true, false, _monitor);
138 } catch (CoreException e) {
141 final IFolder binFolder = project.getFolder(new Path("wpbin"));
142 if (!binFolder.exists()) {
144 binFolder.create(true, false, _monitor);
145 } catch (CoreException e) {
152 public static String getWikiTextsPath(IResource file) {
153 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
156 public static String getProjectsWikiTextsPath(IProject project) {
157 return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
160 public static String getWikiFileName(String wikiLink, IFile currentFile, String key) {
161 // String basePath = currentFile.getProject().getLocation().toString();
162 String basePath = getWikiTextsPath(currentFile);
163 return basePath + "/" + FilterUtil.normalizeWikiLink(wikiLink) + ".wp";
166 public static String getFileWikiName(IFile currentFile, String key) {
167 String filePath = currentFile.getLocation().toString();
168 // String basePath = currentFile.getProject().getLocation().toString();
169 String basePath = getWikiTextsPath(currentFile);
170 StringBuffer result = new StringBuffer();
171 int lastIndex = filePath.lastIndexOf(".wp");
173 lastIndex = filePath.length();
176 for (int i = basePath.length() + 1; i < lastIndex; i++) {
177 ch = filePath.charAt(i);
186 return result.toString();
189 public static String getWikiTitle(IFile currentFile) {
190 String fileName = currentFile.getName();
191 String fileExt = currentFile.getFileExtension().toLowerCase();
192 if (fileExt.equals("wp")) {
193 return fileName.substring(0, fileName.length() - 3);