219858529e19705fae0c54ba0c231577d860e87f
[phpeclipse.git] /
1 /**
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved.   This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser.internal;
12
13 import java.util.*;
14 import java.net.URL;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 /**
18  * Preferences for the Web browser.
19  */
20 public class WebBrowserPreference {
21         protected static final String PREF_BROWSER_HISTORY = "webBrowserHistory";
22         protected static final String PREF_INTERNAL_WEB_BROWSER_HISTORY = "internalWebBrowserHistory";
23         protected static final String PREF_INTERNAL_WEB_BROWSER_FAVORITES = "internalWebBrowserFavorites";
24         protected static final String PREF_INTERNAL_WEB_BROWSER_OLD_FAVORITES = "internalWebBrowserOldFavorites";
25         protected static final String URL_PARAMETER = "%URL%";
26         
27         /**
28          * WebBrowserPreference constructor comment.
29          */
30         private WebBrowserPreference() {
31                 super();
32         }
33
34         /**
35          * Returns the URL to the homepage.
36          * 
37          * @return java.lang.String
38          */
39         public static String getHomePageURL() {
40                 try {
41                         // get the default home page
42                         URL url = WebBrowserUIPlugin.getInstance().getBundle().getEntry("home/home.html");
43                         url = Platform.resolve(url);
44                         return url.toExternalForm();
45                 } catch (Exception e) {
46                         return "http://www.eclipse.org";
47                 }
48         }
49
50         /**
51          * Returns the preference store.
52          *
53          * @return org.eclipse.jface.preference.IPreferenceStore
54          */
55         protected static IPreferenceStore getPreferenceStore() {
56                 return WebBrowserUIPlugin.getInstance().getPreferenceStore();
57         }
58         
59         /**
60          * Returns the Web browser history list.
61          * 
62          * @return java.util.List
63          */
64         public static List getInternalWebBrowserHistory() {
65                 String temp = getPreferenceStore().getString(PREF_INTERNAL_WEB_BROWSER_HISTORY);
66                 StringTokenizer st = new StringTokenizer(temp, "|*|");
67                 List l = new ArrayList();
68                 while (st.hasMoreTokens()) {
69                         String s = st.nextToken();
70                         l.add(s);
71                 }
72                 return l;
73         }
74         
75         /**
76          * Returns the Web browser favorites.
77          * 
78          * @return java.util.List
79          */
80         public static List getInternalWebBrowserFavorites() {
81                 String temp = getPreferenceStore().getString(PREF_INTERNAL_WEB_BROWSER_FAVORITES);
82                 StringTokenizer st = new StringTokenizer(temp, "|*|");
83                 List l = new ArrayList();
84                 while (st.hasMoreTokens()) {
85                         l.add(new Favorite(st.nextToken(), st.nextToken()));
86                 }
87                 return l;
88         }
89
90         /**
91          * Initialize the default preferences.
92          */
93         public static void initializeDefaultPreferences() {
94                 IPreferenceStore store = getPreferenceStore();
95                 
96                 String temp = store.getString(PREF_INTERNAL_WEB_BROWSER_OLD_FAVORITES);
97                 StringTokenizer st = new StringTokenizer(temp, "|*|");
98                 List def = new ArrayList();
99                 while (st.hasMoreTokens()) {
100                         def.add(new Favorite(st.nextToken(), st.nextToken()));
101                 }
102                 
103                 List list = getInternalWebBrowserFavorites(); 
104                 Iterator iterator = WebBrowserUtil.getUnlockedFavorites().iterator();
105                 while (iterator.hasNext()) {
106                         Favorite f = (Favorite) iterator.next();
107                         if (!def.contains(f))
108                                 list.add(f);
109                 }
110                 setInternalWebBrowserFavorites(list);
111                 
112                 StringBuffer sb = new StringBuffer();
113                 iterator = WebBrowserUtil.getUnlockedFavorites().iterator();
114                 while (iterator.hasNext()) {
115                         Favorite f = (Favorite) iterator.next();
116                         sb.append(f.getName());
117                         sb.append("|*|");
118                         sb.append(f.getURL());
119                         sb.append("|*|");
120                 }
121                 store.setValue(PREF_INTERNAL_WEB_BROWSER_OLD_FAVORITES, sb.toString());
122                 WebBrowserUIPlugin.getInstance().savePluginPreferences();
123         }
124         
125         /**
126          * Sets the Web browser history.
127          *
128          * @param java.util.List
129          */
130         public static void setInternalWebBrowserHistory(List list) {
131                 StringBuffer sb = new StringBuffer();
132                 if (list != null) {
133                         Iterator iterator = list.iterator();
134                         while (iterator.hasNext()) {
135                                 String s = (String) iterator.next();
136                                 sb.append(s);
137                                 sb.append("|*|");
138                         }
139                 }
140                 getPreferenceStore().setValue(PREF_INTERNAL_WEB_BROWSER_HISTORY, sb.toString());
141                 WebBrowserUIPlugin.getInstance().savePluginPreferences();
142         }
143         
144         /**
145          * Sets the Web browser favorites.
146          *
147          * @param java.util.List
148          */
149         public static void setInternalWebBrowserFavorites(List list) {
150                 StringBuffer sb = new StringBuffer();
151                 if (list != null) {
152                         Iterator iterator = list.iterator();
153                         while (iterator.hasNext()) {
154                                 Favorite f = (Favorite) iterator.next();
155                                 sb.append(f.getName());
156                                 sb.append("|*|");
157                                 sb.append(f.getURL());
158                                 sb.append("|*|");
159                         }
160                 }
161                 getPreferenceStore().setValue(PREF_INTERNAL_WEB_BROWSER_FAVORITES, sb.toString());
162                 WebBrowserUIPlugin.getInstance().savePluginPreferences();
163         }
164 }