1 19 20 package org.netbeans.modules.tasklist.suggestions; 21 22 import org.netbeans.modules.tasklist.client.SuggestionManager; 23 import org.netbeans.modules.tasklist.client.Suggestion; 24 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 25 import org.netbeans.modules.tasklist.client.SuggestionAgent; 26 27 import java.util.List ; 28 import java.util.ListIterator ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import org.openide.filesystems.FileObject; 32 33 50 public class DefaultSuggestionManager extends SuggestionManager { 51 52 public SuggestionAgent createSuggestion(FileObject fo, String type, 54 String summary, SuggestionPerformer action, Object data) { 55 56 if (summary.indexOf('\n') != -1) { 62 int n = summary.length(); 63 StringBuffer sb = new StringBuffer (2 * n); for (int i = 0; i < n; i++) { 65 char c = summary.charAt(i); 66 if (c == '\n') { 67 sb.append(':'); 68 sb.append(' '); 69 } else if (c != '\r') { 70 sb.append(c); 71 } 72 } 73 summary = sb.toString(); 74 } 75 76 SPIMonitor.log(" create type: " + type + " summary: " + summary); 78 SuggestionType st = SuggestionTypes.getDefault().getType(type); 79 if (st == null) { 80 throw new IllegalArgumentException ("type " + st + " is not registered"); 82 } 83 SuggestionImpl s = new SuggestionImpl(fo, 84 summary, st, action, data); 85 return new SuggestionAgent(s); 86 } 87 88 public boolean isEnabled(String id) { 89 return true; 90 } 91 92 public boolean isObserved(String id) { 93 return true; 94 } 95 96 99 public void register(String type, List add, List remove) { 100 assert false : "This public contract is not implemented use private one!"; 101 } 102 103 104 114 public void register(String typeName, 115 List addList, List removeList, 116 SuggestionList tasklist, boolean sizeKnown) { 117 118 122 123 125 SuggestionType type = null; 129 if (typeName != null) { 130 type = SuggestionTypes.getDefault().getType(typeName); 131 if (type == null) { 132 throw new IllegalArgumentException ("No such SuggestionType: " + typeName); 133 } 134 } 135 136 159 160 161 boolean split = (type == null); 164 ListIterator ita = null; 165 ListIterator itr = null; 166 if (split) { 167 List allAdds = addList; 168 List allRems = removeList; 169 if (allAdds != null) { 170 ita = allAdds.listIterator(); 171 addList = new ArrayList (allAdds.size()); 172 } 173 if (allRems != null) { 174 itr = allRems.listIterator(); 175 removeList = new ArrayList (allRems.size()); 176 } 177 } 178 while (true) { 179 180 if (split) { 183 if ((ita != null) && (ita.hasNext())) { 184 addList.clear(); type = null; 186 while (ita.hasNext()) { 187 SuggestionImpl s = (SuggestionImpl) ita.next(); 188 if (type == null) { 189 type = s.getSType(); 190 } else if (s.getSType() != type) { 191 ita.previous(); break; 193 } 194 addList.add(s); 195 } 196 } else { 197 addList = null; 198 } 199 200 if ((itr != null) && (itr.hasNext())) { 201 removeList.clear(); 202 type = null; 203 while (itr.hasNext()) { 204 SuggestionImpl s = (SuggestionImpl) itr.next(); 205 if (type == null) { 206 type = s.getSType(); 207 } else if (s.getSType() != type) { 208 itr.previous(); break; 210 } 211 removeList.add(s); 212 } 213 } else { 214 removeList = null; 215 } 216 217 if ((addList == null) && (removeList == null)) { 218 break; 219 } 220 } 221 222 SuggestionImpl category = tasklist.getCategoryTask(type, false); 223 224 225 int currnum = 0; 231 if (category != null) { 232 currnum = category.subtasksCount(); 233 } else { 234 Iterator it = tasklist.getTasks().iterator(); 235 while (it.hasNext()) { 236 SuggestionImpl s = (SuggestionImpl) it.next(); 237 if (s.getSType() == type) { 238 currnum++; 239 } 240 } 241 } 242 int addnum = (addList != null) ? addList.size() : 0; 243 int remnum = (removeList != null) ? removeList.size() : 0; 244 int newSize = currnum + addnum - remnum; 246 if ((newSize > tasklist.getGroupTreshold()) && (getUnfilteredType() == null)) { 247 251 if (category == null) { 252 category = tasklist.getCategoryTask(type, true); 255 synchronized (this) { 256 List leftover = null; 257 if (removeList != null) { 258 tasklist.addRemove(null, removeList, true, null, null); 259 } 260 if (currnum - remnum > 0) { 261 leftover = new ArrayList (currnum); 262 Iterator it = tasklist.getTasks().iterator(); 263 while (it.hasNext()) { 264 SuggestionImpl s = (SuggestionImpl) it.next(); 265 if ((s.getSType() == type) && 266 (s != category)) { 267 leftover.add(s); 268 } 269 } 270 } 271 if ((leftover != null) && (leftover.size() > 0)) { 272 tasklist.addRemove(null, leftover, false, null, null); 273 tasklist.addRemove(leftover, null, true, category, null); 274 } 275 tasklist.addRemove(addList, null, true, category, null); 276 } 277 } else { 278 tasklist.addRemove(addList, removeList, false, category, null); 280 } 281 282 updateCategoryCount(category, sizeKnown); } else { 289 SuggestionImpl after = tasklist.findAfter(type); 290 if (category == null) { 291 boolean append = (after == null); 294 tasklist.addRemove(addList, removeList, append, null, after); 295 } else { 296 synchronized (this) { 299 if (removeList != null) { 300 tasklist.addRemove(null, removeList, false, category, 301 null); 302 } 303 List leftover = category.getSubtasks(); 304 if (addList != null) { 305 tasklist.addRemove(addList, null, true, null, after); 306 } 307 if ((leftover != null) && (leftover.size() > 0)) { 308 tasklist.addRemove(leftover, null, true, null, after); 309 } 310 } 311 tasklist.removeCategory(category, true); 312 } 313 } 314 if (!split) { 315 break; 316 } 317 318 } 319 } 320 321 private static void updateCategoryCount(SuggestionImpl category, boolean sizeKnown) { 322 SuggestionType type = category.getSType(); 323 int count = category.subtasksCount(); 324 String summary; 325 if ((count != 0) || sizeKnown) { 326 summary = type.getLocalizedName() + " (" + Integer.toString(count) + ")"; } else { 329 summary = type.getLocalizedName(); 330 } 331 category.setSummary(summary); 332 } 333 334 338 345 private SuggestionType unfilteredType = null; 346 347 protected final SuggestionType getUnfilteredType() { 348 return unfilteredType; 349 } 350 351 protected final void setUnfilteredType(SuggestionType unfilteredType) { 352 this.unfilteredType = unfilteredType; 353 } 354 355 } 356 | Popular Tags |