|
1 /* |
|
2 * Copyright (c) 2009 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 the License "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.carbide.cpp.internal.pi.model; |
|
19 |
|
20 import java.io.File; |
|
21 import java.io.FileNotFoundException; |
|
22 import java.io.FileReader; |
|
23 import java.io.FileWriter; |
|
24 import java.io.IOException; |
|
25 import java.util.ArrayList; |
|
26 import java.util.Arrays; |
|
27 import java.util.Collection; |
|
28 import java.util.HashSet; |
|
29 import java.util.Iterator; |
|
30 |
|
31 import org.eclipse.ui.IMemento; |
|
32 import org.eclipse.ui.XMLMemento; |
|
33 |
|
34 import com.nokia.carbide.cpp.internal.pi.analyser.PIChangeEvent; |
|
35 import com.nokia.carbide.cpp.internal.pi.interfaces.IPiItem; |
|
36 import com.nokia.carbide.cpp.pi.PiPlugin; |
|
37 |
|
38 |
|
39 |
|
40 public class PiManager { |
|
41 private static PiManager manager; |
|
42 private Collection<IPiItem> favorites; |
|
43 private ArrayList<PiManagerListener> listeners = new ArrayList<PiManagerListener>(); |
|
44 |
|
45 private PiManager() { |
|
46 } |
|
47 |
|
48 ///////////// IPiItem accessors /////////////////////// |
|
49 |
|
50 public static PiManager getManager() { |
|
51 if (manager == null) |
|
52 manager = new PiManager(); |
|
53 return manager; |
|
54 } |
|
55 |
|
56 public void synchronizePI() { |
|
57 PIChangeEvent.action("synchronise"); //$NON-NLS-1$ |
|
58 } |
|
59 |
|
60 public IPiItem[] getFavorites() { |
|
61 if (favorites == null) |
|
62 loadFavorites(); |
|
63 return favorites.toArray( |
|
64 new IPiItem[favorites.size()]); |
|
65 } |
|
66 |
|
67 public IPiItem newFavoriteFor(Object obj) { |
|
68 PiItemType[] types = |
|
69 PiItemType.getTypes(); |
|
70 for (int i = 0; i < types.length; i++) { |
|
71 IPiItem item = types[i].newFavorite(obj); |
|
72 if (item != null) |
|
73 return item; |
|
74 } |
|
75 return null; |
|
76 } |
|
77 |
|
78 public IPiItem[] newFavoritesFor(Iterator iter) { |
|
79 if (iter == null) |
|
80 return IPiItem.NONE; |
|
81 Collection<IPiItem> items = new HashSet<IPiItem>(20); |
|
82 while (iter.hasNext()) { |
|
83 IPiItem item = |
|
84 newFavoriteFor((Object) iter.next()); |
|
85 if (item != null) |
|
86 items.add(item); |
|
87 } |
|
88 return (IPiItem[]) items.toArray( |
|
89 new IPiItem[items.size()]); |
|
90 } |
|
91 |
|
92 public IPiItem[] newFavoritesFor(Object[] objects) { |
|
93 if (objects == null) |
|
94 return IPiItem.NONE; |
|
95 return newFavoritesFor( |
|
96 Arrays.asList(objects).iterator()); |
|
97 } |
|
98 |
|
99 public IPiItem existingFavoriteFor(Object obj) { |
|
100 if (obj == null) |
|
101 return null; |
|
102 Iterator<IPiItem> iter = favorites.iterator(); |
|
103 while (iter.hasNext()) { |
|
104 IPiItem item = iter.next(); |
|
105 if (item.isPIFor(obj)) |
|
106 return item; |
|
107 } |
|
108 return null; |
|
109 } |
|
110 |
|
111 public IPiItem[] existingFavoritesFor(Iterator iter) |
|
112 { |
|
113 ArrayList<IPiItem> result = new ArrayList<IPiItem>(10); |
|
114 while (iter.hasNext()) { |
|
115 IPiItem item = |
|
116 existingFavoriteFor(iter.next()); |
|
117 if (item != null) |
|
118 result.add(item); |
|
119 } |
|
120 return (IPiItem[]) result.toArray( |
|
121 new IPiItem[result.size()]); |
|
122 } |
|
123 |
|
124 public void addFavorites(IPiItem[] items) { |
|
125 if (favorites == null) |
|
126 loadFavorites(); |
|
127 if (favorites.addAll(Arrays.asList(items))) |
|
128 fireFavoritesChanged(items, IPiItem.NONE); |
|
129 } |
|
130 |
|
131 public void removeFavorites(IPiItem[] items) { |
|
132 if (favorites == null) |
|
133 loadFavorites(); |
|
134 if (favorites.removeAll(Arrays.asList(items))) |
|
135 fireFavoritesChanged(IPiItem.NONE, items); |
|
136 } |
|
137 |
|
138 /////////// Loading and Saving Favorites ///////////////// |
|
139 |
|
140 private static final String TAG_FAVORITES = "Favorites"; //$NON-NLS-1$ |
|
141 private static final String TAG_FAVORITE = "Favorite"; //$NON-NLS-1$ |
|
142 private static final String TAG_TYPEID = "TypeId"; //$NON-NLS-1$ |
|
143 private static final String TAG_INFO = "Info"; //$NON-NLS-1$ |
|
144 |
|
145 private void loadFavorites() { |
|
146 favorites = new HashSet<IPiItem>(20); |
|
147 FileReader reader = null; |
|
148 try { |
|
149 reader = new FileReader(getFavoritesFile()); |
|
150 loadFavorites(XMLMemento.createReadRoot(reader)); |
|
151 } |
|
152 catch (FileNotFoundException e) { |
|
153 // ignored... no favorites exist yet |
|
154 } |
|
155 catch (Exception e) { |
|
156 // log the exception and move on |
|
157 // PILog.logError(e); |
|
158 } |
|
159 finally { |
|
160 try { |
|
161 if (reader != null) |
|
162 reader.close(); |
|
163 } |
|
164 catch (IOException e) { |
|
165 // PILog.logError(e); |
|
166 } |
|
167 } |
|
168 } |
|
169 |
|
170 private void loadFavorites(XMLMemento memento) { |
|
171 IMemento[] children = |
|
172 memento.getChildren(TAG_FAVORITE); |
|
173 for (int i = 0; i < children.length; i++) { |
|
174 IPiItem item = |
|
175 newFavoriteFor( |
|
176 children[i].getString(TAG_TYPEID), |
|
177 children[i].getString(TAG_INFO)); |
|
178 if (item != null) |
|
179 favorites.add(item); |
|
180 } |
|
181 } |
|
182 |
|
183 public IPiItem newFavoriteFor(String typeId, String info) { |
|
184 PiItemType[] types = PiItemType.getTypes(); |
|
185 for (int i = 0; i < types.length; i++) |
|
186 if (types[i].getID().equals(typeId)) |
|
187 return types[i].loadFavorite(info); |
|
188 return null; |
|
189 } |
|
190 |
|
191 public void saveFavorites() { |
|
192 if (favorites == null) |
|
193 return; |
|
194 XMLMemento memento = |
|
195 XMLMemento.createWriteRoot(TAG_FAVORITES); |
|
196 saveFavorites(memento); |
|
197 FileWriter writer = null; |
|
198 try { |
|
199 writer = new FileWriter(getFavoritesFile()); |
|
200 memento.save(writer); |
|
201 } |
|
202 catch (IOException e) { |
|
203 // PILog.logError(e); |
|
204 } |
|
205 finally { |
|
206 try { |
|
207 if (writer != null) |
|
208 writer.close(); |
|
209 } |
|
210 catch (IOException e) { |
|
211 // PILog.logError(e); |
|
212 } |
|
213 } |
|
214 } |
|
215 |
|
216 private void saveFavorites(XMLMemento memento) { |
|
217 Iterator iter = favorites.iterator(); |
|
218 while (iter.hasNext()) { |
|
219 IPiItem item = (IPiItem) iter.next(); |
|
220 IMemento child = memento.createChild(TAG_FAVORITE); |
|
221 child.putString(TAG_TYPEID, item.getType().getID()); |
|
222 child.putString(TAG_INFO, item.getInfo()); |
|
223 } |
|
224 } |
|
225 |
|
226 private File getFavoritesFile() { |
|
227 return PiPlugin |
|
228 .getDefault() |
|
229 .getStateLocation() |
|
230 .append(Messages.getString("PiManager.favoritesXmlFile")) //$NON-NLS-1$ |
|
231 .toFile(); |
|
232 } |
|
233 |
|
234 ///////////// FavoriteManagerListener methods /////////////////////// |
|
235 |
|
236 public void addFavoritesManagerListener(PiManagerListener listener) { |
|
237 if (!listeners.contains(listener)) |
|
238 listeners.add(listener); |
|
239 } |
|
240 |
|
241 public void removeFavoritesManagerListener(PiManagerListener listener) { |
|
242 listeners.remove(listener); |
|
243 } |
|
244 |
|
245 private void fireFavoritesChanged(IPiItem[] itemsAdded, IPiItem[] itemsRemoved) { |
|
246 PiManagerEvent event = new PiManagerEvent(this, itemsAdded, itemsRemoved); |
|
247 for (Iterator<PiManagerListener> iter = listeners.iterator(); iter.hasNext();) |
|
248 iter.next().piChanged(event); |
|
249 } |
|
250 |
|
251 ///////////// IResourceChangeListener ////////////////////// |
|
252 |
|
253 } |