misc
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / overlaypages / ProjectPrefUtil.java
1 package net.sourceforge.phpeclipse.ui.overlaypages;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.StringTokenizer;
7
8 import net.sourceforge.phpeclipse.ui.IPreferenceConstants;
9 import net.sourceforge.phpeclipse.ui.WebUI;
10 import net.sourceforge.phpeclipse.ui.preferences.PHPMiscProjectPreferences;
11 import net.sourceforge.phpeclipse.ui.preferences.PHPPreviewProjectPreferences;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.QualifiedName;
19
20 public class ProjectPrefUtil {
21   public static String getMiscProjectsPreferenceValue(IResource resource, String key) {
22     return getOverlayedPrefProjectValue(resource, PHPMiscProjectPreferences.PREF_ID, key);
23   }
24
25   public static List getIncludePaths(IResource resource) {
26     String includePaths = getMiscProjectsPreferenceValue(resource, IPreferenceConstants.PHP_INCLUDE_PATHS);
27     ArrayList list = new ArrayList();
28     if (includePaths != null) {
29       StringTokenizer st = new StringTokenizer(includePaths, File.pathSeparator + "\n\r");//$NON-NLS-1$
30       while (st.hasMoreElements()) {
31         list.add(st.nextElement());
32       }
33     }
34     return list;
35   }
36
37   public static IPath getDocumentRoot(IResource resource) {
38     String documentRoot = getMiscProjectsPreferenceValue(resource, IPreferenceConstants.PHP_DOCUMENTROOT_PREF);
39     IPath path = new Path(documentRoot);
40     //    documentRoot = documentRoot.replace('\\', '/');
41     return path;
42   }
43
44   public static String getOverlayedPrefProjectValue(IResource resource, String pageId, String key) {
45     IProject project = resource.getProject();
46     String value = null;
47     if (useProjectSettings(project, pageId)) {
48       value = getProperty(resource, pageId, key);
49     }
50     if (value != null)
51       return value;
52     return WebUI.getDefault().getPreferenceStore().getString(key);
53   }
54
55   public static String getOverlayedPrefResourceValue(IResource resource, String pageId, String key) {
56     String value = null;
57     if (useProjectSettings(resource, pageId)) {
58       value = getProperty(resource, pageId, key);
59     }
60     if (value != null)
61       return value;
62     return WebUI.getDefault().getPreferenceStore().getString(key);
63   }
64
65   public static boolean getPreviewBooleanValue(IResource resource, String key) {
66     return getOverlayedPrefResourceValue(resource, PHPPreviewProjectPreferences.PREF_ID, key).equals("true");
67   }
68
69   public static String getPreviewStringValue(IResource resource, String key) {
70     return getOverlayedPrefResourceValue(resource, PHPPreviewProjectPreferences.PREF_ID, key);
71   }
72
73   private static String getProperty(IResource resource, String pageId, String key) {
74     try {
75       return resource.getPersistentProperty(new QualifiedName(pageId, key));
76     } catch (CoreException e) {
77     }
78     return null;
79   }
80
81   private static boolean useProjectSettings(IResource resource, String pageId) {
82     String use = getProperty(resource, pageId, FieldEditorOverlayPage.USEPROJECTSETTINGS);
83     return "true".equals(use);
84   }
85 }