KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > remote > RemoteHelp


1 /*******************************************************************************
2  * Copyright (c) 2006 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.help.internal.base.remote;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.ListenerList;
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
19 import org.eclipse.help.internal.base.BaseHelpSystem;
20 import org.eclipse.help.internal.base.HelpBasePlugin;
21 import org.eclipse.help.internal.base.IHelpBaseConstants;
22
23 /*
24  * Convenience methods for querying the remote help settings.
25  */

26 public class RemoteHelp {
27
28     private static final String JavaDoc PROTOCOL_HTTP = "http"; //$NON-NLS-1$
29
private static ListenerList listeners;
30     private static Throwable JavaDoc error;
31
32     /*
33      * Adds a listener that will be notified whenever the user changes the
34      * remote help server preferences.
35      */

36     public static void addPreferenceChangeListener(IPreferenceChangeListener listener) {
37         if (listeners == null) {
38             listeners = new ListenerList();
39         }
40         listeners.add(listener);
41     }
42     
43     /*
44      * Removes a listener.
45      */

46     public static void removePreferenceChangeListener(IPreferenceChangeListener listener) {
47         if (listeners != null) {
48             listeners.remove(listener);
49         }
50     }
51     
52     /*
53      * Signals all registered listeners that remote help preferences have
54      * changed.
55      */

56     public static void notifyPreferenceChange() {
57         if (listeners != null) {
58             Object JavaDoc[] array = listeners.getListeners();
59             for (int i=0;i<array.length;++i) {
60                 IPreferenceChangeListener listener = (IPreferenceChangeListener)array[i];
61                 listener.preferenceChange(null);
62             }
63         }
64     }
65     
66     public static URL JavaDoc getURL(String JavaDoc pathSuffix) throws MalformedURLException JavaDoc {
67         String JavaDoc host = RemoteHelp.getHost();
68         String JavaDoc path = RemoteHelp.getPath() + pathSuffix;
69         int port = RemoteHelp.getPort();
70         return new URL JavaDoc(PROTOCOL_HTTP, host, port, path);
71     }
72     
73     /*
74      * Returns whether or not the help system is allowed to be enabled in the
75      * current mode.
76      */

77     public static boolean isAllowed() {
78         return (BaseHelpSystem.getMode() != BaseHelpSystem.MODE_INFOCENTER);
79     }
80     
81     /*
82      * Returns whether or not the help system is currently configured for remote
83      * help content.
84      */

85     public static boolean isEnabled() {
86         if (isAllowed()) {
87             Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
88             return prefs.getBoolean(IHelpBaseConstants.P_KEY_REMOTE_HELP_ON);
89         }
90         return false;
91     }
92     
93     /*
94      * Clears the error status for remote help.
95      */

96     public static void clearError() {
97         error = null;
98     }
99     
100     /*
101      * Returns the error produced during a previous remote help
102      * query, or null if there was none.
103      */

104     public static Throwable JavaDoc getError() {
105         return error;
106     }
107     
108     /*
109      * Sets the latest exception to have occured while communicating
110      * with the remote help server.
111      */

112     public static void setError(Throwable JavaDoc t) {
113         error = t;
114     }
115     
116     /*
117      * Returns the hostname of the remote help server to use, or empty string
118      * if not configured.
119      */

120     private static String JavaDoc getHost() {
121         return HelpBasePlugin.getDefault().getPluginPreferences().getString(IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST);
122     }
123
124     /*
125      * Returns the path of the remote help server to use. Ensures that there
126      * is a leading slash and no trailing slash, e.g. "/myPath"
127      */

128     private static String JavaDoc getPath() {
129         String JavaDoc path = HelpBasePlugin.getDefault().getPluginPreferences().getString(IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH);
130         if (!path.startsWith("/")) { //$NON-NLS-1$
131
path = '/' + path;
132         }
133         if (path.endsWith("/")) { //$NON-NLS-1$
134
path = path.substring(0, path.length() - 1);
135         }
136         return path;
137     }
138
139     /*
140      * Returns the port to use for connecting to the remote help server.
141      */

142     private static int getPort() {
143         Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
144         if (prefs.getBoolean(IHelpBaseConstants.P_KEY_REMOTE_HELP_DEFAULT_PORT) == true) {
145             prefs.getDefaultInt(IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT);
146         }
147         return prefs.getInt(IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT);
148     }
149
150 }
151
Popular Tags