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