Add predefined Velocity and CSS template 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 setWikiTextsPath(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     }
122     String htmlFolder = store.getString(WikiEditorPlugin.HTML_OUTPUT_PATH);
123     if (htmlFolder == null || htmlFolder.equals("")) {  
124       // set a global default 
125       store.setValue(WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
126     }
127     setProperty(project, WikiProjectPreferences.PREF_ID, FieldEditorOverlayPage.USEPROJECTSETTINGS, "true");
128     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH, value + "/wpsrc");
129     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.HTML_OUTPUT_PATH, value + "/wpbin");
130     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME, value + "/wpsrc/main.vm");
131     setProperty(project, WikiProjectPreferences.PREF_ID, WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME, value + "/wpsrc/export.vm");
132     //    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
133     //    IResource resource = root.findMember(project.getLocation());
134     NullProgressMonitor _monitor = new NullProgressMonitor();
135     //    if (resource!=null && resource.exists() && (resource instanceof IContainer)) {
136     //      IContainer container = (IContainer) resource;
137     final IFolder srcFolder = project.getFolder(new Path("wpsrc"));
138     if (!srcFolder.exists()) {
139       try {
140         srcFolder.create(true, false, _monitor);
141       } catch (CoreException e) {
142       }
143     }
144     final IFolder binFolder = project.getFolder(new Path("wpbin"));
145     if (!binFolder.exists()) {
146       try {
147         binFolder.create(true, false, _monitor);
148       } catch (CoreException e) {
149       }
150     }
151     //    }
152
153   }
154
155   public static String getWikiTextsPath(IResource file) {
156     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
157   }
158
159   public static String getLocalTemplate(IResource file) {
160     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.LOCAL_TEMPLATE_FILE_NAME);
161   }
162   
163   public static String getExportTemplate(IResource file) {
164     return Util.getPreferenceValue(file.getProject(), WikiEditorPlugin.EXPORT_TEMPLATE_FILE_NAME);
165   }
166   
167   public static String getProjectsWikiTextsPath(IProject project) {
168     return Util.getPreferenceValue(project, WikiEditorPlugin.WIKI_TEXTS_BASE_PATH);
169   }
170   
171   public static String getWikiFileName(String wikiLink, IFile currentFile, String key) {
172     //    String basePath = currentFile.getProject().getLocation().toString();
173     String basePath = getWikiTextsPath(currentFile);
174     return basePath + "/" + FilterUtil.normalizeWikiLink(wikiLink) + ".wp";
175   }
176
177   public static String getFileWikiName(IFile currentFile, String key) {
178     String filePath = currentFile.getLocation().toString();
179     //    String basePath = currentFile.getProject().getLocation().toString();
180     String basePath = getWikiTextsPath(currentFile);
181     StringBuffer result = new StringBuffer();
182     int lastIndex = filePath.lastIndexOf(".wp");
183     if (lastIndex < 0) {
184       lastIndex = filePath.length();
185     }
186     char ch;
187     for (int i = basePath.length() + 1; i < lastIndex; i++) {
188       ch = filePath.charAt(i);
189       switch (ch) {
190       case '/':
191         result.append(':');
192         break;
193       default:
194         result.append(ch);
195       }
196     }
197     return result.toString();
198   }
199
200   public static String getWikiTitle(IFile currentFile) {
201     String fileName = currentFile.getName();
202     String fileExt = currentFile.getFileExtension().toLowerCase();
203     if (fileExt.equals("wp")) {
204       return fileName.substring(0, fileName.length() - 3);
205     }
206     return null;
207   }
208 }