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.IProgressMonitor;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.core.runtime.QualifiedName;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.PlatformUI;
22 import org.plog4u.wiki.filter.FilterUtil;
25 public static String titleToDB(String in) {
26 return in.replaceAll(" ", "_");
29 public static String db2Title(String in) {
30 return in.replaceAll("_", " ");
33 public static String db2TitleLink(String in) {
34 return "[[" + in.replaceAll("_", " ") + "]]";
37 public static Shell findShell() {
38 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
40 return window.getShell();
42 Display display = Display.getCurrent();
43 if (display == null) {
44 display = Display.getDefault();
45 return display.getActiveShell();
47 // worst case, just create our own.
48 return new Shell(WikiEditorPlugin.getStandardDisplay());
51 public static String getProjectsWikiOutputPath(IResource resource, String key) {
52 return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
53 // int index = temp.indexOf(File.pathSeparatorChar);
55 // temp = temp.substring(0, index);
60 public static String getPreferenceValue(IResource resource, String key) {
61 return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
64 public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath) {
65 return getHTMLFileName(file,binBasePath,srcBasePath, ".html");
67 public static String getXMLFileName(IFile file, String binBasePath, String srcBasePath) {
68 return getHTMLFileName(file,binBasePath,srcBasePath, ".xml");
70 public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath, String fileExtension) {
71 StringBuffer htmlBuffer = new StringBuffer();
72 String htmlName = null;
73 // String srcBasePath = Util.getWikiTextsPath(file);
74 // String fileName = file.getProjectRelativePath().toString();
75 String fileName = file.getLocation().toString();
76 if (fileName.startsWith(srcBasePath)) {
77 fileName = fileName.substring(srcBasePath.length());
78 if (fileName.charAt(0) != '/') {
79 fileName = "/" + fileName;
81 int index = fileName.lastIndexOf(".wp");
83 htmlName = binBasePath + fileName.substring(0, index) + fileExtension;
85 htmlName = binBasePath + fileName;
91 public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) {
92 IProject project = resource.getProject();
94 if (useProjectSettings(project, pageId)) {
95 value = getProperty(resource, pageId, key);
99 return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
102 public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) {
104 if (useProjectSettings(resource, pageId)) {
105 value = getProperty(resource, pageId, key);
109 return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
112 private static String getProperty(IResource resource, String pageId, String key) {
114 return resource.getPersistentProperty(new QualifiedName(pageId, key));
115 } catch (CoreException e) {
120 private static void setProperty(IResource resource, String pageId, String key, String value) {
122 resource.setPersistentProperty(new QualifiedName(pageId, key), value);
123 } catch (CoreException e) {
127 private static boolean useProjectSettings(IResource resource, String pageId) {
128 String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS);
129 return "true".equals(use);
132 public static void setWikiBuilderPreferences(IProject project) {
133 String value = project.getLocation().toString();
134 IPreferenceStore store = WikiEditorPlugin.getDefault().getPreferenceStore();
135 String globalBasePath = store.getString(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
136 if (globalBasePath == null || globalBasePath.equals("")) {
137 store.setValue(WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
138 store.setValue(WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
139 store.setValue(WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
140 store.setValue(WikiEditorPlugin.LOCAL_CSS_URL, "file://" + value + "/wpsrc/main.css");
141 store.setValue(WikiEditorPlugin.EXPORT_CSS_URL, "file://" + value + "/wpsrc/main.css");
143 String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH);
144 if (htmlFolder == null || htmlFolder.equals("")) {
145 // set a global default
146 store.setValue(WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
148 setProperty(project, WikiProjectPreferences.PREF_ID, FieldEditorOverlayPage.USEPROJECTSETTINGS, "true");
149 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
150 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
151 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
152 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
153 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_CSS_URL, "file://" + value + "/wpsrc/main.css");
154 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_CSS_URL, "file://" + value + "/wpsrc/main.css");
155 // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
156 // IResource resource = root.findMember(project.getLocation());
157 NullProgressMonitor _monitor = new NullProgressMonitor();
158 // if (resource!=null && resource.exists() && (resource instanceof IContainer)) {
159 // IContainer container = (IContainer) resource;
160 final IFolder srcFolder = project.getFolder(new Path("wpsrc"));
161 if (!srcFolder.exists()) {
163 srcFolder.create(true, false, _monitor);
164 } catch (CoreException e) {
167 final IFolder binFolder = project.getFolder(new Path("wpbin"));
168 if (!binFolder.exists()) {
170 binFolder.create(true, false, _monitor);
171 } catch (CoreException e) {
178 public static String getWikiTextsPath(IResource file) {
179 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
182 public static String getLocalTemplate(IResource file) {
183 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
186 public static String getLocalCssUrl(IResource file) {
187 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_CSS_URL);
190 public static String getExportTemplate(IResource file) {
191 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
194 public static String getExportCssUrl(IResource file) {
195 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_CSS_URL);
198 public static String getProjectsWikiTextsPath(IProject project) {
199 return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
202 public static String getWikiFileName(String wikiLink, IFile currentFile, String key) {
203 // String basePath = currentFile.getProject().getLocation().toString();
204 String basePath = getWikiTextsPath(currentFile);
205 return basePath + "/" + FilterUtil.normalizeWikiLink(wikiLink) + ".wp";
209 * Extract the wiki name from the current filename, but doesn't replace '_' characters
214 public static String getURLWikiName(IFile currentFile) {
215 return getURLWikiName(currentFile, false);
219 * Extract the wiki name from the current filename, replaces '_' with ' ' characters if
220 * <code>replaceUnderscore==true</code>
223 * @param replaceUnderscore
226 public static String getURLWikiName(IFile currentFile, boolean replaceUnderscore) {
227 String filePath = currentFile.getLocation().toString();
228 String basePath = getWikiTextsPath(currentFile);
229 StringBuffer result = new StringBuffer();
230 int lastIndex = filePath.lastIndexOf(".wp");
232 lastIndex = filePath.length();
235 for (int i = basePath.length() + 1; i < lastIndex; i++) {
236 ch = filePath.charAt(i);
239 // result.append(':');
242 if (ch == '_' && replaceUnderscore) {
249 return result.toString();
253 * Extract the wiki name from the current filename, replaces '/' with ':' characters, and replaces all '_' with ' ' characters
258 public static String getReadableWikiName(IFile currentFile) {
259 return getURLWikiName(currentFile, true);
262 public static String getWikiTitle(IFile currentFile) {
263 String fileName = currentFile.getName();
264 String fileExt = currentFile.getFileExtension().toLowerCase();
265 if (fileExt.equals("wp")) {
266 return fileName.substring(0, fileName.length() - 3);
272 * Create the folder resource in the workspace
273 * Recursive to create any folders which does not exist already
275 * @param folderHandle
278 public static void createFolder(IFolder folderHandle, IProgressMonitor monitor) {
280 // Create the folder resource in the workspace
281 // Recursive to create any folders which does not exist already
282 if (!folderHandle.exists()) {
283 IContainer parent = folderHandle.getParent();
284 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
285 createFolder((IFolder) parent, monitor);
287 // if (linkTargetPath != null)
288 // folderHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor);
290 folderHandle.create(false, true, monitor);
292 } catch (CoreException e) {