KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > WebBrowserPreference


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.ui.IEditorDescriptor;
20 import org.eclipse.ui.IEditorRegistry;
21 import org.eclipse.ui.PlatformUI;
22
23 /**
24  * Preferences for the Web browser.
25  */

26 public class WebBrowserPreference {
27     protected static final String JavaDoc PREF_BROWSER_HISTORY = "webBrowserHistory"; //$NON-NLS-1$
28

29     protected static final String JavaDoc PREF_INTERNAL_WEB_BROWSER_HISTORY = "internalWebBrowserHistory"; //$NON-NLS-1$
30

31     protected static final String JavaDoc PREF_BROWSER_CHOICE = "browser-choice"; //$NON-NLS-1$
32

33     private static final String JavaDoc INTERNAL_BROWSER_ID = "org.eclipse.ui.browser.editor"; //$NON-NLS-1$
34

35     private static final String JavaDoc BROWSER_SUPPORT_ID = "org.eclipse.ui.browser.editorSupport"; //$NON-NLS-1$
36

37     public static final int INTERNAL = 0;
38
39     public static final int EXTERNAL = 1;
40
41     /**
42      * WebBrowserPreference constructor comment.
43      */

44     private WebBrowserPreference() {
45         super();
46     }
47
48     /**
49      * Returns the preference store.
50      *
51      * @return the preference store
52      */

53     protected static IPreferenceStore getPreferenceStore() {
54         return WebBrowserUIPlugin.getInstance().getPreferenceStore();
55     }
56
57     /**
58      * Returns the Web browser history list.
59      *
60      * @return java.util.List
61      */

62     public static List JavaDoc getInternalWebBrowserHistory() {
63         String JavaDoc temp = getPreferenceStore().getString(
64                 PREF_INTERNAL_WEB_BROWSER_HISTORY);
65         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(temp, "|*|"); //$NON-NLS-1$
66
List JavaDoc l = new ArrayList JavaDoc();
67         while (st.hasMoreTokens()) {
68             String JavaDoc s = st.nextToken();
69             l.add(s);
70         }
71         return l;
72     }
73
74     /**
75      * Sets the Web browser history.
76      *
77      * @param list
78      * the history
79      */

80     public static void setInternalWebBrowserHistory(List JavaDoc list) {
81         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
82         if (list != null) {
83             Iterator JavaDoc iterator = list.iterator();
84             while (iterator.hasNext()) {
85                 String JavaDoc s = (String JavaDoc) iterator.next();
86                 sb.append(s);
87                 sb.append("|*|"); //$NON-NLS-1$
88
}
89         }
90         getPreferenceStore().setValue(PREF_INTERNAL_WEB_BROWSER_HISTORY,
91                 sb.toString());
92         WebBrowserUIPlugin.getInstance().savePluginPreferences();
93     }
94
95     /**
96      * Returns whether the internal browser is used by default
97      *
98      * @return true if the internal browser is used by default
99      */

100     public static boolean isDefaultUseInternalBrowser() {
101         return WebBrowserUtil.canUseInternalWebBrowser();
102     }
103
104     /**
105      * Returns whether the system browser is used by default
106      *
107      * @return true if the system browser is used by default
108      */

109     public static boolean isDefaultUseSystemBrowser() {
110         return WebBrowserUtil.canUseSystemBrowser();
111     }
112
113     /**
114      * Returns whether the internal or external browser is being used
115      *
116      * @return one of <code>INTERNAL</code> or <code>EXTERNAL</code>.
117      */

118     public static int getBrowserChoice() {
119         int choice = getPreferenceStore().getInt(PREF_BROWSER_CHOICE);
120         if (choice == 2)
121             return EXTERNAL;
122         if (choice == INTERNAL && !WebBrowserUtil.canUseInternalWebBrowser())
123             return EXTERNAL;
124         return choice;
125     }
126
127     /**
128      * Sets whether the internal, system and external browser is used
129      *
130      * @param choice
131      * </code>INTERNAL</code>, <code>SYSTEM</code> and <code>EXTERNAL</code>
132      */

133     public static void setBrowserChoice(int choice) {
134         getPreferenceStore().setValue(PREF_BROWSER_CHOICE, choice);
135         WebBrowserUIPlugin.getInstance().savePluginPreferences();
136         updateDefaultEditor(choice);
137     }
138
139     private static void updateDefaultEditor(int choice) {
140         // Toggle from internal editor to browser support
141
// to avoid confusion between default editors and
142
// Web browser preference page
143
IEditorRegistry registry = PlatformUI.getWorkbench()
144                 .getEditorRegistry();
145         String JavaDoc oldId = choice == INTERNAL ? BROWSER_SUPPORT_ID
146                 : INTERNAL_BROWSER_ID;
147         String JavaDoc newId = choice == INTERNAL ? INTERNAL_BROWSER_ID
148                 : BROWSER_SUPPORT_ID;
149
150         String JavaDoc[][] extensions = { { "a.html", "*.html" }, { "a.htm", "*.htm" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
151
{ "a.shtml", "*.shtml" } }; //$NON-NLS-1$//$NON-NLS-2$
152

153         // For each default editor that matches the oldId, change
154
// the default editor to the newId
155
for (int i = 0; i < extensions.length; i++) {
156             String JavaDoc[] ext = extensions[i];
157             IEditorDescriptor ddesc = registry.getDefaultEditor(ext[0]);
158             if (ddesc != null && ddesc.getId().equals(oldId)) {
159                 registry.setDefaultEditor(ext[1], newId);
160             }
161         }
162     }
163 }
Popular Tags