public void testUpdateOnTodoChange() throws Exception { TodoPattern pattern = new TodoPattern("newtodo", TodoAttributesUtil.createDefault(), true); TodoPattern[] oldPatterns = TodoConfiguration.getInstance().getTodoPatterns(); TodoConfiguration.getInstance().setTodoPatterns(new TodoPattern[]{pattern}); try{ final TodoCacheManager todocache = TodoCacheManager.SERVICE.getInstance(myProject); checkResult(new String[]{"2.java"}, convert(todocache.getFilesWithTodoItems())); assertEquals(0, todocache.getTodoCount(myRootDir.findChild("1.java"), TodoIndexPatternProvider.getInstance())); assertEquals(1, todocache.getTodoCount(myRootDir.findChild("2.java"), TodoIndexPatternProvider.getInstance())); assertEquals(0, todocache.getTodoCount(myRootDir.findChild("3.java"), TodoIndexPatternProvider.getInstance())); } finally{ TodoConfiguration.getInstance().setTodoPatterns(oldPatterns); } }
/** * @param element with filter's data. * @param patterns all available patterns */ public void readExternal(Element element, @NotNull List<TodoPattern> patterns) { myName = element.getAttributeValue(ATTRIBUTE_NAME); if (myName == null) { throw new IllegalArgumentException(); } myTodoPatterns.clear(); for (Element child : element.getChildren()) { if (!ELEMENT_PATTERN.equals(child.getName())) { continue; } try { int index = Integer.parseInt(child.getAttributeValue(ATTRIBUTE_INDEX)); if (index < 0 || index > patterns.size() - 1) { continue; } TodoPattern pattern = patterns.get(index); if (myTodoPatterns.contains(pattern)) { continue; } myTodoPatterns.add(pattern); } catch (NumberFormatException ignored) { } } }
public boolean equals(Object obj){ if(!(obj instanceof TodoFilter)){ return false; } TodoFilter filter=(TodoFilter)obj; if(!myName.equals(filter.myName)){ return false; } if(myTodoPatterns.size()!=filter.myTodoPatterns.size()){ return false; } for (TodoPattern pattern : myTodoPatterns) { if (!filter.contains(pattern)) { return false; } } return true; }
/** * @param parent parent component. * @param filter filter to be edited. * @param filterIndex index of <code>filter</code> in the <code>filters</code>. This parameter is * needed to not compare filter with itself when validating. * @param filters all already configured filters. This parameter is used to * @param patterns all patterns available in this filter. */ public FilterDialog(Component parent, TodoFilter filter, int filterIndex, List<TodoFilter> filters, List<TodoPattern> patterns) { super(parent, true); myFilter = filter; myFilterIndex = filterIndex; myPatterns = patterns; myFilters = filters; myNameField = new JTextField(filter.getName()); MyModel model = new MyModel(); myTable = new Table(model); init(); }
@Override public void setValueAt(Object value, int row, int column) { switch (column) { case 0: TodoPattern pattern = myPatterns.get(row); if (((Boolean)value).booleanValue()) { if (!myFilter.contains(pattern)) { myFilter.addTodoPattern(pattern); } } else { myFilter.removeTodoPattern(pattern); } break; default: throw new IllegalArgumentException(); } }
private void editSelectedPattern() { stopEditing(); int selectedIndex = myPatternsTable.getSelectedRow(); if (selectedIndex < 0 || selectedIndex >= myPatternsModel.getRowCount()) { return; } TodoPattern sourcePattern = myPatterns.get(selectedIndex); TodoPattern pattern = sourcePattern.clone(); PatternDialog dialog = new PatternDialog(myPanel, pattern); dialog.setTitle(IdeBundle.message("title.edit.todo.pattern")); if (!dialog.showAndGet()) { return; } myPatterns.set(selectedIndex, pattern); myPatternsModel.fireTableRowsUpdated(selectedIndex, selectedIndex); myPatternsTable.getSelectionModel().setSelectionInterval(selectedIndex, selectedIndex); // Update model with patterns for (int i = 0; i < myFilters.size(); i++) { TodoFilter filter = myFilters.get(i); if (filter.contains(sourcePattern)) { filter.removeTodoPattern(sourcePattern); filter.addTodoPattern(pattern); myFiltersModel.fireTableRowsUpdated(i, i); } } }
@Override public void reset() { // Patterns myPatterns.clear(); TodoConfiguration todoConfiguration = TodoConfiguration.getInstance(); TodoPattern[] patterns = todoConfiguration.getTodoPatterns(); for (TodoPattern pattern : patterns) { myPatterns.add(pattern.clone()); } myPatternsModel.fireTableDataChanged(); // Filters myFilters.clear(); TodoFilter[] filters = todoConfiguration.getTodoFilters(); for (TodoFilter filter : filters) { myFilters.add(filter.clone()); } myFiltersModel.fireTableDataChanged(); }
@Override public Object getValueAt(int row,int column){ TodoFilter filter=myFilters.get(row); switch(column){ case 0:{ // "Name" column return filter.getName(); }case 1:{ StringBuffer sb=new StringBuffer(); for(Iterator i=filter.iterator();i.hasNext();){ TodoPattern pattern=(TodoPattern)i.next(); sb.append(pattern.getPatternString()); if(i.hasNext()){ sb.append(" | "); } } return sb.toString(); }default:{ throw new IllegalArgumentException(); } } }
@Override public void setValueAt(Object value,int row,int column){ TodoPattern pattern=myPatterns.get(row); switch(column){ case 0:{ pattern.getAttributes().setIcon((Icon)value); break; }case 1:{ pattern.setCaseSensitive(((Boolean)value).booleanValue()); break; }case 2:{ pattern.setPatternString(((String)value).trim()); break; }default:{ throw new IllegalArgumentException(); } } }
@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ){ super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column); TodoPattern pattern=myPatterns.get(row); if(isSelected){ setForeground(UIUtil.getTableSelectionForeground()); }else{ if(pattern.getPattern()==null){ setForeground(JBColor.RED); }else{ setForeground(UIUtil.getTableForeground()); } } return this; }
public void testUpdateOnTodoChange() throws Exception { TodoPattern pattern = new TodoPattern("newtodo", TodoAttributes.createDefault(), true); TodoPattern[] oldPatterns = TodoConfiguration.getInstance().getTodoPatterns(); TodoConfiguration.getInstance().setTodoPatterns(new TodoPattern[]{pattern}); try{ final TodoCacheManager todocache = TodoCacheManager.SERVICE.getInstance(myProject); checkResult(new String[]{"2.java"}, convert(todocache.getFilesWithTodoItems())); assertEquals(0, todocache.getTodoCount(myRootDir.findChild("1.java"), TodoIndexPatternProvider.getInstance())); assertEquals(1, todocache.getTodoCount(myRootDir.findChild("2.java"), TodoIndexPatternProvider.getInstance())); assertEquals(0, todocache.getTodoCount(myRootDir.findChild("3.java"), TodoIndexPatternProvider.getInstance())); } finally{ TodoConfiguration.getInstance().setTodoPatterns(oldPatterns); } }
private void doSetTodoPatterns(@NotNull TodoPattern[] todoPatterns, final boolean shouldNotifyIndices) { TodoPattern[] oldTodoPatterns = myTodoPatterns; IndexPattern[] oldIndexPatterns = myIndexPatterns; myTodoPatterns = todoPatterns; buildIndexPatterns(); // only trigger index refresh actual index patterns have changed if (shouldNotifyIndices && !Arrays.deepEquals(myIndexPatterns, oldIndexPatterns)) { final PropertyChangeEvent event = new PropertyChangeEvent(this, IndexPatternProvider.PROP_INDEX_PATTERNS, oldTodoPatterns, todoPatterns); myMessageBus.syncPublisher(IndexPatternProvider.INDEX_PATTERNS_CHANGED).propertyChange(event); } // only trigger gui and code daemon refresh when either the index patterns or presentation attributes have changed if (!Arrays.deepEquals(myTodoPatterns, oldTodoPatterns)) { final PropertyChangeListener multicaster = myPropertyChangeMulticaster.getMulticaster(); multicaster.propertyChange(new PropertyChangeEvent(this, PROP_TODO_PATTERNS, oldTodoPatterns, todoPatterns)); } }
@Override public void readExternal(Element element) throws InvalidDataException { ArrayList<TodoPattern> patternsList = new ArrayList<TodoPattern>(); ArrayList<TodoFilter> filtersList = new ArrayList<TodoFilter>(); for (Object o : element.getChildren()) { Element child = (Element)o; if (ELEMENT_PATTERN.equals(child.getName())) { TodoPattern pattern = new TodoPattern(); pattern.readExternal(child); patternsList.add(pattern); } else if (ELEMENT_FILTER.equals(child.getName())) { TodoPattern[] patterns = patternsList.toArray(new TodoPattern[patternsList.size()]); TodoFilter filter = new TodoFilter(); filter.readExternal(child, patterns); filtersList.add(filter); } } doSetTodoPatterns(patternsList.toArray(new TodoPattern[patternsList.size()]), false); setTodoFilters(filtersList.toArray(new TodoFilter[filtersList.size()])); }
@Override public void setValueAt(Object value, int row, int column) { switch (column) { case 0: TodoPattern pattern = myPatterns.get(row); if (((Boolean)value).booleanValue()) { myFilter.addTodoPattern(pattern); } else { myFilter.removeTodoPattern(pattern); } break; default: throw new IllegalArgumentException(); } }
/** * @return number of <code>TodoItem</code>s located in the file. */ public final int getTodoItemCount(PsiFile psiFile) { int count = 0; if (psiFile != null) { if (myTodoFilter != null) { for (Iterator i = myTodoFilter.iterator(); i.hasNext(); ) { TodoPattern pattern = (TodoPattern)i.next(); count += getSearchHelper().getTodoItemsCount(psiFile, pattern); } } else { count = getSearchHelper().getTodoItemsCount(psiFile); } } return count; }
public void testUpdateOnTodoChange() throws Exception { TodoPattern pattern = new TodoPattern("newtodo", TodoAttributesUtil.createDefault(), true); TodoPattern[] oldPatterns = TodoConfiguration.getInstance().getTodoPatterns(); TodoConfiguration.getInstance().setTodoPatterns(new TodoPattern[]{pattern}); try{ final TodoCacheManager todocache = TodoCacheManager.getInstance(myProject); checkResult(new String[]{"2.java"}, convert(todocache.getFilesWithTodoItems())); assertEquals(0, todocache.getTodoCount(myRootDir.findChild("1.java"), TodoIndexPatternProvider.getInstance())); assertEquals(1, todocache.getTodoCount(myRootDir.findChild("2.java"), TodoIndexPatternProvider.getInstance())); assertEquals(0, todocache.getTodoCount(myRootDir.findChild("3.java"), TodoIndexPatternProvider.getInstance())); } finally{ TodoConfiguration.getInstance().setTodoPatterns(oldPatterns); } }
@NotNull private TodoPattern mapPattern(@NotNull IndexPattern pattern) { for(TodoPattern todoPattern: myTodoPatterns) { if (todoPattern.getIndexPattern() == pattern) { return todoPattern; } } LOG.error("Could not find matching TODO pattern for index pattern " + pattern.getPatternString()); return null; }
@Override public int getTodoItemsCount(@NotNull PsiFile file, @NotNull TodoPattern pattern) { int count = TodoCacheManager.SERVICE.getInstance(myManager.getProject()).getTodoCount(file.getVirtualFile(), pattern.getIndexPattern()); if (count != -1) return count; TodoItem[] items = findTodoItems(file); count = 0; for (TodoItem item : items) { if (item.getPattern().equals(pattern)) count++; } return count; }
/** * @return <code>true</code> if and only if specified <code>psiFile</code> has * <code>TodoItem</code>s accepted by the filter. */ public boolean accept(PsiTodoSearchHelper searchHelper,PsiFile psiFile){ for(Iterator<TodoPattern> i=iterator();i.hasNext();){ TodoPattern todoPattern= i.next(); if(searchHelper.getTodoItemsCount(psiFile,todoPattern)>0){ return true; } } return false; }
/** * @param element in which all data will be stored * @param patterns all available patterns */ public void writeExternal(Element element, TodoPattern[] patterns){ element.setAttribute(ATTRIBUTE_NAME,myName); for (TodoPattern pattern : myTodoPatterns) { int index = ArrayUtilRt.find(patterns, pattern); LOG.assertTrue(index != -1); Element child = new Element(ELEMENT_PATTERN); child.setAttribute(ATTRIBUTE_INDEX, Integer.toString(index)); element.addContent(child); } }
@Override public TodoFilter clone(){ try{ TodoFilter filter = (TodoFilter)super.clone(); filter.myTodoPatterns=new HashSet<TodoPattern>(myTodoPatterns); return filter; }catch(CloneNotSupportedException e){ LOG.error(e); return null; } }
/** * @return number of <code>TodoItem</code>s located in the file. */ public final int getTodoItemCount(PsiFile psiFile){ int count=0; if(psiFile != null){ if(myTodoFilter!=null){ for(Iterator i=myTodoFilter.iterator();i.hasNext();){ TodoPattern pattern=(TodoPattern)i.next(); count+=getSearchHelper().getTodoItemsCount(psiFile,pattern); } }else{ count=getSearchHelper().getTodoItemsCount(psiFile); } } return count; }
@Override public Object getValueAt(int row, int column) { TodoPattern pattern = myPatterns.get(row); switch (column) { case 0: // "Available" column return myFilter.contains(pattern); case 1: // "Pattern" column return pattern.getPatternString(); default: throw new IllegalArgumentException(); } }
/** * Invoked by reflection */ public TodoConfigurable() { myPatterns = new ArrayList<TodoPattern>(); myFilters = new ArrayList<TodoFilter>(); myFiltersModel = new FiltersTableModel(myFilters); myPatternsModel = new PatternsTableModel(myPatterns); }
private boolean arePatternsModified() { TodoConfiguration todoConfiguration = TodoConfiguration.getInstance(); TodoPattern[] initialPatterns = todoConfiguration.getTodoPatterns(); if (initialPatterns.length != myPatterns.size()) { return true; } for (TodoPattern initialPattern : initialPatterns) { if (!myPatterns.contains(initialPattern)) { return true; } } return false; }
@Override public void apply() throws ConfigurationException { stopEditing(); if (arePatternsModified()) { TodoPattern[] patterns = myPatterns.toArray(new TodoPattern[myPatterns.size()]); TodoConfiguration.getInstance().setTodoPatterns(patterns); } if (areFiltersModified()) { TodoFilter[] filters = myFilters.toArray(new TodoFilter[myFilters.size()]); TodoConfiguration.getInstance().setTodoFilters(filters); } }
@Override public Object getValueAt(int row,int column){ TodoPattern pattern=myPatterns.get(row); switch(column){ case 0:{ // "Icon" column return pattern.getAttributes().getIcon(); }case 1:{ // "Case Sensitive" column return pattern.isCaseSensitive()?Boolean.TRUE:Boolean.FALSE; }case 2:{ // "Pattern" column return pattern.getPatternString(); }default:{ throw new IllegalArgumentException(); } } }