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.phpeclipse.wiki.internal;
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.phpeclipse.webbrowser.internal.Trace;
21 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
23 import org.eclipse.core.runtime.Preferences;
27 public class ConfigurationManager {
28 private static final int ADD = 0;
29 private static final int CHANGE = 1;
30 private static final int REMOVE = 2;
33 protected List configurations;
34 protected Map threads = new HashMap();
36 protected List configurationListeners = new ArrayList();
38 private Preferences.IPropertyChangeListener pcl;
39 protected boolean ignorePreferenceChanges = false;
41 protected static ConfigurationManager instance;
43 public static ConfigurationManager getInstance() {
45 instance = new ConfigurationManager();
49 private ConfigurationManager() {
52 pcl = new Preferences.IPropertyChangeListener() {
53 public void propertyChange(Preferences.PropertyChangeEvent event) {
54 if (ignorePreferenceChanges)
56 String property = event.getProperty();
57 if (property.equals("configurations")) {
63 WikiEditorPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(pcl);
66 protected void dispose() {
67 WikiEditorPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(pcl);
70 public IConfigurationWorkingCopy createConfiguration() {
71 return new ConfigurationWorkingCopy();
74 public List getConfigurations() {
75 return new ArrayList(configurations);
78 protected void addConfiguration(IConfiguration configuration) {
79 if (!configurations.contains(configuration))
80 configurations.add(configuration);
81 fireConfigurationEvent(configuration, ADD);
85 protected boolean isActive(IConfiguration configuration) {
86 return (threads.get(configuration) != null);
89 protected void removeConfiguration(IConfiguration configuration) {
90 configurations.remove(configuration);
91 fireConfigurationEvent(configuration, REMOVE);
95 protected void configurationChanged(IConfiguration configuration) {
96 fireConfigurationEvent(configuration, CHANGE);
101 * Add monitor listener.
105 public void addConfigurationListener(IConfigurationListener listener) {
106 configurationListeners.add(listener);
110 * Remove monitor listener.
114 public void removeConfigurationListener(IConfigurationListener listener) {
115 configurationListeners.remove(listener);
119 * Fire a monitor event.
123 protected void fireConfigurationEvent(IConfiguration configuration, int type) {
124 Object[] obj = configurationListeners.toArray();
126 int size = obj.length;
127 for (int i = 0; i < size; i++) {
128 IConfigurationListener listener = (IConfigurationListener) obj[i];
130 listener.configurationAdded(configuration);
131 else if (type == CHANGE)
132 listener.configurationChanged(configuration);
133 else if (type == REMOVE)
134 listener.configurationRemoved(configuration);
141 protected void loadConfigurations() {
142 Trace.trace(Trace.FINEST, "Loading Configurations");
144 configurations = new ArrayList();
145 Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences();
146 String xmlString = prefs.getString(WikiEditorPlugin.PREF_STRING_CONFIGURATIONS);
147 if (xmlString != null && xmlString.length() > 0) {
149 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
150 IMemento memento = XMLMemento.loadMemento(in);
152 IMemento[] children = memento.getChildren("config");
153 if (children != null) {
154 int size = children.length;
155 for (int i = 0; i < size; i++) {
156 Configuration configuration = new ConfigurationWorkingCopy();
157 configuration.load(children[i]);
158 configurations.add(configuration);
161 } catch (Exception e) {
162 Trace.trace(Trace.WARNING, "Could not load configurations: " + e.getMessage());
167 protected void saveConfigurations() {
169 ignorePreferenceChanges = true;
170 XMLMemento memento = XMLMemento.createWriteRoot("configurations");
172 Iterator iterator = configurations.iterator();
173 while (iterator.hasNext()) {
174 Configuration monitor = (Configuration) iterator.next();
175 IMemento child = memento.createChild("config");
179 String xmlString = memento.saveToString();
180 Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences();
181 prefs.setValue("configurations", xmlString);
182 WikiEditorPlugin.getDefault().savePluginPreferences();
183 } catch (Exception e) {
184 Trace.trace(Trace.SEVERE, "Could not save Configurations", e);
186 ignorePreferenceChanges = false;