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