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 setWikiBuilderPreferences(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");
119 store.setValue(WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
120 store.setValue(WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
121 store.setValue(WikiEditorPlugin.LOCAL_CSS_URL, "file://"+value+"/wpsrc/main.css");
122 store.setValue(WikiEditorPlugin.EXPORT_CSS_URL, "file://"+value+"/wpsrc/main.css");
124 String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH);
125 if (htmlFolder == null || htmlFolder.equals("")) {
126 // set a global default
127 store.setValue(WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
129 setProperty(project, WikiProjectPreferences.PREF_ID, FieldEditorOverlayPage.USEPROJECTSETTINGS, "true");
130 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
131 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
132 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
133 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
134 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_CSS_URL, "file://"+value+"/wpsrc/main.css");
135 setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_CSS_URL, "file://"+value+"/wpsrc/main.css");
136 // IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
137 // IResource resource = root.findMember(project.getLocation());
138 NullProgressMonitor _monitor = new NullProgressMonitor();
139 // if (resource!=null && resource.exists() && (resource instanceof IContainer)) {
140 // IContainer container = (IContainer) resource;
141 final IFolder srcFolder = project.getFolder(new Path("wpsrc"));
142 if (!srcFolder.exists()) {
144 srcFolder.create(true, false, _monitor);
145 } catch (CoreException e) {
148 final IFolder binFolder = project.getFolder(new Path("wpbin"));
149 if (!binFolder.exists()) {
151 binFolder.create(true, false, _monitor);
152 } catch (CoreException e) {
159 public static String getWikiTextsPath(IResource file) {
160 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
163 public static String getLocalTemplate(IResource file) {
164 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
166 public static String getLocalCssUrl(IResource file) {
167 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_CSS_URL);
169 public static String getExportTemplate(IResource file) {
170 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
172 public static String getExportCssUrl(IResource file) {
173 return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_CSS_URL);
175 public static String getProjectsWikiTextsPath(IProject project) {
176 return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
179 public static String getWikiFileName(String wikiLink, IFile currentFile, String key) {
180 // String basePath = currentFile.getProject().getLocation().toString();
181 String basePath = getWikiTextsPath(currentFile);
182 return basePath + "/" + FilterUtil.normalizeWikiLink(wikiLink) + ".wp";
185 public static String getFileWikiName(IFile currentFile, String key) {
186 String filePath = currentFile.getLocation().toString();
187 // String basePath = currentFile.getProject().getLocation().toString();
188 String basePath = getWikiTextsPath(currentFile);
189 StringBuffer result = new StringBuffer();
190 int lastIndex = filePath.lastIndexOf(".wp");
192 lastIndex = filePath.length();
195 for (int i = basePath.length() + 1; i < lastIndex; i++) {
196 ch = filePath.charAt(i);
205 return result.toString();
208 public static String getWikiTitle(IFile currentFile) {
209 String fileName = currentFile.getName();
210 String fileExt = currentFile.getFileExtension().toLowerCase();
211 if (fileExt.equals("wp")) {
212 return fileName.substring(0, fileName.length() - 3);