b98673773dd936a4dfad31fce37a87330ee1ab74
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / preferences / Util.java
1 package net.sourceforge.phpeclipse.wiki.preferences;
2
3 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
4
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;
23
24 public class Util {
25   public static String titleToDB(String in) {
26     return in.replaceAll(" ", "_");
27   }
28
29   public static String db2Title(String in) {
30     return in.replaceAll("_", " ");
31   }
32
33   public static String db2TitleLink(String in) {
34     return "[[" + in.replaceAll("_", " ") + "]]";
35   }
36
37   public static Shell findShell() {
38     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
39     if (window != null) {
40       return window.getShell();
41     }
42     Display display = Display.getCurrent();
43     if (display == null) {
44       display = Display.getDefault();
45       return display.getActiveShell();
46     }
47     // worst case, just create our own.
48     return new Shell(WikiEditorPlugin.getStandardDisplay());
49   }
50
51   public static String getProjectsWikiOutputPath(IResource resource, String key) {
52     return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
53     //    int index = temp.indexOf(File.pathSeparatorChar);
54     //    if (index >= 0) {
55     //      temp = temp.substring(0, index);
56     //    }
57     //    return temp;
58   }
59
60   public static String getPreferenceValue(IResource resource, String key) {
61     return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
62   }
63
64   public static String getHTMLFileName(IFile file, String binBasePath, String srcBasePath) {
65     return getHTMLFileName(file,binBasePath,srcBasePath, ".html");
66   }
67   public static String getXMLFileName(IFile file, String binBasePath, String srcBasePath) {
68     return getHTMLFileName(file,binBasePath,srcBasePath, ".xml");
69   }
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;
80       }
81       int index = fileName.lastIndexOf(".wp");
82       if (index >= 0) {
83         htmlName = binBasePath + fileName.substring(0, index) + fileExtension;
84       } else {
85         htmlName = binBasePath + fileName;
86       }
87     }
88     return htmlName;
89   }
90
91   public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) {
92     IProject project = resource.getProject();
93     String value = null;
94     if (useProjectSettings(project, pageId)) {
95       value = getProperty(resource, pageId, key);
96     }
97     if (value != null)
98       return value;
99     return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
100   }
101
102   public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) {
103     String value = null;
104     if (useProjectSettings(resource, pageId)) {
105       value = getProperty(resource, pageId, key);
106     }
107     if (value != null)
108       return value;
109     return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
110   }
111
112   private static String getProperty(IResource resource, String pageId, String key) {
113     try {
114       return resource.getPersistentProperty(new QualifiedName(pageId, key));
115     } catch (CoreException e) {
116     }
117     return null;
118   }
119
120   private static void setProperty(IResource resource, String pageId, String key, String value) {
121     try {
122       resource.setPersistentProperty(new QualifiedName(pageId, key), value);
123     } catch (CoreException e) {
124     }
125   }
126
127   private static boolean useProjectSettings(IResource resource, String pageId) {
128     String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS);
129     return "true".equals(use);
130   }
131
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");
142     }
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");
147     }
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()) {
162       try {
163         srcFolder.create(true, false, _monitor);
164       } catch (CoreException e) {
165       }
166     }
167     final IFolder binFolder = project.getFolder(new Path("wpbin"));
168     if (!binFolder.exists()) {
169       try {
170         binFolder.create(true, false, _monitor);
171       } catch (CoreException e) {
172       }
173     }
174     //    }
175
176   }
177
178   public static String getWikiTextsPath(IResource file) {
179     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
180   }
181
182   public static String getLocalTemplate(IResource file) {
183     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
184   }
185
186   public static String getLocalCssUrl(IResource file) {
187     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_CSS_URL);
188   }
189
190   public static String getExportTemplate(IResource file) {
191     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
192   }
193
194   public static String getExportCssUrl(IResource file) {
195     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_CSS_URL);
196   }
197
198   public static String getProjectsWikiTextsPath(IProject project) {
199     return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
200   }
201
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";
206   }
207
208   /**
209    * Extract the wiki name from the current filename, but doesn't replace '_' characters
210    * 
211    * @param currentFile
212    * @return
213    */
214   public static String getURLWikiName(IFile currentFile) {
215     return getURLWikiName(currentFile, false);
216   }
217
218   /**
219    * Extract the wiki name from the current filename, replaces '_' with ' ' characters if
220    * <code>replaceUnderscore==true</code>
221    * 
222    * @param currentFile
223    * @param replaceUnderscore
224    * @return
225    */
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");
231     if (lastIndex < 0) {
232       lastIndex = filePath.length();
233     }
234     char ch;
235     for (int i = basePath.length() + 1; i < lastIndex; i++) {
236       ch = filePath.charAt(i);
237       switch (ch) {
238 //      case '/':
239 //        result.append(':');
240 //        break;
241       default:
242         if (ch == '_' && replaceUnderscore) {
243           result.append(' ');
244         } else { 
245           result.append(ch);
246         }
247       }
248     }
249     return result.toString();
250   }
251
252   /**
253    * Extract the wiki name from the current filename, replaces '/' with ':' characters, and replaces all '_' with ' ' characters
254    * 
255    * @param currentFile
256    * @return
257    */
258   public static String getReadableWikiName(IFile currentFile) {
259     return getURLWikiName(currentFile, true);
260   }
261
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);
267     }
268     return null;
269   }
270
271   /**
272    * Create the folder resource in the workspace
273    * Recursive to create any folders which does not exist already
274    * 
275    * @param folderHandle
276    * @param monitor
277    */
278   public static void createFolder(IFolder folderHandle, IProgressMonitor monitor) {
279     try {
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);
286         }
287         //                  if (linkTargetPath != null)
288         //                              folderHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor);
289         //                  else
290         folderHandle.create(false, true, monitor);
291       }
292     } catch (CoreException e) {
293   
294     }
295   }
296 }