Add predefined Velocity and CSS templates if wiki builder is assigned to a project
[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.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;
22
23 public class Util {
24   public static Shell findShell() {
25     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
26     if (window != null) {
27       return window.getShell();
28     }
29     Display display = Display.getCurrent();
30     if (display == null) {
31       display = Display.getDefault();
32       return display.getActiveShell();
33     }
34     // worst case, just create our own.
35     return new Shell(WikiEditorPlugin.getStandardDisplay());
36   }
37
38   public static String getProjectsWikiOutputPath(IResource resource, String key) {
39     return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
40     //    int index = temp.indexOf(File.pathSeparatorChar);
41     //    if (index >= 0) {
42     //      temp = temp.substring(0, index);
43     //    }
44     //    return temp;
45   }
46
47   public static String getPreferenceValue(IResource resource, String key) {
48     return getOverlayedPrefProjectValue(resource, WikiProjectPreferences.PREF_ID, key);
49   }
50
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;
61       }
62       int index = fileName.lastIndexOf(".wp");
63       if (index >= 0) {
64         htmlName = binBasePath + fileName.substring(0, index) + ".html";
65       } else {
66         htmlName = binBasePath + fileName;
67       }
68     }
69     return htmlName;
70   }
71
72   public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) {
73     IProject project = resource.getProject();
74     String value = null;
75     if (useProjectSettings(project, pageId)) {
76       value = getProperty(resource, pageId, key);
77     }
78     if (value != null)
79       return value;
80     return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
81   }
82
83   public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) {
84     String value = null;
85     if (useProjectSettings(resource, pageId)) {
86       value = getProperty(resource, pageId, key);
87     }
88     if (value != null)
89       return value;
90     return WikiEditorPlugin.getDefault().getPreferenceStore().getString(key);
91   }
92
93   private static String getProperty(IResource resource, String pageId, String key) {
94     try {
95       return resource.getPersistentProperty(new QualifiedName(pageId, key));
96     } catch (CoreException e) {
97     }
98     return null;
99   }
100
101   private static void setProperty(IResource resource, String pageId, String key, String value) {
102     try {
103       resource.setPersistentProperty(new QualifiedName(pageId, key), value);
104     } catch (CoreException e) {
105     }
106   }
107
108   private static boolean useProjectSettings(IResource resource, String pageId) {
109     String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS);
110     return "true".equals(use);
111   }
112
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");
123     }
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");
128     }
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()) {
143       try {
144         srcFolder.create(true, false, _monitor);
145       } catch (CoreException e) {
146       }
147     }
148     final IFolder binFolder = project.getFolder(new Path("wpbin"));
149     if (!binFolder.exists()) {
150       try {
151         binFolder.create(true, false, _monitor);
152       } catch (CoreException e) {
153       }
154     }
155     //    }
156
157   }
158
159   public static String getWikiTextsPath(IResource file) {
160     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
161   }
162
163   public static String getLocalTemplate(IResource file) {
164     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
165   }
166   public static String getLocalCssUrl(IResource file) {
167     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_CSS_URL);
168   }
169   public static String getExportTemplate(IResource file) {
170     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
171   }
172   public static String getExportCssUrl(IResource file) {
173     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_CSS_URL);
174   }
175   public static String getProjectsWikiTextsPath(IProject project) {
176     return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
177   }
178   
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";
183   }
184
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");
191     if (lastIndex < 0) {
192       lastIndex = filePath.length();
193     }
194     char ch;
195     for (int i = basePath.length() + 1; i < lastIndex; i++) {
196       ch = filePath.charAt(i);
197       switch (ch) {
198       case '/':
199         result.append(':');
200         break;
201       default:
202         result.append(ch);
203       }
204     }
205     return result.toString();
206   }
207
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);
213     }
214     return null;
215   }
216 }