initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / internal / ConfigurationManager.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/internal/ConfigurationManager.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/internal/ConfigurationManager.java
new file mode 100644 (file)
index 0000000..034bb14
--- /dev/null
@@ -0,0 +1,188 @@
+/**********************************************************************
+ * Copyright (c) 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ *    IBM - Initial API and implementation
+ **********************************************************************/
+package net.sourceforge.phpeclipse.wiki.internal;
+
+import java.io.ByteArrayInputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import net.sourceforge.phpeclipse.webbrowser.internal.Trace;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+
+import org.eclipse.core.runtime.Preferences;
+/**
+ * 
+ */
+public class ConfigurationManager {
+       private static final int ADD = 0;
+       private static final int CHANGE = 1;
+       private static final int REMOVE = 2;
+
+       // configurations
+       protected List configurations;
+       protected Map threads = new HashMap();
+       
+       protected List monitorListeners = new ArrayList();
+       
+       private Preferences.IPropertyChangeListener pcl;
+       protected boolean ignorePreferenceChanges = false;
+       
+       protected static ConfigurationManager instance;
+       
+       public static ConfigurationManager getInstance() {
+               if (instance == null)
+                       instance = new ConfigurationManager();
+               return instance;
+       }
+       
+       private ConfigurationManager() {
+               loadConfigurations();
+               
+               pcl = new Preferences.IPropertyChangeListener() {
+                       public void propertyChange(Preferences.PropertyChangeEvent event) {
+                               if (ignorePreferenceChanges)
+                                       return;
+                               String property = event.getProperty();
+                               if (property.equals("configurations")) {
+                                       loadConfigurations();
+                               }
+                       }
+               };
+               
+               WikiEditorPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(pcl);
+       }
+       
+       protected void dispose() {
+               WikiEditorPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(pcl);
+       }
+       
+       public IConfigurationWorkingCopy createConfiguration() {
+               return new ConfigurationWorkingCopy();
+       }
+       
+       public List getConfigurations() {
+               return new ArrayList(configurations);
+       }
+
+       protected void addConfiguration(IConfiguration configuration) {
+               if (!configurations.contains(configuration))
+                       configurations.add(configuration);
+               fireConfigurationEvent(configuration, ADD);
+               saveConfigurations();
+       }
+       
+       protected boolean isActive(IConfiguration configuration) {
+               return (threads.get(configuration) != null);
+       }
+
+       protected void removeConfiguration(IConfiguration configuration) {
+               configurations.remove(configuration);
+               fireConfigurationEvent(configuration, REMOVE);
+               saveConfigurations();
+       }
+       
+       protected void configurationChanged(IConfiguration configuration) {
+               fireConfigurationEvent(configuration, CHANGE);
+               saveConfigurations();
+       }
+       
+       /**
+        * Add monitor listener.
+        * 
+        * @param listener
+        */
+       public void addConfigurationListener(IConfigurationListener listener) {
+               monitorListeners.add(listener);
+       }
+
+       /**
+        * Remove monitor listener.
+        * 
+        * @param listener
+        */
+       public void removeConfigurationListener(IConfigurationListener listener) {
+               monitorListeners.remove(listener);
+       }
+       
+       /**
+        * Fire a monitor event.
+        * @param rr
+        * @param fType
+        */
+       protected void fireConfigurationEvent(IConfiguration monitor, int type) {
+               Object[] obj = monitorListeners.toArray();
+               
+               int size = obj.length;
+               for (int i = 0; i < size; i++) {
+                       IConfigurationListener listener = (IConfigurationListener) obj[i];
+                       if (type == ADD)
+                               listener.monitorAdded(monitor);
+                       else if (type == CHANGE)
+                               listener.monitorChanged(monitor);
+                       else if (type == REMOVE)
+                               listener.monitorRemoved(monitor);
+               }
+       }
+       
+       
+       
+       
+       protected void loadConfigurations() {
+               Trace.trace(Trace.FINEST, "Loading Configurations");
+               
+               configurations = new ArrayList();
+               Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences();
+               String xmlString = prefs.getString("configurations");
+               if (xmlString != null && xmlString.length() > 0) {
+                       try {
+                               ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
+                               IMemento memento = XMLMemento.loadMemento(in);
+               
+                               IMemento[] children = memento.getChildren("config");
+                               if (children != null) {
+                                       int size = children.length;
+                                       for (int i = 0; i < size; i++) {
+                                               Configuration monitor = new Configuration();
+                                               monitor.load(children[i]);
+                                               configurations.add(monitor);
+                                       }
+                               }
+                       } catch (Exception e) {
+                               Trace.trace(Trace.WARNING, "Could not load configurations: " + e.getMessage());
+                       }
+               }
+       }
+       
+       protected void saveConfigurations() {
+               try {
+                       ignorePreferenceChanges = true;
+                       XMLMemento memento = XMLMemento.createWriteRoot("configurations");
+
+                       Iterator iterator = configurations.iterator();
+                       while (iterator.hasNext()) {
+                               Configuration monitor = (Configuration) iterator.next();
+                               IMemento child = memento.createChild("config");
+                               monitor.save(child);
+                       }
+                       
+                       String xmlString = memento.saveToString();
+                       Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences();
+                       prefs.setValue("configurations", xmlString);
+                       WikiEditorPlugin.getDefault().savePluginPreferences();
+               } catch (Exception e) {
+                       Trace.trace(Trace.SEVERE, "Could not save Configurations", e);
+               }
+               ignorePreferenceChanges = false;
+       }
+}
\ No newline at end of file