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
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.httpquery.config;
13 import java.io.ByteArrayInputStream;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
20 import net.sourceforge.phpdt.phphelp.PHPHelpPlugin;
22 import org.eclipse.core.runtime.Preferences;
26 public class ConfigurationManager {
27 private static final int ADD = 0;
28 private static final int CHANGE = 1;
29 private static final int REMOVE = 2;
32 protected List configurations;
33 protected Map threads = new HashMap();
35 protected List configurationListeners = new ArrayList();
37 private Preferences.IPropertyChangeListener pcl;
38 protected boolean ignorePreferenceChanges = false;
40 protected static ConfigurationManager instance;
42 public static ConfigurationManager getInstance() {
44 instance = new ConfigurationManager();
48 private ConfigurationManager() {
51 pcl = new Preferences.IPropertyChangeListener() {
52 public void propertyChange(Preferences.PropertyChangeEvent event) {
53 if (ignorePreferenceChanges)
55 String property = event.getProperty();
56 if (property.equals(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS)) {
62 PHPHelpPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(pcl);
65 protected void dispose() {
66 PHPHelpPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(pcl);
69 public IConfigurationWorkingCopy createConfiguration() {
70 return new ConfigurationWorkingCopy();
73 public List getConfigurations() {
74 return new ArrayList(configurations);
77 protected void addConfiguration(IConfiguration configuration) {
78 if (!configurations.contains(configuration))
79 configurations.add(configuration);
80 fireConfigurationEvent(configuration, ADD);
84 protected boolean isActive(IConfiguration configuration) {
85 return (threads.get(configuration) != null);
88 protected void removeConfiguration(IConfiguration configuration) {
89 configurations.remove(configuration);
90 fireConfigurationEvent(configuration, REMOVE);
94 protected void configurationChanged(IConfiguration configuration) {
95 fireConfigurationEvent(configuration, CHANGE);
100 * Add monitor listener.
104 public void addConfigurationListener(IConfigurationListener listener) {
105 configurationListeners.add(listener);
109 * Remove monitor listener.
113 public void removeConfigurationListener(IConfigurationListener listener) {
114 configurationListeners.remove(listener);
118 * Fire a monitor event.
122 protected void fireConfigurationEvent(IConfiguration configuration, int type) {
123 Object[] obj = configurationListeners.toArray();
125 int size = obj.length;
126 for (int i = 0; i < size; i++) {
127 IConfigurationListener listener = (IConfigurationListener) obj[i];
129 listener.configurationAdded(configuration);
130 else if (type == CHANGE)
131 listener.configurationChanged(configuration);
132 else if (type == REMOVE)
133 listener.configurationRemoved(configuration);
140 protected void loadConfigurations() {
142 configurations = new ArrayList();
143 Preferences prefs = PHPHelpPlugin.getDefault().getPluginPreferences();
144 String xmlString = prefs.getString(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS);
145 if (xmlString != null && xmlString.length() > 0) {
147 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
148 IMemento memento = XMLMemento.loadMemento(in);
150 IMemento[] children = memento.getChildren("config");
151 if (children != null) {
152 int size = children.length;
153 for (int i = 0; i < size; i++) {
154 Configuration configuration = new ConfigurationWorkingCopy();
155 configuration.load(children[i]);
156 configurations.add(configuration);
159 } catch (Exception e) {
164 protected void saveConfigurations() {
166 ignorePreferenceChanges = true;
167 XMLMemento memento = XMLMemento.createWriteRoot(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS);
169 Iterator iterator = configurations.iterator();
170 while (iterator.hasNext()) {
171 Configuration monitor = (Configuration) iterator.next();
172 IMemento child = memento.createChild("config");
176 String xmlString = memento.saveToString();
177 Preferences prefs = PHPHelpPlugin.getDefault().getPluginPreferences();
178 prefs.setValue(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS, xmlString);
179 PHPHelpPlugin.getDefault().savePluginPreferences();
180 } catch (Exception e) {
182 ignorePreferenceChanges = false;