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 String titleToDB(String in) {
25 return in.replaceAll(" ", "_");
27 public static String db2Title(String in) {
28 return in.replaceAll("_", " ");
30 public static String db2TitleLink(String in) {
31 return "[["+in.replaceAll("_", " ")+"]]";
33 public static Shell findShell() {
34 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
36 return window.getShell();
38 Display display = Display.getCurrent();
39 if (display == null) {
40 display = Display.getDefault();
41 return display.getActiveShell();
43 // worst case, just create our own.
44 return new Shell(WikiEditorPlugin.getStandardDisplay());
47 public static String getProjectsWikiOutputPath(IResource resource, String key) {
48 return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
49 // int index = temp.indexOf(File.pathSeparatorChar);
51 // temp = temp.substring(0, index);
56 public static String getPreferenceValue(IResource resource, String key) {
57 return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
60 public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath) {
61 StringBuffer htmlBuffer = new StringBuffer();
62 String htmlName = null;
63 // String srcBasePath = Util.getWikiTextsPath(file);
64 // String fileName = file.getProjectRelativePath().toString();
65 String fileName = file.getLocation().toString();
66 if (fileName.startsWith(srcBasePath)) {
67 fileName = fileName.substring(srcBasePath.length());
68 if (fileName.charAt(0) != '/') {
69 fileName = "/" + fileName;
71 int index = fileName.lastIndexOf(".wp");
73 htmlName = binBasePath + fileName.substring(0, index) + ".html";
75 htmlName = binBasePath + fileName;
81 public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) {
82 IProject project = resource.getProject();
84 if (useProjectSettings(project, pageId)) {
85 value = getProperty(resource, pageId, key);
89 return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
92 public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) {
94 if (useProjectSettings(resource, pageId)) {
95 value = getProperty(resource, pageId, key);
99 return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
102 private static String getProperty(IResource resource, String pageId, String key) {
104 return resource.getPersistentProperty(new QualifiedName(pageId, key));
105 } catch (CoreException e) {
110 private static void setProperty(IResource resource, String pageId, String key, String value) {
112 resource.setPersistentProperty(new QualifiedName(pageId, key), value);
113 } catch (CoreException e) {
117 private static boolean useProjectSettings(IResource resource, String pageId) {
118 String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS);
119 return "true".equals(use);
122 public static void setWikiBuilderPreferences(IProject project) {
123 String value = project.getLocation().toString();
124 IPreferenceStore store = WikiEditorPlugin.getDefault().getPreferenceStore();
125 String globalBasePath = store.getString(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
126 if (globalBasePath == null || globalBasePath.equals("")) {
127 store.setValue(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
128 store.setValue(WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
129 store.setValue(WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
130 store.setValue(WikiEditorPlugin.LOCAL_CSS_URL, "file://"+value+"/wpsrc/main.css");
131 store.setValue(WikiEditorPlugin.EXPORT_CSS_URL, "file://"+value+"/wpsrc/main.css");
133 String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH);
134 if (htmlFolder == null || htmlFolder.equals("")) {
135 // set a global default
136 store.setValue(WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
138 setProperty(project, WikiProjectPreferences.PREF_ID, FieldEditorOverlayPage.USEPROJECTSETTINGS, "true");
139 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
140 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
141 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
142 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
143 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_CSS_URL, "file://"+value+"/wpsrc/main.css");
144 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_CSS_URL, "file://"+value+"/wpsrc/main.css");
145 // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
146 // IResource resource = root.findMember(project.getLocation());
147 NullProgressMonitor _monitor = new NullProgressMonitor();
148 // if (resource!=null && resource.exists() && (resource instanceof IContainer)) {
149 // IContainer container = (IContainer) resource;
150 final IFolder srcFolder = project.getFolder(new Path("wpsrc"));
151 if (!srcFolder.exists()) {
153 srcFolder.create(true, false, _monitor);
154 } catch (CoreException e) {
157 final IFolder binFolder = project.getFolder(new Path("wpbin"));
158 if (!binFolder.exists()) {
160 binFolder.create(true, false, _monitor);
161 } catch (CoreException e) {
168 public static String getWikiTextsPath(IResource file) {
169 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
172 public static String getLocalTemplate(IResource file) {
173 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
175 public static String getLocalCssUrl(IResource file) {
176 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_CSS_URL);
178 public static String getExportTemplate(IResource file) {
179 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
181 public static String getExportCssUrl(IResource file) {
182 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_CSS_URL);
184 public static String getProjectsWikiTextsPath(IProject project) {
185 return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
188 public static String getWikiFileName(String wikiLink, IFile currentFile, String key) {
189 // String basePath = currentFile.getProject().getLocation().toString();
190 String basePath = getWikiTextsPath(currentFile);
191 return basePath + "/" + FilterUtil.normalizeWikiLink(wikiLink) + ".wp";
194 public static String getFileWikiName(IFile currentFile, String key) {
195 String filePath = currentFile.getLocation().toString();
196 // String basePath = currentFile.getProject().getLocation().toString();
197 String basePath = getWikiTextsPath(currentFile);
198 StringBuffer result = new StringBuffer();
199 int lastIndex = filePath.lastIndexOf(".wp");
201 lastIndex = filePath.length();
204 for (int i = basePath.length() + 1; i < lastIndex; i++) {
205 ch = filePath.charAt(i);
214 return result.toString();
217 public static String getWikiTitle(IFile currentFile) {
218 String fileName = currentFile.getName();
219 String fileExt = currentFile.getFileExtension().toLowerCase();
220 if (fileExt.equals("wp")) {
221 return fileName.substring(0, fileName.length() - 3);