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