A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / config / 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.phpdt.httpquery.config;
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.phpdt.phphelp.PHPHelpPlugin;
21
22 import org.eclipse.core.runtime.Preferences;
23
24 /**
25  * 
26  */
27 public class ConfigurationManager {
28         private static final int ADD = 0;
29
30         private static final int CHANGE = 1;
31
32         private static final int REMOVE = 2;
33
34         // configurations
35         protected List configurations;
36
37         protected Map threads = new HashMap();
38
39         protected List configurationListeners = new ArrayList();
40
41         private Preferences.IPropertyChangeListener pcl;
42
43         protected boolean ignorePreferenceChanges = false;
44
45         protected static ConfigurationManager instance;
46
47         public static ConfigurationManager getInstance() {
48                 if (instance == null)
49                         instance = new ConfigurationManager();
50                 return instance;
51         }
52
53         private ConfigurationManager() {
54                 loadConfigurations();
55
56                 pcl = new Preferences.IPropertyChangeListener() {
57                         public void propertyChange(Preferences.PropertyChangeEvent event) {
58                                 if (ignorePreferenceChanges)
59                                         return;
60                                 String property = event.getProperty();
61                                 if (property.equals(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS)) {
62                                         loadConfigurations();
63                                 }
64                         }
65                 };
66
67                 PHPHelpPlugin.getDefault().getPluginPreferences()
68                                 .addPropertyChangeListener(pcl);
69         }
70
71         protected void dispose() {
72                 PHPHelpPlugin.getDefault().getPluginPreferences()
73                                 .removePropertyChangeListener(pcl);
74         }
75
76         public IConfigurationWorkingCopy createConfiguration() {
77                 return new ConfigurationWorkingCopy();
78         }
79
80         public List getConfigurations() {
81                 return new ArrayList(configurations);
82         }
83
84         protected void addConfiguration(IConfiguration configuration) {
85                 if (!configurations.contains(configuration))
86                         configurations.add(configuration);
87                 fireConfigurationEvent(configuration, ADD);
88                 saveConfigurations();
89         }
90
91         protected boolean isActive(IConfiguration configuration) {
92                 return (threads.get(configuration) != null);
93         }
94
95         protected void removeConfiguration(IConfiguration configuration) {
96                 configurations.remove(configuration);
97                 fireConfigurationEvent(configuration, REMOVE);
98                 saveConfigurations();
99         }
100
101         protected void configurationChanged(IConfiguration configuration) {
102                 fireConfigurationEvent(configuration, CHANGE);
103                 saveConfigurations();
104         }
105
106         /**
107          * Add monitor listener.
108          * 
109          * @param listener
110          */
111         public void addConfigurationListener(IConfigurationListener listener) {
112                 configurationListeners.add(listener);
113         }
114
115         /**
116          * Remove monitor listener.
117          * 
118          * @param listener
119          */
120         public void removeConfigurationListener(IConfigurationListener listener) {
121                 configurationListeners.remove(listener);
122         }
123
124         /**
125          * Fire a monitor event.
126          * 
127          * @param rr
128          * @param fType
129          */
130         protected void fireConfigurationEvent(IConfiguration configuration, int type) {
131                 Object[] obj = configurationListeners.toArray();
132
133                 int size = obj.length;
134                 for (int i = 0; i < size; i++) {
135                         IConfigurationListener listener = (IConfigurationListener) obj[i];
136                         if (type == ADD)
137                                 listener.configurationAdded(configuration);
138                         else if (type == CHANGE)
139                                 listener.configurationChanged(configuration);
140                         else if (type == REMOVE)
141                                 listener.configurationRemoved(configuration);
142                 }
143         }
144
145         protected void loadConfigurations() {
146
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) {
152                         try {
153                                 ByteArrayInputStream in = new ByteArrayInputStream(xmlString
154                                                 .getBytes());
155                                 IMemento memento = XMLMemento.loadMemento(in);
156
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);
164                                         }
165                                 }
166                         } catch (Exception e) {
167                         }
168                 }
169         }
170
171         protected void saveConfigurations() {
172                 try {
173                         ignorePreferenceChanges = true;
174                         XMLMemento memento = XMLMemento
175                                         .createWriteRoot(PHPHelpPlugin.PREF_STRING_CONFIGURATIONS);
176
177                         Iterator iterator = configurations.iterator();
178                         while (iterator.hasNext()) {
179                                 Configuration monitor = (Configuration) iterator.next();
180                                 IMemento child = memento.createChild("config");
181                                 monitor.save(child);
182                         }
183
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) {
190                 }
191                 ignorePreferenceChanges = false;
192         }
193 }