improved settings dialogs
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / internal / ConfigurationManager.java
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpeclipse.wiki.internal;
12
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;
18 import java.util.Map;
19
20 import net.sourceforge.phpeclipse.webbrowser.internal.Trace;
21 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
22
23 import org.eclipse.core.runtime.Preferences;
24 /**
25  * 
26  */
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;
31
32         // configurations
33         protected List configurations;
34         protected Map threads = new HashMap();
35         
36         protected List configurationListeners = new ArrayList();
37         
38         private Preferences.IPropertyChangeListener pcl;
39         protected boolean ignorePreferenceChanges = false;
40         
41         protected static ConfigurationManager instance;
42         
43         public static ConfigurationManager getInstance() {
44                 if (instance == null)
45                         instance = new ConfigurationManager();
46                 return instance;
47         }
48         
49         private ConfigurationManager() {
50                 loadConfigurations();
51                 
52                 pcl = new Preferences.IPropertyChangeListener() {
53                         public void propertyChange(Preferences.PropertyChangeEvent event) {
54                                 if (ignorePreferenceChanges)
55                                         return;
56                                 String property = event.getProperty();
57                                 if (property.equals("configurations")) {
58                                         loadConfigurations();
59                                 }
60                         }
61                 };
62                 
63                 WikiEditorPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(pcl);
64         }
65         
66         protected void dispose() {
67                 WikiEditorPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(pcl);
68         }
69         
70         public IConfigurationWorkingCopy createConfiguration() {
71                 return new ConfigurationWorkingCopy();
72         }
73         
74         public List getConfigurations() {
75                 return new ArrayList(configurations);
76         }
77
78         protected void addConfiguration(IConfiguration configuration) {
79                 if (!configurations.contains(configuration))
80                         configurations.add(configuration);
81                 fireConfigurationEvent(configuration, ADD);
82                 saveConfigurations();
83         }
84         
85         protected boolean isActive(IConfiguration configuration) {
86                 return (threads.get(configuration) != null);
87         }
88
89         protected void removeConfiguration(IConfiguration configuration) {
90                 configurations.remove(configuration);
91                 fireConfigurationEvent(configuration, REMOVE);
92                 saveConfigurations();
93         }
94         
95         protected void configurationChanged(IConfiguration configuration) {
96                 fireConfigurationEvent(configuration, CHANGE);
97                 saveConfigurations();
98         }
99         
100         /**
101          * Add monitor listener.
102          * 
103          * @param listener
104          */
105         public void addConfigurationListener(IConfigurationListener listener) {
106                 configurationListeners.add(listener);
107         }
108
109         /**
110          * Remove monitor listener.
111          * 
112          * @param listener
113          */
114         public void removeConfigurationListener(IConfigurationListener listener) {
115                 configurationListeners.remove(listener);
116         }
117         
118         /**
119          * Fire a monitor event.
120          * @param rr
121          * @param fType
122          */
123         protected void fireConfigurationEvent(IConfiguration configuration, int type) {
124                 Object[] obj = configurationListeners.toArray();
125                 
126                 int size = obj.length;
127                 for (int i = 0; i < size; i++) {
128                         IConfigurationListener listener = (IConfigurationListener) obj[i];
129                         if (type == ADD)
130                                 listener.configurationAdded(configuration);
131                         else if (type == CHANGE)
132                                 listener.configurationChanged(configuration);
133                         else if (type == REMOVE)
134                                 listener.configurationRemoved(configuration);
135                 }
136         }
137         
138         
139         
140         
141         protected void loadConfigurations() {
142                 Trace.trace(Trace.FINEST, "Loading Configurations");
143                 
144                 configurations = new ArrayList();
145                 Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences();
146                 String xmlString = prefs.getString("configurations");
147                 if (xmlString != null && xmlString.length() > 0) {
148                         try {
149                                 ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes());
150                                 IMemento memento = XMLMemento.loadMemento(in);
151                 
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 monitor = new Configuration();
157                                                 monitor.load(children[i]);
158                                                 configurations.add(monitor);
159                                         }
160                                 }
161                         } catch (Exception e) {
162                                 Trace.trace(Trace.WARNING, "Could not load configurations: " + e.getMessage());
163                         }
164                 }
165         }
166         
167         protected void saveConfigurations() {
168                 try {
169                         ignorePreferenceChanges = true;
170                         XMLMemento memento = XMLMemento.createWriteRoot("configurations");
171
172                         Iterator iterator = configurations.iterator();
173                         while (iterator.hasNext()) {
174                                 Configuration monitor = (Configuration) iterator.next();
175                                 IMemento child = memento.createChild("config");
176                                 monitor.save(child);
177                         }
178                         
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);
185                 }
186                 ignorePreferenceChanges = false;
187         }
188 }