Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68878e6af33d46f3fed43d38c946ca957f50fe70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (c) 2011, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.help.ui.internal.preferences;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.IHelpBaseConstants;
import org.osgi.service.prefs.BackingStoreException;


public class ICPreferences {

	public final static String DELIMITER = ","; //$NON-NLS-1$


	public static void setICs(List<IC> ics)
	{
		String name = "", host = "", path = "", protocol="", port = "", enabled = ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$

		for (int i=0;i<ics.size();i++)
		{
			name += ics.get(i).getName() + DELIMITER;
			protocol += ics.get(i).getProtocol() + DELIMITER;
			host += ics.get(i).getHost() + DELIMITER;
			port += ics.get(i).getPort() + DELIMITER;
			path += ics.get(i).getPath() + DELIMITER;
			enabled += ics.get(i).isEnabled() + DELIMITER;
		}

		// Remove trailing commas
		if(ics.size()!=0)
		{
			name = name.substring(0,name.length()-1);
			protocol = protocol.substring(0,protocol.length()-1);
			host = host.substring(0,host.length()-1);
			port = port.substring(0,port.length()-1);
			path = path.substring(0,path.length()-1);
			enabled = enabled.substring(0,enabled.length()-1);
		}

		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_NAME, name); //$NON-NLS-1$
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST, host); //$NON-NLS-1$
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH, path); //$NON-NLS-1$
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PROTOCOL, protocol); //$NON-NLS-1$
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT, port); //$NON-NLS-1$
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_ICEnabled, enabled); //$NON-NLS-1$

		HelpPlugin.getTocManager().clearCache();
	}

	public static List<IC> getICs()
	{
		return prefsToICs(
				ICPreferences.get("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_NAME).split(DELIMITER), //$NON-NLS-1$
				ICPreferences.get("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PROTOCOL).split(DELIMITER), //$NON-NLS-1$
				ICPreferences.get("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST).split(DELIMITER), //$NON-NLS-1$
				ICPreferences.get("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT).split(DELIMITER), //$NON-NLS-1$
				ICPreferences.get("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH).split(DELIMITER), //$NON-NLS-1$
				ICPreferences.get("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_ICEnabled).split(DELIMITER)); //$NON-NLS-1$
	}

	public static List<IC> prefsToICs(String names[],String protocols[],String hosts[],String ports[],String paths[],String states[])
	{
		List<IC> ics = new ArrayList<>();

		for (int i=0;i<names.length;i++)
		{
			if (!names[i].equals("")) //$NON-NLS-1$
			{
				try {
					IC ic = new IC(
						names[i],
						(protocols.length>i ? protocols[i] : "http") + "://" + //$NON-NLS-1$ //$NON-NLS-2$
						hosts[i]+":"+ //$NON-NLS-1$
						ports[i]+
						paths[i],
						"true".equalsIgnoreCase(states[i])); //$NON-NLS-1$
					ics.add(ic);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return ics;
	}



	public static List<IC> getDefaultICs()
	{
		return prefsToICs(
				getDefault("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_NAME).split(DELIMITER), //$NON-NLS-1$
				getDefault("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PROTOCOL).split(DELIMITER), //$NON-NLS-1$
				getDefault("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_HOST).split(DELIMITER), //$NON-NLS-1$
				getDefault("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PORT).split(DELIMITER), //$NON-NLS-1$
				getDefault("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PATH).split(DELIMITER), //$NON-NLS-1$
				getDefault("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_ICEnabled).split(DELIMITER)); //$NON-NLS-1$
	}


	/**
	 * Returns a default preference for the given name
	 *
	 * @param plugin - Name of the plugin containing this preference
	 * @param name - Name of the preference to retrieve
	 * @return value, or empty string if no preference found
	 */
	public static String getDefault(String plugin,String name)
	{
		return getDefaultNode(plugin).get(name, ""); //$NON-NLS-1$
	}

	public static void setRemoteHelp(boolean enabled)
	{
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_ON,enabled+""); //$NON-NLS-1$ //$NON-NLS-2$
		HelpPlugin.getTocManager().clearCache();
	}

	public static void setRemoteHelpPreferred(boolean remotePreferred)
	{
		set("org.eclipse.help.base",IHelpBaseConstants.P_KEY_REMOTE_HELP_PREFERRED,remotePreferred+""); //$NON-NLS-1$ //$NON-NLS-2$
		HelpPlugin.getTocManager().clearCache();
	}

	/**
	 * Sets a preference
	 *
	 * @param plugin - Name of the plugin containing this preference
	 * @param name - Name of the preference
	 * @param value - Value to set
	 */
	public static void set(String plugin,String name,String value)
	{
		set(getNode(plugin),name,value);
	}

	/**
	 * Set a preference in the given node.
	 *
	 * @param node
	 * @param name
	 * @param value
	 */
	public static void set(IEclipsePreferences node,String name,String value)
	{
		node.put(name, value);
		try {
			node.flush();
		} catch (BackingStoreException e) {} //Nothing we can do, move on
	}

	/**
	 * Returns the preference found for the given name
	 *
	 * @param plugin - Name of the plugin containing this preference
	 * @param name - Name of the preference to retrieve
	 * @return value, or empty string if no preference found
	 */
	public static String get(String plugin,String name)
	{
		return getNode(plugin).get(name, ""); //$NON-NLS-1$
	}

	/**
	 * Get the IEclipsePreferences node for the given plugin
	 *
	 * @param plugin
	 * @return
	 */
	public static IEclipsePreferences getNode(String plugin)
	{
		IEclipsePreferences p = InstanceScope.INSTANCE.getNode(plugin);
		return p;
	}


	/**
	 * Get the default IEclipsePreferences node for the given plugin
	 *
	 * @param plugin
	 * @return
	 */
	public static IEclipsePreferences getDefaultNode(String plugin)
	{
		IEclipsePreferences p = DefaultScope.INSTANCE.getNode(plugin);
		return p;
	}
}

Back to the top