misc.
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / WebBrowserPreference.java
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.net.URL;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.StringTokenizer;
18
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.preference.IPreferenceStore;
21
22 /**
23  * Preferences for the Web browser.
24  */
25 public class WebBrowserPreference {
26         protected static final String PREF_BROWSER_HISTORY = "webBrowserHistory";
27
28         protected static final String PREF_INTERNAL_WEB_BROWSER_HISTORY = "internalWebBrowserHistory";
29
30         protected static final String PREF_INTERNAL_WEB_BROWSER_FAVORITES = "internalWebBrowserFavorites";
31
32         protected static final String PREF_INTERNAL_WEB_BROWSER_OLD_FAVORITES = "internalWebBrowserOldFavorites";
33
34         protected static final String URL_PARAMETER = "%URL%";
35
36         /**
37          * WebBrowserPreference constructor comment.
38          */
39         private WebBrowserPreference() {
40                 super();
41         }
42
43         /**
44          * Returns the URL to the homepage.
45          * 
46          * @return java.lang.String
47          */
48         public static String getHomePageURL() {
49                 try {
50                         // get the default home page
51                         URL url = WebBrowserUIPlugin.getInstance().getBundle().getEntry(
52                                         "home/home.html");
53                         url = Platform.resolve(url);
54                         return url.toExternalForm();
55                 } catch (Exception e) {
56                         return "http://www.eclipse.org";
57                 }
58         }
59
60         /**
61          * Returns the preference store.
62          * 
63          * @return org.eclipse.jface.preference.IPreferenceStore
64          */
65         protected static IPreferenceStore getPreferenceStore() {
66                 return WebBrowserUIPlugin.getInstance().getPreferenceStore();
67         }
68
69         /**
70          * Returns the Web browser history list.
71          * 
72          * @return java.util.List
73          */
74         public static List getInternalWebBrowserHistory() {
75                 String temp = getPreferenceStore().getString(
76                                 PREF_INTERNAL_WEB_BROWSER_HISTORY);
77                 StringTokenizer st = new StringTokenizer(temp, "|*|");
78                 List l = new ArrayList();
79                 while (st.hasMoreTokens()) {
80                         String s = st.nextToken();
81                         l.add(s);
82                 }
83                 return l;
84         }
85
86         /**
87          * Returns the Web browser favorites.
88          * 
89          * @return java.util.List
90          */
91         public static List getInternalWebBrowserFavorites() {
92                 String temp = getPreferenceStore().getString(
93                                 PREF_INTERNAL_WEB_BROWSER_FAVORITES);
94                 StringTokenizer st = new StringTokenizer(temp, "|*|");
95                 List l = new ArrayList();
96                 try {
97                         while (st.hasMoreTokens()) {
98                                 l.add(new Favorite(st.nextToken(), st.nextToken()));
99                         }
100                 } catch (Exception e) {
101                         // ignore
102                 }
103                 return l;
104         }
105
106         /**
107          * Initialize the default preferences.
108          */
109         public static void initializeDefaultPreferences() {
110                 IPreferenceStore store = getPreferenceStore();
111
112                 String temp = store.getString(PREF_INTERNAL_WEB_BROWSER_OLD_FAVORITES);
113                 StringTokenizer st = new StringTokenizer(temp, "|*|");
114                 List def = new ArrayList();
115                 try {
116                         while (st.hasMoreTokens()) {
117                                 def.add(new Favorite(st.nextToken(), st.nextToken()));
118                         }
119                 } catch (Exception e) {
120                         // ignore
121                 }
122                 List list = getInternalWebBrowserFavorites();
123                 Iterator iterator = WebBrowserUtil.getUnlockedFavorites().iterator();
124                 while (iterator.hasNext()) {
125                         Favorite f = (Favorite) iterator.next();
126                         if (!def.contains(f))
127                                 list.add(f);
128                 }
129                 setInternalWebBrowserFavorites(list);
130
131                 StringBuffer sb = new StringBuffer();
132                 iterator = WebBrowserUtil.getUnlockedFavorites().iterator();
133                 while (iterator.hasNext()) {
134                         Favorite f = (Favorite) iterator.next();
135                         sb.append(f.getName());
136                         sb.append("|*|");
137                         sb.append(f.getURL());
138                         sb.append("|*|");
139                 }
140                 store.setValue(PREF_INTERNAL_WEB_BROWSER_OLD_FAVORITES, sb.toString());
141                 WebBrowserUIPlugin.getInstance().savePluginPreferences();
142         }
143
144         /**
145          * Sets the Web browser history.
146          * 
147          * @param java.util.List
148          */
149         public static void setInternalWebBrowserHistory(List list) {
150                 StringBuffer sb = new StringBuffer();
151                 if (list != null) {
152                         Iterator iterator = list.iterator();
153                         while (iterator.hasNext()) {
154                                 String s = (String) iterator.next();
155                                 sb.append(s);
156                                 sb.append("|*|");
157                         }
158                 }
159                 getPreferenceStore().setValue(PREF_INTERNAL_WEB_BROWSER_HISTORY,
160                                 sb.toString());
161                 WebBrowserUIPlugin.getInstance().savePluginPreferences();
162         }
163
164         /**
165          * Sets the Web browser favorites.
166          * 
167          * @param java.util.List
168          */
169         public static void setInternalWebBrowserFavorites(List list) {
170                 StringBuffer sb = new StringBuffer();
171                 if (list != null) {
172                         Iterator iterator = list.iterator();
173                         while (iterator.hasNext()) {
174                                 Favorite f = (Favorite) iterator.next();
175                                 sb.append(f.getName());
176                                 sb.append("|*|");
177                                 sb.append(f.getURL());
178                                 sb.append("|*|");
179                         }
180                 }
181                 getPreferenceStore().setValue(PREF_INTERNAL_WEB_BROWSER_FAVORITES,
182                                 sb.toString());
183                 WebBrowserUIPlugin.getInstance().savePluginPreferences();
184         }
185 }