|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.s60tools.apiquery.shared.datatypes.config; |
|
19 |
|
20 import java.io.IOException; |
|
21 import java.util.ArrayList; |
|
22 import java.util.Collection; |
|
23 import java.util.Iterator; |
|
24 import java.util.LinkedHashMap; |
|
25 import java.util.Map; |
|
26 |
|
27 import com.nokia.s60tools.apiquery.shared.resources.Messages; |
|
28 |
|
29 /** |
|
30 * Abstract entry storage base class that contains only |
|
31 * the methods that are common for all entry types. |
|
32 * |
|
33 * To be subclassed in order to create concrete entry storage. |
|
34 */ |
|
35 public abstract class AbstractEntryStorage { |
|
36 |
|
37 /** |
|
38 * Entry storage Map. |
|
39 */ |
|
40 protected Map<String, AbstractEntry> entriesMap = new LinkedHashMap<String, AbstractEntry>(); |
|
41 /** |
|
42 * Configuration change listener list. |
|
43 */ |
|
44 protected ArrayList<IConfigurationChangedListener> configChangeListeners = new ArrayList<IConfigurationChangedListener>(); |
|
45 |
|
46 /** |
|
47 * Constructor |
|
48 */ |
|
49 protected AbstractEntryStorage(){ |
|
50 } |
|
51 |
|
52 /** |
|
53 * Adds configuration change listener. |
|
54 * @param listener Configuration change listener. |
|
55 */ |
|
56 public void addConfigurationChangeListener(IConfigurationChangedListener listener) { |
|
57 configChangeListeners.add(listener); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Removes configuration change listener. |
|
62 * @param listener Configuration change listener. |
|
63 */ |
|
64 public void removeConfigurationChangeListener(IConfigurationChangedListener listener) { |
|
65 configChangeListeners.remove(listener); |
|
66 } |
|
67 |
|
68 /** |
|
69 * Notifies listeners about the configuration change. |
|
70 * @param eventType Event type . |
|
71 */ |
|
72 public void notifyConfigurationChangeListeners(int eventType) { |
|
73 for (IConfigurationChangedListener listener : configChangeListeners) { |
|
74 listener.configurationChanged(eventType); |
|
75 } |
|
76 } |
|
77 |
|
78 /** |
|
79 * Adds an entry to the storage. |
|
80 * @param entry Entry to be added. |
|
81 * @throws DuplicateEntryException |
|
82 */ |
|
83 public void addEntry(AbstractEntry entry) throws DuplicateEntryException { |
|
84 if(entriesMap.get(entry.getId()) != null){ |
|
85 String duplicateEntriesErrMsg = Messages.getString("AbstractEntryStorage.Duplicate_ErrMsg"); //$NON-NLS-1$ |
|
86 throw new DuplicateEntryException(duplicateEntriesErrMsg); |
|
87 } |
|
88 entriesMap.put(entry.getId(), entry); |
|
89 notifyConfigurationChangeListeners(IConfigurationChangedListener.ENTRY_ADDED); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Updates an entry to the storage. |
|
94 * @param entryWithNewData Entry object containing new data. |
|
95 * @throws EntryNotFoundException |
|
96 */ |
|
97 public void updateEntry(AbstractEntry entryWithNewData) throws EntryNotFoundException { |
|
98 AbstractEntry entryWithOldData = (AbstractEntry) entriesMap.get(entryWithNewData.getId()); |
|
99 if(entryWithOldData == null){ |
|
100 String nonExistingEntryMsg = Messages.getString("AbstractEntryStorage.NonExistingEntry_ErrMsg") + entryWithNewData.getId(); //$NON-NLS-1$ |
|
101 throw new EntryNotFoundException(nonExistingEntryMsg); |
|
102 } |
|
103 // Updating data fields (which triggers notification to configuration change listeners) |
|
104 entryWithOldData.updateDataFields(entryWithNewData); |
|
105 } |
|
106 |
|
107 /** |
|
108 * Removes the given entry from the storage. |
|
109 * @param entry Entry. |
|
110 */ |
|
111 public void removeEntry(AbstractEntry entry) { |
|
112 entriesMap.remove(entry.getId()); |
|
113 notifyConfigurationChangeListeners(IConfigurationChangedListener.ENTRY_REMOVED); |
|
114 } |
|
115 |
|
116 /** |
|
117 * Remove all entrys. |
|
118 */ |
|
119 public void removeAllEntrys() { |
|
120 entriesMap.clear(); |
|
121 notifyConfigurationChangeListeners(IConfigurationChangedListener.ALL_ENTRYS_REMOVED); |
|
122 } |
|
123 |
|
124 |
|
125 /** |
|
126 * Returns the iterator for the currently registered entries. |
|
127 * @return Returns the currently registered entries. |
|
128 */ |
|
129 public Collection<AbstractEntry> getEntries() { |
|
130 return entriesMap.values(); |
|
131 } |
|
132 |
|
133 /** |
|
134 * Gets the amount of currently registered entries. |
|
135 * @return Returns the amount of currently registered entries. |
|
136 */ |
|
137 public int getEntryCount() { |
|
138 return getEntries().size(); |
|
139 } |
|
140 |
|
141 /** |
|
142 * Gets the entry that matches with the given id. |
|
143 * @param entryId Id for the entry. |
|
144 * @return The entry matching the id or <code>null</code> if not found. |
|
145 */ |
|
146 public AbstractEntry getByEntryId(String entryId) { |
|
147 for (AbstractEntry entry : getEntries()) { |
|
148 if(entry.getId().equals(entryId)){ |
|
149 return entry; |
|
150 } |
|
151 } |
|
152 return null; |
|
153 } |
|
154 |
|
155 |
|
156 /** |
|
157 * Returns those entries that are selected by used from entry storage. |
|
158 * @return Colletion of selected entries. |
|
159 */ |
|
160 public Collection<AbstractEntry> getSelectedEntriesCollection() { |
|
161 ArrayList<AbstractEntry> selectedList = new ArrayList<AbstractEntry>(); |
|
162 for (Iterator<AbstractEntry> iter = getEntries().iterator(); iter.hasNext();) { |
|
163 AbstractEntry entry = (AbstractEntry) iter.next(); |
|
164 if(entry.isSelected()){ |
|
165 selectedList.add(entry); |
|
166 } |
|
167 } |
|
168 return selectedList; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Returns those entries that are selected by used from entry storage. |
|
173 * @return Array of selected entries. |
|
174 */ |
|
175 public AbstractEntry[] getSelectedEntries() { |
|
176 ArrayList<AbstractEntry> selectedList = new ArrayList<AbstractEntry>(); |
|
177 for (Iterator<AbstractEntry> iter = getEntries().iterator(); iter.hasNext();) { |
|
178 AbstractEntry entry = (AbstractEntry) iter.next(); |
|
179 if(entry.isSelected()){ |
|
180 selectedList.add(entry); |
|
181 } |
|
182 } |
|
183 return selectedList.toArray(new AbstractEntry[0]); |
|
184 } |
|
185 |
|
186 /** |
|
187 * Checks if the given entry is part of the storage. |
|
188 * @param entry Entry object. |
|
189 * @return <code>true</code> if part of the storage, otherwise <code>false</code>. |
|
190 */ |
|
191 public boolean contains(AbstractEntry entry) { |
|
192 return entriesMap.containsKey(entry.getId()); |
|
193 } |
|
194 |
|
195 /** |
|
196 * Informs storage that an entry has been modified. Storage |
|
197 * informs further the configuration listeners, if this |
|
198 * entry was part active configuration. |
|
199 * @param entry Entry object. |
|
200 * @param eventType Event type for modification. |
|
201 */ |
|
202 public void entryModified(AbstractEntry entry, int eventType) { |
|
203 if(contains(entry)){ |
|
204 // Active configuration has been changed. |
|
205 notifyConfigurationChangeListeners(eventType); |
|
206 } |
|
207 } |
|
208 |
|
209 /** |
|
210 * Selects all stored entries and notifies listeners. |
|
211 */ |
|
212 public void selectAll() { |
|
213 for (AbstractEntry entry : getEntries()) { |
|
214 ((AbstractEntry)entry).setSelected(true); |
|
215 } |
|
216 notifyConfigurationChangeListeners(IConfigurationChangedListener.ALL_ENTRIES_CHECKED); |
|
217 } |
|
218 |
|
219 /** |
|
220 * Selects all stored entries and notifies listeners. |
|
221 */ |
|
222 public void deselectAll() { |
|
223 for (AbstractEntry entry : getEntries()) { |
|
224 ((AbstractEntry)entry).setSelected(false); |
|
225 } |
|
226 notifyConfigurationChangeListeners(IConfigurationChangedListener.ALL_ENTRIES_UNCHECKED); |
|
227 } |
|
228 |
|
229 /** |
|
230 * Selects all stored entries and notifies listeners. |
|
231 * @param notifyListenersOnlyWhenAllDeselected if <code>true</code> listeners will be |
|
232 * notified only when all entrys is deselected, not one by one when deselecting entrys. |
|
233 */ |
|
234 public void deselectAll(boolean notifyListenersOnlyWhenAllDeselected) { |
|
235 |
|
236 if(notifyListenersOnlyWhenAllDeselected){ |
|
237 for (AbstractEntry entry : getEntries()) { |
|
238 ((AbstractEntry)entry).setSelected(false, true); |
|
239 } |
|
240 notifyConfigurationChangeListeners(IConfigurationChangedListener.ALL_ENTRIES_UNCHECKED); |
|
241 }else{ |
|
242 deselectAll(); |
|
243 } |
|
244 |
|
245 } |
|
246 |
|
247 /** |
|
248 * Saves the configuration into an external configuration file. |
|
249 * <b>Note</b>: Overwrites the contents of the external file. |
|
250 * @param destinationFileAbsolutePathName Absolute pathname for the external configuration file to be used. |
|
251 * @throws IOException |
|
252 */ |
|
253 public abstract void save(String destinationFileAbsolutePathName) throws IOException; |
|
254 |
|
255 /** |
|
256 * Loads the configuration into an external configuration file. |
|
257 * @param storageFilePathName Absolute path name for the configuration file. |
|
258 * @throws IOException |
|
259 */ |
|
260 public abstract void load(String storageFilePathName) throws IOException; |
|
261 } |