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;
27 public class ConfigurationManager {
28 private static final int ADD = 0;
30 private static final int CHANGE = 1;
32 private static final int REMOVE = 2;
35 protected List configurations;
37 protected Map threads = new HashMap();
39 protected List configurationListeners = new ArrayList();
41 private Preferences.IPropertyChangeListener pcl;
43 protected boolean ignorePreferenceChanges = false;
45 protected static ConfigurationManager instance;
47 public static ConfigurationManager getInstance() {
49 instance = new ConfigurationManager();
53 private ConfigurationManager() {
56 pcl = new Preferences.IPropertyChangeListener() {
57 public void propertyChange(Preferences.PropertyChangeEvent event) {
58 if (ignorePreferenceChanges)
60 String property = event.getProperty();
61 if (property.equals(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS)) {
67 PHPHelpPlugin.getDefault().getPluginPreferences()
68 .addPropertyChangeListener(pcl);
71 protected void dispose() {
72 PHPHelpPlugin.getDefault().getPluginPreferences()
73 .removePropertyChangeListener(pcl);
76 public IConfigurationWorkingCopy createConfiguration() {
77 return new ConfigurationWorkingCopy();
80 public List getConfigurations() {
81 return new ArrayList(configurations);
84 protected void addConfiguration(IConfiguration configuration) {
85 if (!configurations.contains(configuration))
86 configurations.add(configuration);
87 fireConfigurationEvent(configuration, ADD);
91 protected boolean isActive(IConfiguration configuration) {
92 return (threads.get(configuration) != null);
95 protected void removeConfiguration(IConfiguration configuration) {
96 configurations.remove(configuration);
97 fireConfigurationEvent(configuration, REMOVE);
101 protected void configurationChanged(IConfiguration configuration) {
102 fireConfigurationEvent(configuration, CHANGE);
103 saveConfigurations();
107 * Add monitor listener.
111 public void addConfigurationListener(IConfigurationListener listener) {
112 configurationListeners.add(listener);
116 * Remove monitor listener.
120 public void removeConfigurationListener(IConfigurationListener listener) {
121 configurationListeners.remove(listener);
125 * Fire a monitor event.
130 protected void fireConfigurationEvent(IConfiguration configuration, int type) {
131 Object[] obj = configurationListeners.toArray();
133 int size = obj.length;
134 for (int i = 0; i < size; i++) {
135 IConfigurationListener listener = (IConfigurationListener) obj[i];
137 listener.configurationAdded(configuration);
138 else if (type == CHANGE)
139 listener.configurationChanged(configuration);
140 else if (type == REMOVE)
141 listener.configurationRemoved(configuration);
145 protected void loadConfigurations() {
147 configurations = new ArrayList();
148 Preferences prefs = PHPHelpPlugin.getDefault().getPluginPreferences();
149 String xmlString = prefs
150 .getString(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS);
151 if (xmlString != null && xmlString.length() > 0) {
153 ByteArrayInputStream in = new ByteArrayInputStream(xmlString
155 IMemento memento = XMLMemento.loadMemento(in);
157 IMemento[] children = memento.getChildren("config");
158 if (children != null) {
159 int size = children.length;
160 for (int i = 0; i < size; i++) {
161 Configuration configuration = new ConfigurationWorkingCopy();
162 configuration.load(children[i]);
163 configurations.add(configuration);
166 } catch (Exception e) {
171 protected void saveConfigurations() {
173 ignorePreferenceChanges = true;
174 XMLMemento memento = XMLMemento
175 .createWriteRoot(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS);
177 Iterator iterator = configurations.iterator();
178 while (iterator.hasNext()) {
179 Configuration monitor = (Configuration) iterator.next();
180 IMemento child = memento.createChild("config");
184 String xmlString = memento.saveToString();
185 Preferences prefs = PHPHelpPlugin.getDefault()
186 .getPluginPreferences();
187 prefs.setValue(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS, xmlString);
188 PHPHelpPlugin.getDefault().savePluginPreferences();
189 } catch (Exception e) {
191 ignorePreferenceChanges = false;