separate predefined scopes (IDEA-91275)

This commit is contained in:
Anna Kozlova
2012-09-12 17:53:25 +04:00
parent 6173b7cbf0
commit 1d5fa7211b
2 changed files with 65 additions and 12 deletions
@@ -31,13 +31,14 @@ import com.intellij.psi.search.scope.packageSet.NamedScopesHolder;
import com.intellij.ui.ListCellRendererWrapper;
import com.intellij.ui.content.Content;
import com.intellij.util.Alarm;
import com.intellij.util.ArrayUtil;
import javax.swing.*;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;
public class ScopeBasedTodosPanel extends TodoPanel {
private static final String SELECTED_SCOPE = "TODO_SCOPE";
@@ -56,7 +57,7 @@ public class ScopeBasedTodosPanel extends TodoPanel {
myScopeListener = new NamedScopesHolder.ScopeListener() {
@Override
public void scopesChanged() {
final NamedScope scope = (NamedScope)myScopes.getSelectedItem();
final ScopeWrapper scope = (ScopeWrapper)myScopes.getSelectedItem();
rebuildModel(project, scope != null ? scope.getName() : null);
}
};
@@ -67,17 +68,20 @@ public class ScopeBasedTodosPanel extends TodoPanel {
myValidationManager = DependencyValidationManager.getInstance(project);
myValidationManager.addScopeListener(myScopeListener);
myScopes.setRenderer(new ListCellRendererWrapper<NamedScope>(){
myScopes.setRenderer(new ListCellRendererWrapper<ScopeWrapper>(){
@Override
public void customize(JList list, NamedScope value, int index, boolean selected, boolean hasFocus) {
public void customize(JList list, ScopeWrapper value, int index, boolean selected, boolean hasFocus) {
setText(value.getName());
if (value.isSeparator()) {
setSeparator();
}
}
});
myScopes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rebuildWithAlarm(ScopeBasedTodosPanel.this.myAlarm);
final NamedScope selectedItem = (NamedScope)myScopes.getSelectedItem();
final ScopeWrapper selectedItem = (ScopeWrapper)myScopes.getSelectedItem();
if (selectedItem != null) {
PropertiesComponent.getInstance(myProject).setValue(SELECTED_SCOPE, selectedItem.getName());
}
@@ -94,12 +98,23 @@ public class ScopeBasedTodosPanel extends TodoPanel {
}
private void rebuildModel(Project project, String scopeName) {
NamedScope[] scopes = DependencyValidationManager.getInstance(project).getScopes();
scopes = ArrayUtil.mergeArrays(scopes, NamedScopeManager.getInstance(project).getScopes());
scopes = NonProjectFilesScope.removeFromList(scopes);
myScopes.setModel(new DefaultComboBoxModel(scopes));
final ArrayList<ScopeWrapper> scopes = new ArrayList<ScopeWrapper>();
final DependencyValidationManager manager = DependencyValidationManager.getInstance(project);
scopes.add(new ScopeWrapper("Predefined Scopes", true));
List<NamedScope> predefinedScopesList = manager.getPredefinedScopes();
NamedScope[] predefinedScopes = predefinedScopesList.toArray(new NamedScope[predefinedScopesList.size()]);
predefinedScopes = NonProjectFilesScope.removeFromList(predefinedScopes);
for (NamedScope predefinedScope : predefinedScopes) {
scopes.add(new ScopeWrapper(predefinedScope, false));
}
collectEditableScopes(scopes, manager, "Custom Project Scopes");
collectEditableScopes(scopes, NamedScopeManager.getInstance(project), "Custom Local Scopes");
myScopes.setModel(new DefaultComboBoxModel(scopes.toArray(new ScopeWrapper[scopes.size()])));
if (scopeName != null) {
for (NamedScope scope : scopes) {
for (ScopeWrapper scope : scopes) {
if (Comparing.strEqual(scopeName, scope.getName())) {
myScopes.setSelectedItem(scope);
break;
@@ -108,6 +123,16 @@ public class ScopeBasedTodosPanel extends TodoPanel {
}
}
private static void collectEditableScopes(ArrayList<ScopeWrapper> scopes, NamedScopesHolder manager, String separatorTitle) {
NamedScope[] editableScopes = manager.getEditableScopes();
if (editableScopes.length > 0) {
scopes.add(new ScopeWrapper(separatorTitle, true));
for (NamedScope scope : editableScopes) {
scopes.add(new ScopeWrapper(scope, false));
}
}
}
@Override
protected JComponent createCenterComponent() {
JPanel panel = new JPanel(new BorderLayout());
@@ -137,4 +162,32 @@ public class ScopeBasedTodosPanel extends TodoPanel {
builder.init();
return builder;
}
public static class ScopeWrapper {
private final String myName;
private final boolean mySeparator;
private NamedScope myNamedScope;
private ScopeWrapper(NamedScope namedScope, boolean separator) {
myNamedScope = namedScope;
mySeparator = separator;
myName = myNamedScope.getName();
}
private ScopeWrapper(String name, boolean separator) {
mySeparator = separator;
myName = name;
}
public String getName() {
return myName;
}
public NamedScope getNamedScope() {
return myNamedScope;
}
public boolean isSeparator() {
return mySeparator;
}
}
}
@@ -47,9 +47,9 @@ public class ScopeBasedTodosTreeStructure extends TodoTreeStructure {
public boolean accept(final PsiFile psiFile) {
if (!psiFile.isValid()) return false;
boolean isAffected = false;
final NamedScope scope = (NamedScope)myScopes.getSelectedItem();
final ScopeBasedTodosPanel.ScopeWrapper scope = (ScopeBasedTodosPanel.ScopeWrapper)myScopes.getSelectedItem();
if (scope != null) {
final PackageSet value = scope.getValue();
final PackageSet value = scope.getNamedScope().getValue();
if (value != null) {
isAffected = value.contains(psiFile, NamedScopesHolder.getHolder(myProject, scope.getName(), DependencyValidationManager.getInstance(myProject)));
}