Revision 2130
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultFilteredTreeController.java | ||
---|---|---|
4 | 4 |
import java.awt.event.KeyAdapter; |
5 | 5 |
import java.awt.event.KeyEvent; |
6 | 6 |
import javax.swing.JButton; |
7 |
import javax.swing.JList; |
|
8 | 7 |
import javax.swing.JTree; |
9 | 8 |
import javax.swing.text.JTextComponent; |
10 |
import javax.swing.tree.TreeModel; |
|
11 |
import org.gvsig.tools.swing.api.FilteredListController; |
|
12 |
import org.gvsig.tools.swing.api.FilteredListModel; |
|
9 |
import org.apache.commons.lang3.StringUtils; |
|
10 |
import org.gvsig.tools.swing.api.TreeModelRecursiveWildcarMatchFilter; |
|
13 | 11 |
import org.gvsig.tools.swing.api.FilteredTreeController; |
14 | 12 |
import org.gvsig.tools.swing.api.FilteredTreeModel; |
13 |
import org.gvsig.tools.swing.api.FilteredTreeModel.TreeModelFilter; |
|
15 | 14 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
16 | 15 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
17 | 16 |
|
... | ... | |
38 | 37 |
this.tree.setModel(model); |
39 | 38 |
toolsSwingManager.addClearButton(this.text, (ActionEvent e) -> { |
40 | 39 |
text.setText(""); |
41 |
this.getModel().setFilter(null);
|
|
40 |
clearFilter();
|
|
42 | 41 |
}); |
43 | 42 |
toolsSwingManager.setDefaultPopupMenu(this.text); |
44 | 43 |
this.text.addKeyListener(new KeyAdapter() { |
45 | 44 |
@Override |
46 | 45 |
public void keyTyped(KeyEvent e) { |
47 | 46 |
if (e.getKeyChar() == '\n') { |
48 |
getModel().setFilter(text.getText());
|
|
47 |
setFilter(text.getText()); |
|
49 | 48 |
} |
50 | 49 |
} |
51 | 50 |
}); |
52 | 51 |
this.button.addActionListener((ActionEvent e) -> { |
53 |
this.getModel().setFilter(text.getText());
|
|
52 |
setFilter(text.getText()); |
|
54 | 53 |
}); |
55 | 54 |
} |
56 | 55 |
|
... | ... | |
61 | 60 |
return model; |
62 | 61 |
} |
63 | 62 |
|
63 |
@Override |
|
64 | 64 |
public FilteredTreeModel getModel() { |
65 | 65 |
return (FilteredTreeModel) this.tree.getModel(); |
66 | 66 |
} |
67 | 67 |
|
68 |
@Override |
|
69 |
public void clearFilter() { |
|
70 |
this.getModel().setFilter(null); |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public void setFilter(String filter) { |
|
75 |
if( StringUtils.isBlank(filter) ) { |
|
76 |
this.clearFilter(); |
|
77 |
} else { |
|
78 |
this.setFilter(new TreeModelRecursiveWildcarMatchFilter(filter)); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
|
83 |
public void setFilter(TreeModelFilter filter) { |
|
84 |
this.getModel().setFilter(filter); |
|
85 |
} |
|
68 | 86 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/FilteredTreeModelImpl.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.swing.impl; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import javax.swing.SwingUtilities; |
|
5 | 3 |
import javax.swing.event.EventListenerList; |
6 | 4 |
import javax.swing.event.TreeModelEvent; |
7 | 5 |
import javax.swing.event.TreeModelListener; |
8 |
import javax.swing.tree.DefaultTreeModel; |
|
9 | 6 |
import javax.swing.tree.TreeModel; |
10 | 7 |
import javax.swing.tree.TreePath; |
11 |
import org.apache.commons.io.FilenameUtils; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 | 8 |
import org.gvsig.tools.swing.api.FilteredTreeModel; |
14 |
import org.gvsig.tools.util.LabeledValue; |
|
15 | 9 |
|
16 | 10 |
/** |
17 | 11 |
* |
... | ... | |
19 | 13 |
*/ |
20 | 14 |
public final class FilteredTreeModelImpl implements FilteredTreeModel { |
21 | 15 |
|
22 |
private TreeModel treeModel; |
|
23 |
private String filter;
|
|
16 |
private final TreeModel treeModel;
|
|
17 |
private TreeModelFilter filter;
|
|
24 | 18 |
protected EventListenerList listenerList = new EventListenerList(); |
25 | 19 |
|
26 | 20 |
public FilteredTreeModelImpl(final TreeModel treeModel) { |
27 | 21 |
this.treeModel = treeModel; |
28 |
this.filter = "";
|
|
22 |
this.filter = null;
|
|
29 | 23 |
} |
30 | 24 |
|
31 | 25 |
@Override |
... | ... | |
34 | 28 |
} |
35 | 29 |
|
36 | 30 |
@Override |
37 |
public void setFilter(final String filter) { |
|
38 |
if (StringUtils.isBlank(filter)) { |
|
39 |
this.filter = filter; |
|
40 |
} else { |
|
41 |
if (StringUtils.containsAny(filter, "*?")) { |
|
42 |
this.filter = filter; |
|
43 |
} else { |
|
44 |
this.filter = "*" + filter + "*"; |
|
45 |
} |
|
46 |
} |
|
47 |
|
|
31 |
public void setFilter(final TreeModelFilter filter) { |
|
32 |
this.filter = filter; |
|
48 | 33 |
TreePath tp = new TreePath(new Object[]{this.getRoot()}); |
49 | 34 |
fireTreeStructureChanged(this, tp); |
50 | 35 |
} |
51 | 36 |
|
52 |
private void fireContentsChanged() { |
|
53 |
// if( !SwingUtilities.isEventDispatchThread() ) { |
|
54 |
// SwingUtilities.invokeLater(new Runnable() { |
|
55 |
// @Override |
|
56 |
// public void run() { |
|
57 |
// fireContentsChanged(); |
|
58 |
// } |
|
59 |
// }); |
|
60 |
// return; |
|
61 |
// } |
|
62 |
// |
|
63 |
// |
|
64 |
// this.treeModel.notifyAll(); |
|
65 |
} |
|
66 |
|
|
67 |
private boolean recursiveMatch(final Object node, final String filter) { |
|
68 |
|
|
69 |
String nodeString = node.toString(); |
|
70 |
boolean matches; |
|
71 |
if (StringUtils.isBlank(filter)) { |
|
72 |
matches = true; |
|
73 |
} else { |
|
74 |
matches = FilenameUtils.wildcardMatch(nodeString, filter); |
|
75 |
} |
|
76 |
|
|
77 |
int childCount = treeModel.getChildCount(node); |
|
78 |
for (int i = 0; i < childCount; i++) { |
|
79 |
Object child = treeModel.getChild(node, i); |
|
80 |
matches |= recursiveMatch(child, filter); |
|
81 |
} |
|
82 |
|
|
83 |
return matches; |
|
84 |
} |
|
85 |
|
|
86 | 37 |
@Override |
87 | 38 |
public Object getRoot() { |
88 | 39 |
return treeModel.getRoot(); |
... | ... | |
94 | 45 |
int childCount = treeModel.getChildCount(parent); |
95 | 46 |
for (int i = 0; i < childCount; i++) { |
96 | 47 |
Object child = treeModel.getChild(parent, i); |
97 |
if (recursiveMatch(child, filter)) {
|
|
48 |
if( filter==null || filter.test(treeModel, child) ) {
|
|
98 | 49 |
if (count == index) { |
99 | 50 |
return child; |
100 | 51 |
} |
... | ... | |
110 | 61 |
int childCount = treeModel.getChildCount(parent); |
111 | 62 |
for (int i = 0; i < childCount; i++) { |
112 | 63 |
Object child = treeModel.getChild(parent, i); |
113 |
if (recursiveMatch(child, filter)) {
|
|
64 |
if( filter==null || filter.test(treeModel, child) ) {
|
|
114 | 65 |
count++; |
115 | 66 |
} |
116 | 67 |
} |
... | ... | |
132 | 83 |
int childCount = treeModel.getChildCount(parent); |
133 | 84 |
for (int i = 0; i < childCount; i++) { |
134 | 85 |
Object child = treeModel.getChild(parent, i); |
135 |
if (recursiveMatch(child, filter)) {
|
|
86 |
if( filter==null || filter.test(treeModel, child) ) {
|
|
136 | 87 |
if (childToFind.equals(child)) { |
137 | 88 |
return i; |
138 | 89 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/TreeModelRecursiveWildcarMatchFilter.java | ||
---|---|---|
1 |
package org.gvsig.tools.swing.api; |
|
2 |
|
|
3 |
import javax.swing.tree.TreeModel; |
|
4 |
import org.apache.commons.io.FilenameUtils; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.tools.swing.api.FilteredTreeModel.TreeModelFilter; |
|
7 |
|
|
8 |
/** |
|
9 |
* |
|
10 |
* @author jjdelcerro |
|
11 |
*/ |
|
12 |
public class TreeModelRecursiveWildcarMatchFilter implements TreeModelFilter { |
|
13 |
|
|
14 |
private final String filter; |
|
15 |
|
|
16 |
public TreeModelRecursiveWildcarMatchFilter(String filter) { |
|
17 |
if (StringUtils.isBlank(filter)) { |
|
18 |
this.filter = null; |
|
19 |
} else { |
|
20 |
if (StringUtils.containsAny(filter, "*?")) { |
|
21 |
this.filter = filter.toUpperCase(); |
|
22 |
} else { |
|
23 |
this.filter = "*" + filter.toUpperCase() + "*"; |
|
24 |
} |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public boolean test(TreeModel treeModel, Object node) { |
|
30 |
if ( this.filter==null ) { |
|
31 |
return true; |
|
32 |
} |
|
33 |
if ( node==null ) { |
|
34 |
return true; |
|
35 |
} |
|
36 |
String nodeString = node.toString().toUpperCase(); |
|
37 |
boolean matches = FilenameUtils.wildcardMatch(nodeString, filter); |
|
38 |
|
|
39 |
int childCount = treeModel.getChildCount(node); |
|
40 |
for (int i = 0; i < childCount; i++) { |
|
41 |
Object child = treeModel.getChild(node, i); |
|
42 |
matches |= test(treeModel, child); |
|
43 |
} |
|
44 |
return matches; |
|
45 |
} |
|
46 |
|
|
47 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/FilteredTreeController.java | ||
---|---|---|
24 | 24 |
package org.gvsig.tools.swing.api; |
25 | 25 |
|
26 | 26 |
import javax.swing.JTree; |
27 |
import org.apache.commons.lang3.StringUtils; |
|
28 |
import org.gvsig.tools.swing.api.FilteredTreeModel.TreeModelFilter; |
|
27 | 29 |
|
28 | 30 |
/** |
29 | 31 |
* |
... | ... | |
34 | 36 |
public FilteredTreeModel createModel(JTree tree); |
35 | 37 |
|
36 | 38 |
public FilteredTreeModel getModel(); |
39 |
|
|
40 |
public void clearFilter(); |
|
41 |
|
|
42 |
public void setFilter(String filter); |
|
43 |
|
|
44 |
public void setFilter(TreeModelFilter filter); |
|
37 | 45 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/FilteredTreeModel.java | ||
---|---|---|
8 | 8 |
*/ |
9 | 9 |
public interface FilteredTreeModel extends TreeModel { |
10 | 10 |
|
11 |
public interface TreeModelFilter { |
|
12 |
public boolean test(TreeModel model, Object node); |
|
13 |
} |
|
14 |
|
|
11 | 15 |
public TreeModel getTreeModel(); |
12 |
public void setFilter(String filter);
|
|
16 |
public void setFilter(TreeModelFilter filter);
|
|
13 | 17 |
|
14 | 18 |
|
15 | 19 |
} |
Also available in: Unified diff